import java.util.*; import java.io.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.sip.*; public class SipSender extends MIDlet implements CommandListener{ private Display display; private Form form; public ImageItem userImageItem; public Image userImage; private TextField addressTextField; private TextField subjectTextField; private TextField messageTextField; private Command sendCommand; private Command exitCommand; public SipSender() { display=Display.getDisplay(this); form = new Form("SIP Sender"); try{ userImage = Image.createImage("/male_user.png"); } catch (Exception e){ e.printStackTrace(); } userImageItem = new ImageItem("William Young", userImage, Item.LAYOUT_CENTER, ""); form.append(userImageItem); addressTextField = new TextField("Address", "sip:user@localhost:5070", 30, TextField.LAYOUT_LEFT); subjectTextField = new TextField("Subject", "", 30, TextField.LAYOUT_LEFT); messageTextField = new TextField("Message text", "", 30, TextField.LAYOUT_LEFT); form.append(addressTextField); form.append(subjectTextField); form.append(messageTextField); sendCommand = new Command("Send", Command.ITEM, 1); form.addCommand(sendCommand); exitCommand = new Command("Exit", Command.EXIT, 1); form.addCommand(exitCommand); form.setCommandListener(this); } public void commandAction(Command command, Displayable display) { if(command == sendCommand) { Thread dataTransferThread = new DataTransfer(); dataTransferThread.start(); } if(command == exitCommand ) { destroyApp(true); } } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean b) { notifyDestroyed(); } class DataTransfer extends Thread implements SipClientConnectionListener{ public void run() { SipClientConnection sc = null; try { sc = (SipClientConnection) Connector.open(addressTextField.getString()); sc.setListener(this); String message = messageTextField.getString(); sc.initRequest("MESSAGE", null); sc.setHeader("From", addressTextField.getString()); sc.setHeader("Subject", subjectTextField.getString()); sc.setHeader("Content-Type", "text/plain"); sc.setHeader("Content-Length", "" + message.length()); OutputStream os = sc.openContentOutputStream(); os.write(message.getBytes()); os.close(); } catch(Exception e) { e.printStackTrace(); } } public void notifyResponse(SipClientConnection scc) { try { scc.receive(1); form.append("notifyResponse: "+scc.getStatusCode()+" "+scc.getReasonPhrase()); scc.close(); } catch(Exception e) { form.append(e.getMessage()); } } } }