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.
Windows phones only supports minimal networking
• Can only use HTTP and push notifications
• Can use sockets too
Bruce Scharlau, University of Aberdeen, 2012
Bruce Scharlau, University of Aberdeen, 2012
Java uses Internet for direct connections
http://developers.sun.com/mobility/midp/articles/network/
Network operator
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
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
Internet MIDPDevice
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 fromthe MyServlet
Bruce Scharlau, University of Aberdeen, 2012
Opening a Connection is easyConnection 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
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
Bruce Scharlau, University of Aberdeen, 2012
‘extra’ APIs can enhance applications
Just remember that they are ‘optional’, so may not be available
http://java.sun.com/javame/overview/products.jsp
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 handsetParse the XML to object on handset
Check links on course web siteCheck links on course web site
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 doesn’t hang while waiting for a response
Bruce Scharlau, University of Aberdeen, 2012
Java Object reuse and development
Same code in servlet and MIDletSame code in servlet and MIDlet
Repeated code in MIDletRepeated code in MIDlet
Bruce Scharlau, University of Aberdeen, 2012
Use same OOD principles for MIDlets as for other code
Refactor out methods classesRefactor out methods classes
Apply design patterns as appropriate Apply design patterns as appropriate
Remember that it’s mobile, so constrain for reduced memory
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.Tightly coupled to the service.
Need to use web services to interoperate with other systems.
Need to use web services to interoperate with other systems.
A change in one will require change in otherA change in one will require change in other
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
Can do most networking on Android
Bluetooth only on 2.0 onwards – look at that later.
Bruce Scharlau, University of Aberdeen, 2012
Bruce Scharlau, University of Aberdeen, 2012
Android has many components
Bruce Scharlau, University of Aberdeen, 2012
Android emulator runs as ‘localhost’, 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
Bruce Scharlau, University of Aberdeen, 2012
Activity is one thing you can do
From fundamentals page in sdk
Apps move through states during lifecycle
Bruce Scharlau, University of Aberdeen, 2012Source: unlocking android, p 68
onPause is last state to preserve state and cleanup before app possibly destroyed
Method returns string from network
Bruce Scharlau, University of Aberdeen, 2012
Log output for debugging, etc
Simple method to call URL and return value
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
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
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
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
Understanding the virtual machine aids development
*.classClassloader
Executionengine
Scheduler
Memorymanager
Bytecodeinterpreter
Run-time data
Classarea
Heap
Stack
App area
loads
loads classes into
use
Garbage collection
calls
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 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, tablesLinear: linked lists, tables
Non-linear: graphs, treesNon-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)
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
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,0002010
Bruce Scharlau, University of Aberdeen, 2012
Use StringBuffer instead of String
Method Allocated bytes Allocated objects
useString 39,000 450
useStringBuffer 304 5
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