Class: StropheRuby::Connection

Inherits:
Object
  • Object
show all
Defined in:
ext/strophe_ruby.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Object

Initialize a connection object. We register instance variables that will hold the various callbacks

You should not manipulate instance variables. Use add_handler instead.
Maybe put this into xmpp_conn_new?


121
122
123
124
125
126
127
128
129
# File 'ext/strophe_ruby.c', line 121

static VALUE t_xmpp_conn_init(VALUE self, VALUE ctx) {
  rb_iv_set(self, "@ctx", ctx);  
  rb_iv_set(self, "@presence_handlers", rb_ary_new());
  rb_iv_set(self, "@message_handlers", rb_ary_new());
  rb_iv_set(self, "@iq_handlers", rb_ary_new());
  rb_iv_set(self, "@id_handlers", rb_ary_new());
  rb_gv_set("rb_conn",self);
  return self;
}

Class Method Details

.new(rb_ctx) ⇒ Object

create a connection object then call the initialize method for it



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/strophe_ruby.c', line 132

VALUE t_xmpp_conn_new(VALUE class, VALUE rb_ctx) {
  //Get the context in a format that C can understand
  xmpp_ctx_t *ctx;
  Data_Get_Struct(rb_ctx, xmpp_ctx_t, ctx);
  
  xmpp_conn_t *conn = xmpp_conn_new(ctx);
  VALUE tdata = Data_Wrap_Struct(class, 0, free, conn);
  VALUE argv[1];
  argv[0] = rb_ctx;
  
  //call ruby "initialize"
  rb_obj_call_init(tdata, 1, argv);
  return tdata;
}

Instance Method Details

#add_handler(rb_name) ⇒ Object

Add an handler for events in the stream (message, presence or iqs), We store the block we just received in the correct instance variable to invoke it later



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'ext/strophe_ruby.c', line 301

static VALUE t_xmpp_handler_add(VALUE self,VALUE rb_name) {    
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);    
    char *name = StringValuePtr(rb_name);
    VALUE arr;
    xmpp_handler handler;
    
    if(strcmp(name,"message") == 0) {
	arr = rb_iv_get(self, "@message_handlers");	
	handler = _message_handler;
    } else {
	if(strcmp(name,"presence") == 0) {
	    arr = rb_iv_get(self, "@presence_handlers");
	    handler = _presence_handler;
	} else {
	    arr = rb_iv_get(self, "@iq_handlers");
	    handler = _iq_handler;
	}
    }

    xmpp_handler_add(conn, handler, NULL, name, NULL, conn->ctx);
    rb_ary_push(arr, rb_block_proc());        
    return Qnil;
}

#add_id_handler(rb_id) ⇒ Object

Add an handler for ID stanzas. TODO:Test this!



327
328
329
330
331
332
333
334
335
336
337
# File 'ext/strophe_ruby.c', line 327

static VALUE t_xmpp_id_handler_add(VALUE self, VALUE rb_id) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);    
    char *id = StringValuePtr(rb_id);
    VALUE arr;
    
    arr = rb_iv_get(self, "@id_handlers");
    rb_ary_push(arr, rb_block_proc());
    xmpp_id_handler_add(conn, _id_handler, StringValuePtr(id), conn->ctx);
    return Qnil;
}

#cloneObject

Clone a connection



148
149
150
151
# File 'ext/strophe_ruby.c', line 148

static VALUE t_xmpp_conn_clone(VALUE self) {
    xmpp_conn_t *conn = xmpp_conn_clone((xmpp_conn_t *)self);    
    return Data_Wrap_Struct(cConnection, 0, t_xmpp_conn_release, conn);
}

#connectObject

Connect and authenticate. We store the block in the client_conn_handler variable to invoke it later



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'ext/strophe_ruby.c', line 340

static VALUE t_xmpp_connect_client(VALUE self) {
    xmpp_conn_t *conn;
    xmpp_ctx_t *ctx;
    Data_Get_Struct(rb_iv_get(self, "@ctx"), xmpp_ctx_t, ctx);    
    Data_Get_Struct(self, xmpp_conn_t, conn);
    
    /*The user might have passed a block... however we don't want to invoke it right now.
    We store it to invoke it later in _xmpp_conn_handler */
    if (rb_block_given_p())
	client_conn_handler = rb_block_proc();
    
    int result = xmpp_connect_client(conn, NULL, 0, _conn_handler, ctx);
    return INT2FIX(result);
}

