aboutsummaryrefslogtreecommitdiffstats
path: root/mq_ntf.c
blob: 7f2953aba14973486bb3efa794c240b1c71d26bd (plain) (blame)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "mq_ntf.h"

mq_ntf_mdt *mq_ntf_create(int id)
{
	int fret;
	mq_ntf_mdt *ret = NULL;
	
	ret = malloc(sizeof(mq_ntf_mdt));
	if (ret == NULL)
	{
		PERM();
		return NULL;
	}

	memset(ret, 0, sizeof(mq_ntf_mdt));

	fret = mq_ntf_open(ret, id);
	if (fret == -1)
	{
		PERM();
		return NULL;
	}

	return ret;
}

/*
return -1 on error
*/
int mq_ntf_open(mq_ntf_mdt *mq, int id)
{
	const int name_size = 32;
	char name[name_size];

	if (!mq)
	{
		PERM();
		return -1;
	}
	mq->id = id;
	snprintf(name, name_size, MQ_PREFIX"%d-in",id);
	/*create with default configs*/
	mq->mq_in = mq_open(name,
		O_RDWR | O_CREAT, 0666, NULL);
	if (mq->mq_in == -1)
	{
		ENL();
		perror("mq_open");
		return -1;
	}
	snprintf(name, name_size, MQ_PREFIX"%d-out",id);
	/*create with default configs*/
	mq->mq_out = mq_open(name,
		O_RDWR | O_CREAT, 0666, NULL);
	if (mq->mq_out == -1)
	{
		perror("mq_open");
		return -1;
	}

	PRINT("opened = %d %d\n", mq->mq_in, mq->mq_out);

	return 0;
}

/* return number if there is something to read
RETURN:
	-1        some error
	0x00      nothing to read
	0x01      something in queue

*/
//if mplx havent done, everything could blow up
int mq_ntf_select(mq_ntf_mdt *mq, int dir)
{
	int fdnum, fd;
	int ret=0;
	fd_set io_fds;
	struct timeval tv;

	if (mq == NULL)
	{
		PERM();
		return -1;
	}

	//we should allways have just 2 descriptors to listen
	//io_fds = mq->io_mplx.io_fds;
	if (dir == MQ_IN)
	{
		fd = mq->mq_in;
	} else if (dir == MQ_OUT)
	{
		fd = mq->mq_out;
	} else
	{
		ENL();
	}
	//PRINT("fd=%d\n", fd);
	//PNL();
	FD_ZERO(&io_fds);
	FD_SET(fd, &io_fds);
	tv.tv_sec = 0;
	tv.tv_usec = 100000;
	//PNL();
	//first argument magic numbder
	fdnum = select(fd+1,
		&io_fds,
		NULL,
		NULL,
		&tv );

	//PNL();
	if (fdnum == -1)
	{
		//ENL();
		return -1;
	} else if (fdnum == 0) {
		//ENL();
		return 0;
	} else {
		//ENL();
		if (FD_ISSET(fd, &io_fds))
		{
			//PNL();
			return 1;
		}
	}
	//PNL();

	return ret;
}

int mq_ntf_read(mq_ntf_mdt *mq, int dir, char *buf, size_t size)
{
	struct mq_attr attr;
	size_t bytes;
	mqd_t mqd;

	unsigned int prio = 10;

	if (dir == MQ_IN)
	{
		mqd = mq->mq_in;
	} else if (dir == MQ_OUT)
	{
		mqd = mq->mq_out;
	} else
	{
		perror("Wrong direction");
	}

	mq_getattr(mqd, &attr);
	if (attr.mq_maxmsg == attr.mq_curmsgs)
	{
		printf("queue %d out full\n", mq->id);
		return -1;
	}
	//PNL();
	/*bytes = mq_receive(mqd, buf,
		size > attr.mq_msgsize ? attr.mq_msgsize : size,
		&prio);*/
	bytes = mq_receive(mqd, buf, size, &prio);
	//PNL();
	if (bytes == -1)
	{
		perror("mq_receive read out");
		return -1;
	}

	return bytes;
}

