zeromq 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -67,6 +67,12 @@ void zeromq_context_release( struct zeromq_context* context )
67
67
 
68
68
  //-----------------------------------------------------------------------------
69
69
 
70
+ /*
71
+ * Document-class: ZeroMQ::Context
72
+ *
73
+ * The ZeroMQ::Context class ...
74
+ */
75
+
70
76
  VALUE context_declare( VALUE zeromq_module )
71
77
  //------------------------------------------
72
78
  {
@@ -90,6 +96,11 @@ VALUE context_declare( VALUE zeromq_module )
90
96
  return klass;
91
97
  }
92
98
 
99
+ /*
100
+ * @!method default
101
+ *
102
+ * The +default+ method ...
103
+ */
93
104
  VALUE context_default( VALUE self )
94
105
  //---------------------------------
95
106
  {
@@ -134,7 +145,7 @@ static VALUE context_initialize( int arg_count, VALUE* args, VALUE self )
134
145
  context->context = zmq_ctx_new();
135
146
  if ( !context->context )
136
147
  {
137
- rb_raise( exception_type, "%s", zmq_strerror( zmq_errno() ) );
148
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
138
149
  return Qnil;
139
150
  }
140
151
 
@@ -1,3 +1,9 @@
1
+ /*
2
+ * Document-class: ZeroMQ::Error
3
+ *
4
+ * The ZeroMQ::Error class ...
5
+ */
6
+
1
7
  //-----------------------------------------------------------------------------
2
8
 
3
9
  #include "error.h"
@@ -6,7 +6,7 @@
6
6
  #include "socket.h"
7
7
 
8
8
  VALUE module;
9
- VALUE exception_type;
9
+ VALUE exception_class;
10
10
  VALUE context_class;
11
11
  VALUE socket_class;
12
12
 
@@ -15,7 +15,7 @@ void Init_zeromq()
15
15
  {
16
16
  module = module_declare();
17
17
 
18
- exception_type = error_declare( module );
18
+ exception_class = error_declare( module );
19
19
  context_class = context_declare( module );
20
20
  socket_class = socket_declare( module );
21
21
  }
@@ -60,4 +60,4 @@ typedef unsigned __int64 uint64_t;
60
60
 
61
61
  extern VALUE context_class;
62
62
  extern VALUE socket_class;
63
- extern VALUE exception_type;
63
+ extern VALUE exception_class;
@@ -31,6 +31,15 @@ static VALUE socket_send( int argc_, VALUE* argv_, VALUE self_ );
31
31
  //-----------------------------------------------------------------------------
32
32
  // zeromq socket helper functions
33
33
 
34
+ struct zeromq_socket_io_request
35
+ {
36
+ void* socket;
37
+ zmq_msg_t* message;
38
+ int flags;
39
+
40
+ int result;
41
+ };
42
+
34
43
  struct zeromq_socket* zeromq_socket_create()
35
44
  //------------------------------------------
36
45
  {
@@ -60,6 +69,37 @@ void zeromq_socket_destroy( struct zeromq_socket* zeromq_socket )
60
69
  xfree( zeromq_socket );
61
70
  }
62
71
 
72
+ static VALUE zeromq_socket_send_blocking_region( void* argument )
73
+ //---------------------------------------------------------------
74
+ {
75
+
76
+ struct zeromq_socket_io_request* io_request =
77
+ ( struct zeromq_socket_io_request* )argument;
78
+
79
+ io_request->result = zmq_sendmsg(
80
+ io_request->socket,
81
+ io_request->message,
82
+ io_request->flags
83
+ );
84
+
85
+ return Qnil;
86
+ }
87
+
88
+ static VALUE zeromq_socket_receive_blocking_region( void* argument )
89
+ //------------------------------------------------------------------
90
+ {
91
+ struct zeromq_socket_io_request* io_request =
92
+ ( struct zeromq_socket_io_request* )argument;
93
+
94
+ io_request->result = zmq_recvmsg(
95
+ io_request->socket,
96
+ io_request->message,
97
+ io_request->flags
98
+ );
99
+
100
+ return Qnil;
101
+ }
102
+
63
103
  int zeromq_apply_socket_option( VALUE option, VALUE value, VALUE socket )
64
104
  //-----------------------------------------------------------------------
65
105
  {
@@ -67,8 +107,18 @@ int zeromq_apply_socket_option( VALUE option, VALUE value, VALUE socket )
67
107
  return ST_CONTINUE;
68
108
  }
69
109
 
70
- //-----------------------------------------------------------------------------
71
- // ZeroMQ::Socket class initialization
110
+ /*
111
+ * Document-class: ZeroMQ::Socket
112
+ *
113
+ * The Socket class ...
114
+ */
115
+
116
+ /* call-seq: new()
117
+ *
118
+ * The new method does some stuff.
119
+ */
120
+
121
+ //----------------------------------------------------------------------------
72
122
 
73
123
  VALUE socket_declare( VALUE zeromq_module )
74
124
  //-----------------------------------------
@@ -87,6 +137,8 @@ VALUE socket_declare( VALUE zeromq_module )
87
137
  rb_define_method( klass, "recv", socket_recv, -1 );
88
138
  rb_define_method( klass, "close", socket_close, 0 );
89
139
 
140
+ rb_define_alias( klass, "receive", "recv" );
141
+
90
142
  return klass;
91
143
  }
