summaryrefslogtreecommitdiff
path: root/bindings/ruby/rbzmq.cpp
blob: 83bb2b6cc1ce73b94dff78c30cc09c0041c78912 (plain)
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
/*
    Copyright (c) 2007-2009 FastMQ Inc.

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <ruby.h>

#include "../c/zmq.h"

static void context_free (void *ctx)
{
    if (ctx) {
       int rc = zmq_term (ctx);
       assert (rc == 0);
    }
}

static VALUE context_alloc (VALUE class_)
{
    return rb_data_object_alloc (class_, NULL, 0, context_free);
}

static VALUE context_initialize (VALUE self_, VALUE app_threads_,
    VALUE io_threads_, VALUE flags_)
{
    assert (!DATA_PTR (self_));
    void *ctx = zmq_init (NUM2INT (app_threads_), NUM2INT (io_threads_),
        NUM2INT (flags_));
    if (!ctx) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    DATA_PTR (self_) = (void*) ctx;
    return self_;
}

static void socket_free (void *s)
{
    if (s) {
       int rc = zmq_close (s);
       assert (rc == 0);
    }
}

static VALUE socket_alloc (VALUE class_)
{
    return rb_data_object_alloc (class_, NULL, 0, socket_free);
}

static VALUE socket_initialize (VALUE self_, VALUE context_, VALUE type_)
{
    assert (!DATA_PTR (self_));

    if (strcmp (rb_obj_classname (context_), "Context") != 0) {
        rb_raise (rb_eArgError, "expected Context object");
        return Qnil;
    }

    void *s = zmq_socket (DATA_PTR (context_), NUM2INT (type_));
    if (!s) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    DATA_PTR (self_) = (void*) s;
    return self_;
}


static VALUE socket_setsockopt (VALUE self_, VALUE option_,
    VALUE optval_)
{

    int rc = 0;

    switch (NUM2INT (option_)) {
    case ZMQ_HWM:
    case ZMQ_LWM:
    case ZMQ_SWAP:
    case ZMQ_AFFINITY:
    case ZMQ_RATE:
    case ZMQ_RECOVERY_IVL:
    case ZMQ_MCAST_LOOP:
        {
            long optval = FIX2LONG (optval_);

            //  Forward the code to native 0MQ library.
            rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), 
                (void *) &optval, 4);
        }
        break;

    case ZMQ_IDENTITY:
    case ZMQ_SUBSCRIBE:
    case ZMQ_UNSUBSCRIBE:

        //  Forward the code to native 0MQ library.
        rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), 
	    (void *) StringValueCStr (optval_), RSTRING_LEN (optval_));	
        break;

    default:
        rc = -1;
        errno = EINVAL;
    }

    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    return self_;
}


static VALUE socket_bind (VALUE self_, VALUE addr_)
{
    assert (DATA_PTR (self_));

    int rc = zmq_bind (DATA_PTR (self_), rb_string_value_cstr (&addr_));
    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    return Qnil;
}

static VALUE socket_connect (VALUE self_, VALUE addr_)
{
    assert (DATA_PTR (self_));

    int rc = zmq_connect (DATA_PTR (self_), rb_string_value_cstr (&addr_));
    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    return Qnil;
}

static VALUE socket_send (VALUE self_, VALUE msg_, VALUE flags_)
{
    assert (DATA_PTR (self_));

    Check_Type (msg_, T_STRING);

    zmq_msg_t msg;
    int rc = zmq_msg_init_size (&msg, RSTRING_LEN (msg_));
    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }
    memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), RSTRING_LEN (msg_));
 
    rc = zmq_send (DATA_PTR (self_), &msg, NUM2INT (flags_));
    if (rc != 0 && errno == EAGAIN) {
        rc = zmq_msg_close (&msg);
        assert (rc == 0);
        return Qfalse;
    }

    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        rc = zmq_msg_close (&msg);
        assert (rc == 0);
        return Qnil;
    }

    rc = zmq_msg_close (&msg);
    assert (rc == 0);
    return Qtrue;
}

static VALUE socket_flush (VALUE self_)
{
    assert (DATA_PTR (self_));

    int rc = zmq_flush (DATA_PTR (self_));
    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        return Qnil;
    }

    return Qnil;
}

static VALUE socket_recv (VALUE self_, VALUE flags_)
{
    assert (DATA_PTR (self_));

    zmq_msg_t msg;
    int rc = zmq_msg_init (&msg);
    assert (rc == 0);

    rc = zmq_recv (DATA_PTR (self_), &msg, NUM2INT (flags_));
    if (rc != 0 && errno == EAGAIN) {
        rc = zmq_msg_close (&msg);
        assert (rc == 0);
        return Qnil;
    }

    if (rc != 0) {
        rb_raise (rb_eRuntimeError, strerror (errno));
        rc = zmq_msg_close (&msg);
        assert (rc == 0);
        return Qnil;
    }

    VALUE message = rb_str_new ((char*) zmq_msg_data (&msg),
        zmq_msg_size (&msg));
    rc = zmq_msg_close (&msg);
    assert (rc == 0);
    return message;
}

extern "C" void Init_librbzmq ()
{	
    VALUE context_type = rb_define_class ("Context", rb_cObject);
    rb_define_alloc_func (context_type, context_alloc);
    rb_define_method (context_type, "initialize",
        (VALUE(*)(...)) context_initialize, 3);

    VALUE socket_type = rb_define_class ("Socket", rb_cObject);
    rb_define_alloc_func (socket_type, socket_alloc);
    rb_define_method (socket_type, "initialize",
        (VALUE(*)(...)) socket_initialize, 2);
    rb_define_method (socket_type, "setsockopt",
        (VALUE(*)(...)) socket_setsockopt, 2);
    rb_define_method (socket_type, "bind",
        (VALUE(*)(...)) socket_bind, 1);
    rb_define_method (socket_type, "connect",
        (VALUE(*)(...)) socket_connect, 1);
    rb_define_method (socket_type, "send",
        (VALUE(*)(...)) socket_send, 2);
    rb_define_method (socket_type, "flush",
        (VALUE(*)(...)) socket_flush, 0);
    rb_define_method (socket_type, "recv",
        (VALUE(*)(...)) socket_recv, 1);

    rb_define_global_const ("HWM", INT2NUM (ZMQ_HWM));
    rb_define_global_const ("LWM", INT2NUM (ZMQ_LWM));
    rb_define_global_const ("SWAP", INT2NUM (ZMQ_SWAP));
    rb_define_global_const ("AFFINITY", INT2NUM (ZMQ_AFFINITY));
    rb_define_global_const ("IDENTITY", INT2NUM (ZMQ_IDENTITY));
    rb_define_global_const ("SUBSCRIBE", INT2NUM (ZMQ_SUBSCRIBE));
    rb_define_global_const ("UNSUBSCRIBE", INT2NUM (ZMQ_UNSUBSCRIBE));
    rb_define_global_const ("RATE", INT2NUM (ZMQ_RATE));
    rb_define_global_const ("RECOVERY_IVL", INT2NUM (ZMQ_RECOVERY_IVL));
    rb_define_global_const ("MCAST_LOOP", INT2NUM (ZMQ_MCAST_LOOP));

    rb_define_global_const ("NOBLOCK", INT2NUM (ZMQ_NOBLOCK));
    rb_define_global_const ("NOFLUSH", INT2NUM (ZMQ_NOFLUSH));

    rb_define_global_const ("P2P", INT2NUM (ZMQ_P2P));
    rb_define_global_const ("SUB", INT2NUM (ZMQ_SUB));
    rb_define_global_const ("PUB", INT2NUM (ZMQ_PUB));
    rb_define_global_const ("REQ", INT2NUM (ZMQ_REQ));
    rb_define_global_const ("REP", INT2NUM (ZMQ_REP));

    rb_define_global_const ("POLL", INT2NUM (ZMQ_POLL));
}