From b90ba316b54db321d3e8aaac7df93b46d80b9d9c Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 22 十二月 2020 15:47:41 +0800
Subject: [PATCH] 三个没有回收的信号

---
 test_net_socket/test_net_mod_socket.c |  183 +++++++++++++++++++++++++++++++--------------
 1 files changed, 124 insertions(+), 59 deletions(-)

diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index 8dafcfc..f777773 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -1,8 +1,12 @@
 #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 "usg_common.h"
 #include <getopt.h>
+
+#define  SCALE  100000
 
 typedef struct Targ {
 	char *nodelist;
@@ -25,15 +29,43 @@
 int parse_node_list(char *str, net_node_t *node_arr_addr[]) ;
 void print_node_list(net_node_t *node_arr, int len);
 
+
+
+void * client;
+
+void *proxy_server_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) {
+      net_mod_server_socket_close(sockt);
+      break;
+    } else {
+      printf("Invalide action\n");
+    }
+  }  
+}
+
 void start_net_proxy(int port) {
+  pthread_t tid;
   printf("Start net proxy\n");
 	void *serverSocket  = net_mod_server_socket_open(port);
+
+   // 鍒涘缓涓�涓嚎绋�,鍙互鍏抽棴server
+  pthread_create(&tid, NULL, proxy_server_handler, serverSocket);
 	if(net_mod_server_socket_start(serverSocket) != 0) {
 		err_exit(errno, "net_mod_server_socket_start");
 	}
 }
 
-
+// 鎵撳嵃鎺ュ彈鍒扮殑璁㈤槄娑堟伅
 void *print_sub_msg(void *sockt) {
   pthread_detach(pthread_self());
   void *recvbuf;
@@ -46,8 +78,64 @@
   
 }
 
+
+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");
+    }
+  }
+  
+}
+
+
+
+void start_bus_server() {
+  printf("Start bus server\n");
+  void * server_socket = bus_server_socket_wrapper_open();
+  pthread_t tid;
+  // 鍒涘缓涓�涓嚎绋�,鍙互鍏抽棴bus
+  // pthread_create(&tid, NULL, bus_handler, server_socket);
+  if(bus_server_socket_wrapper_start_bus(server_socket) != 0) {
+    printf("start bus failed\n");
+    exit(1);
+  }
+}
+
+ 
+
+void start_reply(int key) {
+  printf("start reply\n");
+  void *client = net_mod_socket_open();
+  net_mod_socket_bind(client, 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) {
+   // 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);
+    free(recvbuf);
+  }
+}
+
+// 浜や簰寮忓鎴风
 void start_net_client(char *sendlist, char*publist ){
-	void * client = net_mod_socket_open();
+	client = net_mod_socket_open();
 	char content[MAXLINE];
 	char action[512];
   char topic[512];
@@ -57,6 +145,7 @@
 	net_mod_recv_msg_t *recv_arr;
 
   pthread_t tid;
+  // 鍒涘缓涓�涓嚎绋嬫帴鍙楄闃呮秷鎭�
   pthread_create(&tid, NULL, print_sub_msg, client);
 	 
   //192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.5.104:5000:11
@@ -87,9 +176,8 @@
     	
 		  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);
