首页 文章 精选 留言 我的

精选列表

搜索[分布式总线],共10005篇文章
优秀的个人博客,低调大师

润小云解读鸿蒙OS系列(六):分布式软总线之discovery+COAP全流程

简介 Discovery 是一种基于coap 通信协议的设备发现机制;Coap(Constrained Application Protocol)是一种可以使用在资源受限的物联网设备上,并支持可靠传输的轻量化类web协议。它详细规范定义在 RFC 7252, coap 协议支持IP多播, 即可以同时向多个设备发送请求,鸿蒙OS的设备发现功能也是基于这个特性;用户使用discovery功能时,需要保证发现端设备与被发现端设备在同一个局域网内,并且都能收到对方coap协议报文;目前discovery服务仅支持基于Wi-Fi通信方式的设备发现机制。 代码分析 代码目录结构如下图: Discovery 对外提供PublishService() 接口来实现设备的发现功能,其函数实现解读如下: PublishService主要的代码流程图如下,由于篇幅有限我们本次不做详细的介绍。 被发现端主要是通过PublishService()这个函数发布服务。PublishService()函数的实现在discovery_service.c文件中,我们来看看这个函数的主流程代码; 函数参数三个: moduleName:调用者的模块名称 info:PublishInfo结构体,发布的信息 cb:发布成功或者失败的回调函数 在函数实现中,我们可以看到权限检查,参数检验,信号量创建之类等代码;这里就不做介绍;我们从初始化服务 InitService()函数看, InitCommonManager() 函数主要是调用InitLocalDeviceInfo()给g_deviceInfo结构体初始化; RegisterWifiCallback(WifiEventTrigger)函数将WifiEventTrigger(unsigned int para)函数赋值给全局变量g_wifiCallback 最主要看CoapInit()函数 这里面我们优先分析下CoapInitSocket() 和 CreateCoapListenThread() CoapInitSocket()函数实现如下: 可以看到CoapInitSocket()函数里面其实就是调用了socket()函数创建了socket,然后调用bind()绑定到指定的ip跟port,然后将socket描述符赋值给全局变量g_serverFd。以便后面GetCoapServerSocket()函数调用获取socket描述符。 CreateCoapListenThread() 创建线程接收消息,函数实现如下; CoapReadHandle 接收并处理收到的消息 HandleReadEvent函数实现如下,我们分别看看CoapSocketRecv()、COAP_SoftBusDecode()、PostServiceDiscover()函数; CoapSocketRecv()实现就是调用recvfrom()接收消息。 收到消息放到recvBuffer里面 然后调用COAP_SoftBusDecode()解码收到的消息。解码之后放到decodePacket里面然后调用PostServiceDiscover()函数对接收到的消息进行回应。 PostServiceDiscover()函数代码如下: 其中GetServiceDiscoverInfo(),这个函数可以获取到对端的ip 和remoteUrl。 这里可以获取到设备信息,也就是deviceInfo 结构体成员如下: 获取到这些信息之后我们就可以调用CoapResponseService()函数回复消息了。这里就看看主要的回复消息流程,其他的流程有兴趣可以自己继续钻研。 调用socket()创建socket 并将socket描述符返回跟全局变量g_clientFd,以便后面函数GetCoapClientSocket()获取socket描述符。 调用sendto发送消息。 此文档只是介绍了收发消息的主要流程,其他的细节这里并没有详细介绍。感兴趣的同学可以根据这个主流程继续钻研下其他的功能实现。

优秀的个人博客,低调大师

跟踪总线事件

spring.cloud.bus.trace.enabled=true [{ "timestamp": 1516583843951, "info": { "signal": "spring.cloud.bus.ack", "event": "RefreshRemoteApplicationEvent", "id": "7fe840f5-ccd4-45f3-a511-bf13388938a8", "origin": "config-client:8002", "destination": "**" } }, { "timestamp": 1516583842814, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:17:22 GMT", "status": "200" } }, "timeTaken": "4068" } }, { "timestamp": 1516583842302, "info": { "signal": "spring.cloud.bus.ack", "event": "RefreshRemoteApplicationEvent", "id": "7fe840f5-ccd4-45f3-a511-bf13388938a8", "origin": "config-client:8003", "destination": "**" } }, { "timestamp": 1516583840668, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:17:20 GMT", "status": "200" } }, "timeTaken": "3106" } }, { "timestamp": 1516583838429, "info": { "method": "POST", "path": "/bus/refresh", "headers": { "request": { "host": "localhost:6201", "user-agent": "curl/7.56.1", "accept": "*/*" }, "response": { "X-Application-Context": "config-server:6201", "status": "200" } }, "timeTaken": "4054" } }, { "timestamp": 1516583834702, "info": { "signal": "spring.cloud.bus.sent", "type": "RefreshRemoteApplicationEvent", "id": "7fe840f5-ccd4-45f3-a511-bf13388938a8", "origin": "config-server:6201", "destination": "**:**" } }, { "timestamp": 1516583834702, "info": { "signal": "spring.cloud.bus.ack", "event": "RefreshRemoteApplicationEvent", "id": "7fe840f5-ccd4-45f3-a511-bf13388938a8", "origin": "config-server:6201", "destination": "**" } }, { "timestamp": 1516583673848, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "host": "localhost:6201", "connection": "keep-alive", "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36", "upgrade-insecure-requests": "1", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "accept-encoding": "gzip, deflate, br", "accept-language": "zh-CN,zh;q=0.9" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:14:33 GMT", "status": "200" } }, "timeTaken": "2114" } }, { "timestamp": 1516583643244, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:14:02 GMT", "status": "200" } }, "timeTaken": "1678" } }, { "timestamp": 1516583631731, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:13:51 GMT", "status": "200" } }, "timeTaken": "1260" } }, { "timestamp": 1516583608702, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:13:28 GMT", "status": "200" } }, "timeTaken": "1917" } }, { "timestamp": 1516583596624, "info": { "method": "GET", "path": "/config-client/dev/master", "headers": { "request": { "accept": "application/json, application/*+json", "user-agent": "Java/1.8.0_144", "host": "localhost:6201", "connection": "keep-alive" }, "response": { "X-Application-Context": "config-server:6201", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Mon, 22 Jan 2018 01:13:16 GMT", "status": "200" } }, "timeTaken": "4715" } }]

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册