Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bruce Scharlau, University of Aberdeen, 2012 Networking for Mobiles Mobile Computing Some slides from MobEduNet This covers HTTP in detail, and mentions.

Similar presentations


Presentation on theme: "Bruce Scharlau, University of Aberdeen, 2012 Networking for Mobiles Mobile Computing Some slides from MobEduNet This covers HTTP in detail, and mentions."— Presentation transcript:

1 Bruce Scharlau, University of Aberdeen, 2012 Networking for Mobiles Mobile Computing Some slides from MobEduNet This covers HTTP in detail, and mentions other means. Bluetooth will be covered later.

2 Windows phones only supports minimal networking Can only use HTTP and push notifications Can use sockets too Bruce Scharlau, University of Aberdeen, 2012

3 Java uses Internet for direct connections http://developers.sun.com/mobility/midp/articles/network/ Network operator

4 Bruce Scharlau, University of Aberdeen, 2012 Java end to end is possible From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html

5 Bruce Scharlau, University of Aberdeen, 2012 Network URIs http://java.sun.com for HttpConnection socket://time-a.nist.gov:13 for StreamConnection serversocket://:4444 for StreamConnectionNotifier comm:0;baudrate=2400 for CommConnection datagram://127.0.0.1 for DatagramConnection file://address.dat for FileConnection bluetooth://psm=1001 for StreamConnection

6 Bruce Scharlau, University of Aberdeen, 2012 Generic Connection Framework

7 Bruce Scharlau, University of Aberdeen, 2012 Basic Architecture is simple Internet MIDP Device Web Server User requests information from an Application (e.g. MyServlet) Web server passes output from MyServlet back to the MIDlet Web Server launches MyServlet program and sends it parameters the MIDlet requested Web Server retrieves output from the MyServlet

