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:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/rbczmq/frame.c', line 72

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();
    }
    ZmqRegisterFrame(fr);
    frame = Data_Wrap_Struct(rb_cZmqFrame, 0, rb_czmq_free_frame_gc, fr);
    rb_obj_call_init(frame, 0, NULL);
    return frame;
}

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)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/rbczmq/frame.c', line 313

static VALUE rb_czmq_frame_cmp(VALUE obj, VALUE other_frame)
{
    long diff;
    zframe_t *other = NULL;
    if (obj == other_frame) return INT2NUM(0);
    ZmqGetFrame(obj);
    ZmqAssertFrame(other_frame);
    Data_Get_Struct(other_frame, zframe_t, other);
    if (!other) rb_raise(rb_eTypeError, "uninitialized ZMQ frame!"); \
    if (!(st_lookup(frames_map, (st_data_t)other, 0))) rb_raise(rb_eZmqError, "object %p has been destroyed by the ZMQ framework", (void *)other_frame);
    diff = (zframe_size(frame) - zframe_size(other));
    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)


294
295
296
297
298
# File 'ext/rbczmq/frame.c', line 294

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)


147
148
149
150
151
152
153
# File 'ext/rbczmq/frame.c', line 147

static VALUE rb_czmq_frame_data(VALUE obj)
{
    size_t size;
    ZmqGetFrame(obj);
    size = zframe_size(frame);
    return ZmqEncode(rb_str_new((char *)zframe_data(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)


231
232
233
234
235
236
# File 'ext/rbczmq/frame.c', line 231

static VALUE rb_czmq_frame_data_equals_p(VALUE obj, VALUE data)
{
    ZmqGetFrame(obj);
    Check_Type(data, T_STRING);
    return (zframe_streq(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)


108
109
110
111
112
113
# File 'ext/rbczmq/frame.c', line 108

static VALUE rb_czmq_frame_destroy(VALUE obj)
{
    ZmqGetFrame(obj);
    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:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/rbczmq/frame.c', line 202

static VALUE rb_czmq_frame_dup(VALUE obj)
{
    VALUE dup;
    zframe_t *dup_fr = NULL;
    errno = 0;
    ZmqGetFrame(obj);
    dup_fr = zframe_dup(frame);
    if (dup_fr == NULL) {
        ZmqAssertSysError();
        rb_memerror();
    }
    ZmqRegisterFrame(dup_fr);
    dup = Data_Wrap_Struct(rb_cZmqFrame, 0, rb_czmq_free_frame_gc, dup_fr);
    rb_obj_call_init(dup, 0, NULL);
    return dup;
}

#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)


269
270
271
272
273
274
275
276
277
278
# File 'ext/rbczmq/frame.c', line 269

static VALUE rb_czmq_frame_eql_p(VALUE obj, VALUE other_frame)
{
    zframe_t *other = NULL;
    ZmqGetFrame(obj);
    ZmqAssertFrame(other_frame);
    Data_Get_Struct(other_frame, zframe_t, other);
    if (!other) rb_raise(rb_eTypeError, "uninitialized ZMQ frame!"); \
    if (!(st_lookup(frames_map, (st_data_t)other, 0))) rb_raise(rb_eZmqError, "object %p has been destroyed by the ZMQ framework", (void *)other_frame);
    return (zframe_eq(frame, other)) ? Qtrue : Qfalse;
}

#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)


250
251
252
253
254
# File 'ext/rbczmq/frame.c', line 250

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

Dumps out frame contents to stderr

Examples

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

Returns:

  • (nil)


341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/rbczmq/frame.c', line 341

static VALUE rb_czmq_frame_print(int argc, VALUE *argv, VALUE obj)
{
    VALUE prefix;
    const char *print_prefix = NULL;
    ZmqGetFrame(obj);
    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, (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)


370
371
372
373
374
375
376
377
378
# File 'ext/rbczmq/frame.c', line 370

static VALUE rb_czmq_frame_reset(VALUE obj, VALUE data)
{
    errno = 0;
    ZmqGetFrame(obj);
    Check_Type(data, T_STRING);
    zframe_reset(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)


127
128
129
130
131
132
133
# File 'ext/rbczmq/frame.c', line 127

static VALUE rb_czmq_frame_size(VALUE obj)
{
    size_t size;
    ZmqGetFrame(obj);
    size = zframe_size(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)


184
185
186
187
188
# File 'ext/rbczmq/frame.c', line 184

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

#to_sString

Returns a String representation of this frame.

Examples

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

Returns:

  • (String)


167
168
169
170
# File 'ext/rbczmq/frame.c', line 167

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)


167
168
169
170
# File 'ext/rbczmq/frame.c', line 167

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