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?


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

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



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

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'ext/strophe_ruby/strophe_ruby.c', line 267

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!



293
294
295
296
297
298
299
300
301
302
303
# File 'ext/strophe_ruby/strophe_ruby.c', line 293

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



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

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



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

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



322
323
324
325
326
327
# File 'ext/strophe_ruby/strophe_ruby.c', line 322

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



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

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



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

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



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

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



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

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

#releaseObject

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



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

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



330
331
332
333
334
335
336
337
338
339
340
# File 'ext/strophe_ruby/strophe_ruby.c', line 330

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