import java.util.*; import java.io.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.sip.*; public class SipReceiver extends MIDlet implements CommandListener{ public Display display; public long startTime; public Form form; public TextField portTextField; public ImageItem userImageItem; public Image userImage; public Command startCommand; public Command exitCommand ; public SipConnectionNotifier scn = null; public SipServerConnection ssc = null; public SipReceiver() { display = Display.getDisplay(this); form = new Form("SIP Receiver"); try{ userImage = Image.createImage("/female_user.png"); } catch (Exception e){ e.printStackTrace(); } userImageItem = new ImageItem("Sally Jones", userImage, Item.LAYOUT_CENTER, ""); form.append(userImageItem); portTextField= new TextField("Listening port:", "5070", 30, TextField.LAYOUT_LEFT); form.append(portTextField); startCommand = new Command("Start", Command.ITEM, 1); exitCommand = new Command("Exit", Command.EXIT, 1); form.addCommand(startCommand); form.addCommand(exitCommand); form.setCommandListener(this); } public void commandAction(Command command, Displayable display) { if(command == startCommand) { Thread dataTransferThread = new DataTransfer(); dataTransferThread.start(); } if(command == exitCommand ) { if(scn != null) { try { scn.close(); } catch(IOException e) { } } destroyApp(true); } } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean b) { notifyDestroyed(); } class DataTransfer extends Thread implements SipServerConnectionListener { public void run(){ try { if(scn != null) scn.close(); scn = (SipConnectionNotifier) Connector.open("sip:" + portTextField.getString()); scn.setListener(this); form.append("Listening to port: " + scn.getLocalPort()); } catch(Exception ex) { ex.printStackTrace(); } } public void notifyRequest(SipConnectionNotifier scn) { try { ssc = scn.acceptAndOpen(); if(ssc.getMethod().equals("MESSAGE")) { String contentType = ssc.getHeader("Content-Type"); String contentLength = ssc.getHeader("Content-Length"); int length = Integer.parseInt(contentLength); if((contentType != null) && contentType.equals("text/plain")) { InputStream is = ssc.openContentInputStream(); int i=0; byte testBuffer[] = new byte[length]; i = is.read(testBuffer); String message = new String(testBuffer, 0, i); form.append(new StringItem("Subject:", ssc.getHeader("Subject"))); form.append(new StringItem("Message:", message)); } ssc.initResponse(200); ssc.send(); } } catch(IOException ex) { form.append("Exception: "+ex.getMessage()); } } } }