An Introduction to the Java ME Project Jens A Andersson Agenda Java ME MIDlets NetBeans Your task: The Application Simple Session Protocol and SSPServer SSP help classes Why Threads 2/12 Java ME Sun groupes its Java technologies into 3 editions: • • • Java EE (Java Enterprise Edition) - servers Java SE (Java Standard Edition) – personal computers Java ME (Java Micro Edition) – mobile phones and PDAs In this course we will use: • MIDP (Mobile Information Device Profile) • • MIDP 1.0, 2.0, and 2.1 – backward compatible CDLC (Connected Limited Device Configuration) • CLDC 1.0 and 1.1 – backward compatible 3/12 MIDlets An application written for MIDP is called a MIDlet. All MIDlets extend the MIDlet class: • public class MyMIDlet extends MIDlet { } The MIDlet class provides methods for starting, pausing, and terminating the MIDlet application: • startApp() • pauseApp() • destroyApp(boolean unconditional) The department has four Sony Ericsson K700 phones that support MIDP 2.0. 4/12 A Complete MIDlet Example import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloMIDlet extends MIDlet { public void startApp() { TextBox textBox = new TextBox(“My MIDlet", "Hello world :=)",15,0); Display.getDisplay(this).setCurrent(textBox); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } } 5/12 NetBeans NetBeans is a free and open-source IDE (Integrated Development Environment) for developing Java applications. NetBeans runs on Windows, Linux, Mac OS, etc. It has support for CLDC 1.0/1.1 and MIDP 1.0/2.0/2.1. With NetBeans you can write, emulate, and debug applications for mobile phones. So they contain all you need to develop Java ME applications. 6/12 Application (overview) Peer-to-peer application: Of your choice Minimum: Simple chat application Two mobile devices communicate and exchange messages Session server (hub/proxy) to find other party Application (considerations) Hub to find other party NAT makes it impossible to communicate directly between mobile devices Proxy for two-way communication (tunnelling) Application (figure) NAT! Global addresses Private addresses Private addresses NAT & PAT NAT = Network Address Translation PAT = Port and network Address Translation zFigure 19.27 Translation Alternative: Source address 200.24.5.8 goes here Simple Session Protocol SSP UDP based Control messages Data/Tunnel messages The SSPServer SSP (Simple Session Protocol) is a simple protocol developed for this course. SSPServer is a Java SE application and allows mobile phones to communicate to each other through a server. Two ways of using the SSPServer: • Download and run it locally on any computer – connect your application to port 3333 on localhost • No downloading needed - connect your mobile phone to port 3333 on sspserver.eit.lth.se (public address) 13/12 SSPServer Users login/logout Holds a list of active users/opponents Controls state also on clients Reacts only on incoming packets (no time outs) Forwards application data to other party TUNNEL SSP packet format TYPE ID (1) DATA (variable) TYPE ID CONTROL 0x01 • (The following data is control data) TUNNEL 0x02 • (The following data is application data) Control Header Format TYPE ID (1) CTRL TYPE ID (1) USER ID (8) PEER ID (8) 0x01 CTRL TYPE ID LOGIN REQ 0x11 • (The user logs in with the specified user id) LOGIN ACC 0x12 • (The user login is accepted by the hub) LOGIN REJ 0x13 • (The user login is rejected by the hub) LOGIN ERR 0x1f • (Server in LOGIN state but receives ctrl packet for other state) Control Header Format (cont) TERMINATE REQ 0x21 • (The user/hub terminates and is logged out) SETUP LST REQ 0x04 • (The user requests a list over available opponents from the hub) SETUP LST RES 0x05 • (The hub sends a list with available opponents) Control Header Format (cont) SETUP REQ 0x01 • (The user requests the specified user as an opponent) SETUP ACC 0x02 • (The user accepts request from other user) SETUP REJ 0x03 • (The user rejects request from other user) These ctrl packets are forwarded by the hub to the users. Peer lists CONTROL HEADER (16) NBR PEERS (1) PEER1 (8) SETUP LST RES List of available opponents From Hub to mobile device PEER2 (8) PEER3 (8) ... Tunnel Header Format TYPE ID (1 ) RESERVED (1 ) USER ID (8) PEER ID (8) DATA (variable) 0x02 USERID = you; PEERID = opponent Your application protocol goes in the DATA field Application Sequence Login to server If accepted get user list select peer request session with peer If accepted Start ”application” and data transfer in tunnel At end logout from server SSP Java class Methods for datagram Parse request Build request Validate request See API on project web page for details http://www.eit.lth.se/index.php?id=javame ByteString Java Class Application data type = String DatagramConnection (sending/recieving UDP datagram) uses byte[ ] ByteString: ”interface” between String and byte[ ] See API on course home page for details Application protocol Data transfer in UDP based tunnel Best effort! Application shall handle errors! Time outs Packet loss Packet out of sequence How? You decide! Select/Design error handling method Tunnel Header Format TYPE ID (1 ) RESERVED (1 ) USER ID (8) PEER ID (8) DATA (variable) 0x02 Your application protocol goes in the DATA field Applications protocol (cont.) Is control information needed? Keep track of messages/datagrams/packets? How is DATA field interpreted? … This is your task for section 2 GUI GUI is handled by main thread inhibited when waiting for response in your methods Send REQ, Get response Wait for move from opponent GUI events must be handled! An application “The Application” MyMIDlet startApp commandAction An application (cont.) “The Application” MyMIDlet startApp GUI “stalled” commandAction . . wait for response . An application (cont..) “The Application” MyMIDlet startApp GUI active commandAction . . wait for response in thread . Threads (cont.) Send REQ application waiting for GPRS setup? application waiting for free GPRS slots? application sending datagram? GUI blocked until Send REQ is completed Threads (cont..) Get Response application setup GPRS etc? application waiting for incoming datagram? application waiting for response from opponent? GUI blocked until Get Response request is completed Threads (cont…) Do in parallel thread Send REQ Get Response In main thread Wait for completion of parallel threads Handle GUI exceptions Get Started! 1) Visit the home page of the project: http://www.eit.lth.se/index.php?id=javame 2) Download the SSPServer and run it locally. 3) Download the skeleton code and run it. 4) Study and understand the structure of the skeleton code. 36/12