物联网协议Coap中Californium CoapClient解析

目录

前言

一、CoapClient对象

1、类定义

2、Client方法调用

二、发送请求

1、构建请求

2、发起请求

3、接收响应

总结


前言

在之前的博客中物联网协议Coap之Californium CoapServer解析,文中简单介绍了CoapServer的实现。在物联网开发环境中,除了Server端需要定义,很多的开发场景是在客户端的开发,这涉及设备端的交互,比如传感器的数据采集,需要通过Client的put方法进行采集数据的提交,同时通过get方法获取服务器端的指令,然后在Client端进行采集。

本次我们简单来看看CoapClient的具体实现,博文将继续采用面向对象分析的方法,结合类图、实际代码、时序图来讲讲解CoapClient类,方便了解和掌握其相关的配置,同时掌握其运行原理。在实际的终端开发中有的放矢。行文仓促,定有不当之处,欢迎各位读者批评指正,再此感谢。

一、CoapClient对象

在Coap的世界中,并不是像http协议一样,只要是浏览器就能发http请求,Coap需要实现对应的CoapClient,以此来跟Server建立通讯,实现数数据的提交,服务的交互。

1、类定义

在CoapClient的构造方法中,有三种构造的方式:

java 复制代码
/**
	 * Constructs a new CoapClient that sends requests to the specified URI.
	 *
	 * @param uri the uri
	 */
	public CoapClient(String uri) {
		this.uri = uri;
	}

	/**
	 * Constructs a new CoapClient that sends request to the specified URI.
	 * 
	 * @param uri the uri
	 */
	public CoapClient(URI uri) {
		this(uri.toString());
	}

	/**
	 * Constructs a new CoapClient with the specified scheme, host, port and
	 * path as URI.
	 *
	 * @param scheme the scheme
	 * @param host the host
	 * @param port the port
	 * @param path the path
	 */
	public CoapClient(String scheme, String host, int port, String... path) {
		StringBuilder builder = new StringBuilder().append(scheme).append("://").append(host).append(":").append(port);
		for (String element : path) {
			builder.append("/").append(element);
		}
		this.uri = builder.toString();
	}

2、Client方法调用

在CoapClient中,定义了包括get、put、delete、post等方法的定义,在这里只是进行入口函数的编写。下节将重点讲解,在CoapClient中如何进行相应请求的发送。

java 复制代码
// Asynchronous GET

	/**
	 * Sends a GET request and invokes the specified handler when a response
	 * arrives.
	 *
	 * @param handler the Response handler
	 */
	public void get(CoapHandler handler) {
		asynchronous(newGet().setURI(uri), handler);
	}

	/**
	 * Sends aGET request with the specified Accept option and invokes the
	 * handler when a response arrives.
	 * 
	 * @param handler the Response handler
	 * @param accept the Accept option
	 */
	public void get(CoapHandler handler, int accept) {
		asynchronous(accept(newGet().setURI(uri), accept), handler);
	}

	// Synchronous POST

	/**
	 * Sends a POST request with the specified payload, the specified content
	 * format and accept and invokes the specified handler when a response
	 * arrives.
	 * 
	 * @param handler the Response handler
	 * @param payload the payload
	 * @param format the Content-Format
	 * @param accept the Accept option
	 */
	public void post(CoapHandler handler, byte[] payload, int format, int accept) {
		asynchronous(accept(format(newPost().setURI(uri).setPayload(payload), format), accept), handler);
	}

	/**
	 * Sends a PUT request with the specified payload and the specified content
	 * format and invokes the specified handler when a response arrives.
	 * 
	 * @param handler the Response handler
	 * @param payload the payload
	 * @param format the Content-Format
	 */
	public void put(CoapHandler handler, byte[] payload, int format) {
		asynchronous(format(newPut().setURI(uri).setPayload(payload), format), handler);
	}


	/**
	 * Sends a DELETE request and invokes the specified handler when a response
	 * arrives.
	 *
	 * @param handler the response handler
	 */
	public void delete(CoapHandler handler) {
		asynchronous(newDelete().setURI(uri), handler);
	}

