+ All Categories
Home > Documents > Networking for Mobiles

Networking for Mobiles

Date post: 03-Jan-2016
Category:
Upload: adam-kirkland
View: 19 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
43
Bruce Scharlau, University of Aberdeen, 2012 Networking for Mobiles Mobile Computing me slides from MobEduNet This covers HTTP in detail, and mentions other means. Bluetooth will be covered later.
Transcript
Page 1: Networking for Mobiles

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.

Page 2: Networking for Mobiles

Windows phones only supports minimal networking

• Can only use HTTP and push notifications

• Can use sockets too

Bruce Scharlau, University of Aberdeen, 2012

Page 3: Networking for Mobiles

Bruce Scharlau, University of Aberdeen, 2012

Java uses Internet for direct connections

http://developers.sun.com/mobility/midp/articles/network/

Network operator

Page 4: Networking for Mobiles

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

Page 5: Networking for Mobiles

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

Page 6: Networking for Mobiles

Bruce Scharlau, University of Aberdeen, 2012

Generic Connection Framework

Page 7: Networking for Mobiles

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

Page 8: Networking for Mobiles

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

Page 9: Networking for Mobiles

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

Page 10: Networking for Mobiles

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

Page 11: Networking for Mobiles

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

Page 12: Networking for Mobiles

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

Page 13: Networking for Mobiles

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

Page 14: Networking for Mobiles

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

Page 15: Networking for Mobiles

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

Page 16: Networking for Mobiles

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

Page 17: Networking for Mobiles

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

Page 18: Networking for Mobiles

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

Page 19: Networking for Mobiles

Can do most networking on Android

Bluetooth only on 2.0 onwards – look at that later.

Bruce Scharlau, University of Aberdeen, 2012

Page 20: Networking for Mobiles

Bruce Scharlau, University of Aberdeen, 2012

Android has many components

Page 21: Networking for Mobiles

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

Page 22: Networking for Mobiles

Bruce Scharlau, University of Aberdeen, 2012

Activity is one thing you can do

From fundamentals page in sdk

Page 23: Networking for Mobiles

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

Page 24: Networking for Mobiles

Method returns string from network

Bruce Scharlau, University of Aberdeen, 2012

Log output for debugging, etc

Simple method to call URL and return value

Page 25: Networking for Mobiles

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

Page 26: Networking for Mobiles

Parse xml response and populate Java bean instance

Bruce Scharlau, University of Aberdeen, 2012

Page 27: Networking for Mobiles

Add permissions to manifest for connection and network

Bruce Scharlau, University of Aberdeen, 2012

Page 28: Networking for Mobiles

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

Page 29: Networking for Mobiles

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

Page 30: Networking for Mobiles

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)

Page 31: Networking for Mobiles

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

Page 32: Networking for Mobiles

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

Page 33: Networking for Mobiles

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.

Page 34: Networking for Mobiles

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

Page 35: Networking for Mobiles

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

Page 36: Networking for Mobiles

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)

Page 37: Networking for Mobiles

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

Page 38: Networking for Mobiles

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

Page 39: Networking for Mobiles

Bruce Scharlau, University of Aberdeen, 2012

Use StringBuffer instead of String

Method Allocated bytes Allocated objects

useString 39,000 450

useStringBuffer 304 5

Page 40: Networking for Mobiles

Don’t access network on UI thread

• Use separate thread for network so app doesn’t hang

Bruce Scharlau, University of Aberdeen, 2012

Page 41: Networking for Mobiles

Cache sensible data for speedy response

• No sense waiting for things we could cache

Bruce Scharlau, University of Aberdeen, 2012

Page 42: Networking for Mobiles

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

Page 43: Networking for Mobiles

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


Recommended