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
|
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "convey.h"
#include "nng.h"
#include <string.h>
static int nclients = 200;
static char *addr = "inproc:///atscale";
void
serve(void *arg)
{
nng_socket rep = *(nng_socket *) arg;
nng_msg * msg;
for (;;) {
if (nng_recvmsg(rep, &msg, 0) != 0) {
nng_close(rep);
return;
}
if (nng_sendmsg(rep, msg, 0) != 0) {
nng_close(rep);
return;
}
}
}
int
openclients(nng_socket *clients, int num)
{
int rv;
int i;
uint64_t t;
for (i = 0; i < num; i++) {
if ((rv = nng_req_open(&clients[i])) != 0) {
printf("open #%d: %s\n", i, nng_strerror(rv));
return (rv);
}
t = 100000; // 100ms
rv = nng_setopt(clients[i], NNG_OPT_RCVTIMEO, &t, sizeof(t));
if (rv != 0) {
printf(
"setopt(RCVTIMEO) #%d: %s\n", i, nng_strerror(rv));
return (rv);
}
t = 100000; // 100ms
rv = nng_setopt(clients[i], NNG_OPT_SNDTIMEO, &t, sizeof(t));
if (rv != 0) {
printf(
"setopt(SNDTIMEO) #%d: %s\n", i, nng_strerror(rv));
return (rv);
}
rv = nng_dial(clients[i], addr, NULL, NNG_FLAG_SYNCH);
if (rv != 0) {
printf("dial #%d: %s\n", i, nng_strerror(rv));
return (rv);
}
}
return (0);
}
int
transact(nng_socket *clients, int num)
{
nng_msg *msg;
int rv;
int i;
for (i = 0; i < num; i++) {
if ((rv = nng_msg_alloc(&msg, 0)) != 0) {
printf("alloc #%d: %s\n", i, nng_strerror(rv));
return (rv);
}
if ((rv = nng_sendmsg(clients[i], msg, 0)) != 0) {
nng_msg_free(msg);
printf("sendmsg #%d: %s", i, nng_strerror(rv));
return (rv);
}
if ((rv = nng_recvmsg(clients[i], &msg, 0)) != 0) {
printf("recvmsg #%d: %s", i, nng_strerror(rv));
return (rv);
}
nng_msg_free(msg);
}
return (0);
}
void
closeclients(nng_socket *clients, int num)
{
int i;
nng_usleep(1000);
for (i = 0; i < num; i++) {
if (clients[i] > 0) {
nng_close(clients[i]);
}
}
}
Main({
nng_socket *clients;
void * server;
int * results;
int depth = 256;
nng_socket rep;
clients = calloc(nclients, sizeof(nng_socket));
results = calloc(nclients, sizeof(int));
if ((nng_rep_open(&rep) != 0) ||
(nng_setopt(rep, NNG_OPT_RCVBUF, &depth, sizeof(depth)) != 0) ||
(nng_setopt(rep, NNG_OPT_SNDBUF, &depth, sizeof(depth)) != 0) ||
(nng_listen(rep, addr, NULL, NNG_FLAG_SYNCH) != 0) ||
(nng_thread_create(&server, serve, &rep) != 0)) {
fprintf(stderr, "Unable to set up server!\n");
exit(1);
}
Test("Scalability", {
Convey("We can handle many many clients", {
int i;
So(openclients(clients, nclients) == 0);
So(transact(clients, nclients) == 0);
for (i = 0; i < nclients; i++) {
So(nng_close(clients[i]) == 0);
}
});
});
});
|