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; ...@@ -2,53 +2,98 @@ package org.ws4d.coap.client;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Random;
import org.ws4d.coap.Constants; import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager; import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.connection.BasicCoapClientChannel;
import org.ws4d.coap.interfaces.CoapChannelManager; import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapClient; import org.ws4d.coap.interfaces.CoapClient;
import org.ws4d.coap.interfaces.CoapClientChannel; import org.ws4d.coap.interfaces.CoapClientChannel;
import org.ws4d.coap.interfaces.CoapRequest; import org.ws4d.coap.interfaces.CoapRequest;
import org.ws4d.coap.interfaces.CoapResponse; import org.ws4d.coap.interfaces.CoapResponse;
import org.ws4d.coap.messages.CoapBlockOption;
import org.ws4d.coap.messages.CoapEmptyMessage; import org.ws4d.coap.messages.CoapEmptyMessage;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.messages.CoapRequestCode; 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 { public class BasicCoapClient implements CoapClient {
private static final String SERVER_ADDRESS = "localhost"; private String SERVER_ADDRESS;
private static final int PORT = Constants.COAP_DEFAULT_PORT; private int PORT;
static int counter = 0; static int counter = 0;
CoapChannelManager channelManager = null; private CoapChannelManager channelManager = null;
CoapClientChannel clientChannel = null; private BasicCoapClientChannel clientChannel = null;
private Random tokenGen = null;
public static void main(String[] args) {
System.out.println("Start CoAP Client"); public BasicCoapClient(String server_addr, int port ){
BasicCoapClient client = new BasicCoapClient(); super();
client.channelManager = BasicCoapChannelManager.getInstance(); this.SERVER_ADDRESS = server_addr;
client.runTestClient(); 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 { try {
clientChannel = channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT); clientChannel = (BasicCoapClientChannel) channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
CoapRequest coapRequest = clientChannel.createRequest(true, CoapRequestCode.GET); } catch( UnknownHostException e ){
// coapRequest.setContentType(CoapMediaType.octet_stream); e.printStackTrace();
// coapRequest.setToken("ABCD".getBytes()); return false;
// coapRequest.setUriHost("123.123.123.123"); }
// coapRequest.setUriPort(1234);
// coapRequest.setUriPath("/sub1/sub2/sub3/"); return true;
// 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();
}
} }
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 @Override
public void onConnectionFailed(CoapClientChannel channel, boolean notReachable, boolean resetByServer) { public void onConnectionFailed(CoapClientChannel channel, boolean notReachable, boolean resetByServer) {
System.out.println("Connection Failed"); System.out.println("Connection Failed");
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
package org.ws4d.coap.server; package org.ws4d.coap.server;
import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager; import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.interfaces.CoapChannelManager; import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapMessage; import org.ws4d.coap.interfaces.CoapMessage;
...@@ -23,51 +24,49 @@ import org.ws4d.coap.interfaces.CoapServer; ...@@ -23,51 +24,49 @@ import org.ws4d.coap.interfaces.CoapServer;
import org.ws4d.coap.interfaces.CoapServerChannel; import org.ws4d.coap.interfaces.CoapServerChannel;
import org.ws4d.coap.messages.CoapMediaType; import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.messages.CoapResponseCode; 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> * @author Christian Lerche <christian.lerche@uni-rostock.de>
*/ */
public class BasicCoapServer implements CoapServer { public class BasicCoapServer {
private static final int PORT = 5683; private int port = Constants.COAP_DEFAULT_PORT;
private CoapResourceServer resourceServer = null;
static int counter = 0; static int counter = 0;
public static void main(String[] args) { public BasicCoapServer(int port){
System.out.println("Start CoAP Server on port " + PORT);
BasicCoapServer server = new BasicCoapServer();
CoapChannelManager channelManager = BasicCoapChannelManager.getInstance();
channelManager.createServerListener(server, PORT);
} }
@Override public BasicCoapServer(){
public CoapServer onAccept(CoapRequest request) {
System.out.println("Accept connection..."); }
return this;
} public void init() {
BasicCoapChannelManager.getInstance().setMessageId(2000);
@Override if (resourceServer != null)
public void onRequest(CoapServerChannel channel, CoapRequest request) { resourceServer.stop();
System.out.println("Received message: " + request.toString()+ " URI: " + request.getUriPath()); resourceServer = new CoapResourceServer();
}
CoapMessage response = channel.createResponse(request,
CoapResponseCode.Content_205); public boolean addResource( CoapResource resource ){
response.setContentType(CoapMediaType.text_plain); if( resourceServer != null ) {
resourceServer.createResource( resource );
response.setPayload("payload...".getBytes()); return true;
}
if (request.getObserveOption() != null){
System.out.println("Client wants to observe this resource."); 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