wangzhengquan
2020-11-26 72b7aebb0022f8e391c999348763acd5f7a16133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "net_mod_server_socket_wrapper.h"
#include "net_mod_socket_wrapper.h"
#include "shm_mm.h"
#include "usg_common.h"
#include <getopt.h>
 
typedef struct Targ {
    int port;
    int id;
 
}Targ;
 
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 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;
 
 
    net_mod_recv_msg_t *recv_arr;
    //192.168.20.104
  int node_arr_size = 3;
    net_node_t node_arr[] = {
        {"192.168.5.22", port, 11},
        {"192.168.20.10", port, 11},
        {"192.168.20.104", port, 11}
    };
 
    int pub_node_arr_size = 3;
    net_node_t pub_node_arr[] = {
        {"192.168.5.22", port, 8},
        {"192.168.20.10", port, 8},
        {"192.168.20.104", port, 8}
    };
    
  while (true) {
    //printf("Usage: pub <topic> [content] or sub <topic>\n");
    printf("Can I help you? pub, send or quit\n");
    scanf("%s",action);
    
    if(strcmp(action, "pub") == 0) {
        printf("Please input topic and content\n");
      scanf("%s %s", topic, content);
 
        n = net_mod_socket_pub(client, pub_node_arr, pub_node_arr_size, topic, strlen(topic)+1, content, strlen(content)+1);
        printf("pub %d nodes\n", n);
    }
    else if(strcmp(action, "send") == 0) {
        getc(stdin);
        printf("Please input  content\n");
        
          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);
            for(i=0; i<recv_arr_size; i++) {
                printf("host:%s, port: %d, key:%d, content: %s\n", 
                    recv_arr[i].host,
                    recv_arr[i].port,
                    recv_arr[i].key,
                    recv_arr[i].content
                );
            }
            
                // 使用完后,不要忘记释放掉
            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 {
      printf("error input argument\n");
      continue;
    }
   
  }
  net_mod_socket_close(client);
 
  
}
 
 
#define  SCALE  100000
 
void *runclient(void *arg) {
  Targ *targ = (Targ *)arg;
  int port = targ->port;
  char sendbuf[512];
 
  int i,j, n, recv_arr_size;
  net_mod_recv_msg_t *recv_arr;
 
  int node_arr_size = 1;
    //192.168.20.104
    net_node_t node_arr[] = {
        //{0, port, 11}
    {"192.168.20.10", port, 11}
    // {"192.168.20.104", port, 11}
    };
 
  void * client = net_mod_socket_open();
    
    char filename[512];
    sprintf(filename, "test%d.tmp", targ->id);
    FILE *fp = NULL;
    fp = fopen(filename, "w+");
    // fp = stdout;
 
    int recvsize;
    void *recvbuf;
  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);
    for(j=0; j < recv_arr_size; j++) {
        fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", 
            recv_arr[j].host,
            recv_arr[j].port,
            recv_arr[j].key,
            recv_arr[j].content
        );
    }
        // 使用完后,不要忘记释放掉
        net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
  }
  fclose(fp);
  net_mod_socket_close(client);
  return (void *)i;
}
 
void start_net_mclient(int port) {
 
  int status, i = 0, processors = 1;
  void *res[processors];
  // Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
  Targ targs[processors];
  pthread_t tids[processors];
  char sendbuf[512];
  struct timeval start, end;
  long total = 0;
  
printf("开始测试...\n");  
  gettimeofday(&start, NULL);
  for (i = 0; i < processors; i++) {
    targs[i].port = port;
    targs[i].id = i;
    pthread_create(&tids[i], NULL, runclient, (void *)&targs[i]);
  }
 
  for (i = 0; i < processors; i++) {
    if (pthread_join(tids[i], &res[i]) != 0) {
      perror("multyThreadClient pthread_join");
    } else {
        total += (long)res[i];
      //fprintf(stderr, "client(%d) 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }
 
  gettimeofday(&end, NULL);
 
  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 );
  // 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);
 
 // port = atoi(argv[2]);
     
 
    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);
  }
 
  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);
 
  }
 
 
}