Class: ZMQ::Frame

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/zmq/frame.rb,
ext/rbczmq/frame.c

Constant Summary collapse

MORE =
INT2NUM(ZFRAME_MORE)
REUSE =
INT2NUM(ZFRAME_REUSE)
DONTWAIT =
INT2NUM(ZFRAME_DONTWAIT)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ZMQ::Frame.newZMQ::Frame .ZMQ::Frame.new("data") ⇒ ZMQ::Frame

Creates a new ZMQ::Frame instance. Can be initialized with or without data. A frame corresponds to one zmq_msg_t.

Examples

ZMQ::Frame.new    =>  ZMQ::Frame
ZMQ::Frame.new("data")    =>  ZMQ::Frame

Overloads:


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/rbczmq/frame.c', line 65

static VALUE rb_czmq_frame_s_new(int argc, VALUE *argv, VALUE frame)
{
    VALUE data;
    errno = 0;
    zframe_t *fr;
    rb_scan_args(argc, argv, "01", &data);
    if (NIL_P(data)) {
        fr = zframe_new(NULL, 0);
    } else {
        Check_Type(data, T_STRING);
        fr = zframe_new(RSTRING_PTR(data), (size_t)RSTRING_LEN(data));
    }
    if (fr == NULL) {
        ZmqAssertSysError();
        rb_memerror();
    }
    return rb_czmq_alloc_frame(fr);
}

Instance Method Details

#<=>(other) ⇒ Boolean

Comparable support for ZMQ::Frame instances.

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
other_frame = ZMQ::Frame.new("other")
frame > other_frame     =>  true

Returns:

  • (Boolean)

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/rbczmq/frame.c', line 306

static VALUE rb_czmq_frame_cmp(VALUE obj, VALUE other_frame)
{
    long diff;
    zmq_frame_wrapper *other = NULL;
    if (obj == other_frame) return INT2NUM(0);
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    ZmqAssertFrame(other_frame);
    Data_Get_Struct(other_frame, zmq_frame_wrapper, other);
    if (!other || !other->frame) rb_raise(rb_eTypeError, "uninitialized ZMQ frame!");
    ZmqAssertFrameOwned(frame);
    diff = (zframe_size(frame->frame) - zframe_size(other->frame));
    if (diff == 0) return INT2NUM(0);
    if (diff > 0) return INT2NUM(1);
    return INT2NUM(-1);
}

#==(other) ⇒ Boolean

Determines if the current frame is equal to another (identical size and data).

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
other_frame = ZMQ::Frame.new("other")
frame == other_frame     =>  false
frame == frame      =>  true

Returns:

  • (Boolean)

287
288
289
290
291
# File 'ext/rbczmq/frame.c', line 287

static VALUE rb_czmq_frame_equals(VALUE obj, VALUE other_frame)
{
    if (obj == other_frame) return Qtrue;
    return rb_czmq_frame_eql_p(obj, other_frame);
}

#dataString

Returns the data represented by the frame as a string.

Examples

frame = ZMQ::Frame.new("data")    =>  ZMQ::Frame
frame.data     =>  "data"

Returns:

  • (String)

139
140
141
142
143
144
145
146
# File 'ext/rbczmq/frame.c', line 139

static VALUE rb_czmq_frame_data(VALUE obj)
{
    size_t size;
    ZmqGetFrame(obj);
    ZmqReturnNilUnlessFrameOwned(frame);
    size = zframe_size(frame->frame);
    return ZmqEncode(rb_str_new((char *)zframe_data(frame->frame), (long)size));
}

#data_equals?("data") ⇒ Boolean

Determines if the current frame's payload matches a given string

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.data_equals?("message")     =>  true

Returns:

  • (Boolean)

221
222
223
224
225
226
227
# File 'ext/rbczmq/frame.c', line 221

static VALUE rb_czmq_frame_data_equals_p(VALUE obj, VALUE data)
{
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    Check_Type(data, T_STRING);
    return (zframe_streq(frame->frame, RSTRING_PTR(data)) == true) ? Qtrue : Qfalse;
}

#destroynil

Explicitly destroys an allocated ZMQ::Frame instance. Useful for manual memory management, otherwise the GC will take the same action if a frame object is not reachable anymore on the next GC cycle. This is a lower level API.

Examples

frame = ZMQ::Frame.new("data")    =>  ZMQ::Frame
frame.destroy     =>  nil

Returns:

  • (nil)

98
99
100
101
102
103
104
# File 'ext/rbczmq/frame.c', line 98

static VALUE rb_czmq_frame_destroy(VALUE obj)
{
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    rb_czmq_free_frame(frame);
    return Qnil;
}

#dupZMQ::Frame

Returns a copy of the current frame

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.dup     =>  ZMQ::Frame

Returns:


196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/rbczmq/frame.c', line 196

static VALUE rb_czmq_frame_dup(VALUE obj)
{
    zframe_t *dup_fr = NULL;
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    dup_fr = zframe_dup(frame->frame);
    if (dup_fr == NULL) {
        ZmqAssertSysError();
        rb_memerror();
    }
    return rb_czmq_alloc_frame(dup_fr);
}

#eql?(other) ⇒ Boolean

