430 likes | 532 Views
Networking for Mobiles. This covers HTTP in detail, and mentions other means. Bluetooth will be covered later. Mobile Computing. Some slides from MobEduNet. Windows phones only supports minimal networking. Can only use HTTP and push notifications Can use sockets too.
E N D
Networking for Mobiles This covers HTTP in detail, and mentions other means. Bluetooth will be covered later. Mobile Computing Bruce Scharlau, University of Aberdeen, 2012 Some slides from MobEduNet
Windows phones only supports minimal networking • Can only use HTTP and push notifications • Can use sockets too Bruce Scharlau, University of Aberdeen, 2012
Java uses Internet for direct connections Network operator Bruce Scharlau, University of Aberdeen, 2012 http://developers.sun.com/mobility/midp/articles/network/
Java end to end is possible From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html 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 Bruce Scharlau, University of Aberdeen, 2012
Generic Connection Framework Bruce Scharlau, University of Aberdeen, 2012
Basic Architecture is simple User requests information from an Application (e.g. MyServlet) Web Server launches MyServlet program and sends it parameters the MIDlet requested MIDP Device Internet Web Server Web server passes output from MyServlet back to the MIDlet Web Server retrieves output from the MyServlet Bruce Scharlau, University of Aberdeen, 2012
Opening a Connection is easy Connection throws IOException so can report errors back to application try{ Connectionconnection= Connector.open(http://java.sun.com); }catch(IOExceptionioe){ //report error } Connection also has close() method Bruce Scharlau, University of Aberdeen, 2012
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
‘extra’ APIs can enhance applications Just remember that they are ‘optional’, so may not be available Bruce Scharlau, University of Aberdeen, 2012 http://java.sun.com/javame/overview/products.jsp
Use JSR 179 and JSR 280 to parse RESTful XML Parse the XML to object on handset Check links on course web site Bruce Scharlau, University of Aberdeen, 2012 http://developers.sun.com/mobility/midp/articles/parsingxml/
Need to use threads for multi-tasking in application Start a connection in a new thread so the application doesn’t hang while waiting for a response Bruce Scharlau, University of Aberdeen, 2012
Java Object reuse and development Same code in servlet and MIDlet Repeated code in MIDlet 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 it’s mobile, so constrain for reduced memory Bruce Scharlau, University of Aberdeen, 2012
Abstract out repeated code into methods AuctionMIDlet repeats lots of code so that it’s clear what’s 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 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 Bruce Scharlau, University of Aberdeen, 2012
Binary code makes it a pure Java solution Tightly coupled to the service. A change in one will require change in other Need to use web services to interoperate with other systems. 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 Bruce Scharlau, University of Aberdeen, 2012
Can do most networking on Android Bluetooth only on 2.0 onwards – look at that later. Bruce Scharlau, University of Aberdeen, 2012
Android has many components Bruce Scharlau, University of Aberdeen, 2012
Android emulator runs as ‘localhost’, ie ‘loopback’ http://developer.android.com/guide/developing/tools/emulator.html Emulator is at 127.0.0.1 so need to call service at IP address of service to test network apps on developer machine Bruce Scharlau, University of Aberdeen, 2012
Activity is one thing you can do Bruce Scharlau, University of Aberdeen, 2012 From fundamentals page in sdk
Apps move through states during lifecycle onPause is last state to preserve state and cleanup before app possibly destroyed Source: unlocking android, p 68 Bruce Scharlau, University of Aberdeen, 2012
Method returns string from network Log output for debugging, etc Bruce Scharlau, University of Aberdeen, 2012 Simple method to call URL and return value
Might need helper calls to network Handler waits for message and then returns Put heavy lifting work in HTTPRequestHelper so accessible to other classes Bruce Scharlau, University of Aberdeen, 2012
Parse xml response and populate Java bean instance Bruce Scharlau, University of Aberdeen, 2012
Add permissions to manifest for connection and network Bruce Scharlau, University of Aberdeen, 2012
Need to consider state in application As with web apps, need to determine if, and how you will deal with state 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 don’t rely upon them Some operators use transcoding, which messes up your application See: http://wurfl.sourceforge.net/manifesto/index.htm for details 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) Bruce Scharlau, University of Aberdeen, 2012
Class area Heap Stack App area Run-time data Understanding the virtual machine aids development loads Execution engine Class loader calls *.class Scheduler loads classes into Garbage collection Memory manager Bytecode interpreter use 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 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. 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 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 Bruce Scharlau, University of Aberdeen, 2012
Avoid dependencies where possible Save items from joining the constant pool of the application’s memory load Look to reuse classes already loaded, or use indirection (abstraction) Bruce Scharlau, University of Aberdeen, 2012
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
Use Arrays instead of Vectors where possible Method Allocated bytes Allocated objects arrayImplementation 8016 1 vectorImplementation 40,000 2002 vectorImplSimple 52,000 2010 Bruce Scharlau, University of Aberdeen, 2012
Use StringBuffer instead of String Method Allocated bytes Allocated objects useString 39,000 450 useStringBuffer 304 5 Bruce Scharlau, University of Aberdeen, 2012
Don’t access network on UI thread • Use separate thread for network so app doesn’t hang Bruce Scharlau, University of Aberdeen, 2012
Cache sensible data for speedy response • No sense waiting for things we could cache Bruce Scharlau, University of Aberdeen, 2012
Don’t 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
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