如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2009年2月24日星期二

SunSPOT BroadcastSample Code

/*
* Copyright (c) 2007 Sun Microsystems, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**/


package org.sunspotworld.demo;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.spot.io.j2me.radiogram.RadiogramConnection;

import com.sun.spot.util.Utils;

/**
* @author: David Mercier <david.mercier@sun.com>
*
* This simple demo shows you how to use the radio to broadcast
* some data to any listening SPOT(s).
*
* There is one thread (startSenderThread) that sends data on a particular
* channel.
* There is a second thread (startReceiverThread) that receives
* data on that same channel.
*
*/

public class BroadcastSample extends javax.microedition.midlet.MIDlet {

protected void startApp() throws MIDletStateChangeException {

System.out.println("I'm about to rock that SPOT !");

startSenderThread();
startReceiverThread();
}

/**
* The receiver thread blocks on the receive function
* so you don't have to sleep between each receive.
*/

public void startReceiverThread() {
new Thread() {
public void run() {
String tmp = null;
RadiogramConnection dgConnection = null;
Datagram dg = null;

try {
dgConnection = (RadiogramConnection) Connector.open("radiogram://:37");
// Then, we ask for a datagram with the maximum size allowed
dg = dgConnection.newDatagram(dgConnection.getMaximumLength());
} catch (IOException e) {
System.out.println("Could not open radiogram receiver connection");
e.printStackTrace();
return;
}

while(true){
try {
dg.reset();
dgConnection.receive(dg);
tmp = dg.readUTF();
System.out.println("Received: " + tmp + " from " + dg.getAddress());
} catch (IOException e) {
System.out.println("Nothing received");
}
}
}
}.start();
}

/**
* The sender thread sends a string each 500 ms
*/

synchronized public void startSenderThread() {
new Thread() {
public void run() {
// We create a DatagramConnection
DatagramConnection dgConnection = null;
Datagram dg = null;
try {
// The Connection is a broadcast so we specify it in the creation string
dgConnection = (DatagramConnection) Connector.open("radiogram://broadcast:37");
// Then, we ask for a datagram with the maximum size allowed
dg = dgConnection.newDatagram(dgConnection.getMaximumLength());
} catch (IOException ex) {
System.out.println("Could not open radiogram broadcast connection");
ex.printStackTrace();
return;
}

while(true){
try {
// We send the message (UTF encoded)
dg.reset();
dg.writeUTF("I rock");
dgConnection.send(dg);
System.out.println("Broadcast is going through");
} catch (IOException ex) {
ex.printStackTrace();
}
Utils.sleep(500);
}
}
}.start();
}

protected void pauseApp() {
}

/**
* Called if the MIDlet is terminated by the system.
* I.e. if startApp throws any exception other than MIDletStateChangeException,
* if the isolate running the MIDlet is killed with Isolate.exit(), or
* if VM.stopVM() is called.
*
* It is not called if MIDlet.notifyDestroyed() was called.
*
* @param unconditional If true when this method is called, the MIDlet must
* cleanup and release all resources. If false the MIDlet may throw
* MIDletStateChangeException to indicate it does not want to be destroyed
* at this time.
*/

protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
}

}