Class: StropheRuby::Connection

Inherits:
Object
  • Object
show all
Defined in:
ext/strophe_ruby/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?


116
117
118
119
120
121
122
123
124
# File 'ext/strophe_ruby/strophe_ruby.c', line 116

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/strophe_ruby/strophe_ruby.c', line 127

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, t_xmpp_conn_release, 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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/strophe_ruby/strophe_ruby.c', line 263

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 = STR2CSTR(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!



289
290
291
292
293
294
295
296
297
298
299
# File 'ext/strophe_ruby/strophe_ruby.c', line 289

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 = STR2CSTR(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, STR2CSTR(id), conn->ctx);
    return Qnil;
}

#cloneObject

Clone a connection



143
144
145
146
# File 'ext/strophe_ruby/strophe_ruby.c', line 143

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/strophe_ruby/strophe_ruby.c', line 302

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);
}

#disconnectObject

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



318
319
320
321
322
323
# File 'ext/strophe_ruby/strophe_ruby.c', line 318

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

#jidObject

Get the jid



150
151
152
153
154
# File 'ext/strophe_ruby/strophe_ruby.c', line 150

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



157
158
159
160
161
162
# File 'ext/strophe_ruby/strophe_ruby.c', line 157

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, STR2CSTR(jid));
    return jid;
}

#passwordObject

get the password



165
166
167
168
169
# File 'ext/strophe_ruby/strophe_ruby.c', line 165

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



172
173
174
175
176
177
# File 'ext/strophe_ruby/strophe_ruby.c', line 172

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, STR2CSTR(pass));
    return pass;
}

#send(rb_stanza) ⇒ Object

Send a stanza in the stream



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

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;
}