From 8df1ff06b931b0e414ed435a033f508867b345b7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 23 二月 2021 14:40:38 +0800
Subject: [PATCH] update

---
 test_net_socket/test_net_mod_socket.cpp |  354 ++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 196 insertions(+), 158 deletions(-)

diff --git a/test_net_socket/test_net_mod_socket.cpp b/test_net_socket/test_net_mod_socket.cpp
index 9de375e..ab46f31 100644
--- a/test_net_socket/test_net_mod_socket.cpp
+++ b/test_net_socket/test_net_mod_socket.cpp
@@ -8,7 +8,9 @@
 #include <getopt.h>
 #include "logger_factory.h"
 
-#define  SCALE  1000000
+#define  SCALE  100000
+
+static Logger *logger = LoggerFactory::getLogger();
 
 typedef struct Targ {
   net_node_t *node;
@@ -73,6 +75,11 @@
 	}
 }
 
+void start_resycle() {
+  shm_mm_wrapper_start_resycle();
+}
+
+
 // 鎵撳嵃鎺ュ彈鍒扮殑璁㈤槄娑堟伅
 void *print_sub_msg(void *sockt) {
   pthread_detach(pthread_self());
@@ -81,7 +88,7 @@
   int key;
   int rv;
   while ((rv = net_mod_socket_recvfrom( sockt, &recvbuf, &size, &key) ) == 0) {
-    printf("鏀跺埌璁㈤槄娑堟伅:%s\n", recvbuf);
+    printf("鏀跺埌璁㈤槄娑堟伅:%s\n", (char *)recvbuf);
     free(recvbuf);
   }
 
@@ -89,65 +96,89 @@
   
 }
 
+ 
+void * bus_server;
 