Determines if the current frame is equal to another (identical size and data).

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
other_frame = ZMQ::Frame.new("other")
frame.eql?(other_frame)     =>  false

Returns:

  • (Boolean)

261
262
263
264
265
266
267
268
269
270
271
# File 'ext/rbczmq/frame.c', line 261

static VALUE rb_czmq_frame_eql_p(VALUE obj, VALUE other_frame)
{
    zmq_frame_wrapper *other = NULL;
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    ZmqAssertFrame(other_frame);
    Data_Get_Struct(other_frame, zmq_frame_wrapper, other);
    if (!other || !other->frame) rb_raise(rb_eTypeError, "uninitialized ZMQ frame!"); \
    ZmqAssertFrameOwned(other);
    return (zframe_eq(frame->frame, other->frame)) ? Qtrue : Qfalse;
}

#gone?(# = > false) ⇒ Boolean

frame.gone? #=> true

Return boolean indicating if the ZMQ::Frame is gone (sent or destroyed). If the message is gone, accessor methods will return nil and methods requiring data or methods that mutate the message will raise an exception.

Returns:

  • (Boolean)

387
388
389
390
391
392
393
# File 'ext/rbczmq/frame.c', line 387

static VALUE rb_czmq_frame_gone(VALUE obj)
{
    ZmqGetFrame(obj);
    return (frame->flags & ZMQ_FRAME_OWNED) ||
           (frame->message != NULL && (frame->message->flags & ZMQ_MESSAGE_OWNED))
               ? Qfalse : Qtrue;
}

#more?Boolean

Determines if the current frame is part of a multipart message.

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.more?     =>  false

Returns:

  • (Boolean)

241
242
243
244
245
246
# File 'ext/rbczmq/frame.c', line 241

static VALUE rb_czmq_frame_more_p(VALUE obj)
{
    ZmqGetFrame(obj);
    ZmqReturnNilUnlessFrameOwned(frame);
    return (zframe_more(frame->frame) == ZFRAME_MORE) ? Qtrue : Qfalse;
}

Dumps out frame contents to stderr

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.print  =>  nil

Returns:

  • (nil)

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'ext/rbczmq/frame.c', line 335

static VALUE rb_czmq_frame_print(int argc, VALUE *argv, VALUE obj)
{
    VALUE prefix;
    const char *print_prefix = NULL;
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    rb_scan_args(argc, argv, "01", &prefix);
    if (NIL_P(prefix)) {
        print_prefix = "";
    } else {
        Check_Type(prefix, T_STRING);
        print_prefix = RSTRING_PTR(prefix);
    }
    zframe_print(frame->frame, (char *)print_prefix);
    return Qnil;
}

#reset("new") ⇒ nil

Sets new content for this frame

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.reset("new")   =>  nil
frame.data   =>   "new"

Returns:

  • (nil)

365
366
367
368
369
370
371
372
373
374
# File 'ext/rbczmq/frame.c', line 365

static VALUE rb_czmq_frame_reset(VALUE obj, VALUE data)
{
    errno = 0;
    ZmqGetFrame(obj);
    ZmqAssertFrameOwned(frame);
    Check_Type(data, T_STRING);
    zframe_reset(frame->frame, (char *)RSTRING_PTR(data), (size_t)RSTRING_LEN(data));
    ZmqAssertSysError();
    return Qnil;
}

#sizeFixnum

Returns the size of the frame.

Examples

frame = ZMQ::Frame.new("data")    =>  ZMQ::Frame
frame.size     =>  4

Returns:

  • (Fixnum)

118
119
120
121
122
123
124
125
# File 'ext/rbczmq/frame.c', line 118

static VALUE rb_czmq_frame_size(VALUE obj)
{
    size_t size;
    ZmqGetFrame(obj);
    ZmqReturnNilUnlessFrameOwned(frame);
    size = zframe_size(frame->frame);
    return LONG2FIX(size);
}

#strhexString

Returns a printable hex representation of this frame

Examples

frame = ZMQ::Frame.new("message")    =>  ZMQ::Frame
frame.strhex     =>  "6D657373616765"

Returns:

  • (String)

177
178
179
180
181
182
# File 'ext/rbczmq/frame.c', line 177

static VALUE rb_czmq_frame_strhex(VALUE obj)
{
    ZmqGetFrame(obj);
    ZmqReturnNilUnlessFrameOwned(frame);
    return rb_str_new2(zframe_strhex(frame->frame));
}

#to_sString

Returns a String representation of this frame.

Examples

frame = ZMQ::Frame.new("data")    =>  ZMQ::Frame
frame.to_s     =>  "data"

Returns:

  • (String)

160
161
162
163
# File 'ext/rbczmq/frame.c', line 160

static VALUE rb_czmq_frame_to_s(VALUE obj)
{
    return rb_funcall(obj, intern_data, 0, 0);
}

#to_sString

Returns a String representation of this frame.

Examples

frame = ZMQ::Frame.new("data")    =>  ZMQ::Frame
frame.to_s     =>  "data"

Returns:

  • (String)

160
161
162
163
# File 'ext/rbczmq/frame.c', line 160

static VALUE rb_czmq_frame_to_s(VALUE obj)
{
    return rb_funcall(obj, intern_data, 0, 0);
}