package kr.ac.kaist.resl.lilliput.util;
import java.net.Inet6Address;
import java.net.UnknownHostException;
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.CoapEmptyMessage;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.messages.CoapRequestCode;
public class BasicCoapClient implements CoapClient
{
static int counter = 0;
CoapChannelManager channelManager = null;
CoapClientChannel clientChannel = null;
String sensors;
public BasicCoapClient()
{
sensors = null;
}
public void runTestClient(String SERVER_ADDRESS, int PORT){
try {
clientChannel = channelManager.connect(this, Inet6Address.getByName(SERVER_ADDRESS), PORT);
CoapRequest coapRequest1 = clientChannel.createRequest(false, CoapRequestCode.GET);
coapRequest1.setContentType(CoapMediaType.text_plain);
coapRequest1.setUriPath("/sensors");
clientChannel.sendMessage(coapRequest1);
System.out.println("Sent Request");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void onResponse(CoapClientChannel channel, CoapResponse response) {
// TODO Auto-generated method stub
System.out.println("Received response");
System.out.println(new String(response.getPayload()));
System.out.println("Channel : " + response.getChannel().toString() + "MessageID : " + response.getMessageID() + "Code Value " + response.getMessageCodeValue());
sensors = new String(response.getPayload());
}
public void onConnectionFailed(CoapClientChannel channel,
boolean notReachable, boolean resetByServer) {
// TODO Auto-generated method stub
System.out.println("Connection Failed");
}
public void onSeparateResponseAck(CoapClientChannel channel,
CoapEmptyMessage message) {
// TODO Auto-generated method stub
System.out.println("Received Ack of Separate Response");
}
public void onSeparateResponse(CoapClientChannel channel,
CoapResponse response) {
// TODO Auto-generated method stub
System.out.println("Received Separate Response");
}
}