-		    printf("send %d nodes\n", n);
+		    n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
+		    printf(" %d nodes reply\n", n);
 		    for(i=0; i<recv_arr_size; i++) {
 		    	printf("host:%s, port: %d, key:%d, content: %s\n", 
 		    		recv_arr[i].host,
@@ -104,10 +192,10 @@
 		  }
     }
     else if(strcmp(action, "desub") == 0) {
-      printf("Please input buskey and topic!\n");
+      printf("Please input topic!\n");
        
-      scanf("%d %s", &buskey, topic);
-      if (net_mod_socket_desub(client, topic, strlen(topic),  buskey) == 0) {
+      scanf("%s", topic);
+      if (net_mod_socket_desub(client, topic, strlen(topic)) == 0) {
          printf("%d Desub success!\n", net_mod_socket_get_key(client));
       } else {
         printf("Desub failture!\n");
@@ -116,11 +204,10 @@
      
     } 
     else if(strcmp(action, "sub") == 0) {
-      printf("Please input buskey and topic!\n");
-      scanf("%d %s",&buskey, topic);
+      printf("Please input topic!\n");
+      scanf("%s",topic);
 
-      printf("===%d %s\n",buskey, topic);
-      if (net_mod_socket_sub(client, topic, strlen(topic),  buskey) == 0) {
+      if (net_mod_socket_sub(client, topic, strlen(topic)) == 0) {
          printf("%d Sub success!\n", net_mod_socket_get_key(client));
       } else {
         printf("Sub failture!\n");
@@ -141,39 +228,6 @@
   
 }
 
-
-void start_bus_server(int key) {
-  printf("Start bus server\n");
-  void * server_socket = net_mod_socket_open();
-  
-  net_mod_socket_bind(server_socket, key);
-   
-  net_mod_socket_start_bus(server_socket);
-}
-
- 
-
-void start_reply(int key) {
-  printf("start reply\n");
-  void *socket = net_mod_socket_open();
-  net_mod_socket_bind(socket, key);
-  int size;
-  void *recvbuf;
-  char sendbuf[512];
-  int rv;
-  int remote_port;
-  while ( (rv = net_mod_socket_recvfrom(socket, &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(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
-    free(recvbuf);
-  }
-}
-
- 
-
-#define  SCALE  100000
-
 void *_run_sendandrecv_(void *arg) {
   Targ *targ = (Targ *)arg;
   char sendbuf[512];
@@ -187,8 +241,6 @@
   int node_arr_size = parse_node_list(targ->nodelist, &node_arr);
  
 
-  void * client = net_mod_socket_open();
-	
 	char filename[512];
 	sprintf(filename, "test%d.tmp", targ->id);
 	FILE *fp = NULL;
@@ -215,7 +267,7 @@
     total += n;
   }
   fclose(fp);
-  net_mod_socket_close(client);
+  
   return (void *)total;
 }
 
@@ -230,6 +282,7 @@
   struct timeval start, end;
   long total = 0;
   
+  client = net_mod_socket_open();
   printf("寮�濮嬫祴璇�...\n");  
   gettimeofday(&start, NULL);
   for (i = 0; i < processors; i++) {
@@ -254,6 +307,7 @@
   long diffusec = difftime - diffsec*1000000;
   fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total );
   // fflush(stdout);
+  net_mod_socket_close(client);
 }
 
 
@@ -269,7 +323,7 @@
  
   char *topic = "news";
 
-  void * client = net_mod_socket_open();
+ 
   
   // char filename[512];
   // sprintf(filename, "test%d.tmp", targ->id);
@@ -287,7 +341,7 @@
     total += n;
   }
   // fclose(fp);
-  net_mod_socket_close(client);
+ 
   return (void *)total;
 }
 
@@ -301,6 +355,7 @@
   char sendbuf[512];
   struct timeval start, end;
   long total = 0;
+  client = net_mod_socket_open();
   
 printf("寮�濮嬫祴璇�...\n");  
   gettimeofday(&start, NULL);
@@ -326,6 +381,7 @@
   long diffusec = difftime - diffsec*1000000;
   fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total );
   // fflush(stdout);
+  net_mod_socket_close(client);
 }
 
 
@@ -338,6 +394,10 @@
 
  // 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) {
@@ -348,11 +408,8 @@
     
   }
   else if (strcmp("start_bus_server", opt.fun) == 0) {
-    if(opt.key == 0) {
-      usage(argv[0]);
-      exit(1);
-    }
-    start_bus_server(opt.key);
+     
+    start_bus_server();
   }
   else if (strcmp("start_reply", opt.fun) == 0) {
     if(opt.key == 0) {
@@ -398,6 +455,9 @@
     exit(1);
 
   }
+
+  printf("==========end========\n");
+  shm_mm_wrapper_destroy();
 
 }
 
@@ -547,11 +607,16 @@
   net_node_t *node_arr = (net_node_t *) calloc(entry_arr_len, sizeof(net_node_t));
   for(i = 0; i < entry_arr_len; i++) {
     property_arr_len = str_split(entry_arr[i], ":", &property_arr);
-  printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]);
-    node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), atoi(property_arr[2])};
-    free(entry_arr[i]);
+  // printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]);
+    node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), 0};
+   
     free(property_arr[1]);
-    free(property_arr[2]);
+    if(property_arr_len == 3) {
+      node_arr[i].key = atoi(property_arr[2]);
+      free(property_arr[2]);
+    }
+    free(entry_arr[i]);
+   
   }
   *node_arr_addr = node_arr;
 

--
Gitblit v1.8.0