8 Bruce Scharlau, University of Aberdeen, 2012 Opening a Connection is easy Connection throws IOException so can report errors back to application try { Connection connection = Connector.open(http://java.sun.com); } catch (IOException ioe) { //report error } Connection also has close() method

9 MIDP 3 is MSA (Mobile Service Architecture) Aims to provide wider functionality for mobiles Should work with CDC and CLDC devices Should allow RFID and NFC object communication Should enable XML parsing Bruce Scharlau, University of Aberdeen, 2012

10 extra APIs can enhance applications Just remember that they are optional, so may not be available http://java.sun.com/javame/overview/products.jsp

11 Bruce Scharlau, University of Aberdeen, 2012 Use JSR 179 and JSR 280 to parse RESTful XML http://developers.sun.com/mobility/midp/articles/parsingxml/ Parse the XML to object on handset Check links on course web site

12 Bruce Scharlau, University of Aberdeen, 2012 Need to use threads for multi-tasking in application Start a connection in a new thread so the application doesnt hang while waiting for a response

13 Bruce Scharlau, University of Aberdeen, 2012 Java Object reuse and development Same code in servlet and MIDlet Repeated code in MIDlet

14 Bruce Scharlau, University of Aberdeen, 2012 Use same OOD principles for MIDlets as for other code Refactor out methods classes Apply design patterns as appropriate Remember that its mobile, so constrain for reduced memory

15 Bruce Scharlau, University of Aberdeen, 2012 Abstract out repeated code into methods AuctionMIDlet repeats lots of code so that its clear whats happening. These could be removed to a method, or separate class – connection, try/catch, etc There is also the issue of the shared code between the servlet and MIDlet

16 Bruce Scharlau, University of Aberdeen, 2012 Round trip with octet-stream from client to servlet From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html

17 Bruce Scharlau, University of Aberdeen, 2012 Binary code makes it a pure Java solution Tightly coupled to the service. Need to use web services to interoperate with other systems. A change in one will require change in other

18 Bruce Scharlau, University of Aberdeen, 2012 There are trade-offs between binary and XML From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html

19 Can do most networking on Android Bluetooth only on 2.0 onwards – look at that later. Bruce Scharlau, University of Aberdeen, 2012

20 Android has many components

21 Bruce Scharlau, University of Aberdeen, 2012 Android emulator runs aslocalhost, ie loopback Emulator is at 127.0.0.1 so need to call service at IP address of service to test network apps on developer machine http://developer.android.com/guide/developing/tools/emulator.html

22 Bruce Scharlau, University of Aberdeen, 2012 Activity is one thing you can do From fundamentals page in sdk

23 Apps move through states during lifecycle Bruce Scharlau, University of Aberdeen, 2012 Source: unlocking android, p 68 onPause is last state to preserve state and cleanup before app possibly destroyed

24 Method returns string from network Bruce Scharlau, University of Aberdeen, 2012 Log output for debugging, etc Simple method to call URL and return value

25 Might need helper calls to network Bruce Scharlau, University of Aberdeen, 2012 Put heavy lifting work in HTTPRequestHelper so accessible to other classes Handler waits for message and then returns

26 Parse xml response and populate Java bean instance Bruce Scharlau, University of Aberdeen, 2012

27 Add permissions to manifest for connection and network Bruce Scharlau, University of Aberdeen, 2012

28 Need to consider state in application As with web apps, need to determine if, and how you will deal with state

29 Bruce Scharlau, University of Aberdeen, 2012 Sessions can maintain state the same as in regular web applications Use either cookies or, preferably, URL rewriting However, network operators might mangle the headers though, so dont rely upon them Some operators use transcoding, which messes up your application See: http://wurfl.sourceforge.net/manifesto/index.htm for detailshttp://wurfl.sourceforge.net/manifesto/index.htm

30 Bruce Scharlau, University of Aberdeen, 2012 Three levels of software for mobiles Application level is about apps for end users using provided lower-level components Middleware of communication and other libraries for protocols (bluetooth, etc) Low-level software such as kernels and device drivers (platform)

31 Bruce Scharlau, University of Aberdeen, 2012 Understanding the virtual machine aids development *.class Class loader Execution engine Scheduler Memory manager Bytecode interpreter Run-time data Class area Heap Stack App area loads loads classes into use Garbage collection calls

32 Bruce Scharlau, University of Aberdeen, 2012 Allow garbage collection to do its job Release early, allocate late: Deallocate objects as soon as possible, and allocate new objects as late as possible

33 Bruce Scharlau, University of Aberdeen, 2012 Pay attention to your memory usage to improve performance Use what is required for the application. Use single-linked list, instead of double if you only need to traverse it in one direction.

34 Bruce Scharlau, University of Aberdeen, 2012 Use linear data structures to conserve memory Linear data structures use adjacent memory for their components, instead of separate memory for each item. Linear: linked lists, tables Non-linear: graphs, trees

35 Bruce Scharlau, University of Aberdeen, 2012 Avoid small classes in favour of larger ones There is a basic overhead for ANY class Inner classes cause the same problem, as does using lots of separate exceptions

36 Bruce Scharlau, University of Aberdeen, 2012 Avoid dependencies where possible Save items from joining the constant pool of the applications memory load Look to reuse classes already loaded, or use indirection (abstraction)

37 Break Windows apps into smaller assemblies Load app faster if smaller assemblies (libraries) loaded as needed by app So break architecture into smaller components Bruce Scharlau, University of Aberdeen, 2012

38 Use Arrays instead of Vectors where possible MethodAllocated bytesAllocated objects arrayImplementation80161 vectorImplementation 40,0002002 vectorImplSimple52,0002010

39 Bruce Scharlau, University of Aberdeen, 2012 Use StringBuffer instead of String MethodAllocated bytesAllocated objects useString39,000450 useStringBuffer3045

40 Dont access network on UI thread Use separate thread for network so app doesnt hang Bruce Scharlau, University of Aberdeen, 2012

41 Cache sensible data for speedy response No sense waiting for things we could cache Bruce Scharlau, University of Aberdeen, 2012

42 Dont ask for too much data either Waiting for too much is no good either: get something on screen and load rest later in background as needed Bruce Scharlau, University of Aberdeen, 2012

43 Summary Need to consider code reuse carefully to optimise file size (yes, still) Think of garbage collection – release early, allocate late – to help speed Think of what can be done server-side Bruce Scharlau, University of Aberdeen, 2012


Download ppt "Bruce Scharlau, University of Aberdeen, 2012 Networking for Mobiles Mobile Computing Some slides from MobEduNet This covers HTTP in detail, and mentions."

Similar presentations


Ads by Google