Commit 28267579 authored by Bjoern Konieczek's avatar Bjoern Konieczek
Browse files

Wrapper Class for Client and Server

parent 1f96e5bf
......@@ -2,53 +2,98 @@ package org.ws4d.coap.client;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.connection.BasicCoapClientChannel;
import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapClient;
import org.ws4d.coap.interfaces.CoapClientChannel;
import org.ws4d.coap.interfaces.CoapRequest;
import org.ws4d.coap.interfaces.CoapResponse;
import org.ws4d.coap.messages.CoapBlockOption;
import org.ws4d.coap.messages.CoapEmptyMessage;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.messages.CoapRequestCode;
import org.ws4d.coap.messages.CoapBlockOption.CoapBlockSize;
/**
* @author Christian Lerche <christian.lerche@uni-rostock.de>
* @author Christian Lerche <christian.lerche@uni-rostock.de>
* Bjoern Konieczek <bjoern.konieczek@uni-rostock.de>
*/
public class BasicCoapClient implements CoapClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int PORT = Constants.COAP_DEFAULT_PORT;
private String SERVER_ADDRESS;
private int PORT;
static int counter = 0;
CoapChannelManager channelManager = null;
CoapClientChannel clientChannel = null;
private CoapChannelManager channelManager = null;
private BasicCoapClientChannel clientChannel = null;
private Random tokenGen = null;
public static void main(String[] args) {
System.out.println("Start CoAP Client");
BasicCoapClient client = new BasicCoapClient();
client.channelManager = BasicCoapChannelManager.getInstance();
client.runTestClient();
public BasicCoapClient(String server_addr, int port ){
super();
this.SERVER_ADDRESS = server_addr;
this.PORT = port;
this.channelManager = BasicCoapChannelManager.getInstance();
this.tokenGen = new Random();
}
public void runTestClient(){
public BasicCoapClient(){
super();
this.SERVER_ADDRESS = "localhost";
this.PORT = Constants.COAP_DEFAULT_PORT;
this.tokenGen = new Random();
}
public boolean connect(){
try {
clientChannel = channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
CoapRequest coapRequest = clientChannel.createRequest(true, CoapRequestCode.GET);
// coapRequest.setContentType(CoapMediaType.octet_stream);
// coapRequest.setToken("ABCD".getBytes());
// coapRequest.setUriHost("123.123.123.123");
// coapRequest.setUriPort(1234);
// coapRequest.setUriPath("/sub1/sub2/sub3/");
// coapRequest.setUriQuery("a=1&b=2&c=3");
// coapRequest.setProxyUri("http://proxy.org:1234/proxytest");
clientChannel.sendMessage(coapRequest);
System.out.println("Sent Request");
} catch (UnknownHostException e) {
e.printStackTrace();
}
clientChannel = (BasicCoapClientChannel) channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
} catch( UnknownHostException e ){
e.printStackTrace();
return false;
}
return true;
}
public CoapRequest createRequest( boolean reliable, CoapRequestCode reqCode ) {
return clientChannel.createRequest( reliable, reqCode );
}
public void sendRequest( CoapRequest request ){
if( request.getRequestCode() == CoapRequestCode.PUT || request.getRequestCode() == CoapRequestCode.POST ){
if( ( clientChannel.getMaxSendBlocksize() != null || request.getBlock1() != null ) ) {
request = clientChannel.addBlockContext( request);
}
} else if( request.getRequestCode() == CoapRequestCode.GET && ( request.getBlock2() == null && clientChannel.getMaxReceiveBlocksize() != null )) {
CoapBlockOption block2 = new CoapBlockOption( 0, false, clientChannel.getMaxReceiveBlocksize() );
request.setBlock2( block2 );
}
clientChannel.sendMessage(request);
}
public void setReceiveBlockSize( CoapBlockSize size ){
if( clientChannel != null )
clientChannel.setMaxReceiveBlocksize( size );
}
public void setSendBlockSize( CoapBlockSize size ){
if( clientChannel != null )
clientChannel.setMaxSendBlocksize(size);
}
public byte[] generateRequestToken(int tokenLength ){
byte[] token = new byte[tokenLength];
tokenGen.nextBytes(token);
return token;
}
@Override
public void onConnectionFailed(CoapClientChannel channel, boolean notReachable, boolean resetByServer) {
System.out.println("Connection Failed");
......
......@@ -15,6 +15,7 @@
package org.ws4d.coap.server;
import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapMessage;
......@@ -23,51 +24,49 @@ import org.ws4d.coap.interfaces.CoapServer;
import org.ws4d.coap.interfaces.CoapServerChannel;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.messages.CoapResponseCode;
import org.ws4d.coap.rest.BasicCoapResource;
import org.ws4d.coap.rest.CoapResource;
import org.ws4d.coap.rest.CoapResourceServer;
/**
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public class BasicCoapServer implements CoapServer {
private static final int PORT = 5683;
public class BasicCoapServer {
private int port = Constants.COAP_DEFAULT_PORT;
private CoapResourceServer resourceServer = null;
static int counter = 0;
public static void main(String[] args) {
System.out.println("Start CoAP Server on port " + PORT);
BasicCoapServer server = new BasicCoapServer();
CoapChannelManager channelManager = BasicCoapChannelManager.getInstance();
channelManager.createServerListener(server, PORT);
public BasicCoapServer(int port){
}
@Override
public CoapServer onAccept(CoapRequest request) {
System.out.println("Accept connection...");
return this;
}
@Override
public void onRequest(CoapServerChannel channel, CoapRequest request) {
System.out.println("Received message: " + request.toString()+ " URI: " + request.getUriPath());
CoapMessage response = channel.createResponse(request,
CoapResponseCode.Content_205);
response.setContentType(CoapMediaType.text_plain);
response.setPayload("payload...".getBytes());
if (request.getObserveOption() != null){
System.out.println("Client wants to observe this resource.");
public BasicCoapServer(){
}
public void init() {
BasicCoapChannelManager.getInstance().setMessageId(2000);
if (resourceServer != null)
resourceServer.stop();
resourceServer = new CoapResourceServer();
}
public boolean addResource( CoapResource resource ){
if( resourceServer != null ) {
resourceServer.createResource( resource );
return true;
}
return false;
}
public void run() {
try {
System.out.println("=== Starting Server ===");
resourceServer.start();
} catch (Exception e) {
e.printStackTrace();
}
response.setObserveOption(1);
channel.sendMessage(response);
}
@Override
public void onSeparateResponseFailed(CoapServerChannel channel) {
System.out.println("Separate response transmission failed.");
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment