Mobile Web Server Exercise CoAP Server and Client
# In this exercise, you will create a CoAP server, also your will implement a simple CoAP client within the same application to test your CoAP server. In real world, client and server should be on different application or different machine.
# Since the server and the client will be implemented on the same application, there should be no issue in communication.
# You will be doing this by using WS4D's open source jCoAP library (http://ws4d.org/ws4d- jcoap/).
# Download the source code of WS4D's CoAP library from http://kodu.ut.ee/~chang/org.zip
# Extract the zip file to get 'org' folder, which contains all the source code you need to create CoAP server and client on Android.
PART 1 – CoAP Server
# Create a new Android Project.
# Add permissions in your AndroidManifest.xml
......
# Copy 'org' folder you downloaded before to your project directory under src/main/java/
# Now in your src/java/, you should have: # a folder with your package name and a folder called org # and from the Android Studio view, you should see org.ws4d.coap appeared inside java directory. If you don't see it, try to refresh the list.
# Create a CoAP resource in your MainActivity class
//CoAP attributes private CoapResourceServer resourceServer; private BasicCoapResource bcs;
# Add following content to onCreate method
// CoAP resource server remove if(this.resourceServer != null){this.resourceServer.stop();}
// CoAP resource server new init this.resourceServer = new CoapResourceServer();
// CoAP add resource bcs = new BasicCoapResource("/maha", "82", CoapMediaType.text_plain); this.resourceServer.createResource(bcs); bcs.registerServerListener(this.resourceServer);
// CoAP Server start try { Log.v("*******", "Starting server..."); this.resourceServer.start(5683); } catch (Exception e) { e.printStackTrace(); }
PART 2 – CoAP Client
# Create a new Java class called TestClient within the same directory of your MainActivity class.
# Import following classes: import android.util.Log; import org.ws4d.coap.core.CoapClient; import org.ws4d.coap.core.connection.BasicCoapChannelManager; import org.ws4d.coap.core.connection.api.CoapChannelManager; import org.ws4d.coap.core.connection.api.CoapClientChannel; import org.ws4d.coap.core.enumerations.CoapRequestCode; import org.ws4d.coap.core.messages.api.CoapRequest; import org.ws4d.coap.core.messages.api.CoapResponse; import org.ws4d.coap.core.tools.Encoder; import java.net.InetAddress; import java.net.UnknownHostException;
# make CoapClient as your interface, e.g. public class TestClient implements CoapClient {
# Copy the following content to your client class: private static final boolean exitAfterResponse = false;
public void start(String serverAddress, int serverPort) { System.out.println("===START=== (Run Test Client)");
String sAddress = serverAddress; int sPort = serverPort;
CoapChannelManager channelManager = BasicCoapChannelManager.getInstance();
CoapClientChannel clientChannel = null;
try { clientChannel = channelManager.connect(this, InetAddress.getByName(sAddress), sPort); } catch (UnknownHostException e) { e.printStackTrace(); System.exit(-1); }
CoapRequest request;
if (null == clientChannel) { return; }
request = clientChannel.createRequest(CoapRequestCode.GET, "/maha", true);
clientChannel.sendMessage(request); }
@Override public void onResponse(CoapClientChannel channel, CoapResponse response) { if (response.getPayload() != null) { Log.v("*******", "Response: " + response.toString() + " payload: " + Encoder.ByteToString(response.getPayload())); } else { System.out.println("Response: " + response.toString()); } if (this.exitAfterResponse) { Log.v("*******","===END==="); System.exit(0); } }
@Override public void onMCResponse(CoapClientChannel channel, CoapResponse response, InetAddress srcAddress, int srcPort) { // TODO Auto-generated method stub }
@Override public void onConnectionFailed(CoapClientChannel channel, boolean notReachable, boolean resetByServer) { Log.v("*******","Connection Failed"); System.exit(-1); }
# Add new content to your MainActivity class; at the end of the onCreate() method:
// Test the client - change the IP address according to your device or simply use 'localhost' new Thread(new Runnable() { public void run() { TestClient ct = new TestClient(); ct.start("192.168.2.6", CoapConstants.COAP_DEFAULT_PORT); } }).start();
# Now, run the application.
# Finally, from the Logcat of your Android Studio, you should see the similar message below somewhere in your logs:
V/*******: Response: ACK, Content_205, MsgId: 47186, #Options: 1 ( Content_Format) payload: 82
# which contains the CoAP request and the payload of the response is '82'. # If you cannot see the log, search for '*******'
Homework Modify the response payload from the CoAP server to make the server reply a different value. e.g. the GPS value from Android API. See: https://courses.cs.ut.ee/2018/MAD/fall/Main/Homework5