From e4130d29e80a40884310481ded90ff845c614f43 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 24 十二月 2020 10:03:14 +0800
Subject: [PATCH] bus force bind

---
 test_net_socket/test_net_mod_socket.c |   75 ++++++++++++++++++++++++++++++-------
 1 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index 1b6cf33..2b2a56a 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -15,6 +15,7 @@
 }Targ;
 
 struct argument_t {
+  bool interactive;
   char *fun;
   int port;
   int key;
@@ -33,9 +34,37 @@
 
 void * client;
 
-void start_net_proxy(int port) {
+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);
+      shm_mm_wrapper_destroy();
+      break;
+    } else {
+      printf("Invalide action\n");
+    }
+  }  
+}
+
+void start_net_proxy(argument_t &arg) {
+  pthread_t tid;
   printf("Start net proxy\n");
-	void *serverSocket  = net_mod_server_socket_open(port);
+	void *serverSocket  = net_mod_server_socket_open(arg.port);
+
+   // 鍒涘缓涓�涓嚎绋�,鍙互鍏抽棴server
+  if(arg.interactive) {
+    pthread_create(&tid, NULL, proxy_server_handler, serverSocket);
+  }
+  
 	if(net_mod_server_socket_start(serverSocket) != 0) {
 		err_exit(errno, "net_mod_server_socket_start");
 	}
@@ -56,7 +85,7 @@
 
 
 void *bus_handler(void *sockt) {
-  pthread_detach(pthread_self());
+  // pthread_detach(pthread_self());
   
   char action[512];
   while ( true) {
@@ -78,15 +107,21 @@
 
 
 
-void start_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
-  pthread_create(&tid, NULL, bus_handler, server_socket);
+  if(arg.interactive)
+    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);
+  }
+
+  if (pthread_join(tid, NULL) != 0) {
+    perror(" pthread_join");
   }
 }
 
@@ -106,6 +141,7 @@
     sprintf(sendbuf, "RECEIVED  PORT %d NAME %s", remote_port, recvbuf);
     net_mod_socket_sendto(client, sendbuf, strlen(sendbuf) + 1, remote_port);
     free(recvbuf);
+    sleep(1000);
   }
 }
 
@@ -127,12 +163,12 @@
   //192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.5.104:5000:11
   net_node_t *node_arr;
   int node_arr_size = parse_node_list(sendlist, &node_arr);
-	// print_node_list(node_arr, node_arr_size);
+	print_node_list(node_arr, node_arr_size);
 
   //192.168.5.10:5000:8, 192.168.5.22:5000:8, 192.168.5.104:5000:8
   net_node_t *pub_node_arr;
 	int pub_node_arr_size = parse_node_list(publist, &pub_node_arr);
-  // print_node_list(pub_node_arr, pub_node_arr_size);
+  print_node_list(pub_node_arr, pub_node_arr_size);
 	
   while (true) {
     //printf("Usage: pub <topic> [content] or sub <topic>\n");
@@ -228,8 +264,9 @@
   for (i = 0; i < SCALE; i++) {
     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);
-    //printf("send %d nodes\n", n);
+    // 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);
     for(j=0; j < recv_arr_size; j++) {
     	fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", 
     		recv_arr[j].host,
@@ -380,12 +417,12 @@
       usage(argv[0]);
       exit(1);
     }
-    start_net_proxy(opt.port);
+    start_net_proxy(opt);
     
   }
   else if (strcmp("start_bus_server", opt.fun) == 0) {
      
-    start_bus_server();
+    start_bus_server(opt);
   }
   else if (strcmp("start_reply", opt.fun) == 0) {
     if(opt.key == 0) {
@@ -432,6 +469,9 @@
 
   }
 
+  printf("==========end========\n");
+  shm_mm_wrapper_destroy();
+
 }
 
 
@@ -469,6 +509,7 @@
   argument_t mopt = {};
   
   // mopt.volume_list_size = 0;
+  mopt.interactive = false;
 
   opterr = 0;
 
@@ -480,6 +521,7 @@
     {"fun",  required_argument, 0, 'f'},
     {"key",  required_argument, 0, 'k'},
     {"port",  required_argument, 0, 'p'},
+    {"interactive",  no_argument, 0, 'i'},
     {"sendlist",  required_argument, (int *)mopt.sendlist, 0},
     {"publist",  required_argument, (int *)mopt.publist, 0},
     {0, 0, 0, 0}
@@ -490,7 +532,7 @@
   {
     
 
-    c = getopt_long (argc, argv, "+f:k:p:", long_options, &option_index);
+    c = getopt_long (argc, argv, "+f:k:p:i", long_options, &option_index);
 
     /* Detect the end of the options. */
     if (c == -1)
@@ -524,6 +566,10 @@
 
     case 'k':
       mopt.key = atoi(optarg);
+      break;
+
+    case 'i':
+      mopt.interactive = true;
       break;
 
     case 'p':
@@ -580,7 +626,8 @@
   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]);
+   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]);
@@ -600,7 +647,7 @@
 void print_node_list(net_node_t *node_arr, int len) {
   printf("============node list begin==========\n");
   for(int i = 0; i < len; i++) {
-    printf("%s,%d,%d,\n", node_arr[i].host,  node_arr[i].port,  node_arr[i].key);
+    printf("host=%s, port=%d, key=%d \n", node_arr[i].host,  node_arr[i].port,  node_arr[i].key);
   }
   printf("============node list end==========\n");
 }

--
Gitblit v1.8.0