an introduction to the java me project jens a andersson

34
An Introduction to the Java ME Project Jens A Andersson

Upload: juniper-palmer

Post on 25-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to the Java ME Project Jens A Andersson

An Introduction to the Java ME Project

Jens A Andersson

Page 2: An Introduction to the Java ME Project Jens A Andersson

2/12

Agenda

Java ME MIDlets NetBeans Your task: The Application Simple Session Protocol and SSPServer SSP help classes Why Threads

Page 3: An Introduction to the Java ME Project Jens A Andersson

3/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

Page 4: An Introduction to the Java ME Project Jens A Andersson

4/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.

Page 5: An Introduction to the Java ME Project Jens A Andersson

5/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) { }}

Page 6: An Introduction to the Java ME Project Jens A Andersson

6/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.

Page 7: An Introduction to the Java ME Project Jens A Andersson

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

Page 8: An Introduction to the Java ME Project Jens A Andersson

Application (considerations)

Hub to find other partyNAT makes it impossible to

communicate directly between mobile devices

Proxy for two-way communication (tunnelling)

Page 9: An Introduction to the Java ME Project Jens A Andersson

Application (figure)

NAT!

Private addresses

Private addressesGlobal addresses

Page 10: An Introduction to the Java ME Project Jens A Andersson

NAT & PAT

NAT = Network Address TranslationPAT = Port and network Address

Translation

Page 11: An Introduction to the Java ME Project Jens A Andersson

zFigure 19.27 Translation

Alternative: Source address 200.24.5.8 goes here

Page 12: An Introduction to the Java ME Project Jens A Andersson

Simple Session Protocol

SSPUDP basedControl messagesData/Tunnel messages

Page 13: An Introduction to the Java ME Project Jens A Andersson

13/12

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)

Page 14: An Introduction to the Java ME Project Jens A Andersson

SSPServer

Users login/logoutHolds a list of active users/opponentsControls state also on clientsReacts only on incoming packets

(no time outs)

Forwards application data to other party TUNNEL

Page 15: An Introduction to the Java ME Project Jens A Andersson

SSP packet format

TYPE ID CONTROL 0x01

• (The following data is control data)

TUNNEL 0x02• (The following data is application data)

TYPE ID (1) DATA (variable)

Page 16: An Introduction to the Java ME Project Jens A Andersson

Control Header Format

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)

TYPE ID (1)

0x01CTRL TYPE ID (1)

USER ID (8) PEER ID (8)

Page 17: An Introduction to the Java ME Project Jens A Andersson

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)

Page 18: An Introduction to the Java ME Project Jens A Andersson

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.

Page 19: An Introduction to the Java ME Project Jens A Andersson

Peer lists

SETUP LST RES List of available opponents From Hub to mobile device

CONTROL HEADER (16)

NBR PEERS (1)

PEER1 (8)

PEER2 (8)

PEER3 (8) ...

Page 20: An Introduction to the Java ME Project Jens A Andersson

Tunnel Header Format

USERID = you; PEERID = opponentYour application protocol goes in

the DATA field

TYPE ID (1 )

0x02RESERVED (1 )

USER ID (8) PEER ID (8) DATA (variable)

Page 21: An Introduction to the Java ME Project Jens A Andersson

Application Sequence

Login to serverIf accepted

get user list select peer request session with peer

If accepted Start ”application” and data transfer in tunnel

At end logout from server

Page 22: An Introduction to the Java ME Project Jens A Andersson

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

Page 23: An Introduction to the Java ME Project Jens A Andersson

ByteString Java Class

Application data type = StringDatagramConnection

(sending/recieving UDP datagram) uses byte[ ]

ByteString: ”interface” between String and byte[ ]

See API on course home page for details

Page 24: An Introduction to the Java ME Project Jens A Andersson

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

Page 25: An Introduction to the Java ME Project Jens A Andersson

Tunnel Header Format

Your application protocol goes in the DATA field

TYPE ID (1 )

0x02RESERVED (1 )

USER ID (8) PEER ID (8) DATA (variable)

Page 26: An Introduction to the Java ME Project Jens A Andersson

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

Page 27: An Introduction to the Java ME Project Jens A Andersson

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!

Page 28: An Introduction to the Java ME Project Jens A Andersson

An application

MyMIDlet

startApp

commandAction

“The Application”

Page 29: An Introduction to the Java ME Project Jens A Andersson

An application (cont.)

MyMIDlet

startApp

commandAction

.

.

wait for response

.

“The Application”

GUI“stalled”

Page 30: An Introduction to the Java ME Project Jens A Andersson

An application (cont..)

MyMIDlet

startApp

commandAction

.

.

wait for response in thread

.

“The Application”

GUI active

Page 31: An Introduction to the Java ME Project Jens A Andersson

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

Page 32: An Introduction to the Java ME Project Jens A Andersson

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

Page 33: An Introduction to the Java ME Project Jens A Andersson

Threads (cont…)

Do in parallel thread Send REQ Get Response

In main thread Wait for completion of parallel threads Handle GUI exceptions

Page 34: An Introduction to the Java ME Project Jens A Andersson

36/12

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.