Sun Microsystems V2.0 manual Radiogram protocol

Page 31

Program 2

RadiostreamConnection conn = (RadiostreamConnection) Connector.open("radio://0014.4F01.0000.0007:100");

DataInputStream dis = conn.openDataInputStream(); DataOutputStream dos = conn.openDataOutputStream(); try {

String question = dis.readUTF();

if (question.equals("Hello up there")) { dos.writeUTF("Hello down there");

}else { dos.writeUTF("What???"); dos.flush();

}

} catch (NoRouteException e) {

System.out.println ("No route to 0014.4F01.0000.0007"); } finally {

dis.close();

dos.close();

conn.close();

}

Data is sent over the air when the output stream buffer is full or when a flush() is issued.

The NoRouteException is thrown if no route to the destination can be determined. The stream accesses themselves are fully blocking - that is, the dis.readUTF() call will wait forever (unless a timeout for the connection has been specified – see below).

Behind the scenes, every data transmission between the two devices involves an acknowledgement. The sending stream will always wait until the MAC-level acknowledgement is received from the next hop. If the next hop is the final destination then this is sufficient to ensure the data has been delivered. However if the next hop is not the final destination then an acknowledgement is requested from the final destination, but, to improve performance, the sending stream does not wait for this acknowledgement; it is returned asynchronously. If the acknowledgement is not received despite retries a NoMeshLayerAckException is thrown on the next stream write that causes a send or when the stream is flushed or closed. A NoMeshLayerAckException indicates that a previous send has failed – the application has no way of knowing how much data was successfully delivered to the destination.

Another exception that you may see, which applies to both radiostream and radiogram protocols, is ChannelBusyException. This exception indicates that the radio channel was busy when the SPOT tried to send a radio packet. The normal handling is to catch the exception and retry the send.

The radiogram protocol

The radiogram protocol is a client-server protocol that provides datagram-based IO between two devices.

To open a server connection do:

RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:<portNo>");

where portNo is a port number in the range 0 to 255 that identifies this particular connection. The connection is opened using the default radio channel and default PAN Id (currently channel 26, PAN 3). The section Radio properties shows how to override these defaults.

To open a client connection do:

RadiogramConnection conn = (RadiogramConnection)Connector.open("radiogram://<serveraddr>:<portNo>");

31

Image 31
Contents Page Page Contents Http protocol support Introduction Deploying and running a sample application Building and deploying Sun Spot applicationsBuild Successful Total time 3 seconds \MyApplication Ant -Dport=COM2 info Total time 4 seconds \MyApplication Total time 0 seconds \MyApplication Incorporating utility classes into your application Deploying a pre-existing jarExcluding files from the compilation Manifest and resourcesOther user properties Built-in properties Using the BasestationOverview Set up Remote operation Base Station configurationIntroduction Connect a Sun Spot base station Background Using short names for SPOTsManaging keys and sharing Sun SPOTs Take suitable actions during over-the-air downloadsSharing Sun SPOTs Changing the owner of a Sun SpotLimitations What is protected?Deploying and running a host application Generating a new key-pairMesh routing Configuring network featuresYour own host application Incorporating pre-existing jars into your host applicationTrace route Hardware configurations and USB powerLogging Page Threads Overview of an applicationThread priorities Sun Spot device library Sun Spot device librariesDevice Interface Persistent properties Overriding the Ieee address Accessing flash memoryUsart Radio communication library Program Radiogram protocol Server end Client endYou can open server radiogram connections in a similar way Broadcasting Radio properties Turning the receiver off and on Monitoring radio activity Conserving power using deep sleep modeShallow Sleep Deep Sleep Activating deep sleep modeDevice Condition to permit deep sleep USB inhibits deep sleepPreconditions for deep sleeping Deep sleep behaviour of the standard driversWriting a device driver Http protocol support Configuring the http protocolSocket Proxy GUI mode Configuring projects in an IDEJavadoc/source configuration Classpath configurationDebugging Ant selectapplication Configuring Eclipse as a debug client Configuring NetBeans as a debug clientAdvanced topics Using library suitesWith adderlib as your current directory, execute the command Use the command Using the spot client SpotSerialPortException other exception in serial port comms Persistent system properties ReferenceProperty name Meaning Start address Space Use Contents of the arm directoryContents of the lib directory Memory usageContents of the bin directory preverify.exe Contents of the tests directory Contents of the upgrade directory