Sun Microsystems V2.0 manual Http protocol support, Configuring the http protocol

Page 41

http protocol support

The http protocol is implemented to allow remote SPOT applications to open http connections to any web service accessible from a correctly configured host computer.

To open a connection do:

HttpConnection connection = (HttpConnection)Connector.open("http://host:[port]/filepath");

Where host is the Internet host name in the domain notation, e.g. www.hut.fi or a numerical TCP/IP address. port is the port number, which can usually be omitted, in which case the default of 80 applies. filepath is the path/name of the resource being requested from the web server.

Here's a complete example that retrieves the source html of the home page from the http://www.sunspotworld.com website:

HttpConnection connection = (HttpConnection)Connector.open("http://www.sunspotworld.com/");

connection.setRequestProperty("Connection", "close"); InputStream in = connection.openInputStream(); StringBuffer buf = new StringBuffer();

int ch;

while ((ch = in.read()) > 0) { buf.append((char)ch);

}

System.out.println(buf.toString());

in.close();

connection.close();

In order for the http protocol to have access to the specified URL, the device must be within radio reach of a base station connected to a host running the Socket Proxy. This proxy program is responsible for communicating with the server specified in the URL. The Socket Proxy program is started by

ant socket-proxy

or

ant socket-proxy-gui

Configuring the http protocol

The http protocol is implemented as an HTTP client on the Sun SPOTusing the socket protocol. When an http connection is opened, an initial connection is established with the Socket Proxy over radiogram on port 10 (or as specified in the MANIFEST.MF file of your project). The Socket Proxy then replies with an available radio port that the device connects to in order to start streaming data.

By default, a broadcast is used in order to find the base station that will be used to open a connection to the Socket Proxy. In order to use a specific base station add the following property to the project's MANIFEST.MF file:

com.sun.spot.io.j2me.socket.SocketConnection-BaseStationAddress: <IEEE address>

By default, radiogram port 10 is used to perform the initial connection to the Socket Proxy. This can be overridden by including the following property in the project's MANIFEST.MF file

com.sun.spot.io.j2me.socket.SocketConnection-BaseStationPort: <radiogram port>

41

Image 41
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 jarOther user properties Manifest and resourcesExcluding files from the compilation Overview Using the BasestationBuilt-in properties Set up Introduction Base Station configurationRemote operation Connect a Sun Spot base station Managing keys and sharing Sun SPOTs Using short names for SPOTsTake suitable actions during over-the-air downloads BackgroundSharing Sun SPOTs Changing the owner of a Sun SpotDeploying and running a host application What is protected?Generating a new key-pair LimitationsYour own host application Configuring network featuresIncorporating pre-existing jars into your host application Mesh routingLogging Hardware configurations and USB powerTrace route Page Thread priorities Overview of an applicationThreads Device Interface Sun Spot device librariesSun Spot device library 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 Shallow Sleep Conserving power using deep sleep modeMonitoring radio activity Deep Sleep Activating deep sleep modePreconditions for deep sleeping USB inhibits deep sleepDeep sleep behaviour of the standard drivers Device Condition to permit deep sleepWriting a device driver Http protocol support Configuring the http protocolSocket Proxy GUI mode Configuring projects in an IDEDebugging Classpath configurationJavadoc/source configuration 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 Property name Meaning ReferencePersistent system properties Contents of the lib directory Contents of the arm directoryMemory usage Start address Space UseContents of the bin directory preverify.exe Contents of the tests directory Contents of the upgrade directory