92
144
 
@@ -143,7 +195,7 @@ static VALUE socket_initialize( int arg_count, VALUE* arguments, VALUE self )
143
195
 
144
196
  if ( socket_type == Qnil )
145
197
  {
146
- rb_raise( exception_type, "The socket type is required." );
198
+ rb_raise( exception_class, "The socket type is required." );
147
199
  return Qnil;
148
200
  }
149
201
 
@@ -157,7 +209,7 @@ static VALUE socket_initialize( int arg_count, VALUE* arguments, VALUE self )
157
209
 
158
210
  if ( !socket->socket )
159
211
  {
160
- rb_raise( exception_type, "%s", zmq_strerror( zmq_errno() ) );
212
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
161
213
  return Qnil;
162
214
  }
163
215
 
@@ -180,7 +232,7 @@ static VALUE socket_close( VALUE self_ )
180
232
  if (s->socket != NULL) {
181
233
  int rc = zmq_close(s->socket);
182
234
  if (rc != 0) {
183
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
235
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
184
236
  return Qnil;
185
237
  }
186
238
 
@@ -222,7 +274,7 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
222
274
 
223
275
  if ( rc != 0 )
224
276
  {
225
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
277
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
226
278
  return Qnil;
227
279
  }
228
280
 
@@ -247,7 +299,7 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
247
299
 
248
300
  if ( rc != 0 )
249
301
  {
250
- rb_raise( exception_type, "%s", zmq_strerror( zmq_errno() ) );
302
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
251
303
  return Qnil;
252
304
  }
253
305
 
@@ -280,7 +332,7 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
280
332
 
281
333
  if ( rc != 0 )
282
334
  {
283
- rb_raise( exception_type, "%s", zmq_strerror( zmq_errno() ) );
335
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
284
336
  return Qnil;
285
337
  }
286
338
 
@@ -305,7 +357,7 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
305
357
  &optvalsize);
306
358
 
307
359
  if (rc != 0) {
308
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
360
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
309
361
  return Qnil;
310
362
  }
311
363
 
@@ -325,7 +377,7 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
325
377
  (void *)identity, &optvalsize);
326
378
 
327
379
  if (rc != 0) {
328
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
380
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
329
381
  return Qnil;
330
382
  }
331
383
 
@@ -337,14 +389,15 @@ static VALUE socket_getsockopt( VALUE self_, VALUE option_ )
337
389
  break;
338
390
 
339
391
  default:
340
- rb_raise (exception_type, "%s", zmq_strerror (EINVAL));
392
+ rb_raise (exception_class, "%s", zmq_strerror (EINVAL));
341
393
  return Qnil;
342
394
  }
343
395
 
344
396
  return retval;
345
397
  }
346
398
 
