wangzhengquan
2020-10-21 fd750b33a9814d02691e7681239180048173db65
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
#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"
 
typedef struct Targ {
    int port;
    int id;
 
}Targ;
 
void server(int port) {
    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 * client = net_mod_socket_open();
    char content[MAXLINE];
    char action[512];
  char topic[512];
    
    int recv_arr_size, i, n;
 
 
    net_mod_recv_msg_t *recv_arr;
    //192.168.20.104
  int node_arr_size = 2;
    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, "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 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);
}
 
int main(int argc, char *argv[]) {
    shm_init(512);
 
    int port;
  if (argc < 3) {
    fprintf(stderr, "Usage: %s %s|%s <PORT> \n", argv[0],  "server", "client");
    return 1;
  }
 
  port = atoi(argv[2]);
     
 
    if (strcmp("server", argv[1]) == 0 ) {
     server(port);
  }
 
  if (strcmp("client", argv[1]) == 0)
     client(port);
 
  if (strcmp("mclient", argv[1]) == 0)
    mclient(port);
}