-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMessageQueueWrapper.c
executable file
·233 lines (206 loc) · 6 KB
/
MessageQueueWrapper.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
/**********************************************************************
* File: messageQueueWrapper.c
* Purpose: Provides IPC using message queues.
* Author: Kevin Dinkel
**********************************************************************/
/***************************************************
* Section: Includes
**************************************************/
#include "MessageQueueWrapper.h"
/***************************************************
* Function: messageQueueGet(key_t key, int p)
* Param key: A key (from ftok) used to create a queue.
* Param p: The priority level of the queue you are creating.
* Purpose: Creates a queue based on key.
* Returns: The qid on success, -1 on failure.
**************************************************/
int messageQueueGet(key_t key)
{
int qid;
if ((qid = msgget(key, 0666)) == -1)
return -1;
return qid;
}
/***************************************************
* Function: messageQueueCreate(key_t key, int p)
* Param key: A key (from ftok) used to create a queue.
* Param p: The priority level of the queue you are creating.
* Purpose: Creates a queue based on key.
* Returns: The qid on success, -1 on failure.
**************************************************/
int messageQueueCreate(key_t key)
{
int qid;
struct msqid_ds buf;
if ((qid = msgget(key, IPC_CREAT | 0666)) == -1)
return -1;
/* increase message queue size */
if (msgctl(qid, IPC_STAT, &buf) == -1)
return -1;
/* set new buffer size */
buf.msg_qbytes = 16384; /* look at ipcs -l */
/* set Queue values */
if (msgctl(qid, IPC_SET, &buf) == -1)
return -1;
return qid;
}
/*********************************************************
* Function: messageQueueDelete(int qid)
* Param qid: The queue ID of the message queue to be deleted.
* Purpose: Deletes a message queue to release system resources.
* Returns: -1 on failure, 0 on success.
*********************************************************/
int messageQueueDelete(int qid)
{
return msgctl(qid, IPC_RMID, NULL);
}
/****************************************************
* Function: messageQueueSend(int qid, BMsg *msg)
* Param qid: The queue ID of the message queue.
* Param msg: A BMsg struct that has been initialized
* and will be sent over the message queue
* Purpose: Sends a message over a queue denoted by qid
* Returns: -1 on failure, 0 on success.
****************************************************/
int messageQueueSend(int qid, Message* msg)
{
/*
struct msqid_ds buf;
int temp;
temp = msgctl(qid, IPC_STAT, &buf);
if (temp == -1) {
(void) fprintf(stderr, "msgctl fail");
}
else {
cerr << "msg_perm.uid = "<< buf.msg_perm.uid<< endl;
cerr << "msg_perm.gid = "<< buf.msg_perm.gid<< endl;
cerr << "msg_perm.cuid = "<< buf.msg_perm.cuid<< endl;
cerr << "msg_perm.cgid = "<< buf.msg_perm.cgid<< endl;
cerr << "msg_perm.mode = "<< buf.msg_perm.mode;
cerr << " access permissions = "<< (buf.msg_perm.mode & 0777)<< endl;
cerr << "msg_cbytes = "<< buf.msg_cbytes<< endl;
cerr << "msg_qbytes = "<< buf.msg_qbytes<< endl;
cerr << "msg_qnum = "<< buf.msg_qnum<< endl;
cerr << "msg_lspid = "<< buf.msg_lspid<< endl;
cerr << "msg_lrpid = "<< buf.msg_lrpid << endl;
}
*/
int ret;
if ((ret = msgsnd(qid, msg, sizeof(Message) - sizeof(long), 0)) == -1)
{
msg->err=errno;
return -1;
}
return ret;
}
int messageQueueSend_nowait(int qid, Message* msg)
{
int ret;
if ((ret = msgsnd(qid, msg, sizeof(Message) - sizeof(long), IPC_NOWAIT)) == -1)
{
msg->err=errno;
return -1;
}
return ret;
}
int messageQueueSend_timeout(int qid, Message* msg, int usecs)
{
int ret;
timer timerid;
int sleeptime = 500000;
int st = usecs / 10;
/* Sleep for half a second before checking timeout, unless it should be smaller. */
if ( sleeptime > st )
{
sleeptime = st;
}
/* Create timeout timer: */
timerCreate(&timerid);
timerSet_us(&timerid, usecs);
/* Reset errno to zero: */
errno = 0;
/* Get message: */
while( (ret = msgsnd(qid, msg, sizeof(Message) - sizeof(long), IPC_NOWAIT)) < 0 )
{
if( errno != ENOMSG )
{
msg->err=errno;
timerDelete(&timerid);
return -1;
}
if( isTimerExpired(&timerid) )
{
msg->err=errno;
timerDelete(&timerid);
return -1; /* return -1 here not 0 because it is a send */
}
rt_sleep_us( sleeptime );
}
timerDelete(&timerid);
return ret;
}
/*******************************************************
* Function: messageQueueReceive(int qid, long type, MMsg *msg)
* Param qid: The queue ID of the message queue.
* Param type: The specific type of message you want to get.
* Can be any integer.
* Param msg: A MMsg struct that will store the return data.
* Purpose: Receives a message from the message queue.
* Returns: -1 on failure, 0 on success.
*********************************************************/
int messageQueueReceive(int qid, Message* msg, long type)
{
int ret;
if ((ret = msgrcv(qid, msg, sizeof(Message) - sizeof(long), type, 0)) == -1)
{
msg->err=errno;
return -1;
}
return ret;
}
int messageQueueReceive_nowait(int qid, Message* msg, long type)
{
int ret;
if ((ret = msgrcv(qid, msg, sizeof(Message) - sizeof(long), type, IPC_NOWAIT)) == -1)
{
msg->err=errno;
return -1;
}
return ret;
}
int messageQueueReceive_timeout(int qid, Message* msg, int usecs, long type)
{
int ret;
timer timerid;
int sleeptime = 500000;
int st = usecs / 10;
/* Sleep for half a second before checking timeout, unless it should be smaller. */
if ( sleeptime > st )
{
sleeptime = st;
}
/* Create timeout timer: */
timerCreate(&timerid);
timerSet_us(&timerid, usecs);
/* Reset errno to zero: */
errno = 0;
/* Get message: */
while( (ret = msgrcv(qid, msg, sizeof(Message) - sizeof(long), type, IPC_NOWAIT)) < 0 )
{
if( errno != ENOMSG )
{
msg->err=errno;
timerDelete(&timerid);
return -1;
}
if( isTimerExpired(&timerid) )
{
msg->err=errno;
timerDelete(&timerid);
return 0; /* return 0 here because no bytes returned */
}
rt_sleep_us( sleeptime );
}
timerDelete(&timerid);
return ret;
}