-void *bus_handler(void *sockt) {
-  // pthread_detach(pthread_self());
-  
-  char action[512];
-  while ( true) {
-    printf("Input action: Close?\n");
-    if(scanf("%s",action) < 1) {
-      printf("Invalide action\n");
-      continue;
-    }
-
-    if(strcmp(action, "close") == 0) {
-      bus_server_socket_wrapper_close(sockt);
-      break;
-    } else {
-      printf("Invalide action\n");
-    }
-  }
-  
+static void stop_bus_handler(int sig) {
+  bus_server_socket_wrapper_stop(bus_server);
 }
-
 
 
 void start_bus_server(argument_t &arg) {
   printf("Start bus server\n");
-  void * server_socket = bus_server_socket_wrapper_open();
-  pthread_t tid;
-  // 鍒涘缓涓�涓嚎绋�,鍙互鍏抽棴bus
-  if(arg.interactive)
-    pthread_create(&tid, NULL, bus_handler, server_socket);
+  bus_server = bus_server_socket_wrapper_open();
+  
+  signal(SIGINT,  stop_bus_handler);
+  signal(SIGTERM,  stop_bus_handler);
 
-  if(bus_server_socket_wrapper_start_bus(server_socket) != 0) {
+  if(bus_server_socket_wrapper_start_bus(bus_server) != 0) {
     printf("start bus failed\n");
     exit(1);
   }
 
-  if (pthread_join(tid, NULL) != 0) {
-    perror(" pthread_join");
+  bus_server_socket_wrapper_close(bus_server);
+}
+
+void *serverSockt;
+
+
+static void _recvandsend_callback_(void *recvbuf, int recvsize, int key, void **sendbuf_ptr, int *sendsize_ptr, void * user_data) {
+  char sendbuf[512];
+  printf( "server: RECEIVED REQUEST FROM  %d : %s\n", key, (char *)recvbuf);
+  sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(serverSockt), (char *)recvbuf);
+  // buf 鍜� size鏄繑鍥炲��
+  *sendbuf_ptr = sendbuf;
+  *sendsize_ptr = strlen(sendbuf) + 1;
+  //recvbuf鏄垎閰嶅埌鍫嗛噷鐨勶紝浣跨敤瀹屽悗涓嶈蹇樿閲婃斁鎺�
+  free(recvbuf);
+  return;
+} 
+
+bool stop = false;
+
+static void stop_replyserver_handler(int sig) {
+  printf("stop_handler\n");
+ 
+  int rv = net_mod_socket_stop(serverSockt);
+  if(rv ==0) {
+    logger->debug("send stop suc");
+    return;
+  } else {
+    logger->debug("send stop fail.%s\n", bus_strerror(rv));
   }
 }
 
- 
+void start_reply(int mkey) {
+  logger->debug("start reply\n");
+  signal(SIGINT,  stop_replyserver_handler);
+  signal(SIGTERM,  stop_replyserver_handler);
 
-void start_reply(int key) {
-  printf("start reply\n");
-  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(ser, &recvbuf, &size, &remote_port) ) == 0) {
-   // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
-    sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(ser), recvbuf);
-    net_mod_socket_sendto(ser, sendbuf, strlen(sendbuf) + 1, remote_port);
-    free(recvbuf);
+  serverSockt = net_mod_socket_open();
+  net_mod_socket_bind(serverSockt, mkey);
+ 
+  int rv = 0 ;
+  while(  true) {
+    rv = net_mod_socket_recvandsend(serverSockt, _recvandsend_callback_ , NULL );
+    if (rv == 0)
+      continue;
+    if(rv == EBUS_STOPED) {
+      logger->debug("Stopping\n");
+      break;
+    }
+    logger->debug("net_mod_socket_recvandsend error.%s\n", bus_strerror(rv));
+
   }
+  
+  // rv = net_mod_socket_recvandsend_timeout(serverSockt, _recvandsend_callback_ , 0, 2000000, NULL );
+  net_mod_socket_close(serverSockt);
+  logger->debug("stopted\n");
+
+  // while ( (rv = net_mod_socket_recvfrom(ser, &recvbuf, &size, &key) ) == 0) {
+  //  // printf( "server: RECEIVED REQUEST FROM  %d NAME %s\n", key, recvbuf);
+  //   sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(ser), (char *)recvbuf);
+  //   net_mod_socket_sendto(ser, sendbuf, strlen(sendbuf) + 1, key);
+  //   free(recvbuf);
+  // }
 }
 
 // 浜や簰寮忓鎴风
@@ -194,14 +225,14 @@
 		  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_timeout(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size, 1000);
+        n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size, 1);
 		    printf(" %d nodes reply\n", n);
 		    for(i=0; i<recv_arr_size; i++) {
 		    	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
 		    	);
 		    }
 		    
@@ -272,18 +303,18 @@
     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, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000);
+    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, "%d send '%s' to %d. received  from (host=%s, port= %d, key=%d) '%s'\n",
+      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,
-        recv_arr[j].content
+        (char *)recv_arr[j].content
       );
 
       printf("key == %d\n", net_mod_socket_get_key(client));
@@ -344,7 +375,8 @@
   double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec);
   long diffsec = (long) (difftime/1000000);
   long diffusec = difftime - diffsec*1000000;
-  fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total );
+  fprintf(stderr,"鍙戦�佹暟鐩�:%ld, 鎴愬姛鏁扮洰: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", 
+    SCALE*node_arr_size, total, diffsec, diffusec, difftime/total );
   // fflush(stdout);
  
 }
@@ -359,34 +391,39 @@
   net_node_t *node_arr;
   int node_arr_size = parse_node_list(nodelist, &node_arr);
   char buf[128];
-  pid_t pid, rpid ;
-  unsigned int l , rl;
-  const char *hello_format = "%ld say Hello %u ";
+  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, (long)pid, l);
+    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, 1000);
     printf(" %d nodes reply\n", n);
     for(j = 0; j < recv_arr_size; j++) {
 
-      LoggerFactory::getLogger()->debug("%ld send '%s'. received '%s' from (host:%s, port: %d, key:%d) \n",
+      printf("%ld send '%s' . received '%s' from (host:%s, port: %d, key:%d) \n",
         (long)pid,
         buf,
-        recv_arr[j].content,
+        (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, hello_format, &rpid, &rl) == 2);
-      // assert(rpid == pid);
-      // assert(rl == l);
+  
+
+      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);
     }
     
     // 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀
