学习笔记:openwrt listen & send 方式实现进程间通信源码
来源:互联网 发布:谷歌拍照软件 编辑:程序博客网 时间:2024/06/08 03:04
学习笔记:openwrt listen & send 方式实现进程间通信源码
源码: git clone https://github.com/heenbo/ubus_listen_send_test.git
Makefile
CROSS_COMPILE = mips-openwrt-linux-CC = gccSOURCE = client1.c client2.cPROJECT_CLIENT1 = client1.oPROJECT_CLIENT2 = client2.oPROJECT = $(PROJECT_CLIENT1) $(PROJECT_CLIENT2)OBJECT_CLIENT1 = client1 OBJECT_CLIENT2 = client2OBJECT = $(OBJECT_CLIENT1) $(OBJECT_CLIENT2)LDFLAGS = -L/home/heenbo/work/openwrt-bb/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib -lblobmsg_json -ljson-c -lubox -lubus -pthreadCFLAGS = -g -I/home/heenbo/work/openwrt-bb/build_dir/target-mips_34kc_uClibc-0.9.33.2/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include$(OBJECT) : $(PROJECT) ${CROSS_COMPILE}${CC} $(PROJECT_CLIENT1) -o $(OBJECT_CLIENT1) ${LDFLAGS} ${CROSS_COMPILE}${CC} $(PROJECT_CLIENT2) -o $(OBJECT_CLIENT2) ${LDFLAGS}$(PROJECT) : $(SOURCE) ${CROSS_COMPILE}${CC} -c $^ ${CFLAGS}clean : -rm -f $(PROJECT) $(OBJECT) *.o
client1.c
#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <libubox/ustream.h>#include <libubox/blobmsg_json.h>#include <pthread.h>#include "libubus.h"static struct ubus_context *ctx_listen = NULL;static struct ubus_context *ctx_send = NULL;static struct blob_buf blob_buf_send;static struct ubus_event_handler listener;void * pthread_ubus_send(void * arg);#define UBUS_LISTEN_OBJ_STR "client1"#define UBUS_SEND_OBJ_STR "client2"static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg){ char *str; str = blobmsg_format_json(msg, true); printf("{ \"%s\": %s }\n", type, str); free(str);}static void listen_main(void){ const char *event_listen = NULL; int ret = 0; memset(&listener, 0, sizeof(listener)); listener.cb = receive_event; event_listen = UBUS_LISTEN_OBJ_STR; ret = ubus_register_event_handler(ctx_listen, &listener, event_listen); if (ret) { fprintf(stderr, "Error while registering for event '%s': %s\n", event_listen, ubus_strerror(ret)); return; } uloop_init(); ubus_add_uloop(ctx_listen); uloop_run(); uloop_done();}static int my_ubus_send(char *id, char *json_msg){ int i = 0; blob_buf_init(&blob_buf_send, 0); if (!blobmsg_add_json_from_string(&blob_buf_send, json_msg)) { fprintf(stderr, "Failed to parse message data\n"); return -1; } ubus_send_event(ctx_send, id, blob_buf_send.head); return 0;}void * pthread_ubus_send(void * arg){ int ret = 0; char buf[128] = {0}; char buf_send[256] = {0}; while(1) { scanf("%s", buf); printf("----------------------- input is %s\n", buf); sprintf(buf_send, "{\"heenbo\":\"%s\"}", buf); my_ubus_send(UBUS_SEND_OBJ_STR, buf_send); fflush(stdin); } return NULL;}int main(int argc, char **argv){ const char *ubus_socket_listen = NULL; const char *ubus_socket_send = NULL; printf("sw version: %s, %s\n", __DATE__, __TIME__); ctx_listen = ubus_connect(ubus_socket_listen); if (!ctx_listen) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ctx_send = ubus_connect(ubus_socket_send); if (!ctx_send) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ctx_send->sock.registered = true; pthread_t pthread_ubus_send_id = -1; pthread_create(&pthread_ubus_send_id, NULL, pthread_ubus_send, NULL); listen_main(); ubus_free(ctx_listen); return 0;}
client2.c
#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <libubox/ustream.h>#include <libubox/blobmsg_json.h>#include <pthread.h>#include "libubus.h"static struct ubus_context *ctx_listen = NULL;static struct ubus_context *ctx_send = NULL;static struct blob_buf blob_buf_send;static struct ubus_event_handler listener;void * pthread_ubus_send(void * arg);#define UBUS_LISTEN_OBJ_STR "client2"#define UBUS_SEND_OBJ_STR "client1"static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg){ char *str; str = blobmsg_format_json(msg, true); printf("{ \"%s\": %s }\n", type, str); free(str);}static void listen_main(void){ const char *event_listen = NULL; int ret = 0; memset(&listener, 0, sizeof(listener)); listener.cb = receive_event; event_listen = UBUS_LISTEN_OBJ_STR; ret = ubus_register_event_handler(ctx_listen, &listener, event_listen); if (ret) { fprintf(stderr, "Error while registering for event '%s': %s\n", event_listen, ubus_strerror(ret)); return; } uloop_init(); ubus_add_uloop(ctx_listen); uloop_run(); uloop_done();}static int my_ubus_send(char *id, char *json_msg){ int i = 0; blob_buf_init(&blob_buf_send, 0); if (!blobmsg_add_json_from_string(&blob_buf_send, json_msg)) { fprintf(stderr, "Failed to parse message data\n"); return -1; } ubus_send_event(ctx_send, id, blob_buf_send.head); return 0;}void * pthread_ubus_send(void * arg){ int ret = 0; char buf[128] = {0}; char buf_send[256] = {0}; while(1) { scanf("%s", buf); printf("----------------------- input is %s\n", buf); sprintf(buf_send, "{\"heenbo\":\"%s\"}", buf); my_ubus_send(UBUS_SEND_OBJ_STR, buf_send); fflush(stdin); } return NULL;}int main(int argc, char **argv){ const char *ubus_socket_listen = NULL; const char *ubus_socket_send = NULL; printf("sw version: %s, %s\n", __DATE__, __TIME__); ctx_listen = ubus_connect(ubus_socket_listen); if (!ctx_listen) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ctx_send = ubus_connect(ubus_socket_send); if (!ctx_send) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ctx_send->sock.registered = true; pthread_t pthread_ubus_send_id = -1; pthread_create(&pthread_ubus_send_id, NULL, pthread_ubus_send, NULL); listen_main(); ubus_free(ctx_listen); return 0;}
0 0
- 学习笔记:openwrt listen & send 方式实现进程间通信源码
- 学习笔记:openwrt listen & send 合并在同一个进程准备用于IPC源码(epoll_fd 在 send event 完成后关闭导致listen终止的问题)
- [openwrt] ubus实现进程间通信举例
- tcp通信:多进程共享listen socket方式
- 【Android学习笔记】Android进程间通信方式Messenger
- openwrt中使用ubus实现进程通信
- openwrt中使用ubus实现进程通信
- openwrt中使用ubus实现进程通信
- openwrt中使用ubus实现进程通信
- openwrt中使用ubus实现进程通信
- openwrt中使用ubus实现进程通信
- 学习笔记 Android 使用AIDL实现进程间通信
- 进程间通信--管道的实现方式
- IPC-(进程间通信)-实现方式
- 学习笔记--进程及线程间通信方式的区别及联系
- 进程间通信方式
- 进程间通信方式
- 进程间通信方式
- Linux C——带IO缓冲的文件操作函数
- 【NOIP2011普及组T4】表达式的值-模拟+栈
- codeforces 732D (二分 贪心)
- mongoose
- 转圈游戏noip+快速幂
- 学习笔记:openwrt listen & send 方式实现进程间通信源码
- JSTL
- 贝叶斯分类原理:
- H.264视频码流解析2
- 应用沙盒
- shell入门
- Linux防火墙
- typedef和define的对比
- linux MakeFile教程详解