| | |
| | | #include "shm_mm_wrapper.h" |
| | | #include "usg_common.h" |
| | | #include <getopt.h> |
| | | #include "logger_factory.h" |
| | | |
| | | #define SCALE 100000 |
| | | |
| | |
| | | int remote_port; |
| | | while ( (rv = net_mod_socket_recvfrom(client, &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); |
| | | sprintf(sendbuf, "RECEIVED: %s", recvbuf); |
| | | net_mod_socket_sendto(client, sendbuf, strlen(sendbuf) + 1, remote_port); |
| | | free(recvbuf); |
| | | } |
| | |
| | | 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, |
| | |
| | | Targ *targ = (Targ *)arg; |
| | | char sendbuf[512]; |
| | | |
| | | int i,j, n, recv_arr_size; |
| | | int i,j, n; |
| | | int recv_arr_size; |
| | | net_mod_recv_msg_t *recv_arr; |
| | | int total = 0; |
| | | |
| | |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | 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, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000); |
| | | printf("%d: send %d nodes\n", i, n); |
| | | for(j=0; j < recv_arr_size; j++) { |
| | | fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", |
| | | fprintf(fp, "reply from (host:%s, port: %d, key:%d) >> %s\n", |
| | | recv_arr[j].host, |
| | | recv_arr[j].port, |
| | | recv_arr[j].key, |
| | |
| | | return (void *)total; |
| | | } |
| | | |
| | | //多线程send |
| | | void test_net_sendandrecv_threads(char *nodelist) { |
| | | |
| | | int status, i = 0, processors = 1; |
| | | int status, i = 0, processors = 4; |
| | | void *res[processors]; |
| | | // Targ *targs = (Targ *)calloc(processors, sizeof(Targ)); |
| | | Targ targs[processors]; |
| | |
| | | |
| | | } |
| | | |
| | | // 无限循环send |
| | | void test_net_sendandrecv(char *nodelist) { |
| | | |
| | | int n, i; |
| | | 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 content[128]; |
| | | |
| | | sprintf(content, "pid:%ld say Hello!!", (long)getpid()); |
| | | client = net_mod_socket_open(); |
| | | while(true) { |
| | | 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++) { |
| | | LoggerFactory::getLogger()->debug("%ld received reply from (host:%s, port: %d, key:%d) >> %s\n", (long)getpid(), |
| | | recv_arr[i].host, |
| | | recv_arr[i].port, |
| | | recv_arr[i].key, |
| | | recv_arr[i].content |
| | | ); |
| | | } |
| | | |
| | | // 使用完后,不要忘记释放掉 |
| | | net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); |
| | | } |
| | | |
| | | 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; |
| | |
| | | |
| | | 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); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | | |
| | | test_net_sendandrecv_threads(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) { |
| | | fprintf(stderr, "Missing publist .\n"); |
| | |
| | | |
| | | 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 { |
| | | usage(argv[0]); |