From 36e6a90a33983154633c99f7ac95d09dd68f7bcb Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期三, 24 二月 2021 16:53:06 +0800 Subject: [PATCH] update --- test_net_socket/CMakeLists.txt | 8 +- test_net_socket/shm_util.cpp | 164 ++++++++++++++++++++++++++++++++--------- src/net/net_mod_socket.h | 4 - test_net_socket/net_mod_socket.sh | 29 +++--- CMakeLists.txt | 1 src/net/net_mod_socket.cpp | 27 ------ 6 files changed, 148 insertions(+), 85 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d9be24..8cbc168 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,5 +30,4 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/test) add_subdirectory(${PROJECT_SOURCE_DIR}/test_net_socket) add_subdirectory(${PROJECT_SOURCE_DIR}/test_socket) - add_subdirectory(${PROJECT_SOURCE_DIR}/shm_util) endif() diff --git a/src/net/net_mod_socket.cpp b/src/net/net_mod_socket.cpp index a863f6d..2932d8c 100644 --- a/src/net/net_mod_socket.cpp +++ b/src/net/net_mod_socket.cpp @@ -15,37 +15,14 @@ NetModSocket::NetModSocket() { - int s; if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR) logger->error(errno, "NetModSocket::NetModSocket signal"); - - // gpool = new NetConnPool(); - - // pthread_mutexattr_t mtxAttr; - // s = pthread_mutexattr_init(&mtxAttr); - // if (s != 0) - // err_exit(s, "pthread_mutexattr_init"); - // s = pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK); - // if (s != 0) - // err_exit(s, "pthread_mutexattr_settype"); - // s = pthread_mutex_init(&sendMutex, &mtxAttr); - // if (s != 0) - // err_exit(s, "pthread_mutex_init"); - - // s = pthread_mutexattr_destroy(&mtxAttr); - // if (s != 0) - // err_exit(s, "pthread_mutexattr_destroy"); } NetModSocket::~NetModSocket() { - // int s; - // delete gpool; - // s = pthread_mutex_destroy(&sendMutex); - // if(s != 0) { - // err_exit(s, "shm_socket_close"); - // } + } @@ -193,7 +170,7 @@ err_arr[n_recv_err].key = node->key; err_arr[n_recv_err].code = ret; n_recv_err++; - logger->error("NetModSocket:: %d _sendandrecv_ to key %d failed, %s", get_key(), node->key, bus_strerror(ret)); + logger->error("NetModSocket:: _sendandrecv_ to key %d failed. %s", node->key, bus_strerror(ret)); } diff --git a/src/net/net_mod_socket.h b/src/net/net_mod_socket.h index 68f5294..b02ea6e 100644 --- a/src/net/net_mod_socket.h +++ b/src/net/net_mod_socket.h @@ -256,10 +256,6 @@ int pub_nowait( char *topic, int topic_size, void *content, int content_size, int key); - - - - /** * 鑾峰彇soket key */ diff --git a/test_net_socket/CMakeLists.txt b/test_net_socket/CMakeLists.txt index 81cf641..8abb1c2 100644 --- a/test_net_socket/CMakeLists.txt +++ b/test_net_socket/CMakeLists.txt @@ -7,9 +7,9 @@ # add the executable -add_executable(test_net_mod_socket test_net_mod_socket.cpp ${PROJECT_BINARY_DIR}/bin/net_mod_socket.sh) -target_link_libraries(test_net_mod_socket PRIVATE shm_queue ${EXTRA_LIBS} ) -target_include_directories(test_net_mod_socket PRIVATE +add_executable(shm_util shm_util.cpp ${PROJECT_BINARY_DIR}/bin/net_mod_socket.sh) +target_link_libraries(shm_util PRIVATE shm_queue ${EXTRA_LIBS} ) +target_include_directories(shm_util PRIVATE "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES} ) @@ -31,4 +31,4 @@ # add the install targets -install(TARGETS test_net_mod_socket DESTINATION bin) +install(TARGETS shm_util DESTINATION bin) diff --git a/test_net_socket/net_mod_socket.sh b/test_net_socket/net_mod_socket.sh index 7e12074..00a0d45 100755 --- a/test_net_socket/net_mod_socket.sh +++ b/test_net_socket/net_mod_socket.sh @@ -1,28 +1,27 @@ function server() { # 寮�鍚痓us - ./test_net_mod_socket --fun="start_bus_server" & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_bus_server & server_pid=$! && echo "pid: ${server_pid}" # 寮�鍚綉缁滆浆鍙戜唬鐞� - ./test_net_mod_socket --fun="start_net_proxy" --port=5000 & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_net_proxy --port=5000 & server_pid=$! && echo "pid: ${server_pid}" # 鎵撳紑璇锋眰搴旂瓟娴嬭瘯鐨勬帴鍙楃 - ./test_net_mod_socket --fun="start_reply" --key=100 & server_pid=$! && echo "pid: ${server_pid}" - ./test_net_mod_socket --fun="start_reply" --key=101 & server_pid=$! && echo "pid: ${server_pid}" - ./test_net_mod_socket --fun="start_reply" --key=102 & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_reply --key=100 & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_reply --key=101 & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_reply --key=102 & server_pid=$! && echo "pid: ${server_pid}" # 鎵撳紑鍥為槦鍒楁敹杩涚▼ - ./test_net_mod_socket --fun="start_resycle" & server_pid=$! && echo "pid: ${server_pid}" + ./shm_util start_resycle & server_pid=$! && echo "pid: ${server_pid}" } # 浜や簰寮忓鎴风 function client() { - - # ./test_net_mod_socket --fun="start_net_client" \ + # ./shm_util start_net_client \ # --sendlist="192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.20.104:5000:11" \ # --publist="192.168.5.10:5000:8, 192.168.5.22:5000:8, 192.168.20.104:5000:8" - ./test_net_mod_socket --fun="start_net_client" \ + ./shm_util start_net_client \ --sendlist=" :5000:100" \ --publist="localhost:5000" @@ -31,38 +30,38 @@ # one_to_many send function one_to_many() { - ./test_net_mod_socket --fun="one_sendto_many" \ + ./shm_util one_sendto_many \ --sendlist=" :5000:100, :5000:101, :5000:102" } # function send() { - ./test_net_mod_socket --fun="test_net_sendandrecv" \ + ./shm_util test_net_sendandrecv \ --sendlist=" :5000:100, :5000:101, :5000:102" } # 鏃犻檺寰幆 pub function pub() { - ./test_net_mod_socket --fun="test_net_pub" \ + ./shm_util test_net_pub \ --publist="localhost:5000, localhost:5000" } # 澶氱嚎绋媝ub function mpub() { - ./test_net_mod_socket --fun="test_net_pub_threads" \ + ./shm_util test_net_pub_threads \ --publist="localhost:5000, localhost:5000" } function stop() { - ps -ef | grep -e "test_net_mod_socket" -e "heart_beat"| awk '{print $2}' | xargs -i kill -15 {} + ps -ef | grep -e "shm_util" -e "heart_beat"| awk '{print $2}' | xargs -i kill -15 {} } function close() { - ps -ef | grep -e "test_net_mod_socket" -e "heart_beat"| awk '{print $2}' | xargs -i kill -9 {} + ps -ef | grep -e "shm_util" -e "heart_beat"| awk '{print $2}' | xargs -i kill -9 {} ipcrm -a } diff --git a/test_net_socket/test_net_mod_socket.cpp b/test_net_socket/shm_util.cpp similarity index 86% rename from test_net_socket/test_net_mod_socket.cpp rename to test_net_socket/shm_util.cpp index ab46f31..34efbc4 100644 --- a/test_net_socket/test_net_mod_socket.cpp +++ b/test_net_socket/shm_util.cpp @@ -21,7 +21,6 @@ struct argument_t { bool interactive; - char *fun; int port; int key; char *sendlist; @@ -526,19 +525,77 @@ net_mod_socket_close(client); } +void list () { + hashtable_t *hashtable = mm_get_hashtable(); + hashtable_foreach(hashtable, [&](int key, void * value){ + printf("%d\n", key); + }); +} + + +void remove(int key) { + hashtable_t *hashtable = mm_get_hashtable(); + + LockFreeQueue<shm_packet_t> * mqueue = (LockFreeQueue<shm_packet_t> *)hashtable_get(hashtable, key); + if(mqueue != NULL) { + delete mqueue; + hashtable_remove(hashtable, key); + } +} + +void do_sendandrecv(int key, char *sendbuf) { + int n, j; + int recv_arr_size; + net_mod_recv_msg_t *recv_arr; + + net_node_t node_arr[] = {NULL, 0, key}; + + void * client = net_mod_socket_open(); + n = net_mod_socket_sendandrecv_timeout(client, node_arr, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 5); + if(n == 0) { + printf("send failed\n"); + return; + } + printf(" %d nodes reply\n", n); + for(j=0; j < recv_arr_size; j++) { + + fprintf(stdout, "%d send '%s' to %d. received from (host=%s, port= %d, key=%d) '%s'\n\n", + net_mod_socket_get_key(client), + sendbuf, + key, + recv_arr[j].host, + recv_arr[j].port, + recv_arr[j].key, + (char *)recv_arr[j].content + ); + } + + net_mod_socket_close(client); +} + void usage(char *name) { - fprintf(stderr, "Usage: %s [OPTIONS] [ARG...]\n\n", name); - fprintf(stderr, "Test net mod socket\n\n"); - fprintf(stderr, "Options:\n\n"); #define fpe(str) fprintf(stderr, " %s", str); - fpe("-f, --funciton Function name\n"); + + fprintf(stderr, "Usage: %s {function} [OPTIONS] [ARG...]\n\n", name); + fprintf(stderr, "Test shmsocket\n\n"); + + fprintf(stderr, "Options:\n\n"); fpe("-p, --port TCP/IP Port\n"); fpe("-k, --key SHM Key\n"); fpe("--sendlist format锛�--sendlist=\"192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.20.104:5000:11\"\n"); fpe("--publist format: --publist=\"192.168.5.10:5000:8, 192.168.5.22:5000:8, 192.168.20.104:5000:8\"\n"); + fpe("\n"); + + fprintf(stderr, "Examples:\n\n"); + fpe("# sendandrecv to socket which has key 100\n"); + fpe("./shm_util sendandrecv 100 \"hello\"\n"); + fpe("# list all key\n"); + fpe("./shm_util list\n"); + fpe("# remove key 1001\n"); + fpe("./shm_util rm 1001\n"); fpe("\n"); } @@ -553,10 +610,7 @@ exit(1); } - if(argc == 2 && strcmp(argv[1], "--help") == 0) { - usage(argv[0]); - exit(0); - } + argument_t mopt = {}; @@ -570,7 +624,6 @@ { /* These options set a flag. */ - {"fun", required_argument, 0, 'f'}, {"key", required_argument, 0, 'k'}, {"port", required_argument, 0, 'p'}, {"interactive", no_argument, 0, 'i'}, @@ -612,10 +665,6 @@ break; - case 'f': - mopt.fun = optarg; - break; - case 'k': mopt.key = atoi(optarg); break; @@ -708,37 +757,78 @@ int main(int argc, char *argv[]) { - shm_mm_wrapper_init(512); - - argument_t opt = parse_args(argc, argv); + int i; + char *prog; + char * fun; + argument_t opt; - // port = atoi(argv[2]); - - if(opt.fun == NULL) { + shm_mm_wrapper_init(512); + + if(argc < 2) { usage(argv[0]); exit(1); } + prog = argv[0]; + fun = argv[1]; + argc--; + argv++; - if (strcmp("start_net_proxy", opt.fun) == 0 ) { + + if (strcmp("help", fun) == 0 ) { + usage(prog); + } + else if (strcmp("list", fun) == 0 ) { + list(); + } + else if (strcmp("rm", fun) == 0 ) { + if(argc < 2) { + usage(prog); + exit(1); + } + for(i = 1; i < argc; i++) { + int key = atoi(argv[i]); + remove(key); + } + } + else if (strcmp("sendandrecv", fun) == 0 ) { + if(argc < 3) { + usage(prog); + exit(1); + } + int key = atoi(argv[1]); + char *content = argv[2]; + do_sendandrecv(key, content); + } + else if (strcmp("start_bus_server", fun) == 0) { + + start_bus_server(opt); + } + else if (strcmp("start_resycle", fun) == 0) { + + start_resycle(); + } + + else if (strcmp("start_net_proxy", fun) == 0 ) { + opt = parse_args(argc, argv); if(opt.port == 0) { - usage(argv[0]); + usage(prog); exit(1); } start_net_proxy(opt); } - else if (strcmp("start_bus_server", opt.fun) == 0) { - - start_bus_server(opt); - } - else if (strcmp("start_reply", opt.fun) == 0) { + + else if (strcmp("start_reply", fun) == 0) { + opt = parse_args(argc, argv); + opt = parse_args(argc, argv); if(opt.key == 0) { usage(argv[0]); exit(1); } start_reply(opt.key); } - else if (strcmp("start_net_client", opt.fun) == 0) { + else if (strcmp("start_net_client", fun) == 0) { + opt = parse_args(argc, argv); if(opt.sendlist == 0) { fprintf(stderr, "Missing sendlist .\n"); usage(argv[0]); @@ -751,7 +841,8 @@ } start_net_client(opt.sendlist, opt.publist); } - else if (strcmp("one_sendto_many", opt.fun) == 0) { + else if (strcmp("one_sendto_many", fun) == 0) { + opt = parse_args(argc, argv); if(opt.sendlist == 0) { fprintf(stderr, "Missing sendlist .\n"); usage(argv[0]); @@ -760,7 +851,8 @@ one_sendto_many(opt.sendlist); } - else if (strcmp("test_net_sendandrecv", opt.fun) == 0) { + else if (strcmp("test_net_sendandrecv", fun) == 0) { + opt = parse_args(argc, argv); if(opt.sendlist == 0) { fprintf(stderr, "Missing sendlist .\n"); usage(argv[0]); @@ -769,7 +861,8 @@ test_net_sendandrecv(opt.sendlist); } - else if (strcmp("test_net_pub_threads", opt.fun) == 0) { + else if (strcmp("test_net_pub_threads", fun) == 0) { + opt = parse_args(argc, argv); if(opt.publist == 0) { fprintf(stderr, "Missing publist .\n"); usage(argv[0]); @@ -778,7 +871,8 @@ test_net_pub_threads(opt.publist); } - else if (strcmp("test_net_pub", opt.fun) == 0) { + else if (strcmp("test_net_pub", fun) == 0) { + opt = parse_args(argc, argv); if(opt.publist == 0) { fprintf(stderr, "Missing publist .\n"); usage(argv[0]); @@ -787,11 +881,9 @@ test_net_pub(opt.publist); } - else if (strcmp("start_resycle", opt.fun) == 0) { - start_resycle(); - } - + else { + printf("%Invalid funciton name\n"); usage(argv[0]); exit(1); -- Gitblit v1.8.0