- individual implementation of a server application, creates CoapResourceServer and resources
- TemperatureResource:
- individual resource, inherits from BasicCoapResource
- BasicCoapResource:
- already implemented resource with basic functionality, implements CoapResource (interface)
- CoapResource (interface):
- describes interfaces that must be supported by each resource.
- CoapResourceServer:
- manages a list of resources, enables access of these resources from outside, implements
- CoapServer (interface):
- uses ChannelManager for connection management.
- CoapServer (interface):
- describes interfaces that must be supported by a resource server
- ChannelManager:
- manages channels
- Channel:
- one channel represents a connection to one client
- Client:
- individual implementation of a client application, implements CoapClient (interface)
- CoapClient (interface):
- describes interfaces that must be supported by a client
**Server:** individual implementation of a server application, creates CoapResourceServer and resources
**TemperatureResource:** individual resource, inherits from BasicCoapResource
**BasicCoapResource:** already implemented resource with basic functionality, implements CoapResource (interface)
**CoapResource (interface):** describes interfaces that must be supported by each resource.
**CoapResourceServer:** manages a list of resources, enables access of these resources from outside, implements CoapServer (interface), uses ChannelManager for connection management.
**CoapServer (interface):** describes interfaces that must be supported by a resource server
**ChannelManager:** manages channels
**Channel:** one channel represents a connection to one client
**Client:** individual implementation of a client application, implements CoapClient (interface)
**CoapClient (interface):** describes interfaces that must be supported by a client
* ToDo on server side:
1. Create a new resource class TemperatureResource
...
...
@@ -62,6 +53,7 @@ Client Side
4. Add the TemperatureResource to the ResourceServer
5. Run the ResourceServer
* ToDo on client side:
1. Establish a connection to the Server using the ChannelManager
2. Create a CoapRequest & add some Options
...
...
@@ -69,7 +61,10 @@ Client Side
4. Wait for CoapResponse
5. Print the CoapResponse on the console
## 3. Import of prepared project into Eclipse
## 4. Import of prepared project into Eclipse
You can find the required files in our repository at [file](ws4d/jcoap/tree/master/ws4d-jcoap-handsOn)
1. File > Import
2. General > Existing Projects ...
3. Click ‚Next‘
...
...
@@ -87,7 +82,9 @@ Client Side
* Type „task“
* Select „General > Task“ view
## 4. Task 1: Implementation of client/server and enable simple message exchange
## 5. Task 1: Implementation of client/server and enable simple message exchange
### Server
1. Create a new resource class TemperatureResource (already done in our example Server)
2. Instantiate a new ResourceServer
...
...
@@ -95,8 +92,8 @@ Client Side
4. Add the TemperatureResource to 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:
* returned on GET requests
...
...
@@ -107,81 +104,85 @@ Client Side
* Instead we want a random number to be returned on a GET
* PUT, POST and DELETE are not used
* ->So we implemented TemperatureResource wich extends BasicCoapResource with:
* A constructor
* To initialize the resource
* Disallow POST, PUT and DELETE
* 2 get() Methods
* A constructor to initialize the resource and disallow POST, PUT and DELETE requests
* Two get() Methods:
* Get a list of accepted media types [& query parameters]
* Returns a `byte[ ]` together with its media type
* 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
`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
`CoapResource resource = new CoapResource();`
* 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
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
`resourceServer.createResource(resource);`
5. Run the ResourceServer (Server.java):
#### 5. Run the ResourceServer (Server.java):
`resourceServer.start();`
* Run Server: Click on Run -> Run in the Menu bar
* To stop the server, press the red terminate button in the console/task area
* Test it with Copper: coap://127.0.0.1
* Stretch goal:
** Create another resource type e.g.: humidity or current Time
*** Tip: make a copy of „TemperatureResource.java“
* Create another resource type e.g.: humidity or current Time
* Tip: make a copy of „TemperatureResource.java“
### Client
Client
1. Establish a connection to the Server using the ChannelManager
2. Create a CoapRequest & add some Options
3. Send the CoapRequest
4. Wait for CoapResponse
5. Print the CoapResponse on the console (already done in our example)
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
`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