| | |
| | | #include "usg_common.h" |
| | | #include "dgram_mod_socket.h" |
| | | #include "shm_socket.h" |
| | | #include "shm_allocator.h" |
| | | #include "mem_pool.h" |
| | | #include "hashtable.h" |
| | | #include "sem_util.h" |
| | | #include "logger_factory.h" |
| | | #include <set> |
| | | |
| | | #define ACTION_LIDENTIFIER "<**" |
| | | #define ACTION_RIDENTIFIER "**>" |
| | | #define TOPIC_LIDENTIFIER "{" |
| | | #define TOPIC_RIDENTIFIER "}" |
| | | |
| | | enum socket_mod_t |
| | | { |
| | | PULL_PUSH = 1, |
| | | REQ_REP = 2, |
| | | PAIR = 3, |
| | | PUB_SUB = 4, |
| | | SURVEY = 5, |
| | | BUS = 6 |
| | | |
| | | }; |
| | | |
| | | static Logger logger = LoggerFactory::getLogger(); |
| | | #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<SHMString, SHMKeySet *> > > SHMTopicSubMap; |
| | | #include "shm_mod_socket.h" |
| | | |
| | | typedef struct dgram_mod_socket_t { |
| | | shm_socket_t *shm_socket; |
| | | socket_mod_t mod; |
| | | // pthread_t recv_thread; |
| | | // <主题, 订阅者> |
| | | SHMTopicSubMap *topic_sub_map; |
| | | ShmModSocket *m_socket; |
| | | |
| | | } dgram_mod_socket_t; |
| | | |
| | | static int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len ); |
| | | void * run_pubsub_proxy(dgram_mod_socket_t * socket) ; |
| | | |
| | | int dgram_mod_remove_keys(int keys[], int length){ |
| | | return ShmModSocket::remove_keys(keys, length); |
| | | } |
| | | |
| | | int dgram_mod_remove_key(int key){ |
| | | int keys[] = {key}; |
| | | return ShmModSocket::remove_keys(keys, 1); |
| | | } |
| | | /** |
| | | * 创建socket |
| | | * @return socket地址 |
| | | */ |
| | | void *dgram_mod_open_socket() { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t)); |
| | | // socket->mod = (socket_mod_t)mod; |
| | | socket->shm_socket = shm_open_socket(SHM_SOCKET_DGRAM); |
| | | socket->m_socket = new ShmModSocket; |
| | | return (void *)socket; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 关闭socket |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_close_socket(void * _socket) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; |
| | | SHMKeySet *subscripter_set; |
| | | SHMTopicSubMap::iterator map_iter; |
| | | |
| | | if(topic_sub_map != NULL) { |
| | | for (map_iter = topic_sub_map->begin(); map_iter != topic_sub_map->end(); map_iter++) { |
| | | subscripter_set = map_iter->second; |
| | | subscripter_set->clear(); |
| | | mm_free((void *)subscripter_set); |
| | | //delete subscripter_set; |
| | | // printf("=============delete subscripter_set\n"); |
| | | } |
| | | topic_sub_map->clear(); |
| | | mem_pool_free_by_key(BUS_MAP_KEY); |
| | | } |
| | | // printf("=============close socket\n"); |
| | | shm_close_socket(socket->shm_socket); |
| | | delete socket->m_socket; |
| | | free(_socket); |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | int dgram_mod_bind(void * _socket, int port){ |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_bind(void * _socket, int port) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return shm_socket_bind(socket->shm_socket, port); |
| | | return socket->m_socket->bind(port); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_force_bind(void * _socket, int port) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return shm_socket_force_bind(socket->shm_socket, port); |
| | | return socket->m_socket->force_bind(port); |
| | | } |
| | | |
| | | int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port) { |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return shm_sendto(socket->shm_socket, buf, size, port); |
| | | |
| | | return socket->m_socket->sendto(buf, size,port); |
| | | } |
| | | |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->sendto_timeout(buf, size, port, &timeout); |
| | | } |
| | | |
| | | // 发送信息立刻返回。 |
| | | int dgram_mod_sendto_nowait(void *_socket, const void *buf, const int size, const int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->sendto_nowait(buf, size,port); |
| | | } |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) { |
| | | |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | if(socket->mod == BUS) { |
| | | err_exit(0, "Can not use method recvfrom in a Bus"); |
| | | } |
| | | // printf("dgram_mod_recvfrom before\n"); |
| | | int rv = shm_recvfrom(socket->shm_socket, buf, size, port); |
| | | // printf("dgram_mod_recvfrom after\n"); |
| | | return rv; |
| | | return socket->m_socket->recvfrom(buf, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) { |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size); |
| | | |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->recvfrom_timeout(buf, size, port, &timeout); |
| | | } |
| | | |
| | | |
| | | |
| | | int dgram_mod_get_port(void * _socket) { |
| | | int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->shm_socket->port; |
| | | return socket->m_socket->recvfrom_nowait(buf, size, port); |
| | | } |
| | | |
| | | /** |
| | | * 发送请求信息并等待接收应答 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->sendandrecv(send_buf, send_size, port, recv_buf, recv_size); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_sendandrecv_timeout(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->sendandrecv_timeout(send_buf, send_size, port, recv_buf, recv_size, &timeout); |
| | | } |
| | | int dgram_mod_sendandrecv_nowait(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->sendandrecv_nowait(send_buf, send_size, port, recv_buf, recv_size); |
| | | } |
| | | |
| | | |
| | | void dgram_mod_free(void *buf) { |
| | | free(buf); |
| | | } |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int dgram_mod_start_bus(void * _socket) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | socket->mod = BUS; |
| | | socket->topic_sub_map = mem_pool_attach<SHMTopicSubMap>(BUS_MAP_KEY); |
| | | |
| | | run_pubsub_proxy(socket); |
| | | // pthread_t tid; |
| | | // pthread_create(&tid, NULL, run_accept_sub_request, _socket); |
| | | return 0; |
| | | |
| | | return socket->m_socket->start_bus(); |
| | | } |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int dgram_mod_sub(void * _socket, void *topic, int size, int port) { |
| | | int dgram_mod_sub(void * _socket, void *topic, int size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | char buf[8192]; |
| | | snprintf(buf, 8192, "%ssub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, (char *)topic, TOPIC_RIDENTIFIER); |
| | | return shm_sendto(socket->shm_socket, buf, strlen(buf) + 1, port); |
| | | return socket->m_socket->sub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->sub_timeout((char *)topic, size, port, &timeout); |
| | | } |
| | | int dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->sub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port) { |
| | | |
| | | int dgram_mod_desub(void * _socket, void *topic, int size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | int head_len; |
| | | char buf[8192+content_size]; |
| | | snprintf(buf, 8192, "%spub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, (char *)topic, TOPIC_RIDENTIFIER); |
| | | head_len = strlen(buf); |
| | | memcpy(buf+head_len, content, content_size); |
| | | return shm_sendto(socket->shm_socket, buf, head_len+content_size, port); |
| | | |
| | | return socket->m_socket->desub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->desub_timeout((char *)topic, size, port, &timeout); |
| | | } |
| | | int dgram_mod_desub_nowait(void * _socket, void *topic, int size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->desub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | //========================================================================================================================== |
| | | |
| | | /* |
| | | * 处理订阅 |
| | | */ |
| | | void _proxy_sub(dgram_mod_socket_t *socket, char *topic, int port) { |
| | | SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; |
| | | SHMKeySet *subscripter_set; |
| | | |
| | | SHMTopicSubMap::iterator map_iter; |
| | | SHMKeySet::iterator set_iter; |
| | | |
| | | if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) { |
| | | subscripter_set = map_iter->second; |
| | | } else { |
| | | void *set_ptr = mm_malloc(sizeof(SHMKeySet)); |
| | | subscripter_set = new(set_ptr) SHMKeySet; |
| | | topic_sub_map->insert({topic, subscripter_set}); |
| | | } |
| | | subscripter_set->insert(port); |
| | | /** |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @port 总线端口 |
| | | */ |
| | | int dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->pub((char *)topic, topic_size, content, content_size, port); |
| | | } |
| | | |
| | | /* |
| | | * 处理发布,代理转发 |
| | | */ |
| | | void _proxy_pub(dgram_mod_socket_t * socket, char *topic, size_t head_len, void *buf, size_t size, int port) { |
| | | SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; |
| | | SHMKeySet *subscripter_set; |
| | | |
| | | SHMTopicSubMap::iterator map_iter; |
| | | SHMKeySet::iterator set_iter; |
| | | |
| | | std::vector<int> subscripter_to_del; |
| | | std::vector<int>::iterator vector_iter; |
| | | |
| | | int send_port; |
| | | struct timespec timeout = {1,0}; |
| | | |
| | | if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) { |
| | | subscripter_set = map_iter->second; |
| | | for(set_iter = subscripter_set->begin(); set_iter != subscripter_set->end(); set_iter++) { |
| | | send_port = *set_iter; |
| | | // printf("_proxy_pub send before %d \n", send_port); |
| | | if (shm_sendto(socket->shm_socket, buf+head_len, size-head_len, send_port, &timeout) !=0 ) { |
| | | //对方已关闭的连接放到待删除队列里。如果直接删除会让iter指针出现错乱 |
| | | subscripter_to_del.push_back(send_port); |
| | | } else { |
| | | // printf("_proxy_pub send after: %d \n", send_port); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | // 删除已关闭的端 |
| | | for(vector_iter = subscripter_to_del.begin(); vector_iter != subscripter_to_del.end(); vector_iter++) { |
| | | if((set_iter = subscripter_set->find(*vector_iter)) != subscripter_set->end()) { |
| | | subscripter_set->erase(set_iter); |
| | | printf("remove closed subscripter %d \n", send_port); |
| | | } |
| | | } |
| | | subscripter_to_del.clear(); |
| | | |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int dgram_mod_pub_timeout(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | struct timespec timeout = {sec, nsec}; |
| | | return socket->m_socket->pub_timeout((char *)topic, topic_size, content, content_size, port, &timeout); |
| | | } |
| | | |
| | | void * run_pubsub_proxy(dgram_mod_socket_t * socket) { |
| | | // pthread_detach(pthread_self()); |
| | | int size; |
| | | int port; |
| | | char * action, *topic, *topics, *buf; |
| | | size_t head_len; |
| | | |
| | | const char *topic_delim = ","; |
| | | // printf("run_pubsub_proxy server receive before\n"); |
| | | while(shm_recvfrom(socket->shm_socket, (void **)&buf, &size, &port) == 0) { |
| | | // printf("run_pubsub_proxy server recv after: %s \n", buf); |
| | | if(parse_pubsub_topic(buf, size, &action, &topics, &head_len)) { |
| | | if(strcmp(action, "sub") == 0) { |
| | | // 订阅支持多主题订阅 |
| | | topic = trim(strtok(topics, topic_delim), NULL); |
| | | while(topic) { |
| | | _proxy_sub(socket, topic, port); |
| | | topic = trim(strtok(NULL, topic_delim), NULL); |
| | | } |
| | | |
| | | } else if(strcmp(action, "pub") == 0) { |
| | | _proxy_pub(socket, topics, head_len, buf, size, port); |
| | | } |
| | | |
| | | free(action); |
| | | free(topics); |
| | | } else { |
| | | err_msg(0, "incorrect format msg"); |
| | | } |
| | | free(buf); |
| | | } |
| | | return NULL; |
| | | int dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){ |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->pub_nowait((char *)topic, topic_size, content, content_size, port); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @str "<**sub**>{经济}" |
| | | * 获取soket端口号 |
| | | */ |
| | | int dgram_mod_get_port(void * _socket) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | return socket->m_socket->get_port(); |
| | | } |
| | | |
| | | static int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len ) { |
| | | char *ptr = str; |
| | | char *str_end_ptr = str + size; |
| | | char *action_start_ptr; |
| | | char *action_end_ptr; |
| | | size_t action_len = 0; |
| | | |
| | | char *topic_start_ptr; |
| | | char *topic_end_ptr; |
| | | size_t topic_len = 0; |
| | | |
| | | // if (strlen(identifier) > strlen(str)) { |
| | | // return 0; |
| | | // } |
| | | |
| | | if (strncmp(ptr, ACTION_LIDENTIFIER, strlen(ACTION_LIDENTIFIER)) == 0) { |
| | | ptr += strlen(ACTION_LIDENTIFIER); |
| | | action_start_ptr = ptr; |
| | | while(strncmp(++ptr, ACTION_RIDENTIFIER, strlen(ACTION_RIDENTIFIER)) != 0) { |
| | | if(ptr >= str_end_ptr) { |
| | | return 0; |
| | | } |
| | | } |
| | | // printf("%s\n", ptr); |
| | | action_end_ptr = ptr; |
| | | action_len = action_end_ptr - action_start_ptr; |
| | | ptr += strlen(ACTION_RIDENTIFIER); |
| | | // printf("%s\n", ptr); |
| | | // printf("%s\n", str_end_ptr-1); |
| | | if(strncmp(ptr, TOPIC_LIDENTIFIER, strlen(TOPIC_LIDENTIFIER)) == 0 ) { |
| | | topic_start_ptr = ptr+strlen(TOPIC_LIDENTIFIER); |
| | | |
| | | |
| | | while(strncmp(++ptr, TOPIC_RIDENTIFIER, strlen(TOPIC_RIDENTIFIER)) != 0) { |
| | | if(ptr >= str_end_ptr) { |
| | | return 0; |
| | | } |
| | | } |
| | | topic_end_ptr = ptr; |
| | | topic_len = topic_end_ptr - topic_start_ptr; |
| | | |
| | | ptr += strlen(TOPIC_RIDENTIFIER); |
| | | |
| | | } else { |
| | | return 0; |
| | | } |
| | | } else { |
| | | return 0; |
| | | } |
| | | |
| | | char *topic = (char *)calloc(1, topic_len+1); |
| | | strncpy(topic, topic_start_ptr, topic_len); |
| | | *_topic = topic; |
| | | |
| | | char *action = (char *)calloc(1, action_len+1); |
| | | strncpy(action, action_start_ptr, action_len); |
| | | *_action = action; |
| | | *head_len = ptr-str; |
| | | |
| | | return 1; |
| | | /** |
| | | * 释放存储接收信息的buf |
| | | */ |
| | | void dgram_mod_free(void *buf) { |
| | | free(buf); |
| | | } |