Class: POSIX::Mqueue

Inherits:
Object
  • Object
show all
Defined in:
lib/posix/mqueue/version.rb,
ext/posix/mqueue.c

Defined Under Namespace

Classes: QueueEmpty, QueueFull

Constant Summary collapse

VERSION =
"0.0.8"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/posix/mqueue.c', line 275

VALUE posix_mqueue_initialize(int argc, VALUE* argv, VALUE self)
{
  if(argc < 1)
  {
    rb_raise(rb_eArgError, "initialize requires at least one argument");
  }

  VALUE queue = argv[0];
  if (!RB_TYPE_P(queue, T_STRING)) {
    rb_raise(rb_eTypeError, "Queue name must be a string");
  }

  VALUE options;
  if (argc < 2)
  {
    options = rb_hash_new();
  }
  else
  {
    options = argv[1];
  }

  int msgsize = FIX2INT(rb_hash_lookup2(options, ID2SYM(rb_intern("msgsize")), INT2FIX(4096)));
  int maxmsg = FIX2INT(rb_hash_lookup2(options, ID2SYM(rb_intern("maxmsg")), INT2FIX(10)));

  struct mq_attr attr = {
    .mq_flags   = 0,          // Flags, 0 or O_NONBLOCK
    .mq_maxmsg  = maxmsg,     // Max messages in queue
    .mq_msgsize = msgsize,    // Max message size (bytes)
    .mq_curmsgs = 0           // # currently in queue
  };

  mqueue_t* data;
  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (data->fd != -1) {
    // This would cause a memleak otherwise
    rb_raise(rb_eRuntimeError, "Illegal reinitialization");
  }

  data->attr = attr;
  data->queue_len = RSTRING_LEN(queue);
  data->queue = ruby_strdup(StringValueCStr(queue));
  data->fd = mq_open(data->queue, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &data->attr);

  if (data->fd == (mqd_t)-1) {
    rb_sys_fail("Failed opening the message queue, please consult mq_open(3)");
  }

  VALUE args[1];
  args[0] = INT2FIX(data->fd);

  data->io = rb_class_new_instance(1, args, rb_path2class("IO"));

  return self;
}

Instance Method Details

#msgsizeObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/posix/mqueue.c', line 227

VALUE posix_mqueue_msgsize(VALUE self)
{
  mqueue_t* data;
  struct mq_attr queue;

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (mq_getattr(data->fd, &queue) < 0) {
    rb_sys_fail("Failed reading queue attributes, please consult mq_getattr(3)");
  }

  return INT2FIX(queue.mq_msgsize);
}

#receiveObject



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
# File 'ext/posix/mqueue.c', line 241

VALUE posix_mqueue_receive(VALUE self)
{
  int err;
  size_t buf_size;
  char *buf;
  VALUE str;

  mqueue_t* data;

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  buf_size = data->attr.mq_msgsize + 1;

  // Make sure the buffer is capable
  buf = (char*)malloc(buf_size);

  rb_thread_wait_fd(data->fd);

  err = mq_receive(data->fd, buf, buf_size, NULL);

  if(err < 0 && errno == EINTR) {
    err = mq_receive(data->fd, buf, buf_size, NULL);
  }

  if (err < 0) {
    rb_sys_fail("Message retrieval failed, please consult mq_receive(3)");
  }

  str = rb_str_new(buf, err);
  free(buf);

  return str;
}

#send(message) ⇒ Object



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
# File 'ext/posix/mqueue.c', line 87

VALUE posix_mqueue_send(VALUE self, VALUE message)
{
  int err;
  mqueue_t* data;

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (!RB_TYPE_P(message, T_STRING)) {
    rb_raise(rb_eTypeError, "Message must be a string");
  }

  rb_io_wait_writable(data->fd);

  // TODO: Custom priority
  err = mq_send(data->fd, RSTRING_PTR(message), RSTRING_LEN(message), 10);

  if(err < 0 && errno == EINTR) {
    err = mq_send(data->fd, RSTRING_PTR(message), RSTRING_LEN(message), 10);
  }

  if (err < 0) {
    rb_sys_fail("Message sending failed, please consult mq_send(3)");
  }

  return Qtrue;
}

