Newer
Older
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// API to ingest data into a data transfer unit
//#ifndef DATAINGEST_H
#define DATAINGEST_H
#define version "0.0.1"
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
typedef int bool;
#define true 1
#define false 0
// helper functions for sending and receiving messages
// source: Pieter Hintjens: ZeroMQ; O'Reilly
static char* s_recv (void *socket)
{
zmq_msg_t message;
zmq_msg_init (&message);
int size = zmq_msg_recv (&message, socket, 0);
if (size == -1)
return NULL;
char *string = malloc (size + 1);
memcpy (string, zmq_msg_data (&message), size);
printf("recieved string: %s\n", string);
zmq_msg_close (&message);
string [size] = 0;
return string;
};
static int s_send (void * socket, char* string)
{
printf("Sending: %s\n", string);
int size = zmq_send (socket, string, strlen (string), 0);
if (size == -1) {
printf("Sending failed\n");
// fprintf (stderr, "ERROR: Sending failed\n");
// perror("");
};
return size;
};
typedef struct {
char *extHost;
char *localhost;
char *signalPort;
char *dataPort;
// Prepare our context and socket
void *context;
void *signalSocket;
void *dataSocket;
// Initialize poll set
// zmq::pollitem_t items [];
int numberOfStreams;
// self.recvdCloseFrom = []
char *replyToSignal;
bool allCloseRecvd;
} nexusTransfer;
int nexusTransfer_init (nexusTransfer *nT)
{
nT->extHost = "0.0.0.0";
nT->localhost = "localhost";
nT->signalPort = "50050";
nT->dataPort = "50100";
if ( (nT->context = zmq_ctx_new ()) == NULL )
{
perror("ERROR: Cannot create 0MQ context: ");
exit(9);
}
if ( (nT->signalSocket = zmq_socket (nT->context, ZMQ_REP)) == NULL )
{
perror("ERROR: Could not create 0MQ signalSocket: ");
exit(9);
}
if ( (nT->dataSocket = zmq_socket (nT->context, ZMQ_PULL)) == NULL )
{
perror("ERROR: Could not create 0MQ dataSocket: ");
exit(9);
}
nT->replyToSignal = ""
nT->allCloseRecvd = false
char connectionStr[128];
int rc;
// Create sockets
snprintf(connectionStr, sizeof(connectionStr), "tcp://%s:%s", nT->extlHost, nT->signalPort);
// rc = zmq_connect(nT->signalSocket, connectionStr);
// assert (rc == 0);
if ( zmq_bind(nT->signalSocket, connectionStr) )
{
fprintf (stderr, "ERROR: Could not start signalSocket (bind) for '%s'\n", connectionStr);
perror("");
} else {
printf("signalSocket started (bind) for '%s'\n", connectionStr);
}
snprintf(connectionStr, sizeof(connectionStr), "tcp://%s:%s", nT->localhost, nT->dataPort);
// rc = zmq_connect(nT->dataSocket, connectionStr);
// assert (rc == 0);
if ( zmq_bind(nT->dataSocket, connectionStr) )
{
fprintf (stderr, "ERROR: Could not start dataSocket (bind) for '%s'\n", connectionStr);
perror("");
} else {
printf("dataSocket started (bind) for '%s'\n", connectionStr);
}
return 0;
}
int nexusTransfer_read (nexusTransfer *nT, char *data, int size)
{
zmq_pollitem_t items [] = {
{ nT->signalSocket, 0, ZMQ_POLLIN, 0 },
{ nT->dataSocket, 0, ZMQ_POLLIN, 0 }
};
while (1)
{
printf ("polling");
zmq_poll (items, 2, -1);
if (items [0].revents & ZMQ_POLLIN)
{
printf ("signalSocket is polling\n");
data = s_recv (dI->signalSocket);
printf ("signalSocket recv: '%s'\n", message);
if (data == b"CLOSE_FILE" && nT->allCloseRecvd == false):
nT->replyToSignal = data;
else:
rc = zmq_send (nT-signalSocket, data, strlen(data), 0);
printf ("signalSocket send: '%s'\n", data);
return 0;
}
if (items [1].revents & ZMQ_POLLIN) {
printf ("dataSocket is polling\n");
char *multipartMessage[2];
int j;
rc = getMultipartMessage(dI->dataSocket, multipartMessage)
for (j = 0; j < 2; j++)
{
free(multipartMessage[j]);
};
}
};
int recv_multipartMessage (void *socket, char **multipartMessage, int *len)
{
int i = 0;
int size;
int more;
size_t more_size = sizeof(more);
while (1)
{
// Wait for next request from client
zmq_msg_t message;
zmq_msg_init (&message);
m_size = zmq_msg_recv (&message, socket, 0);
//Process message
multipartMessage[i] = malloc (m_size + 1);
memcpy (multipartMessage[i], zmq_msg_data (&message), m_size);
printf("recieved string: %s\n", multipartMessage[i]);
multipartMessage[i] [m_size] = 0;
i++;
zmq_msg_close(&message);
zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
if (more == 0)
{
printf("last received\n");
*len = i;
break;
};
}
return 0;
};
int getMultipartMessage (void *socket, char *data)
{
char* mulitpartMessage[2];
int len;
int rc;
//TODO multipart
rc = recv_multipartMessage (dI-dataSocket, multipartMessage, len);
printf ("multipartMessage[0]=%s\nmultipartMessage[1]=%s\n", multipartMessage[0], multipartMessage[1]);
//self.log.error("Could not receive data due to unknown error.", exc_info=True)
if (len < 2)
{
printf ("Received mutipart-message is too short. Either config or file content is missing.");
// self.log.debug("multipartMessage=" + str(mutipartMessage))
//TODO coorect errorcode
return -1;
}
if (multipartMessage[0] == "CLOSE_FILE")
{
/*
id = multipartMessage[1]
self.recvdCloseFrom.append(id)
self.log.debug("Received close-file-signal from DataDispatcher-" + id)
# get number of signals to wait for
if not self.numberOfStreams:
self.numberOfStreams = int(id.split("/")[1])
# have all signals arrived?
if len(self.recvdCloseFrom) == self.numberOfStreams:
self.log.info("All close-file-signals arrived")
self.allCloseRecvd = True
if self.replyToSignal:
self.signalSocket.send(self.replyToSignal)
self.log.debug("signalSocket send: " + self.replyToSignal)
self.replyToSignal = False
else:
pass
*/
data = "CLOSE_FILE"
return 0;
} else {
data = multipartMessage[1];
return 0;
}
};
int nexusTransfer_stop (dataIngest *nT)
{
printf ("closing signalSocket...\n");
zmq_close(nT->signalSocket);
printf ("closing dataSocket...\n");
zmq_close(nT->dataSocket);
// self.log.error("closing ZMQ Sockets...failed.", exc_info=True)
printf ("Closing ZMQ context...\n");
zmq_ctx_destroy(nT->context);
// self.log.error("Closing ZMQ context...failed.", exc_info=True)
// free (nT);
printf ("Cleanup finished.\n");
return 0;
};
//#endif
int main ()
{
nexusTransfer *obj;
obj = malloc(sizeof(nexusTransfer));
char *data;
int i;
int size;
int rc;
rc = nexusTransfer_init (obj);
rc = nexusTransfer_read (obj, data, size);
printf("Read data: %s, size: %d\n", data, size);
for (i=0; i < 5; i++)
{
rc = nexusTransfer_read (obj, data, size);
printf("Read data: %s, size: %d\n", data, size);
};
rc = nexusTransfer_read (obj, data, size);
printf("Read data: %s, size: %d\n", data, size);
printf ("Stopping\n");
rc = nexusTransfer_stop(obj);
free(obj);
return 0;
};