347
- static VALUE socket_setsockopt (VALUE self_, VALUE option_, VALUE optval_)
399
+ static VALUE socket_setsockopt( VALUE self_, VALUE option_, VALUE optval_ )
400
+ //-------------------------------------------------------------------------
348
401
  {
349
402
  int rc = 0;
350
403
  struct zeromq_socket * s;
@@ -409,14 +462,14 @@ static VALUE socket_setsockopt (VALUE self_, VALUE option_, VALUE optval_)
409
462
 
410
463
  default:
411
464
  {
412
- rb_raise( exception_type, "%s", zmq_strerror( EINVAL ) );
465
+ rb_raise( exception_class, "%s", zmq_strerror( EINVAL ) );
413
466
  return Qnil;
414
467
  }
415
468
  }
416
469
 
417
470
  if ( rc != 0 )
418
471
  {
419
- rb_raise( exception_type, "%s", zmq_strerror( zmq_errno() ) );
472
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
420
473
  return Qnil;
421
474
  }
422
475
 
@@ -424,6 +477,7 @@ static VALUE socket_setsockopt (VALUE self_, VALUE option_, VALUE optval_)
424
477
  }
425
478
 
426
479
  static VALUE socket_bind( VALUE self_, VALUE addr_ )
480
+ //--------------------------------------------------
427
481
  {
428
482
  struct zeromq_socket * s;
429
483
  Data_Get_Struct (self_, struct zeromq_socket, s);
@@ -431,7 +485,7 @@ static VALUE socket_bind( VALUE self_, VALUE addr_ )
431
485
 
432
486
  int rc = zmq_bind (s->socket, rb_string_value_cstr (&addr_));
433
487
  if (rc != 0) {
434
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
488
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
435
489
  return Qnil;
436
490
  }
437
491
 
@@ -439,6 +493,7 @@ static VALUE socket_bind( VALUE self_, VALUE addr_ )
439
493
  }
440
494
 
441
495
  static VALUE socket_connect( VALUE self_, VALUE addr_ )
496
+ //-----------------------------------------------------
442
497
  {
443
498
  struct zeromq_socket * s;
444
499
  Data_Get_Struct (self_, struct zeromq_socket, s);
@@ -446,146 +501,156 @@ static VALUE socket_connect( VALUE self_, VALUE addr_ )
446
501
 
447
502
  int rc = zmq_connect (s->socket, rb_string_value_cstr (&addr_));
448
503
  if (rc != 0) {
449
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
504
+ rb_raise (exception_class, "%s", zmq_strerror (zmq_errno ()));
450
505
  return Qnil;
451
506
  }
452
507
 
453
508
  return Qnil;
454
509
  }
455
510
 
