bjoern.butzin created page: home authored by Björn Butzin's avatar Björn Butzin
...@@ -15,7 +15,6 @@ The following points will be covered by this tutorial: ...@@ -15,7 +15,6 @@ The following points will be covered by this tutorial:
* JAVA SE JDK 1.6+ * JAVA SE JDK 1.6+
* Eclipse IDE for JAVA development * Eclipse IDE for JAVA development
* Prepared JAVA project files for Hands-on * Prepared JAVA project files for Hands-on
* (Copper plugin for Mozilla Firefox)
## 1. Installation of Copper Plugin for Mozilla Firefox ## 1. Installation of Copper Plugin for Mozilla Firefox
- https://addons.mozilla.org/de/firefox/addon/copper-270430/ - https://addons.mozilla.org/de/firefox/addon/copper-270430/
...@@ -63,7 +62,6 @@ Client Side ...@@ -63,7 +62,6 @@ Client Side
4. Add the TemperatureResource to the ResourceServer 4. Add the TemperatureResource to the ResourceServer
5. Run the ResourceServer 5. Run the ResourceServer
* ToDo on client side: * ToDo on client side:
1. Establish a connection to the Server using the ChannelManager 1. Establish a connection to the Server using the ChannelManager
2. Create a CoapRequest & add some Options 2. Create a CoapRequest & add some Options
...@@ -98,37 +96,41 @@ Client Side ...@@ -98,37 +96,41 @@ Client Side
5. Run the ResourceServer 5. Run the ResourceServer
1. Create a new resource class TemperatureResource (TemperatureResource.java): 1. Create a new resource class TemperatureResource (TemperatureResource.java):
* We could have used the predefined BasicCoapResource
* BasicCoapResource is a resource that just keeps a static `byte[ ]` that is: * We could have used the predefined BasicCoapResource
* returned on GET requests * BasicCoapResource is a resource that just keeps a static `byte[ ]` that is:
* replaced by the payload on PUT requests * returned on GET requests
* appended with the payload on POST requests * replaced by the payload on PUT requests
* deleted on DELETE requests * appended with the payload on POST requests
* We do not want a static `byte[ ]` * deleted on DELETE requests
* Instead we want a random number to be returned on a GET * We do not want a static `byte[ ]`
* PUT, POST and DELETE are not used * Instead we want a random number to be returned on a GET
* ->So we implemented TemperatureResource wich extends BasicCoapResource with: * PUT, POST and DELETE are not used
* A constructor * ->So we implemented TemperatureResource wich extends BasicCoapResource with:
* To initialize the resource * A constructor
* Disallow POST, PUT and DELETE * To initialize the resource
* 2 get() Methods * Disallow POST, PUT and DELETE
* Get a list of accepted media types [& query parameters] * 2 get() Methods
* Returns a `byte[ ]` together with its media type * Get a list of accepted media types [& query parameters]
* And the `getResourceType()` method * Returns a `byte[ ]` together with its media type
* Wich returns a description string of the resource * And the `getResourceType()` method
* Wich returns a description string of the resource
2. Instantiate a new ResourceServer (Server.java, FIXME 1): 2. Instantiate a new ResourceServer (Server.java, FIXME 1):
* Need a CoapResourceServer to maintain resources * Need a CoapResourceServer to maintain resources
`CoapResourceServer resourceServer = new CoapResourceServer();` `CoapResourceServer resourceServer = new CoapResourceServer();`
3. Instantiate a new TemperatureResource (Server.java, FIXME 2): 3. Instantiate a new TemperatureResource (Server.java, FIXME 2):
* Resources are created like normal objects and added to the server * Resources are created like normal objects and added to the server
`CoapResource resource = new CoapResource();` `CoapResource resource = new CoapResource();`
* Use the „Tasks“ view of Eclipse to find the right places * Use the „Tasks“ view of Eclipse to find the right places
* Solve only FIXME 1-3 as described by the following instructions - ignore other FIXMEs or TODOs * Solve only FIXME 1-3 as described by the following instructions - ignore other FIXMEs or TODOs
4. Add the TemperatureResource to the ResourceServer (Server.java, FIXME 3): 4. Add the TemperatureResource to the ResourceServer (Server.java, FIXME 3):
* Resources are created like normal objects and added to the server * Resources are created like normal objects and added to the server
`resourceServer.createResource(resource);` `resourceServer.createResource(resource);`
5. Run the ResourceServer (Server.java): 5. Run the ResourceServer (Server.java):
`resourceServer.start();` `resourceServer.start();`
* Run Server: Click on Run -> Run in the Menu bar * Run Server: Click on Run -> Run in the Menu bar
...@@ -147,23 +149,23 @@ Client ...@@ -147,23 +149,23 @@ Client
1. Establish a connection to the Server using the ChannelManager (Client.java, FIXME 4-5): 1. Establish a connection to the Server using the ChannelManager (Client.java, FIXME 4-5):
* A client must implement CoapClient interface * A client must implement CoapClient interface
public class Client implements CoapClient { `public class Client implements CoapClient {`
* A CoapChannelManager is used to manage different connections and to establish a connection to a server * A CoapChannelManager is used to manage different connections and to establish a connection to a server
`channelManager = BasicCoapChannelManager.getInstance();` `channelManager = BasicCoapChannelManager.getInstance();`
`clientChannel = channelManager.connect(CoapClient client,InetAddress serverIP, int serverPort);` `clientChannel = channelManager.connect(CoapClient client,InetAddress serverIP, int serverPort);`
2. Create a CoapRequest & add some Options (Client.java, FIXME 6-8): 2. Create a CoapRequest & add some Options (Client.java, FIXME 6-8):
* A channel represents a single connection and is used to create and send requests * A channel represents a single connection and is used to create and send requests
`Boolean reliable = false;` `Boolean reliable = false;`
`CoapRequestCode reqCode = CoapRequestCode.GET;` `CoapRequestCode reqCode = CoapRequestCode.GET;`
`CoapRequest request = clientChannel.createRequest(reliable,reqCode);` `CoapRequest request = clientChannel.createRequest(reliable,reqCode);`
`request.setUriPath("/temperature");` `request.setUriPath("/temperature");`
3. Send the CoapRequest (Client.java, FIXME 6-8): 3. Send the CoapRequest (Client.java, FIXME 6-8):
`clientChannel.sendMessage(request);` `clientChannel.sendMessage(request);`
4. Wait for CoapResponse & Print the CoapResponse on the console 4. Wait for CoapResponse & Print the CoapResponse on the console
A client has some callbacks that are invoked, when the corresponding event occurs * A client has some callbacks that are invoked, when the corresponding event occurs
`public void onConnectionFailed(...)` `public void onConnectionFailed(...)`
`public void onResponse(...) // = Unicast` `public void onResponse(...) // = Unicast`
`public void onMCResponse(...) // MC = Multicast` `public void onMCResponse(...) // MC = Multicast`
* Run Server: select Server.java and click on Run -> Run in the Menu bar * Run Server: select Server.java and click on Run -> Run in the Menu bar
* Run Client: select Client.java and click on Run -> Run in the Menu bar * Run Client: select Client.java and click on Run -> Run in the Menu bar
... ...
......