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 Client 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:
050: public Client(DatagramMIDlet m) {
051: parent = m;
052: display = Display.getDisplay(parent);
053: f = new Form("Datagram Client");
054: si = new StringItem("Status:", " ");
055: tf = new TextField("Send:", "", 30, TextField.ANY);
056: f.append(si);
057: f.append(tf);
058: f.addCommand(sendCommand);
059: f.addCommand(exitCommand);
060: f.setCommandListener(this );
061: display.setCurrent(f);
062: }
063:
064: public void start() {
065: Thread t = new Thread(this );
066: t.start();
067: }
068:
069: public void run() {
070: try {
071: DatagramConnection dc = (DatagramConnection) Connector
072: .open("datagram://localhost:5555");
073:
074: si.setText("Connected to server");
075:
076: sender = new Sender(dc);
077:
078: while (true) {
079: Datagram dg = dc.newDatagram(100);
080: dc.receive(dg);
081:
082: // Have we actually received something or
083: // is this just a timeout ?
084: if (dg.getLength() > 0) {
085: si.setText("Message received - "
086: + new String(dg.getData(), 0, dg
087: .getLength()));
088: }
089: }
090: } catch (ConnectionNotFoundException cnfe) {
091: Alert a = new Alert("Client",
092: "Please run Server MIDlet first", null,
093: AlertType.ERROR);
094: a.setTimeout(Alert.FOREVER);
095: display.setCurrent(a);
096: } catch (IOException ioe) {
097: ioe.printStackTrace();
098: }
099: }
100:
101: public void commandAction(Command c, Displayable s) {
102: if ((c == sendCommand) && !parent.isPaused()) {
103: sender.send(null, tf.getString());
104: }
105:
106: if (c == exitCommand) {
107: parent.destroyApp(true);
108: parent.notifyDestroyed();
109: }
110: }
111:
112: public void stop() {
113: }
114: }
|