@@ -408,7 +445,7 @@
   net_node_t *node_arr;
   int node_arr_size = parse_node_list(targ->nodelist, &node_arr);
  
-  char *topic = "news";
+  const char *topic = "news";
   // char filename[512];
   // sprintf(filename, "test%d.tmp", targ->id);
   // FILE *fp = NULL;
@@ -487,103 +524,6 @@
     LoggerFactory::getLogger()->debug( "pub to  %d nodes\n", n);
   }
   net_mod_socket_close(client);
-}
-
-
-
-
-
-
-int main(int argc, char *argv[]) {
-	shm_mm_wrapper_init(512);
- 
-  argument_t opt = parse_args(argc, argv);
-
- // port = atoi(argv[2]);
-	 
-  if(opt.fun == NULL) {
-    usage(argv[0]);
-    exit(1);
-  }
-
-	if (strcmp("start_net_proxy", opt.fun) == 0 ) {
-    if(opt.port == 0) {
-      usage(argv[0]);
-      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) {
-    if(opt.key == 0) {
-      usage(argv[0]);
-      exit(1);
-    }
-    start_reply(opt.key);
-  }
-  else if (strcmp("start_net_client", opt.fun) == 0) {
-    if(opt.sendlist == 0) {
-      fprintf(stderr, "Missing sendlist .\n");
-      usage(argv[0]);
-      exit(1);
-    }
-    if(opt.publist == 0) {
-      fprintf(stderr, "Missing publist.\n");
-      usage(argv[0]);
-      exit(1);
-    }
-    start_net_client(opt.sendlist, opt.publist);
-  }
-  else if (strcmp("one_sendto_many", opt.fun) == 0) {
-    if(opt.sendlist == 0) {
-      fprintf(stderr, "Missing sendlist .\n");
-      usage(argv[0]);
-      exit(1);
-    }
-     
-    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) {
-      fprintf(stderr, "Missing publist .\n");
-      usage(argv[0]);
-      exit(1);
-    }
-     
-    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]);
-    exit(1);
-
-  }
-
-  printf("==========end========\n");
-  shm_mm_wrapper_destroy();
-
 }
 
 
@@ -763,3 +703,101 @@
   }
   printf("============node list end==========\n");
 }
+
+
+
+
+int main(int argc, char *argv[]) {
+  shm_mm_wrapper_init(512);
+ 
+  argument_t opt = parse_args(argc, argv);
+
+ // port = atoi(argv[2]);
+   
+  if(opt.fun == NULL) {
+    usage(argv[0]);
+    exit(1);
+  }
+
+  if (strcmp("start_net_proxy", opt.fun) == 0 ) {
+    if(opt.port == 0) {
+      usage(argv[0]);
+      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) {
+    if(opt.key == 0) {
+      usage(argv[0]);
+      exit(1);
+    }
+    start_reply(opt.key);
+  }
+  else if (strcmp("start_net_client", opt.fun) == 0) {
+    if(opt.sendlist == 0) {
+      fprintf(stderr, "Missing sendlist .\n");
+      usage(argv[0]);
+      exit(1);
+    }
+    if(opt.publist == 0) {
+      fprintf(stderr, "Missing publist.\n");
+      usage(argv[0]);
+      exit(1);
+    }
+    start_net_client(opt.sendlist, opt.publist);
+  }
+  else if (strcmp("one_sendto_many", opt.fun) == 0) {
+    if(opt.sendlist == 0) {
+      fprintf(stderr, "Missing sendlist .\n");
+      usage(argv[0]);
+      exit(1);
+    }
+     
+    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) {
+      fprintf(stderr, "Missing publist .\n");
+      usage(argv[0]);
+      exit(1);
+    }
+     
+    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 if (strcmp("start_resycle", opt.fun) == 0) {
+    start_resycle();
+  }
+  
+  else {
+    usage(argv[0]);
+    exit(1);
+
+  }
+
+  printf("==========end========\n");
+  // shm_mm_wrapper_destroy();
+
+}

--
Gitblit v1.8.0