-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpubxsub.c
232 lines (164 loc) · 4.99 KB
/
xpubxsub.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
#define SERVER "server"
#define CLIENT "client"
#define CLIENTS 15
#define SERVERS 15
#define TOPICPREFIX "s"
#define TOPICLENGTH 3
static long int publishData = 0;
typedef struct {
//Or whatever information that you need
char *url;
char *topic;
int topicLength;
} pubsub_struct;
void fatal(const char *func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
char* date(char *id)
{
char *text = malloc(11 * sizeof(char));;
//time_t now = time(&now);
// struct tm *info = localtime(&now);
publishData = publishData+1;
//printf("GENERATED DATA %ld\n", publishData);
//char *text = asctime(info);
sprintf(text, "%s%ld",id, publishData);
//text[strlen(text)-1] = '\0'; // remove '\n'
//printf("GENERATED DATA string %s\n", text);
return (text);
}
void *server(void *args)
{
pubsub_struct *actual_args = args;
nng_socket PubSock;
int rvPub;
if ((rvPub = nng_pub0_open(&PubSock)) != 0) {
fatal("nng_pub0_open server", rvPub);
}
if ((rvPub = nng_dial(PubSock, actual_args->url, NULL, 0)) < 0) {
fatal("nng_listen server", rvPub);
}
for (;;) {
char *d = date(actual_args->topic);
printf("Server %s: PUBLISHING DATA %s \n",actual_args->topic, d);
if ((rvPub = nng_send(PubSock, d, strlen(d) + 1, 0)) != 0) {
fatal("nng_send", rvPub);
}
free(d);
usleep(500000);
//free(d);
}
free(actual_args);
pthread_exit(NULL);
}
void *client(void *args)
{
pubsub_struct *actual_args = args;
nng_socket SubSock;
int rvSub;
if ((rvSub = nng_sub0_open(&SubSock)) != 0) {
fatal("nng_sub0_open client", rvSub);
}
if ((rvSub = nng_setopt(SubSock, NNG_OPT_SUB_SUBSCRIBE, actual_args->topic,actual_args->topicLength)) != 0) {
fatal("nng_setopt client", rvSub);
}
if ((rvSub = nng_dial(SubSock, actual_args->url, NULL, 0)) != 0) {
fatal("nng_dial client", rvSub);
}
for (;;) {
char *buf = NULL;
size_t sz;
if ((rvSub = nng_recv(SubSock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
fatal("nng_recv", rvSub);
}
printf("-------------------CLIENT %s : RECEIVED %s\n", actual_args->topic,buf);
nng_free(buf, sz);
usleep(500000);
}
free(actual_args);
pthread_exit(NULL);
}
void *device(void *args)
{
pubsub_struct *actual_args = args;
//subscriber
nng_socket RawSubSock;
int rvSubRaw;
if ((rvSubRaw = nng_sub0_open_raw(&RawSubSock)) != 0) {
fatal("nng_sub0_open_raw device", rvSubRaw);
}
if ((rvSubRaw = nng_listen(RawSubSock, "inproc://bar1", NULL, 0)) != 0) {
fatal("device nng_listen", rvSubRaw);
}
//publisher
nng_socket RawPubSock;
int rvPubRaw;
if ((rvPubRaw = nng_pub0_open_raw(&RawPubSock)) != 0) {
fatal("nng_pub0_open_raw device", rvPubRaw);
}
if ((rvPubRaw = nng_listen(RawPubSock, "inproc://bar2", NULL, 0)) < 0) {
fatal("device nng_listen", rvPubRaw);
}
if (nng_device (RawPubSock,RawSubSock) != 0) {
fatal("nng_device", rvPubRaw);
}
free(actual_args);
pthread_exit(NULL);
}
int
main(const int argc, const char **argv)
{
pthread_t device_id;
pthread_t server_id[SERVERS];
pthread_t client_id[CLIENTS];
pubsub_struct *deviceargs = malloc(sizeof *deviceargs);
pubsub_struct serverargs[SERVERS];
pubsub_struct clientargs[CLIENTS];
int i;
char Serverurl[] = "inproc://bar1";
char Clienturl[] = "inproc://bar2";
char topic[] = TOPICPREFIX;
pthread_create(&device_id, NULL, device, deviceargs);
sleep(3);
//list of Publishers
for(i=0;i<SERVERS;i++)
{
serverargs[i].url = Serverurl;
char *text = malloc(3 * sizeof(char));
sprintf(text, "%s%d%s",topic, i+1,",");
serverargs[i].topic= text;
serverargs[i].topicLength= TOPICLENGTH;
pthread_create(&server_id[i], NULL, server, (void *)&serverargs[i]);
}
sleep(3);
//list of subscribers
for(i=0;i<CLIENTS;i++)
{
clientargs[i].url = Clienturl;
char *text = malloc(3 * sizeof(char));
sprintf(text, "%s%d%s",topic, i+1,",");
clientargs[i].topic= text;
clientargs[i].topicLength= TOPICLENGTH;
pthread_create(&client_id[i], NULL, client, (void *)&clientargs[i]);
}
pthread_join(device_id,NULL);
for (i=0;i<SERVERS;i++)
{
pthread_join(server_id[i],NULL);
}
for (i=0;i<CLIENTS;i++)
{
pthread_join(client_id[i],NULL);
}
return 1;
}