-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerJSONthreads_2.c
284 lines (194 loc) · 7.13 KB
/
workerJSONthreads_2.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
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
//
// Created by utente on 27/04/2021.
//
#include <mdp.h>
#include <json-c/json.h>
#include "Queue/queue.h"
#define NUM_WORKERS 1
// "tcp://192.168.0.113:5000"
// "tcp://127.0.0.1:5000"
//"tcp://192.168.1.7:5000"
#define BROKER_ENDPOINT "tcp://127.0.0.1:5000"
#define VENDOR "Renault"
#define POWER "90cv"
static void workerTask(zsock_t *pipe, void *args);
void *consumer(void *arg);
void *producer(void *arg);
zmsg_t *handle_type_request(zmsg_t *request);
void print_parsing_time(const long *start, const long *end);
void print_average_parsing_time(const long *parsing_array, const size_t *num_samples);
int check_option(const int *argc, char *argv[], int *daemonize, int *verbose, int *user_endpoint);
struct counter_t {
int value;
pthread_mutex_t lock;
};
static struct counter_t num_replies_consum;
static struct counter_t num_replies_prod;
double speed = 130.0;
#define BUFFER_SIZE 100
struct Queue *queue;
mdp_worker_t *session;
// Number of producer threads to start
// Must be greater than zero
#define NUM_OF_PRODUCERS 5
#define NUM_OF_CONSUMERS 5
int main(int argc, char *argv[]) {
int verbose = 1;
int daemonize = 0;
int user_endpoint = 0;
check_option(&argc, argv, &daemonize, &verbose, &user_endpoint);
zactor_t *workers[NUM_WORKERS];
for (int i = 0; i < NUM_WORKERS; i++) {
if (streq(argv[0], "-v")) verbose = 1;
workers[i] = zactor_new(workerTask, &verbose);
}
zsys_catch_interrupts();
if (zsys_interrupted) {
for (int i = 0; i < NUM_WORKERS; i++) {
zactor_destroy(&workers[i]);
}
}
for (int i = 0; i < NUM_WORKERS; i++) {
zactor_destroy(&workers[i]);
}
return 0;
}
int check_option(const int *argc, char *argv[], int *daemonize, int *verbose, int *user_endpoint) {
for (int i = 1; i < *argc; i++) {
if (streq(argv[i], "-v")) *verbose = 1;
else if (streq(argv[i], "-d")) *daemonize = 1;
else if (streq(argv[i], "-h")) {
printf("%s [-h] | [-d] [-v] [broker url]\n\t-h This help message\n\t-d Daemon mode.\n\t-v Verbose output\n\tbroker url defaults to tcp://*:5000\n",
argv[0]);
return -1;
} else *user_endpoint = 1;
}
if (*daemonize != 0) {
int rc = daemon(0, 0);
assert (rc == 0);
}
return 0;
}
static void
workerTask(zsock_t *pipe, void *args) {
queue = newQueue(100);
//TODO managing of arguments!!
zsys_catch_interrupts();
int *arguments = (int *) args;
int verbose = arguments[0];
long end;
long time_to_signup;
char *endpoint = BROKER_ENDPOINT;
long start = zclock_usecs();
session = mdp_worker_new(
endpoint, "engine_1", verbose);
end = zclock_usecs();
time_to_signup = end - start;
printf("TIME TO SIGN UP: %ld [micro secs]\n", time_to_signup);
mdp_worker_set_heartbeat(session,
7500); //set the heartbeat time. After this time in seconds, worker will send to worker an heartbeat message
srand(time(NULL));
pthread_t consumers[NUM_OF_CONSUMERS];
int num = 0;
while (!zctx_interrupted) {
zframe_t *reply_to;
zmsg_t *request = mdp_worker_recv(session, &reply_to);
//replies_to[NUM_OF_PRODUCERS % 50] = reply_to;
//incoda
enqueue(queue, request);
//pthread_create(&producers[num % NUM_OF_PRODUCERS], NULL, &producer, &request);
//printf("Starting Thread producer %d\n", num % NUM_OF_PRODUCERS);
pthread_create(&consumers[num % NUM_OF_CONSUMERS], NULL, consumer, &reply_to);
printf("Starting Thread consumer %d\n", num % NUM_OF_CONSUMERS);
pthread_join(consumers[num % NUM_OF_CONSUMERS], NULL);
zmsg_destroy(&request);
num++;
}
mdp_worker_destroy(&session);
}
zmsg_t *handle_type_request(zmsg_t *request) {
zmsg_t *reply = zmsg_new();
json_object *REP;
puts("building a reply...");
REP = json_object_new_object();
json_object_object_add(REP, "VENDOR", json_object_new_string(VENDOR));
json_object_object_add(REP, "POWER", json_object_new_string(POWER));
speed += (double) rand() / RAND_MAX * 2.0 - 1.0;
json_object_object_add(REP, "VALUE", json_object_new_double(speed));
int64_t timestamp = zclock_time();
json_object_object_add(REP, "timestamp", json_object_new_int64(timestamp));
puts("REPLY =" );
json_object_object_foreach(REP, key2, val2) {
printf("\t%s: %s\n", key2, json_object_to_json_string(val2));
}
puts("\n\n");
const char *reply_string = json_object_to_json_string(REP);
zmsg_pushstr(reply, reply_string);
return reply;
}
void print_average_parsing_time(const long *parsing_array, const size_t *num_samples) {
long sum = 0;
for (int i = 0; i < *num_samples; i++) {
sum += parsing_array[i];
}
long double average = (long double) sum / *num_samples;
printf("AVERAGE TIME OF PARSING FOR WORKER: %Lf [micro secs]\n", average);
}
void print_parsing_time(const long *start, const long *end) {
if ((end - start) > 0) {
printf("PARSING TIME: %ld [micro secs]\n", end - start);
} else {
puts("CANNOT PARSING, TIME IS NEGATIVE...\n");
}
}
void *producer(void *arg) {
zmsg_t *request = (zmsg_t *) arg;
zmsg_print(request);
request = arg;
enqueue(queue, request);
zmsg_destroy(&request);
return NULL;
}
void *consumer(void *arg) {
zframe_t *reply_to;
reply_to = (zframe_t *) arg;
zmsg_t *request;
request = dequeue(queue);
//increment(&num_replies_consum);
int size_request = (int) zmsg_size(request);
zframe_t *request_frame;
printf("RECEIVED MESSAGE FROM BROKER\n");
puts("REQUEST BODY FRAMES");
printf("NUMBER OF FRAMES: %d\n", size_request);
char *s; //string extracted from each frame
long start = zclock_usecs(); // time to start popping requests
// extract frames "for loop"
for (int i = 0; i < size_request; i++) {
//create a list of frames and for each one extracts the string
request_frame = zmsg_next(request);
long end = zclock_usecs();
printf("TIME TO POP ONE SINGLE REQUEST: %ld [micro secs] \n", end - start);
//duplicating string of the frame into the printable string
s = zframe_strdup(request_frame);
printf("BODY FRAME[%d]: %s\n", i, s);
free(s);
printf("FRAME TYPE-CONTENT: %s\n", "PLAIN STRING");
printf("(BYTE) SIZE FRAME: %lu\n", zframe_size(request_frame));
zframe_destroy(&request_frame);
}
long start_time_sending_reply;
long end_time_sending_reply;
start_time_sending_reply = zclock_usecs();
//this is reply message initialization
zmsg_t *reply_message = zmsg_new();
//return the reply
reply_message = handle_type_request(request);
//send to broker the reply if exists
mdp_worker_send(session, &reply_message, reply_to);
end_time_sending_reply = zclock_usecs();
printf("TIME TO SEND A REPLY: %ld\n", end_time_sending_reply - start_time_sending_reply);
zframe_destroy(&reply_to);
zmsg_destroy(&reply_message);
zframe_destroy(&request_frame);
return NULL;
}