本文通过构建一个CoAP的服务端和客户端,来简要了解CoAP协议的消息机制。
一,环境准备
1、jdk1.8及以上
2、maven,idea开发工具
二、代码
1、服务端
model包下主要封装服务器要处理的消息信息
package hang.com.model; import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.server.resources.CoapExchange; public class CoAPHumidityModel extends CoapResource{ public CoAPHumidityModel(){ super("CoAPHumidityModel"); getAttributes().setTitle("CoAP-Humidity-Model"); } @Override public void handleGET(CoapExchange exchange){ exchange.respond("get humidity: 32%"); } @Override public void handlePOST(CoapExchange exchange){ exchange.respond("post humidity: 45% "); } @Override public void handlePUT(CoapExchange exchange){ exchange.respond("put humidity: 65% "); } @Override public void handleDELETE(CoapExchange exchange){ exchange.respond("delete humidity: 67% "); } }
package hang.com.model; import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.server.resources.CoapExchange; public class CoAPTemperatureModel extends CoapResource { public CoAPTemperatureModel(){ //set resource identifier super("CoAPTemperatureModel"); getAttributes().setTitle("Coap-Temperature-Model"); } @Override public void handleGET(CoapExchange exchange){ exchange.respond("get temperature: 21℃"); } @Override public void handlePOST(CoapExchange exchange){ exchange.respond("post temperature: 24℃ "); } @Override public void handlePUT(CoapExchange exchange){ exchange.respond("put temperature: 27℃ "); } @Override public void handleDELETE(CoapExchange exchange){ exchange.respond("delete temperature: 21℃ "); } }
服务器端启动代码
package hang.com.server; import hang.com.model.CoAPHumidityModel; import hang.com.model.CoAPTemperatureModel; import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.CoapServer; import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.core.network.EndpointManager; import org.eclipse.californium.core.network.config.NetworkConfig; import org.eclipse.californium.core.server.resources.CoapExchange; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketException; public class COAPServerTest extends CoapServer{ private static final int COAP_PORT = NetworkConfig.getStandard().getInt(NetworkConfig.Keys.COAP_PORT); /* * Application entry point. */ public static void main(String[] args) { try { // create server COAPServerTest server = new COAPServerTest(); // add endpoints on all IP addresses server.addEndpoints(); server.start(); } catch (SocketException e) { System.err.println("Failed to initialize server: " + e.getMessage()); } } /** * Add individual endpoints listening on default CoAP port on all IPv4 addresses of all network interfaces. */ private void addEndpoints() { for (InetAddress addr : EndpointManager.getEndpointManager().getNetworkInterfaces()) { // only binds to IPv4 addresses and localhost if (addr instanceof Inet4Address || addr.isLoopbackAddress()) { InetSocketAddress bindToAddress = new InetSocketAddress(addr, COAP_PORT); addEndpoint(new CoapEndpoint(bindToAddress)); } } } /* * Constructor for a new Hello-World server. Here, the resources * of the server are initialized. */ public COAPServerTest() throws SocketException { // provide an instance of a Hello-World resource add(new CoAPHumidityModel()); add(new CoAPTemperatureModel()); } }
2、客户端
package hang.com.util; import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.CoapResponse; import java.net.URI; import java.net.URISyntaxException; public class CoAPUtils { public static StringBuffer getCoAPDataInfo(String url)throws URISyntaxException { URI uri = new URI(url); CoapClient coapClient = new CoapClient(uri); CoapResponse resource = coapClient.get(); StringBuffer buffer = new StringBuffer(); if(resource != null){ buffer.append(resource.getCode()); buffer.append("&"); buffer.append(resource.getOptions()); buffer.append("&"); buffer.append(resource.getResponseText()); buffer.append("&"); buffer.append(resource.getPayload()); } return buffer; } }
package hang.com.client; import hang.com.util.CoAPUtils; import java.net.URISyntaxException; public class CoAPClientController { private static CoAPUtils coAPUtils; public static void main(String[] args)throws URISyntaxException{ StringBuffer temInfo = coAPUtils.getCoAPDataInfo("coap://127.0.0.1:5683/CoAPTemperatureModel"); System.out.println(temInfo); } }
至此,先启动服务器端,然后在客户端执行main方法进行访问。