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 socket;
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 SocketMIDlet extends MIDlet implements CommandListener {
041: private static final String SERVER = "Server";
042: private static final String CLIENT = "Client";
043: private static final String[] names = { SERVER, CLIENT };
044: private static Display display;
045: private Form f;
046: private ChoiceGroup cg;
047: private boolean isPaused;
048: private Server server;
049: private Client client;
050: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
051: private Command startCommand = new Command("Start", Command.ITEM, 1);
052:
053: public SocketMIDlet() {
054: display = Display.getDisplay(this );
055: f = new Form("Socket Demo");
056: cg = new ChoiceGroup("Please select peer", Choice.EXCLUSIVE,
057: names, null);
058: f.append(cg);
059:
060: f.addCommand(exitCommand);
061: f.addCommand(startCommand);
062: f.setCommandListener(this );
063:
064: display.setCurrent(f);
065: }
066:
067: public boolean isPaused() {
068: return isPaused;
069: }
070:
071: public void startApp() {
072: isPaused = false;
073: }
074:
075: public void pauseApp() {
076: isPaused = true;
077: }
078:
079: public void destroyApp(boolean unconditional) {
080: if (server != null) {
081: server.stop();
082: }
083:
084: if (client != null) {
085: client.stop();
086: }
087: }
088:
089: public void commandAction(Command c, Displayable s) {
090: if (c == exitCommand) {
091: destroyApp(true);
092: notifyDestroyed();
093: } else if (c == startCommand) {
094: String name = cg.getString(cg.getSelectedIndex());
095:
096: if (name.equals(SERVER)) {
097: server = new Server(this );
098: server.start();
099: } else {
100: client = new Client(this);
101: client.start();
102: }
103: }
104: }
105: }
|