消息订阅机制(C语言)

​ 为了实现代码的解耦,根据设计模式中的观察者模式设计了一个消息订阅的机制。当某个模块需要得到某些消息的通知时,可以使用topic_subscribe订阅想要的消息,并将回调函数注册进去,当消息使用msg_notify通知时,会触发已经注册了的回调;


整体代码如下:

.h文件

#ifndef __MSG_SUBSCRIBE_H_
#define __MSG_SUBSCRIBE_H_

/*消息的类型都在这个枚举里*/
typedef enum
{
    ON_OFF_STATE,
    LIGHTNESS,
    WORK_MODE,
    TOPIC_MAX
}topic_e;

typedef void (*observer_handle)(void* msg);

typedef struct
{
    observer_handle func;
    topic_e topic;
}observer_t;

/*消息订阅并将回调注册进去*/
int topic_subscribe(observer_t *observer);
/*消息通知*/
int msg_notify(topic_e topic, void *msg);

#endif /* __MSG_SUBSCRIBE_H_ */

.c文件

#include <stdio.h>
#include "msg_subscribe.h"

typedef struct _Link_List
{
    observer_handle func;
    struct _Link_List* next;
}Observer_Node;

Observer_Node* func_head_node[TOPIC_MAX] = {NULL};

int topic_subscribe(observer_t* observer)
{
    assert(observer->topic >= TOPIC_MAX || observer->func == NULL || observer == NULL);

    Observer_Node* func_link_tail = func_head_node[observer->topic];
    if(func_link_tail != NULL)
    {
        while(func_link_tail->next != NULL)
        {
            func_link_tail = func_link_tail->next;
        }
    }

    Observer_Node* observer_node = (Observer_Node*)malloc(sizeof(Observer_Node));
    assert(observer_node == NULL)
        
    observer_node->func = observer->func;
    observer_node->next = NULL;

    if(func_link_tail == NULL)
    {
        func_head_node[observer->topic] = observer_node;
    } 
    else
    {
        func_link_tail->next = observer_node;
    }
    return 0;
}


int msg_notify(topic_e topic, void* msg)
{
    assert(topic >= TOPIC_MAX);

    Observer_Node* cur_node = func_head_node[topic];
    printf("Msg notify %d\r\n", topic);

    while(cur_node != NULL)
    {
        assert(cur_node->func == NULL);
        cur_node->func(msg);
        cur_node = cur_node->next;
    }
    return 0;
}

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在同一个ROS2节点中包含多个订阅者和发布者的多线程功能,可以使用ROS2提供的多线程库来实现。 具体步骤如下: 1. 创建一个ROS2节点,并在其中创建多个订阅者和发布者。 2. 使用C语言的多线程库,如pthread,创建多个线程。 3. 在每个线程中,分别订阅和发布相应的消息。 4. 在每个线程中,使用ROS2提供的rcl_wait函数来等待消息到达。 5. 在每个线程中,使用ROS2提供的rcl_take函数来获取消息并处理。 6. 在每个线程中,使用ROS2提供的rcl_publish函数来发布消息。 7. 确保在每个线程中使用适当的同步机制,以避免竞争条件和死锁等问题。 8. 在程序结束时,使用ROS2提供的rcl_shutdown函数来关闭节点并释放资源。 以下是一个简单的示例代码,演示了如何在同一个ROS2节点中使用多线程处理多个订阅者和发布者: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include "rcl/rcl.h" #include "std_msgs/msg/int32.h" rcl_node_t node; rcl_subscription_t sub1, sub2; rcl_publisher_t pub1, pub2; void* thread1(void* arg) { rcl_wait_set_t wait_set; rcl_ret_t ret; std_msgs__msg__Int32 msg; rcl_wait_set_init(&wait_set, 2, 0, 0, 0, 0, rcl_get_default_allocator()); while(1) { rcl_wait(&wait_set, RCL_MS_TO_NS(10)); if(rcl_wait_set_is_ready(&wait_set, &sub1)) { ret = rcl_take(&sub1, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 1 received message: %d\n", msg.data); msg.data *= 2; rcl_publish(&pub1, &msg, NULL); } } if(rcl_wait_set_is_ready(&wait_set, &sub2)) { ret = rcl_take(&sub2, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 1 received message: %d\n", msg.data); msg.data *= 2; rcl_publish(&pub2, &msg, NULL); } } } rcl_wait_set_fini(&wait_set); return NULL; } void* thread2(void* arg) { rcl_wait_set_t wait_set; rcl_ret_t ret; std_msgs__msg__Int32 msg; rcl_wait_set_init(&wait_set, 2, 0, 0, 0, 0, rcl_get_default_allocator()); while(1) { rcl_wait(&wait_set, RCL_MS_TO_NS(10)); if(rcl_wait_set_is_ready(&wait_set, &sub1)) { ret = rcl_take(&sub1, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 2 received message: %d\n", msg.data); msg.data *= 3; rcl_publish(&pub1, &msg, NULL); } } if(rcl_wait_set_is_ready(&wait_set, &sub2)) { ret = rcl_take(&sub2, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 2 received message: %d\n", msg.data); msg.data *= 3; rcl_publish(&pub2, &msg, NULL); } } } rcl_wait_set_fini(&wait_set); return NULL; } int main(int argc, char** argv) { rcl_init_options_t options = rcl_get_zero_initialized_init_options(); rcl_init_options_init(&options, rcl_get_default_allocator()); rcl_init(argc, argv, &options, &node); rcl_subscription_init(&sub1, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "topic1", NULL); rcl_subscription_init(&sub2, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "topic2", NULL); rcl_publisher_init(&pub1, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "output1", NULL); rcl_publisher_init(&pub2, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "output2", NULL); pthread_t t1, t2; pthread_create(&t1, NULL, thread1, NULL); pthread_create(&t2, NULL, thread2, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); rcl_subscription_fini(&sub1, &node); rcl_subscription_fini(&sub2, &node); rcl_publisher_fini(&pub1, &node); rcl_publisher_fini(&pub2, &node); rcl_node_fini(&node); rcl_shutdown(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值