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

Page 31
Image 31
Sun Microsystems V2.0 manual Radiogram protocol