From 72b7aebb0022f8e391c999348763acd5f7a16133 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 26 十一月 2020 18:56:33 +0800
Subject: [PATCH] update

---
 test_net_socket/test_net_mod_socket.c |  227 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 210 insertions(+), 17 deletions(-)

diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index f389c59..403530f 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -1,8 +1,8 @@
 #include "net_mod_server_socket_wrapper.h"
 #include "net_mod_socket_wrapper.h"
 #include "shm_mm.h"
-#include "dgram_mod_socket.h"
 #include "usg_common.h"
+#include <getopt.h>
 
 typedef struct Targ {
 	int port;
@@ -10,18 +10,20 @@
 
 }Targ;
 
-void server(int port) {
+void start_net_proxy(int port) {
+  printf("Start net proxy\n");
 	void *serverSocket  = net_mod_server_socket_open(port);
 	if(net_mod_server_socket_start(serverSocket) != 0) {
 		err_exit(errno, "net_mod_server_socket_start");
 	}
 }
 
-void client(int port ){
+void start_net_client(int port ){
 	void * client = net_mod_socket_open();
 	char content[MAXLINE];
 	char action[512];
   char topic[512];
+  int buskey;
 	
 	int recv_arr_size, i, n;
 
@@ -75,6 +77,29 @@
 		    net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
 		  }
     }
+    else if(strcmp(action, "desub") == 0) {
+      printf("Please input buskey topic!\n");
+       
+      scanf("%d %s", buskey, topic);
+      if (net_mod_socket_desub(client, topic, strlen(topic),  buskey) == 0) {
+         printf("%d Desub success!\n", net_mod_socket_get_key(client));
+      } else {
+        printf("Desub failture!\n");
+        exit(0);
+      }
+     
+    } 
+    else if(strcmp(action, "sub") == 0) {
+      printf("Please input topic!\n");
+      scanf("%s", topic);
+      if (net_mod_socket_sub(client, topic, strlen(topic),  buskey) == 0) {
+         printf("%d Sub success!\n", net_mod_socket_get_key(client));
+      } else {
+        printf("Sub failture!\n");
+        exit(0);
+      }
+     
+    } 
     else if(strcmp(action, "quit") == 0) {
       break;
     } else {
@@ -138,7 +163,7 @@
   return (void *)i;
 }
 
-void mclient(int port) {
+void start_net_mclient(int port) {
 
   int status, i = 0, processors = 1;
   void *res[processors];
@@ -175,27 +200,195 @@
   // fflush(stdout);
 }
 
+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) {
+  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);
+  }
+}
+
+
+
+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");
+  fpe("-p, --port                           TCP/IP Port\n");
+  fpe("-k, --key                            SHM Key\n");
+  fpe("\n");
+}
+
+struct argument_t {
+  char *fun;
+  int port;
+  int key;
+  char **cmd_arr;
+  int cmd_arr_len;
+};
+
+argument_t parse_args (int argc, char *argv[])
+{
+  int c;
+
+  if(argc < 2) {
+    usage(argv[0]);
+    exit(1);
+  }
+
+  if(argc == 2 && strcmp(argv[1], "--help") == 0) {
+    usage(argv[0]);
+    exit(0);
+  }
+
+  
+  argument_t mopt = {};
+  
+  // mopt.volume_list_size = 0;
+
+  opterr = 0;
+
+  static struct option long_options[] =
+  {
+    /* These options set a flag. */
+     
+    {"fun",  required_argument, 0, 'f'},
+    {"key",  required_argument, 0, 'k'},
+    {"port",  required_argument, 0, 'p'},
+    {0, 0, 0, 0}
+  };
+  /* getopt_long stores the option index here. */
+  int option_index = 0;
+  while (1)
+  {
+    
+
+    c = getopt_long (argc, argv, "+f:k:p:", long_options, &option_index);
+
+    /* Detect the end of the options. */
+    if (c == -1)
+      break;
+
+    switch (c)
+    {
+    case 0:
+      // printf("ffffffff\n");
+      /* If this option set a flag, do nothing else now. */
+      if (long_options[option_index].flag != 0)
+        break;
+      printf ("option %s", long_options[option_index].name);
+      if (optarg)
+        printf (" with arg %s", optarg);
+      printf ("\n");
+      break;
+     
+    case 'f':
+      mopt.fun = optarg;
+      break;
+
+    case 'k':
+      mopt.key = atoi(optarg);
+      break;
+
+    case 'p':
+       // printf ("==name with value `%s'\n", optarg);
+      mopt.port = atoi(optarg);
+      break;
+
+    case '?':
+     // printf ("==? optopt=%c, %s, `%s', %d\n", optopt, optarg, argv[optind], optind);
+      /* getopt_long already printed an error message. */
+      usage(argv[0]);
+      exit(1);
+      break;
+
+    default:
+      //printf ("==default optopt=%c, %s, `%s'\n",optopt, optarg,  argv[optind]);
+      break;
+    }
+  }
+
+  // printf ("optind = %d, argc=%d \n", optind, argc);
+  /* Print any remaining command line arguments (not options). */
+  if (optind < argc)
+  {
+    mopt.cmd_arr = &argv[optind];
+    mopt.cmd_arr_len = argc - optind;
+    // printf ("non-option ARGV-elements: ");
+    // while (optind < argc)
+    //   printf ("%d, %d, %s \n", optind, argc, argv[optind++]);
+    // putchar ('\n');
+  }
+  return mopt;
+
+}
 int main(int argc, char *argv[]) {
 	shm_init(512);
+ 
+  argument_t opt = parse_args(argc, argv);
 
-	int port;
-  if (argc < 3) {
-    fprintf(stderr, "Usage: %s %s|%s <PORT> \n", argv[0],  "server", "client");
-    return 1;
-  }
-
-  port = atoi(argv[2]);
+ // port = atoi(argv[2]);
 	 
 
-	if (strcmp("server", argv[1]) == 0 ) {
-     server(port);
+	if (strcmp("start_net_proxy", opt.fun) == 0 ) {
+    if(opt.port == 0) {
+      usage(argv[0]);
+      exit(1);
+    }
+    start_net_proxy(opt.port);
+    
+  }
+  else if (strcmp("start_bus_server", opt.fun) == 0) {
+    if(opt.key == 0) {
+      usage(argv[0]);
+      exit(1);
+    }
+    start_bus_server(opt.key);
+  }
+  else if (strcmp("start_reply", opt.fun) == 0) {
+    if(opt.key == 0) {
+      usage(argv[0]);
+      exit(1);
+    }
+    start_reply(opt.key);
   }
 
-  if (strcmp("client", argv[1]) == 0)
-     client(port);
+  else if (strcmp("start_net_client", opt.fun) == 0) {
+    if(opt.port == 0) {
+      usage(argv[0]);
+      exit(1);
+    }
+    start_net_client(opt.port);
+  }
+  else {
+    usage(argv[0]);
+    exit(1);
 
-  if (strcmp("mclient", argv[1]) == 0)
-    mclient(port);
+  }
+
+
 }
 
 

--
Gitblit v1.8.0