001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package datagram;
033:
034: import java.io.*;
035:
036: import javax.microedition.io.*;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.*;
039:
040: public class Server implements Runnable, CommandListener {
041: private DatagramMIDlet parent;
042: private Display display;
043: private Form f;
044: private StringItem si;
045: private TextField tf;
046: private Command sendCommand = new Command("Send", Command.ITEM, 1);
047: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
048: Sender sender;
049: private String address;
050:
051: public Server(DatagramMIDlet m) {
052: parent = m;
053: display = Display.getDisplay(parent);
054: f = new Form("Datagram Server");
055: si = new StringItem("Status:", " ");
056: tf = new TextField("Send:", "", 30, TextField.ANY);
057: f.append(si);
058: f.append(tf);
059: f.addCommand(sendCommand);
060: f.addCommand(exitCommand);
061: f.setCommandListener(this );
062: display.setCurrent(f);
063: }
064:
065: public void start() {
066: Thread t = new Thread(this );
067: t.start();
068: }
069:
070: public void run() {
071: try {
072: si.setText("Waiting for connection");
073:
074: DatagramConnection dc = (DatagramConnection) Connector
075: .open("datagram://:5555");
076:
077: sender = new Sender(dc);
078:
079: while (true) {
080: Datagram dg = dc.newDatagram(100);
081: dc.receive(dg);
082: address = dg.getAddress();
083:
084: si.setText("Message received - "
085: + new String(dg.getData(), 0, dg.getLength()));
086: }
087: } catch (IOException ioe) {
088: Alert a = new Alert("Server",
089: "Port 5000 is already taken.", null,
090: AlertType.ERROR);
091: a.setTimeout(Alert.FOREVER);
092: a.setCommandListener(this );
093: display.setCurrent(a);
094: } catch (Exception e) {
095: e.printStackTrace();
096: }
097: }
098:
099: public void commandAction(Command c, Displayable s) {
100: if ((c == sendCommand) && !parent.isPaused()) {
101: if (address == null) {
102: si.setText("No destination address");
103: } else {
104: sender.send(address, tf.getString());
105: }
106: }
107:
108: if ((c == Alert.DISMISS_COMMAND) || (c == exitCommand)) {
109: parent.destroyApp(true);
110: parent.notifyDestroyed();
111: }
112: }
113:
114: public void stop() {
115: }
116: }
|