#sizeObject



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/posix/mqueue.c', line 205

VALUE posix_mqueue_size(VALUE self)
{
  mqueue_t* data;
  struct mq_attr queue;

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (mq_getattr(data->fd, &queue) < 0) {
    rb_sys_fail("Failed reading queue attributes, please consult mq_getattr(3)");
  }

  return INT2FIX(queue.mq_curmsgs);
}

#timedreceive(args) ⇒ Object



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
# File 'ext/posix/mqueue.c', line 114

VALUE posix_mqueue_timedreceive(VALUE self, VALUE args)
{
  int err;
  mqueue_t* data;
  size_t buf_size;
  char *buf;
  struct timespec timeout;
  VALUE str;
  VALUE seconds = rb_ary_entry(args, 0);
  VALUE nanoseconds = rb_ary_entry(args, 1);

  if (seconds == Qnil) seconds = INT2FIX(0);
  if (nanoseconds == Qnil) nanoseconds = INT2FIX(0);

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (!RB_TYPE_P(seconds, T_FIXNUM)) {
    rb_raise(rb_eTypeError, "First argument must be a Fixnum");
  }

  if (!RB_TYPE_P(nanoseconds, T_FIXNUM)) {
    rb_raise(rb_eTypeError, "Second argument must be a Fixnum");
  }

  timeout.tv_sec  = FIX2ULONG(seconds);
  timeout.tv_nsec = FIX2ULONG(nanoseconds);

  buf_size = data->attr.mq_msgsize + 1;

  // Make sure the buffer is capable
  buf = (char*)malloc(buf_size);

  // TODO: Specify priority
  err = mq_timedreceive(data->fd, buf, buf_size, NULL, &timeout);

  if (err < 0) {
    if(errno == 110) {
      rb_raise(rb_cQueueEmpty, "Queue empty");
    } else {
      rb_sys_fail("Message sending failed, please consult mq_send(3)");
    }
  }

  str = rb_str_new(buf, err);
  free(buf);

  return str;
}

#timedsend(args) ⇒ Object



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
# File 'ext/posix/mqueue.c', line 163

VALUE posix_mqueue_timedsend(VALUE self, VALUE args)
{
  int err;
  mqueue_t* data;
  struct timespec timeout;
  VALUE message = rb_ary_entry(args, 0);
  VALUE seconds = rb_ary_entry(args, 1);
  VALUE nanoseconds = rb_ary_entry(args, 2);

  if (seconds == Qnil) seconds = INT2FIX(0);
  if (nanoseconds == Qnil) nanoseconds = INT2FIX(0);

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (!RB_TYPE_P(message, T_STRING)) {
    rb_raise(rb_eTypeError, "Message must be a string");
  }

  if (!RB_TYPE_P(seconds, T_FIXNUM)) {
    rb_raise(rb_eTypeError, "First argument must be a Fixnum");
  }

  if (!RB_TYPE_P(nanoseconds, T_FIXNUM)) {
    rb_raise(rb_eTypeError, "Second argument must be a Fixnum");
  }

  timeout.tv_sec  = FIX2ULONG(seconds);
  timeout.tv_nsec = FIX2ULONG(nanoseconds);

  err = mq_timedsend(data->fd, RSTRING_PTR(message), RSTRING_LEN(message), 10, &timeout);

  if (err < 0) {
    if(errno == 110) {
      rb_raise(rb_cQueueFull, "Queue full, most likely you want to bump /proc/sys/fs/mqueue/msg_max from the default maximum queue size of 10.");
    } else {
      rb_sys_fail("Message sending failed, please consult mq_send(3)");
    }
  }

  return Qtrue;
}

#to_ioObject



219
220
221
222
223
224
225
# File 'ext/posix/mqueue.c', line 219

VALUE posix_mqueue_to_io(VALUE self)
{
  mqueue_t* data;
  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  return data->io;
}


74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/posix/mqueue.c', line 74

VALUE posix_mqueue_unlink(VALUE self)
{
  mqueue_t* data;

  TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);

  if (mq_unlink(data->queue) == -1) {
    rb_sys_fail("Message queue unlinking failed, please consume mq_unlink(3)");
  }

  return Qtrue;
}