int mq_ntf_write(mq_ntf_mdt *mq, int dir, const char *buf, size_t size)
{
	struct mq_attr attr;
	size_t bytes;
	mqd_t mqd;

	unsigned int prio = 10;

	if (dir == MQ_IN)
	{
		mqd = mq->mq_in;
	} else if (dir == MQ_OUT)
	{
		mqd = mq->mq_out;
	} else
	{
		perror("Wrong direction");
	}

	mq_getattr(mqd, &attr);
	if (attr.mq_maxmsg == attr.mq_curmsgs)
	{
		printf("queue %d out full\n", mq->id);
		return -1;
	}
	//PNL();
	/*bytes = mq_send(mqd, buf,
		size > attr.mq_msgsize ? attr.mq_msgsize : size,
		prio);*/
	bytes = mq_send(mqd, buf, size, prio);
	//PNL();
	if (bytes == -1)
	{
		perror("mq_send");
		return -1;
	}

	return bytes;
}

//dir - dirction 1 in 2 out
int mq_ntf_count(mq_ntf_mdt *mq, int dir)
{
	struct mq_attr attr;
	int mq_fd;

	if (mq == NULL)
	{
		PERM();
		return -1;
	}

	if (dir == 1)
	{
		mq_fd = mq->mq_in;
	} else
	{
		mq_fd = mq->mq_out;
	}

	if (mq_getattr(mq_fd, &attr) != -1)
	{
		return attr.mq_curmsgs;
	}

	return -1;
}


/* close queue */
int mq_ntf_close(mq_ntf_mdt *mq)
{
	const int name_size = 32;
	char name[name_size];
	int id;

	id = mq->id;
	snprintf(name, name_size, MQ_PREFIX"%d-in",id);
	mq_unlink(name);

	snprintf(name, name_size, MQ_PREFIX"%d-out",id);
	mq_unlink(name);

	//clean file descriptors for io_mplx

	return 0;
}

int mq_drain_q(mqd_t mqd)
{
	struct mq_attr attr;
	char *buf = NULL;
	unsigned int prio = 0;
	int cnt_msg = 0;
	int num_read = 0;
	int i = 0;

	if (mq_getattr(mqd, &attr) == -1)
	{
		ENL();
		return -1;
	}

	buf = malloc(attr.mq_msgsize);
	if (buf == NULL)
	{
		ENL();
		return -1;
	}

	for (i=0; i<attr.mq_curmsgs; i++)
	{
		num_read = mq_receive(mqd, buf, attr.mq_msgsize, &prio);
		if (num_read == -1)
		{
			ENL();
			free(buf);
			return -1;
		}
		cnt_msg += 1;
	}

	free(buf);
	return cnt_msg;
}

//drain all mq buffers to zero
//to use at begining and clear all buffers to zero
int mq_ntf_drain(mq_ntf_mdt *mq)
{
	int err;

	err = mq_drain_q(mq->mq_in);
	if (err < 0)
	{
		PERM();
		return -1;
	}
	printf("Drained in %d messages\n", err);

	err = mq_drain_q(mq->mq_out);
	if (err < 0)
	{
		PERM();
		return -1;
	}
	printf("Drained out %d messages\n", err);

	return 0;
}



int mq_ntf_getattr(mq_ntf_mdt *mq, int dir, struct mq_attr **attr)
{
	struct mq_attr gattr;
	mqd_t mqdt;

	if (mq == NULL)
	{
		PERM();
		return -1;
	}

	if (dir == MQ_OUT)
	{
		mqdt = mq->mq_out;
	} else if (dir == MQ_IN)
	{
		mqdt = mq->mq_in;
	} else
	{
		return -1;
	}

	if (mq_getattr(mqdt, &gattr) == -1)
	{
		ENL();
		return -1;
	}

	memcpy(*attr,&gattr,sizeof(struct mq_attr));

	return 0;
}

void mq_ntf_free(mq_ntf_mdt *mq)
{
	if (mq == NULL)
	{

	}
}