| | |
| | | #include <assert.h> |
| | | #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 "shm_mm_wrapper.h" |
| | | #include "usg_common.h" |
| | | #include <getopt.h> |
| | | #include "logger_factory.h" |
| | | |
| | | #define SCALE 100000 |
| | | |
| | | typedef struct Targ { |
| | | net_node_t *node; |
| | | char *nodelist; |
| | | int id; |
| | | long id; |
| | | |
| | | }Targ; |
| | | |
| | |
| | | |
| | | argument_t parse_args (int argc, char *argv[]); |
| | | void usage(char *name); |
| | | int parse_node_list(char *str, net_node_t *node_arr_addr[]) ; |
| | | int parse_node_list(const char *str, net_node_t *node_arr_addr[]) ; |
| | | void print_node_list(net_node_t *node_arr, int len); |
| | | |
| | | |
| | |
| | | void *recvbuf; |
| | | int size; |
| | | int key; |
| | | while (net_mod_socket_recvfrom( sockt, &recvbuf, &size, &key) == 0) { |
| | | printf("收到订阅消息:%s\n", recvbuf); |
| | | int rv; |
| | | while ((rv = net_mod_socket_recvfrom( sockt, &recvbuf, &size, &key) ) == 0) { |
| | | printf("收到订阅消息:%s\n", (char *)recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | |
| | | printf("print_sub_msg return . rv = %d\n", rv); |
| | | |
| | | } |
| | | |
| | |
| | | |
| | | void start_reply(int key) { |
| | | printf("start reply\n"); |
| | | void *client = net_mod_socket_open(); |
| | | net_mod_socket_bind(client, key); |
| | | void *ser = net_mod_socket_open(); |
| | | net_mod_socket_bind(ser, key); |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | int rv; |
| | | int remote_port; |
| | | while ( (rv = net_mod_socket_recvfrom(client, &recvbuf, &size, &remote_port) ) == 0) { |
| | | while ( (rv = net_mod_socket_recvfrom(ser, &recvbuf, &size, &remote_port) ) == 0) { |
| | | // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf); |
| | | sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf); |
| | | net_mod_socket_sendto(client, sendbuf, strlen(sendbuf) + 1, remote_port); |
| | | sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(ser), (char *)recvbuf); |
| | | net_mod_socket_sendto(ser, sendbuf, strlen(sendbuf) + 1, remote_port); |
| | | free(recvbuf); |
| | | } |
| | | } |
| | |
| | | |
| | | if (fgets(content, MAXLINE, stdin) != NULL) { |
| | | // 收到消息的节点即使没有对应的信息, 也要回复一个表示无的消息,否则会一直等待 |
| | | n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size); |
| | | // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size); |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size, 1000); |
| | | printf(" %d nodes reply\n", n); |
| | | for(i=0; i<recv_arr_size; i++) { |
| | | printf("host:%s, port: %d, key:%d, content: %s\n", |
| | | printf("reply from (host:%s, port: %d, key:%d) >> %s\n", |
| | | recv_arr[i].host, |
| | | recv_arr[i].port, |
| | | recv_arr[i].key, |
| | | recv_arr[i].content |
| | | (char *)recv_arr[i].content |
| | | ); |
| | | } |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | void *_run_sendandrecv_(void *arg) { |
| | | void *_run_one_sendto_many_(void *arg) { |
| | | Targ *targ = (Targ *)arg; |
| | | char sendbuf[512]; |
| | | char sendbuf[128]; |
| | | |
| | | int i,j, n, recv_arr_size; |
| | | int j, n; |
| | | int recv_arr_size; |
| | | net_mod_recv_msg_t *recv_arr; |
| | | int total = 0; |
| | | |
| | | void *client = net_mod_socket_open(); |
| | | net_node_t *node_arr; |
| | | int node_arr_size = parse_node_list(targ->nodelist, &node_arr); |
| | | |
| | | int rkey, lkey; |
| | | unsigned int l = 0 , rl; |
| | | const char *hello_format = "%d say Hello %d"; |
| | | const char *reply_format = "%d RECEIVED %d say Hello %d"; |
| | | |
| | | char filename[512]; |
| | | sprintf(filename, "test%d.tmp", targ->id); |
| | | char filename[128]; |
| | | sprintf(filename, "test%d.tmp", targ->node->key); |
| | | FILE *fp = NULL; |
| | | fp = fopen(filename, "w+"); |
| | | // fp = stdout; |
| | | |
| | | int recvsize; |
| | | void *recvbuf; |
| | | for (i = 0; i < SCALE; i++) { |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | fprintf(fp, "requst:%s\n", sendbuf); |
| | | for (l = 0; l < SCALE; l++) { |
| | | sprintf(sendbuf, hello_format, net_mod_socket_get_key(client), l); |
| | | // fprintf(fp, "requst:%s\n", sendbuf); |
| | | // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000); |
| | | printf("send %d nodes\n", n); |
| | | n = net_mod_socket_sendandrecv_timeout(client, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1); |
| | | printf("%d: send %d nodes\n", l, n); |
| | | for(j=0; j < recv_arr_size; j++) { |
| | | fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", |
| | | recv_arr[j].host, |
| | | recv_arr[j].port, |
| | | recv_arr[j].key, |
| | | recv_arr[j].content |
| | | ); |
| | | |
| | | fprintf(stdout, "%d send '%s' to %d. received from (host=%s, port= %d, key=%d) '%s'\n", |
| | | net_mod_socket_get_key(client), |
| | | sendbuf, |
| | | targ->node->key, |
| | | recv_arr[j].host, |
| | | recv_arr[j].port, |
| | | recv_arr[j].key, |
| | | (char *)recv_arr[j].content |
| | | ); |
| | | |
| | | printf("key == %d\n", net_mod_socket_get_key(client)); |
| | | assert(sscanf((const char *)recv_arr[j].content, reply_format, &rkey, &lkey, &rl) == 3); |
| | | assert(targ->node->key == rkey); |
| | | assert(net_mod_socket_get_key(client) == lkey); |
| | | assert(rl == l); |
| | | } |
| | | // 使用完后,不要忘记释放掉 |
| | | net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); |
| | | total += n; |
| | | } |
| | | fclose(fp); |
| | | net_mod_socket_close(client); |
| | | if(fp != NULL) |
| | | fclose(fp); |
| | | // net_mod_socket_close(client); |
| | | return (void *)total; |
| | | } |
| | | |
| | | void test_net_sendandrecv_threads(char *nodelist) { |
| | | //多线程send |
| | | void one_sendto_many(char *nodelist) { |
| | | |
| | | int status, i = 0, processors = 1; |
| | | void *res[processors]; |
| | | int status, i = 0; |
| | | |
| | | // Targ *targs = (Targ *)calloc(processors, sizeof(Targ)); |
| | | Targ targs[processors]; |
| | | pthread_t tids[processors]; |
| | | |
| | | char sendbuf[512]; |
| | | struct timeval start, end; |
| | | long total = 0; |
| | | |
| | | client = net_mod_socket_open(); |
| | | net_mod_socket_bind(client, shm_mm_wrapper_alloc_key()); |
| | | |
| | | net_node_t *node_arr; |
| | | int node_arr_size = parse_node_list(nodelist, &node_arr); |
| | | Targ targs[node_arr_size]; |
| | | pthread_t tids[node_arr_size]; |
| | | void *res[node_arr_size]; |
| | | |
| | | printf("开始测试...\n"); |
| | | gettimeofday(&start, NULL); |
| | | for (i = 0; i < processors; i++) { |
| | | targs[i].nodelist = nodelist; |
| | | for (i = 0; i < node_arr_size; i++) { |
| | | targs[i].node = node_arr + i; |
| | | targs[i].id = i; |
| | | pthread_create(&tids[i], NULL, _run_sendandrecv_, (void *)&targs[i]); |
| | | pthread_create(&tids[i], NULL, _run_one_sendto_many_, (void *)&targs[i]); |
| | | } |
| | | |
| | | for (i = 0; i < processors; i++) { |
| | | for (i = 0; i < node_arr_size; i++) { |
| | | if (pthread_join(tids[i], &res[i]) != 0) { |
| | | perror("multyThreadClient pthread_join"); |
| | | } else { |
| | |
| | | |
| | | } |
| | | |
| | | // 无限循环send |
| | | void test_net_sendandrecv(char *nodelist) { |
| | | |
| | | int n, j; |
| | | void * client; |
| | | int recv_arr_size; |
| | | net_mod_recv_msg_t *recv_arr; |
| | | net_node_t *node_arr; |
| | | int node_arr_size = parse_node_list(nodelist, &node_arr); |
| | | char buf[128]; |
| | | pid_t pid, retPid ; |
| | | unsigned int l , retl; |
| | | int remoteKey; |
| | | const char *hello_format = "%d say Hello %u "; |
| | | const char *reply_format = "%d RECEIVED %d say Hello %d"; |
| | | |
| | | pid = getpid(); |
| | | l = 0; |
| | | |
| | | client = net_mod_socket_open(); |
| | | while(true) { |
| | | sprintf(buf, hello_format, pid, l); |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, buf, strlen(buf)+1, |
| | | &recv_arr, &recv_arr_size, 1); |
| | | printf(" %d nodes reply\n", n); |
| | | for(j = 0; j < recv_arr_size; j++) { |
| | | |
| | | printf("%ld send '%s' . received '%s' from (host:%s, port: %d, key:%d) \n", |
| | | (long)pid, |
| | | buf, |
| | | (char *)recv_arr[j].content, |
| | | recv_arr[j].host, |
| | | recv_arr[j].port, |
| | | recv_arr[j].key |
| | | |
| | | ); |
| | | |
| | | |
| | | |
| | | assert(sscanf((const char *)recv_arr[j].content, reply_format, &remoteKey, &retPid, &retl) == 3); |
| | | assert(retPid == pid); |
| | | assert(retl == l); |
| | | assert(remoteKey == recv_arr[j].key); |
| | | } |
| | | |
| | | // 使用完后,不要忘记释放掉 |
| | | net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); |
| | | l++; |
| | | } |
| | | |
| | | net_mod_socket_close(client); |
| | | |
| | | } |
| | | |
| | | void *_run_pub_(void *arg) { |
| | | Targ *targ = (Targ *)arg; |
| | | char sendbuf[512]; |
| | | char sendbuf[128]; |
| | | |
| | | int i,j, n; |
| | | int total = 0; |
| | |
| | | int node_arr_size = parse_node_list(targ->nodelist, &node_arr); |
| | | |
| | | char *topic = "news"; |
| | | |
| | | |
| | | |
| | | // char filename[512]; |
| | | // sprintf(filename, "test%d.tmp", targ->id); |
| | | // FILE *fp = NULL; |
| | |
| | | |
| | | |
| | | for (i = 0; i < SCALE; i++) { |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | sprintf(sendbuf, "thread(%ld) %d", targ->id, i); |
| | | |
| | | n = net_mod_socket_pub(client, node_arr, node_arr_size, topic, strlen(topic)+1, sendbuf, strlen(sendbuf)+1); |
| | | // n = net_mod_socket_pub(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); |
| | | printf( "pub:%s to %d nodes\n", sendbuf, n); |
| | | LoggerFactory::getLogger()->debug( "pub:%s to %d nodes\n", sendbuf, n); |
| | | total += n; |
| | | } |
| | | // fclose(fp); |
| | |
| | | return (void *)total; |
| | | } |
| | | |
| | | //多线程pub |
| | | void test_net_pub_threads(char *nodelist) { |
| | | |
| | | int status, i = 0, processors = 4; |
| | |
| | | // fflush(stdout); |
| | | net_mod_socket_close(client); |
| | | } |
| | | |
| | | // 无限循环pub |
| | | void test_net_pub(char *nodelist) { |
| | | |
| | | int n; |
| | | char sendbuf[512]; |
| | | net_node_t *node_arr; |
| | | int node_arr_size = parse_node_list(nodelist, &node_arr); |
| | | |
| | | char *topic = "news"; |
| | | sprintf(sendbuf, "pid:%ld: Good news!!", (long)getpid()); |
| | | |
| | | void * client = net_mod_socket_open(); |
| | | while (true) { |
| | | n = net_mod_socket_pub(client, node_arr, node_arr_size, topic, strlen(topic)+1, sendbuf, strlen(sendbuf)+1); |
| | | // n = net_mod_socket_pub(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); |
| | | LoggerFactory::getLogger()->debug( "pub to %d nodes\n", n); |
| | | } |
| | | net_mod_socket_close(client); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | | } |
| | | start_net_client(opt.sendlist, opt.publist); |
| | | } |
| | | else if (strcmp("test_net_sendandrecv_threads", opt.fun) == 0) { |
| | | else if (strcmp("one_sendto_many", opt.fun) == 0) { |
| | | if(opt.sendlist == 0) { |
| | | fprintf(stderr, "Missing sendlist .\n"); |
| | | usage(argv[0]); |
| | | exit(1); |
| | | } |
| | | |
| | | test_net_sendandrecv_threads(opt.sendlist); |
| | | one_sendto_many(opt.sendlist); |
| | | } |
| | | else if (strcmp("test_net_sendandrecv", opt.fun) == 0) { |
| | | if(opt.sendlist == 0) { |
| | | fprintf(stderr, "Missing sendlist .\n"); |
| | | usage(argv[0]); |
| | | exit(1); |
| | | } |
| | | |
| | | test_net_sendandrecv(opt.sendlist); |
| | | } |
| | | else if (strcmp("test_net_pub_threads", opt.fun) == 0) { |
| | | if(opt.publist == 0) { |
| | |
| | | } |
| | | |
| | | test_net_pub_threads(opt.publist); |
| | | } |
| | | else if (strcmp("test_net_pub", opt.fun) == 0) { |
| | | if(opt.publist == 0) { |
| | | fprintf(stderr, "Missing publist .\n"); |
| | | usage(argv[0]); |
| | | exit(1); |
| | | } |
| | | |
| | | test_net_pub(opt.publist); |
| | | } |
| | | |
| | | else { |
| | |
| | | * } |
| | | * @return 数组的长度 |
| | | */ |
| | | int parse_node_list(char *str, net_node_t *node_arr_addr[]) { |
| | | int parse_node_list(const char *str, net_node_t *node_arr_addr[]) { |
| | | int i, j; |
| | | char **property_arr; |
| | | int property_arr_len; |