#connect_timeoutObject

get the connect_timeout



185
186
187
188
189
190
# File 'ext/strophe_ruby.c', line 185

static VALUE t_xmpp_conn_get_connect_timeout(VALUE self) 
{
     xmpp_conn_t *conn;
     Data_Get_Struct(self, xmpp_conn_t, conn);
     return INT2FIX(xmpp_conn_get_connect_timeout(conn));
}

#connect_timeout=(timeout) ⇒ Object

set the connect_timeout



193
194
195
196
197
198
199
# File 'ext/strophe_ruby.c', line 193

static VALUE t_xmpp_conn_set_connect_timeout(VALUE self, VALUE timeout)
{
     xmpp_conn_t *conn;
     Data_Get_Struct(self, xmpp_conn_t, conn);
     xmpp_conn_set_connect_timeout(conn, FIX2INT(timeout));
     return timeout;
}

#disconnectObject

Disconnect from the stream. Is it needed? Not too sure about it. Normally if you just call xmpp_stop you should be fine



356
357
358
359
360
361
362
# File 'ext/strophe_ruby.c', line 356

static VALUE t_xmpp_disconnect(VALUE self) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);
    xmpp_disconnect(conn);
    conn_disconnect(conn);
    return Qtrue;
}

#jidObject

Get the jid



155
156
157
158
159
# File 'ext/strophe_ruby.c', line 155

static VALUE t_xmpp_conn_get_jid(VALUE self) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);
    return rb_str_new2(xmpp_conn_get_jid(conn));    
}

#jid=(jid) ⇒ Object

Set the jid



162
163
164
165
166
167
# File 'ext/strophe_ruby.c', line 162

static VALUE t_xmpp_conn_set_jid(VALUE self, VALUE jid) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);
    xmpp_conn_set_jid(conn, StringValuePtr(jid));
    return jid;
}

#passwordObject

get the password



170
171
172
173
174
# File 'ext/strophe_ruby.c', line 170

static VALUE t_xmpp_conn_get_pass(VALUE self) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);
    return rb_str_new2(xmpp_conn_get_pass(conn));
}

#password=(pass) ⇒ Object

set the password



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

static VALUE t_xmpp_conn_set_pass(VALUE self, VALUE pass) {
    xmpp_conn_t *conn;
    Data_Get_Struct(self, xmpp_conn_t, conn);
    xmpp_conn_set_pass(conn, StringValuePtr(pass));
    return pass;
}

#releaseObject

Release the connection object. (Currently not called at all… because it causes segmentation error once in a while)



111
112
113
114
115
# File 'ext/strophe_ruby.c', line 111

static VALUE t_xmpp_conn_release(VALUE self) {
  xmpp_conn_t *conn;
  Data_Get_Struct(self,xmpp_conn_t,conn);
  xmpp_conn_release(conn);
}

#send(rb_stanza) ⇒ Object

Send a stanza in the stream



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

static VALUE t_xmpp_send(VALUE self, VALUE rb_stanza) {

    xmpp_conn_t *conn;
    xmpp_stanza_t *stanza;
    
    Data_Get_Struct(self, xmpp_conn_t, conn);
    Data_Get_Struct(rb_stanza, xmpp_stanza_t, stanza);
    
    xmpp_send(conn,stanza);
    return Qtrue;
}

#send_raw_string(rb_text) ⇒ Object

Send a stanza in the stream



378
379
380
381
382
383
384
385
386
387
388
# File 'ext/strophe_ruby.c', line 378

static VALUE t_xmpp_send_raw_string(VALUE self, VALUE rb_text) {

    xmpp_conn_t *conn;
    char *text;
    
    Data_Get_Struct(self, xmpp_conn_t, conn);
    text = StringValuePtr(rb_text);
    
    xmpp_send_raw_string(conn, "%s", text);
    return Qtrue;
}