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 example.cbs;
033:
034: import java.io.IOException;
035:
036: import javax.microedition.io.*;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.*;
039:
040: import javax.wireless.messaging.*;
041:
042: /**
043: * An example MIDlet displays text from a CBS MessageConnection
044: */
045: public class CBSReceive extends MIDlet implements CommandListener,
046: Runnable, MessageListener {
047: /** user interface command for indicating Exit request. */
048: Command exitCommand = new Command("Exit", Command.EXIT, 2);
049:
050: /** user interface text box for the "waiting" message. */
051: Alert waiting;
052:
053: /** current display. */
054: Display display;
055:
056: /** instance of a thread for asynchronous networking and user interface. */
057: Thread thread;
058:
059: /** Connections detected at start up. */
060: String[] connections;
061:
062: /** Flag to signal end of processing. */
063: boolean done;
064:
065: /** The Message Identifier of the CBS messages we're listening for */
066: String cbsMessageID;
067:
068: /** CBS message connection for inbound text messages. */
069: MessageConnection cbsconn;
070:
071: /** Current message read from the network. */
072: Message msg;
073:
074: /** Alert displaying the contents of the received message */
075: Alert receivedMessage;
076:
077: /**
078: * Initialize the MIDlet with the current display object and
079: * graphical components.
080: */
081: public CBSReceive() {
082: cbsMessageID = getAppProperty("CBS-Message-Identifier");
083: display = Display.getDisplay(this );
084: waiting = new Alert("CBS Receive", null, null, AlertType.INFO);
085: waiting.addCommand(exitCommand);
086: waiting.setCommandListener(this );
087: waiting.setTimeout(Alert.FOREVER);
088:
089: receivedMessage = new Alert("CBS Received", null, null,
090: AlertType.INFO);
091: receivedMessage.setTimeout(Alert.FOREVER);
092: receivedMessage.addCommand(exitCommand);
093: receivedMessage.setCommandListener(this );
094: }
095:
096: /**
097: * Start creates the thread to do the MessageConnection receive
098: * text.
099: * It should return immediately to keep the dispatcher
100: * from hanging.
101: */
102: public void startApp() {
103: /** CBS connection to be read. */
104: String cbsConnection = "cbs://:" + cbsMessageID;
105:
106: /** Open the message connection. */
107: if (cbsconn == null) {
108: try {
109: cbsconn = (MessageConnection) Connector
110: .open(cbsConnection);
111: cbsconn.setMessageListener(this );
112: } catch (IOException ioe) {
113: ioe.printStackTrace();
114: }
115: }
116:
117: display.setCurrent(waiting);
118:
119: /** Initialize the text if we were started manually. */
120: connections = PushRegistry.listConnections(true);
121:
122: if ((connections == null) || (connections.length == 0)) {
123: waiting.setString("Waiting for CBS with Message ID "
124: + cbsMessageID + "...");
125: } else {
126: done = false;
127: thread = new Thread(this );
128: thread.start();
129: }
130: }
131:
132: /**
133: * Notification that a message arrived.
134: * @param conn the connection with messages available
135: */
136: public void notifyIncomingMessage(MessageConnection conn) {
137: if (thread == null) {
138: done = false;
139: thread = new Thread(this );
140: thread.start();
141: }
142: }
143:
144: /** Message reading thread. */
145: public void run() {
146: /** Check for cbs connection. */
147: try {
148: /**
149: * Loop reading messages and updating the text
150: * window.
151: */
152: while (!done) {
153: msg = cbsconn.receive();
154:
155: if (msg != null) {
156: if (msg instanceof TextMessage) {
157: receivedMessage.setString(((TextMessage) msg)
158: .getPayloadText());
159: } else {
160: StringBuffer buf = new StringBuffer();
161: byte[] data = ((BinaryMessage) msg)
162: .getPayloadData();
163:
164: for (int i = 0; i < data.length; i++) {
165: buf.append(Integer
166: .toHexString((int) data[i]));
167: buf.append(' ');
168: }
169:
170: receivedMessage.setString(buf.toString());
171: }
172:
173: display.setCurrent(receivedMessage);
174: }
175: }
176:
177: cbsconn.close();
178: } catch (IOException e) {
179: // e.printStackTrace();
180: }
181: }
182:
183: /**
184: * Pause signals the thread to stop by clearing the thread field.
185: * If stopped before done with the iterations it will
186: * be restarted from scratch later.
187: */
188: public void pauseApp() {
189: done = true;
190: thread = null;
191: }
192:
193: /**
194: * Destroy must cleanup everything. The thread is signaled
195: * to stop and no result is produced.
196: * @param unconditional true if a forced shutdown was requested
197: */
198: public void destroyApp(boolean unconditional) {
199: done = true;
200: thread = null;
201:
202: if (cbsconn != null) {
203: try {
204: cbsconn.close();
205: } catch (IOException e) {
206: // Ignore any errors on shutdown
207: }
208: }
209: }
210:
211: /**
212: * Respond to commands, including exit
213: * @param c user interface command requested
214: * @param s screen object initiating the request
215: */
216: public void commandAction(Command c, Displayable s) {
217: try {
218: if ((c == exitCommand) || (c == Alert.DISMISS_COMMAND)) {
219: destroyApp(false);
220: notifyDestroyed();
221: }
222: } catch (Exception ex) {
223: ex.printStackTrace();
224: }
225: }
226: }
|