zhangmeng
2024-01-18 8fc23a3bb9f49e88478a2505fa7dee434ec50c16
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
 
#include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
 
#define NODE0 "node0"
#define NODE1 "node1"
#define DATE "DATE"
 
 
typedef struct Targ {
  const char* url;
  int id;
 
}Targ;
 
void
fatal(const char *func, int rv)
{
  fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
  exit(1);
}
 
char *
date(void)
{
  time_t now = time(&now);
  struct tm *info = localtime(&now);
  char *text = asctime(info);
  text[strlen(text) - 1] = '\0'; // remove '\n'
  return (text);
}
 
int
server(const char *url)
{
  nng_socket sock;
  int rv;
  char sendbuf[512];
 
  if ((rv = nng_rep0_open(&sock)) != 0)
  {
    fatal("nng_rep0_open", rv);
  }
  if ((rv = nng_listen(sock, url, NULL, 0)) != 0)
  {
    fatal("nng_listen", rv);
  }
 
  for (;;)
  {
    char *buf = NULL;
    size_t sz;
    if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0)
    {
      fatal("nng_recv", rv);
    }
 
 
    // printf("RECEIVED:%s\n", buf);
    char *d = date();
    sprintf(sendbuf, "%s: %s", d, buf);
    // printf("SENDING DATE %s\n", d);
    if ((rv = nng_send(sock, sendbuf, strlen(sendbuf) + 1, 0)) != 0)
    {
      fatal("nng_send", rv);
    }
    nng_free(buf, sz);
  }
}
 
 
 
int
client(const char *url)
{
  nng_socket sock;
  int rv;
  size_t sz;
  char * buf;
  char sendbuf[512];
 
  if ((rv = nng_req0_open(&sock)) != 0)
  {
    fatal("nng_socket", rv);
  }
  if ((rv = nng_dial(sock, url, NULL, 0)) != 0)
  {
    fatal("nng_dial", rv);
  }
  //printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
  while (true)
  {
    printf("say something:\n");
    scanf("%s", sendbuf);
    if ((rv = nng_send(sock, sendbuf, strlen(sendbuf) + 1, 0)) != 0)
    {
      fatal("nng_send", rv);
    }
    if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0)
    {
      fatal("nng_recv", rv);
    }
    printf("RECEIVED DATE %s\n", buf);
    nng_free(buf, sz);
  }
 
  nng_close(sock);
  return (0);
}
 
 
 
 
#define  SCALE  100000
 
void *runclient(void *arg) {
  Targ *targ = (Targ *)arg;
  const char* url = targ->url;
  char sendbuf[512];
  size_t sz;
  void *buf;
 
  int i,j, n;
  nng_socket sock;
  int rv;
  char filename[512];
  
 
  if ((rv = nng_req0_open(&sock)) != 0)
  {
    fatal("nng_socket", rv);
  }
  if ((rv = nng_dial(sock, url, NULL, 0)) != 0)
  {
    fatal("nng_dial", rv);
  }
  
 
  sprintf(filename, "test%d.tmp", targ->id);
  FILE *fp = NULL;
  fp = fopen(filename, "w+");
  // fp = stdout;
 
 
  for (i = 0; i < SCALE; i++) {
    sprintf(sendbuf, "thread(%d) %d", targ->id, i);
    fprintf(fp, "requst:%s\n", sendbuf);
     
    if ((rv = nng_send(sock, sendbuf, strlen(sendbuf) + 1, 0)) != 0)
    {
      fatal("nng_send", rv);
    }
    if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0)
    {
      fatal("nng_recv", rv);
    }
    fprintf(fp, "NODE1: RECEIVED DATE %s\n", buf);
    nng_free(buf, sz);
 
  }
  fclose(fp);
  nng_close(sock);
  return (void *)i;
}
 
 
void mclient(const char *url) {
 
  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].url = url;
    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(const int argc, const char **argv)
{
  const char *url = "tcp://192.168.20.10:5001";
  //const char *url = "ipc:///tmp/reqrep.ipc";
  
  if (argc < 2) {
    fprintf(stderr, "Usage: %s %s|%s \n", argv[0],  "server", "client");
    return 1;
  }
 
  // url = atoi(argv[2]);
   
 
  if (strcmp("server", argv[1]) == 0 ) {
     server(url);
  }
 
  if (strcmp("client", argv[1]) == 0)
     client(url);
 
  if (strcmp("mclient", argv[1]) == 0)
    mclient(url);
  
}