New file |
| | |
| | | #define BUS_MAP_KEY 1 |
| | | #define BUS_KEY 8 |
New file |
| | |
| | | #include "bus_server_socket_wrapper.h" |
| | | #include "key_def.h" |
| | | static Logger *logger = LoggerFactory::getLogger(); |
| | | |
| | | |
| | | /** |
| | | * 创建 |
| | | */ |
| | | void * bus_server_socket_wrapper_open() { |
| | | |
| | | NetModSocket *sockt = new NetModSocket; |
| | | return (void *)sockt; |
| | | } |
| | | |
| | | /** |
| | | * 关闭 |
| | | */ |
| | | void bus_server_socket_wrapper_close(void *_socket) { |
| | | NetModSocket *sockt = (NetModSocket *)_socket; |
| | | delete sockt; |
| | | } |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int bus_server_socket_wrapper_start_bus(void * _socket) { |
| | | int ret; |
| | | NetModSocket *sockt = (NetModSocket *)_socket; |
| | | |
| | | if( (ret = sockt->bind(BUS_KEY)) == 0) { |
| | | sockt->start_bus(); |
| | | return 0; |
| | | } else { |
| | | logger->error("start bus failed"); |
| | | return -1; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | #ifndef _BUS_SERVER_SOCKET_WRAPPER_H_ |
| | | #define _BUS_SERVER_SOCKET_WRAPPER_H_ |
| | | |
| | | #include "net_mod_socket.h" |
| | | |
| | | #ifdef __cplusplus |
| | | extern "C" { |
| | | #endif |
| | | |
| | | /** |
| | | * 创建 |
| | | */ |
| | | void * bus_server_socket_wrapper_open(); |
| | | |
| | | /** |
| | | * 关闭 |
| | | */ |
| | | void bus_server_socket_wrapper_close(void *_sockt); |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int bus_server_socket_wrapper_start_bus(void * _socket); |
| | | |
| | | |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | | |
| | | #endif |
| | |
| | | #include "socket_io.h" |
| | | #include "net_mod_socket_io.h" |
| | | #include "net_mod_socket.h" |
| | | #include "key_def.h" |
| | | |
| | | static Logger * logger = LoggerFactory::getLogger(); |
| | | |
| | |
| | | // LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | response_head.key = request_head.key; |
| | | // response_head.key = request_head.key; |
| | | |
| | | if(request_head.timeout > 0) { |
| | | timeout.tv_sec = request_head.timeout / 1000; |
| | | timeout.tv_nsec = (request_head.timeout - timeout.tv_sec * 1000) * 10e6; |
| | | ret = shmModSocket.pub_timeout((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key, &timeout); |
| | | ret = shmModSocket.pub_timeout((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY, &timeout); |
| | | } |
| | | else if(request_head.timeout == 0) { |
| | | ret = shmModSocket.pub_nowait((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | ret = shmModSocket.pub_nowait((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY); |
| | | } |
| | | else if(request_head.timeout == -1) { |
| | | ret = shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | ret = shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY); |
| | | } |
| | | |
| | | response_head.code = ret; |
| | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_start_bus(void * _socket) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->start_bus(); |
| | | } |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size, int port) { |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub((char *)topic, size, port); |
| | | return sockt->sockt->sub((char *)topic, size, BUS_KEY); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){ |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int sec, int nsec){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub_timeout((char *)topic, size, port, sec, nsec); |
| | | return sockt->sockt->sub_timeout((char *)topic, size, BUS_KEY, sec, nsec); |
| | | } |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size, int port){ |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub_nowait((char *)topic, size, port); |
| | | return sockt->sockt->sub_nowait((char *)topic, size, BUS_KEY); |
| | | } |
| | | |
| | | |
| | |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size, int port) { |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub((char *)topic, size, port); |
| | | return sockt->sockt->desub((char *)topic, size, BUS_KEY); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec) { |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int sec, int nsec) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub_timeout((char *)topic, size, port, sec, nsec); |
| | | return sockt->sockt->desub_timeout((char *)topic, size, BUS_KEY, sec, nsec); |
| | | } |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size, int port){ |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub_nowait((char *)topic, size, port); |
| | | return sockt->sockt->desub_nowait((char *)topic, size, BUS_KEY); |
| | | } |
| | | |
| | | |
| | |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_start_bus(void * _socket); |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size, int key); |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size, int key); |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int sec, int nsec); |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size); |
| | | |
| | | |
| | | /** |
| | |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size, int key); |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size, int key); |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int sec, int nsec); |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size); |
| | | |
| | | |
| | | /** |
| | |
| | | #include "hashtable.h" |
| | | #include "sem_util.h" |
| | | #include "logger_factory.h" |
| | | #include "key_def.h" |
| | | #include <set> |
| | | |
| | | #define ACTION_LIDENTIFIER "<**" |
| | |
| | | #define TOPIC_LIDENTIFIER "{" |
| | | #define TOPIC_RIDENTIFIER "}" |
| | | |
| | | #define BUS_MAP_KEY 1 |
| | | |
| | | //typedef std::basic_string<char, std::char_traits<char>, SHM_STL_Allocator<char> > SHMString; |
| | | typedef std::set<int, std::less<int>, SHM_STL_Allocator<int> > SHMKeySet; |
| | | typedef std::map<SHMString, SHMKeySet *, std::less<SHMString>, SHM_STL_Allocator<std::pair<const SHMString, SHMKeySet *> > > SHMTopicSubMap; |
| | |
| | | |
| | | ./test_net_mod_socket --fun="start_net_client" \ |
| | | --sendlist="localhost:5000:100" \ |
| | | --publist="localhost:5000:8" |
| | | --publist="localhost:5000" |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | function mpub() { |
| | | ./test_net_mod_socket --fun="test_net_pub_threads" \ |
| | | --publist="localhost:5000:8, localhost:5000:8" |
| | | --publist="localhost:5000, localhost:5000" |
| | | |
| | | } |
| | | |
| | |
| | | #include "net_mod_server_socket_wrapper.h" |
| | | #include "net_mod_socket_wrapper.h" |
| | | #include "bus_server_socket_wrapper.h" |
| | | |
| | | #include "shm_mm_wraper.h" |
| | | #include "usg_common.h" |
| | | #include <getopt.h> |
| | |
| | | } |
| | | |
| | | |
| | | void start_bus_server(int key) { |
| | | void start_bus_server() { |
| | | printf("Start bus server\n"); |
| | | void * server_socket = net_mod_socket_open(); |
| | | |
| | | net_mod_socket_bind(server_socket, key); |
| | | |
| | | net_mod_socket_start_bus(server_socket); |
| | | void * server_socket = bus_server_socket_wrapper_open(); |
| | | if(bus_server_socket_wrapper_start_bus(server_socket) != 0) { |
| | | printf("start bus failed\n"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | else if(strcmp(action, "desub") == 0) { |
| | | printf("Please input buskey and topic!\n"); |
| | | printf("Please input topic!\n"); |
| | | |
| | | scanf("%d %s", &buskey, topic); |
| | | if (net_mod_socket_desub(client, topic, strlen(topic), buskey) == 0) { |
| | | scanf("%s", topic); |
| | | if (net_mod_socket_desub(client, topic, strlen(topic)) == 0) { |
| | | printf("%d Desub success!\n", net_mod_socket_get_key(client)); |
| | | } else { |
| | | printf("Desub failture!\n"); |
| | |
| | | |
| | | } |
| | | else if(strcmp(action, "sub") == 0) { |
| | | printf("Please input buskey and topic!\n"); |
| | | scanf("%d %s",&buskey, topic); |
| | | printf("Please input topic!\n"); |
| | | scanf("%s",topic); |
| | | |
| | | printf("===%d %s\n",buskey, topic); |
| | | if (net_mod_socket_sub(client, topic, strlen(topic), buskey) == 0) { |
| | | if (net_mod_socket_sub(client, topic, strlen(topic)) == 0) { |
| | | printf("%d Sub success!\n", net_mod_socket_get_key(client)); |
| | | } else { |
| | | printf("Sub failture!\n"); |
| | |
| | | usage(argv[0]); |
| | | exit(1); |
| | | } |
| | | start_bus_server(opt.key); |
| | | start_bus_server(); |
| | | } |
| | | else if (strcmp("start_reply", opt.fun) == 0) { |
| | | if(opt.key == 0) { |
| | |
| | | for(i = 0; i < entry_arr_len; i++) { |
| | | property_arr_len = str_split(entry_arr[i], ":", &property_arr); |
| | | // printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]); |
| | | node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), atoi(property_arr[2])}; |
| | | free(entry_arr[i]); |
| | | node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), 0}; |
| | | |
| | | free(property_arr[1]); |
| | | if(property_arr_len == 3) { |
| | | node_arr[i].key = atoi(property_arr[2]); |
| | | free(property_arr[2]); |
| | | } |
| | | free(entry_arr[i]); |
| | | |
| | | } |
| | | *node_arr_addr = node_arr; |
| | | |
| | | |