456
- #ifdef HAVE_RUBY_INTERN_H
457
- struct zmq_send_recv_args {
458
- void *socket;
459
- zmq_msg_t *msg;
460
- int flags;
461
- int rc;
462
- };
463
-
464
- static VALUE zmq_send_blocking( void* args_ )
511
+ static VALUE socket_send( int argument_count, VALUE* arguments, VALUE self )
512
+ //--------------------------------------------------------------------------
465
513
  {
466
- struct zmq_send_recv_args *send_args = (struct zmq_send_recv_args *)args_;
467
-
468
- send_args->rc = zmq_sendmsg(
469
- send_args->socket,
470
- send_args->msg,
471
- send_args->flags
472
- );
514
+ VALUE message_bytes_value, flags_value;
473
515
 
474
- return Qnil;
475
- }
476
- #endif
477
-
478
- static VALUE socket_send (int argc_, VALUE* argv_, VALUE self_)
479
- {
480
- VALUE msg_, flags_;
481
-
482
- rb_scan_args (argc_, argv_, "11", &msg_, &flags_);
483
-
484
- struct zeromq_socket * s;
485
- Data_Get_Struct (self_, struct zeromq_socket, s);
486
- Check_Socket (s);
516
+ rb_scan_args(
517
+ argument_count,
518
+ arguments,
519
+ "11",
520
+ &message_bytes_value,
521
+ &flags_value
522
+ );
487
523
 
488
- Check_Type (msg_, T_STRING);
524
+ Check_Type( message_bytes_value, T_STRING );
525
+ int flags = NIL_P( flags_value ) ? 0 : NUM2INT( flags_value );
489
526
 
490
- int flags = NIL_P (flags_) ? 0 : NUM2INT (flags_);
527
+ struct zeromq_socket* socket;
528
+ Data_Get_Struct( self, struct zeromq_socket, socket );
529
+ Check_Socket( socket );
491
530
 
492
- zmq_msg_t msg;
493
- int msg_len = (int)RSTRING_LEN (msg_);
494
- int rc = zmq_msg_init_size (&msg, msg_len);
495
- if (rc != 0) {
496
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
531
+ zmq_msg_t message;
532
+ int message_length = ( int )RSTRING_LEN( message_bytes_value );
533
+
534
+ int result = zmq_msg_init_size( &message, message_length );
535
+ if ( result != 0 )
536
+ {
537
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
497
538
  return Qnil;
498
539
  }
499
- memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), msg_len);
500
-
501
- #ifdef HAVE_RUBY_INTERN_H
502
- if (!(flags & ZMQ_NOBLOCK)) {
503
- struct zmq_send_recv_args send_args;
504
- send_args.socket = s->socket;
505
- send_args.msg = &msg;
506
- send_args.flags = flags;
507
- rb_thread_blocking_region (zmq_send_blocking, (void*) &send_args, NULL, NULL);
508
- rc = send_args.rc;
540
+
541
+ memcpy(
542
+ zmq_msg_data( &message ),
543
+ RSTRING_PTR( message_bytes_value ),
544
+ message_length
545
+ );
546
+
547
+ if ( !( flags & ZMQ_NOBLOCK ) )
548
+ {
549
+ struct zeromq_socket_io_request io_request;
550
+ io_request.socket = socket->socket;
551
+ io_request.message = &message;
552
+ io_request.flags = flags;
553
+
554
+ rb_thread_blocking_region(
555
+ zeromq_socket_send_blocking_region,
556
+ ( void* )&io_request,
557
+ NULL,
558
+ NULL
559
+ );
560
+
561
+ result = io_request.result;
509
562
  }
510
563
  else
511
- #endif
512
- rc = zmq_sendmsg( s->socket, &msg, flags );
513
- if (rc != 0 && zmq_errno () == EAGAIN) {
514
- rc = zmq_msg_close (&msg);
515
- assert (rc == 0);
564
+ {
565
+ result = zmq_sendmsg( socket->socket, &message, flags );
566
+ }
567
+
568
+ if ( result == -1 && zmq_errno() == EAGAIN )
569
+ {
570
+ result = zmq_msg_close( &message );
571
+ assert( result == 0 );
516
572
  return Qfalse;
517
573
  }
518
574
 
519
- if (rc != 0) {
520
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
521
- rc = zmq_msg_close (&msg);
522
- assert (rc == 0);
575
+ if ( result == -1 )
576
+ {
577
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
578
+
579
+ result = zmq_msg_close( &message );
580
+ assert( result == 0 );
581
+
523
582
  return Qnil;
524
583
  }
525
584
 
526
- rc = zmq_msg_close (&msg);
527
- assert (rc == 0);
585
+ result = zmq_msg_close( &message );
586
+ assert( result == 0 );
587
+
528
588
  return Qtrue;
529
589
  }
530
590
 
531
- #ifdef HAVE_RUBY_INTERN_H
532
- static VALUE zmq_recv_blocking (void* args_)
591
+ static VALUE socket_recv( int argument_count, VALUE* arguments, VALUE self )
592
+ //--------------------------------------------------------------------------
533
593
  {
534
- struct zmq_send_recv_args *recv_args = (struct zmq_send_recv_args *)args_;
535
-
536
- recv_args->rc = zmq_recvmsg(recv_args->socket, recv_args->msg, recv_args->flags);
594
+ VALUE flags_value;
537
595
 
538
- return Qnil;
539
- }
540
- #endif
596
+ rb_scan_args(
597
+ argument_count,
598
+ arguments,
599
+ "01",
600
+ &flags_value
601
+ );
541
602
 
542
- static VALUE socket_recv (int argc_, VALUE* argv_, VALUE self_)
543
- {
544
- VALUE flags_;
545
-
546
- rb_scan_args (argc_, argv_, "01", &flags_);
603
+ int flags = NIL_P( flags_value ) ? 0 : NUM2INT( flags_value );
547
604
 
548
- struct zeromq_socket * s;
549
- Data_Get_Struct (self_, struct zeromq_socket, s);
550
- Check_Socket (s);
605
+ struct zeromq_socket* socket;
606
+ Data_Get_Struct( self, struct zeromq_socket, socket );
607
+ Check_Socket( socket );
551
608
 
552
- int flags = NIL_P (flags_) ? 0 : NUM2INT (flags_);
553
-
554
- zmq_msg_t msg;
555
- int rc = zmq_msg_init (&msg);
556
- assert (rc == 0);
557
-
558
- #ifdef HAVE_RUBY_INTERN_H
559
- if (!(flags & ZMQ_NOBLOCK)) {
560
- struct zmq_send_recv_args recv_args;
561
- recv_args.socket = s->socket;
562
- recv_args.msg = &msg;
563
- recv_args.flags = flags;
564
- rb_thread_blocking_region (zmq_recv_blocking, (void*) &recv_args,
565
- NULL, NULL);
566
- rc = recv_args.rc;
609
+ zmq_msg_t message;
610
+ int rc = zmq_msg_init( &message );
611
+ assert( rc == 0 );
612
+
613
+ if ( !( flags & ZMQ_NOBLOCK ) )
614
+ {
615
+ struct zeromq_socket_io_request io_request;
616
+ io_request.socket = socket->socket;
617
+ io_request.message = &message;
618
+ io_request.flags = flags;
619
+
620
+ rb_thread_blocking_region(
621
+ zeromq_socket_receive_blocking_region,
622
+ ( void* )&io_request,
623
+ NULL,
624
+ NULL
625
+ );
567
626
  }
568
627
  else
569
- #endif
570
- rc = zmq_recvmsg(s->socket, &msg, flags);
571
- if (rc == -1 && zmq_errno () == EAGAIN) {
572
- rc = zmq_msg_close (&msg);
573
- assert (rc == 0);
574
- return Qnil;
628
+ {
629
+ rc = zmq_recvmsg( socket->socket, &message, flags );
575
630
  }
576
631
 
577
- if (rc == -1) {
578
- rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
579
- rc = zmq_msg_close (&msg);
580
- assert (rc == 0);
632
+ if ( rc == -1 && zmq_errno() == EAGAIN )
633
+ {
634
+ rc = zmq_msg_close( &message );
635
+ assert( rc == 0 );
581
636
  return Qnil;
582
637
  }
583
638
 
584
- VALUE message = rb_str_new ((char*) zmq_msg_data (&msg),
585
- zmq_msg_size (&msg));
586
- rc = zmq_msg_close (&msg);
587
- assert (rc == 0);
588
- return message;
589
- }
639
+ if ( rc == -1 )
640
+ {
641
+ rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) );
642
+ rc = zmq_msg_close( &message );
643
+ assert( rc == 0 );
644
+ return Qnil;
645
+ }
590
646
 
647
+ VALUE message_bytes_value = rb_str_new(
648
+ ( char* )zmq_msg_data( &message ),
649
+ zmq_msg_size( &message )
650
+ );
651
+
652
+ rc = zmq_msg_close( &message );
653
+ assert( rc == 0 );
591
654
 
655
+ return message_bytes_value;
656
+ }
@@ -219,7 +219,7 @@ static VALUE internal_select(VALUE argval)
219
219
  rc = zmq_poll (ps.items, ps.nitems, arg->timeout_usec);
220
220
 
221
221
  if (rc == -1) {
222
- rb_raise(exception_type, "%s", zmq_strerror (zmq_errno ()));
222
+ rb_raise(exception_class, "%s", zmq_strerror (zmq_errno ()));
223
223
  return Qnil;
224
224
  }
225
225
  else if (rc == 0)
@@ -1,3 +1,3 @@
1
1
  module ZeroMQ
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeromq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  version: '0'
52
52
  requirements: []
53
53
  rubyforge_project:
54
- rubygems_version: 1.8.24
54
+ rubygems_version: 1.8.25
55
55
  signing_key:
56
56
  specification_version: 3
57
57
  summary: Ruby API for ZeroMQ