二、发送请求

在构建好CoapClient对象后,就可以往目标服务器提交请求并获取响应结果了。这里详细讲解在Coap中如何进行请求的发送。下面是之前创建CoapClient以及发送get请求的关键代码:

java 复制代码
URI uri = null;
//coap://127.0.0.1:5683/core/time?type=1
uri = new URI("coap://localhost:5683/hello"); // 创建一个资源请求hello资源,注意默认端口为5683
//uri = new URI("coap://127.0.0.1:5683/core/time?type=1");
CoapClient client = new CoapClient(uri);
CoapResponse response = client.get();

1、构建请求

通过代码跟踪和时序图,以发送get请求为例,来看看底层究竟是怎么运行的。

第一步,在调用get()方法时,进入以下函数:

第二步,进入核心的请求函数

默认情况下,我们没有给请求设置超时时间,因此它会根据配置文件加载默认的超时时间。然后根据请求方式和携带的参数,都封装到request对象中。

这里可以看到get请求携带的参数如下:

bash 复制代码
CON-GET    MID=   -1, Token=null, OptionSet={"Uri-Host":"localhost", "Uri-Path":"hello"}, no payload

2、发起请求

在这里,通过endPoint对象来进行发送。org.eclipse.californium.core.network.CoapEndpoint中的sendRequest方法。最终的请求参数如下:

java 复制代码
@Override
	public void sendRequest(final Request request) {
		// create context, if not already set
		request.prepareDestinationContext();
		// always use endpoint executor
		runInProtocolStage(new Runnable() {
			@Override
			public void run() {
				coapstack.sendRequest(request);
			}
		});
	}

可以看到,在发送请求的时候,是开启了一个线程池来进行请求发送。

在org.eclipse.californium.core.network.stack.BaseCoapStack

3、接收响应

java 复制代码
ACK-2.05   MID=20132, Token=[643cec40ed6f22c6], OptionSet={"Content-Format":"text/plain"}, "Hello CoAP!This is from ".. 40 bytes

可以看到,通过response对象就可以正常获取从服务端返回的响应信息。

总结

以上就是本文的主要内容,本文将继续采用面向对象分析的方法,结合类图、实际代码、时序图来讲讲解CoapClient类,方便了解和掌握其相关的配置,同时掌握其运行原理。在实际的终端开发中有的放矢。

相关推荐
芯片人00710 小时前
纯硬件一键开关机ASIC芯片,为什么比 MCU 方案更适合更可靠
人工智能·单片机·嵌入式硬件·物联网·芯片
芯片人00712 小时前
武汉广昇科技的GMUX1308A模拟开关芯片成功导入中国台湾上市企业联昌电子,助力汽车电子供应链自主可控
科技·嵌入式硬件·物联网·汽车·芯片
黎阳之光12 小时前
数字孪生赋能全域水网,实现水资源管控与节水降碳双向提升
人工智能·物联网·算法·安全·数字孪生
IT小白杨13 小时前
多店铺防关联指纹浏览器哪个好:从账号关联判定模型到环境隔离架构的一次拆解
经验分享·物联网·矩阵·架构·指纹浏览器
一个学Java小白16 小时前
系统移植11111
物联网
这张生成的图像能检测吗18 小时前
(论文速读)GMA-Net:面向嵌入式物联网设备的航空发动机损伤检测轻量级实时网络
物联网·目标检测·缺陷检测·轻量化·设备部署
wtblszn19 小时前
光伏封装产线智能管理平台方案
网络·物联网
星恒讯工业路由器20 小时前
分布式光伏站点分散难联网?4G/5G点对多点自组网方案解析
分布式·物联网·5g·分布式光伏·自组网·点对多点通信·4g/5g
zcmodeltech21 小时前
智慧矿山沙盘模型多场景控制系统设计——基于STM32与Modbus RTU的智能开采、无人矿卡、数字孪生全场景联动方案
数据库·stm32·单片机·嵌入式硬件·物联网
2601_953988071 天前
Ricon组态实时监控 - 毫秒级数据可视化
前端·物联网·数学建模·信息可视化·架构·前端框架