1 #include "rblm.h" |
|
2 #include "rblm-private.h" |
|
3 |
|
4 VALUE lm_cConnection; |
|
5 |
|
6 VALUE conn_set_server (VALUE self, VALUE server); |
|
7 |
|
8 /* -- START of GMainContext hack -- |
|
9 * This is a hack to get the GMainContext from a ruby VALUE, this will break if |
|
10 * internals change in Ruby/GLib. |
|
11 */ |
|
12 typedef struct { |
|
13 gpointer boxed; |
|
14 gboolean own; |
|
15 gsize type; |
|
16 } boxed_holder; |
|
17 |
|
18 static GMainContext * |
|
19 rb_lm_hack_get_main_context_from_rval (VALUE ctx_rval) |
|
20 { |
|
21 boxed_holder *holder; |
|
22 |
|
23 Data_Get_Struct (ctx_rval, boxed_holder, holder); |
|
24 |
|
25 return holder->boxed; |
|
26 } |
|
27 /* -- END of GMainContext hack -- */ |
|
28 |
|
29 LmConnection * |
|
30 rb_lm_connection_from_ruby_object (VALUE obj) |
|
31 { |
|
32 LmConnection *conn; |
|
33 |
|
34 if (!rb_lm__is_kind_of (obj, lm_cConnection)) { |
|
35 rb_raise (rb_eTypeError, "not a LmConnection"); |
|
36 } |
|
37 |
|
38 Data_Get_Struct (obj, LmConnection, conn); |
|
39 |
|
40 return conn; |
|
41 } |
|
42 |
|
43 void |
|
44 conn_free (LmConnection *self) |
|
45 { |
|
46 lm_connection_unref (self); |
|
47 } |
|
48 |
|
49 VALUE |
|
50 conn_allocate (VALUE klass) |
|
51 { |
|
52 return Data_Wrap_Struct (klass, NULL, conn_free, NULL); |
|
53 } |
|
54 |
|
55 VALUE |
|
56 conn_initialize (int argc, VALUE *argv, VALUE self) |
|
57 { |
|
58 LmConnection *conn; |
|
59 char *srv_str = NULL; |
|
60 VALUE server, context; |
|
61 |
|
62 rb_scan_args (argc, argv, "02", &server, &context); |
|
63 |
|
64 if (!NIL_P (context)) { |
|
65 GMainContext *ctx; |
|
66 |
|
67 ctx = rb_lm_hack_get_main_context_from_rval (context); |
|
68 |
|
69 conn = lm_connection_new_with_context (NULL, ctx); |
|
70 } else { |
|
71 conn = lm_connection_new (NULL); |
|
72 } |
|
73 |
|
74 DATA_PTR (self) = conn; |
|
75 |
|
76 if (!NIL_P (server)) { |
|
77 conn_set_server (self, server); |
|
78 } |
|
79 |
|
80 return self; |
|
81 } |
|
82 |
|
83 static void |
|
84 open_callback (LmConnection *conn, gboolean success, gpointer user_data) |
|
85 { |
|
86 rb_funcall((VALUE)user_data, rb_intern ("call"), 1, |
|
87 GBOOL2RVAL (success)); |
|
88 } |
|
89 |
|
90 VALUE |
|
91 conn_open (int argc, VALUE *argv, VALUE self) |
|
92 { |
|
93 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
94 VALUE func; |
|
95 |
|
96 rb_scan_args (argc, argv, "0&", &func); |
|
97 if (NIL_P (func)) { |
|
98 func = rb_block_proc (); |
|
99 } |
|
100 |
|
101 return GBOOL2RVAL (lm_connection_open (conn, open_callback, |
|
102 (gpointer) func, NULL, NULL)); |
|
103 } |
|
104 |
|
105 VALUE |
|
106 conn_close (VALUE self) |
|
107 { |
|
108 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
109 |
|
110 return GBOOL2RVAL (lm_connection_close (conn, NULL)); |
|
111 } |
|
112 |
|
113 static void |
|
114 auth_callback (LmConnection *conn, gboolean success, gpointer user_data) |
|
115 { |
|
116 rb_funcall((VALUE)user_data, rb_intern ("call"), 1, |
|
117 GBOOL2RVAL (success)); |
|
118 } |
|
119 |
|
120 VALUE |
|
121 conn_auth (int argc, VALUE *argv, VALUE self) |
|
122 { |
|
123 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
124 VALUE name, password, resource, func; |
|
125 |
|
126 rb_scan_args (argc, argv, "21&", &name, &password, &resource, &func); |
|
127 if (NIL_P (func)) { |
|
128 func = rb_block_proc (); |
|
129 } |
|
130 |
|
131 return GBOOL2RVAL (lm_connection_authenticate (conn, |
|
132 StringValuePtr (name), |
|
133 StringValuePtr (password), |
|
134 StringValuePtr (resource), |
|
135 auth_callback, |
|
136 (gpointer) func, NULL, |
|
137 NULL)); |
|
138 } |
|
139 |
|
140 VALUE |
|
141 conn_set_keep_alive_rate (VALUE self, VALUE rate) |
|
142 { |
|
143 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
144 |
|
145 lm_connection_set_keep_alive_rate (conn, NUM2UINT (rate)); |
|
146 |
|
147 return Qnil; |
|
148 } |
|
149 |
|
150 /* |
|
151 * VALUE |
|
152 conn_get_keep_alive_rate (VALUE self) |
|
153 { |
|
154 LmConnection *connection; |
|
155 } */ |
|
156 |
|
157 VALUE |
|
158 conn_is_open (VALUE self) |
|
159 { |
|
160 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
161 |
|
162 return GBOOL2RVAL (lm_connection_is_open (conn)); |
|
163 } |
|
164 |
|
165 VALUE |
|
166 conn_is_authenticated (VALUE self) |
|
167 { |
|
168 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
169 |
|
170 return GBOOL2RVAL (lm_connection_is_authenticated (conn)); |
|
171 } |
|
172 |
|
173 VALUE |
|
174 conn_get_server (VALUE self) |
|
175 { |
|
176 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
177 |
|
178 return rb_str_new2 (lm_connection_get_server (conn)); |
|
179 } |
|
180 |
|
181 VALUE |
|
182 conn_set_server (VALUE self, VALUE server) |
|
183 { |
|
184 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
185 |
|
186 if (!rb_respond_to (server, rb_intern ("to_s"))) { |
|
187 rb_raise (rb_eArgError, "server should respond to to_s"); |
|
188 } else { |
|
189 VALUE str_val = rb_funcall (server, rb_intern ("to_s"), 0); |
|
190 lm_connection_set_server (conn, StringValuePtr (str_val)); |
|
191 } |
|
192 |
|
193 return Qnil; |
|
194 } |
|
195 |
|
196 VALUE |
|
197 conn_get_jid (VALUE self) |
|
198 { |
|
199 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
200 |
|
201 return rb_str_new2 (lm_connection_get_jid (conn)); |
|
202 } |
|
203 |
|
204 VALUE |
|
205 conn_set_jid (VALUE self, VALUE jid) |
|
206 { |
|
207 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
208 |
|
209 if (!rb_respond_to (jid, rb_intern ("to_s"))) { |
|
210 rb_raise (rb_eArgError, "jid should respond to to_s"); |
|
211 } else { |
|
212 VALUE str_val = rb_funcall (jid, rb_intern ("to_s"), 0); |
|
213 lm_connection_set_jid (conn, StringValuePtr (str_val)); |
|
214 } |
|
215 |
|
216 return Qnil; |
|
217 } |
|
218 |
|
219 VALUE |
|
220 conn_get_port (VALUE self) |
|
221 { |
|
222 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
223 |
|
224 return UINT2NUM (lm_connection_get_port (conn)); |
|
225 } |
|
226 |
|
227 VALUE |
|
228 conn_set_port (VALUE self, VALUE port) |
|
229 { |
|
230 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
231 |
|
232 lm_connection_set_port (conn, NUM2UINT (port)); |
|
233 |
|
234 return Qnil; |
|
235 } |
|
236 |
|
237 VALUE |
|
238 conn_get_ssl (VALUE self) |
|
239 { |
|
240 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
241 |
|
242 return LMSSL2RVAL (lm_connection_get_ssl (conn)); |
|
243 } |
|
244 |
|
245 VALUE |
|
246 conn_set_ssl (VALUE self, VALUE ssl_rval) |
|
247 { |
|
248 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
249 LmSSL *ssl = rb_lm_ssl_from_ruby_object (ssl_rval); |
|
250 |
|
251 lm_connection_set_ssl (conn, ssl); |
|
252 |
|
253 return Qnil; |
|
254 } |
|
255 |
|
256 VALUE |
|
257 conn_get_proxy (VALUE self) |
|
258 { |
|
259 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
260 |
|
261 return LMPROXY2RVAL (lm_connection_get_proxy (conn)); |
|
262 } |
|
263 |
|
264 VALUE |
|
265 conn_set_proxy (VALUE self, VALUE proxy_rval) |
|
266 { |
|
267 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
268 LmProxy *proxy = rb_lm_proxy_from_ruby_object (proxy_rval); |
|
269 |
|
270 lm_connection_set_proxy (conn, proxy); |
|
271 |
|
272 return Qnil; |
|
273 } |
|
274 |
|
275 VALUE |
|
276 conn_send (VALUE self, VALUE msg) |
|
277 { |
|
278 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
279 LmMessage *m = rb_lm_message_from_ruby_object (msg); |
|
280 |
|
281 return GBOOL2RVAL (lm_connection_send (conn, m, NULL)); |
|
282 } |
|
283 |
|
284 VALUE |
|
285 conn_get_state (VALUE self) |
|
286 { |
|
287 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
288 |
|
289 return INT2FIX (lm_connection_get_state (conn)); |
|
290 } |
|
291 |
|
292 static LmHandlerResult |
|
293 msg_handler_cb (LmMessageHandler *handler, |
|
294 LmConnection *connection, |
|
295 LmMessage *message, |
|
296 gpointer user_data) |
|
297 { |
|
298 rb_funcall ((VALUE)user_data, rb_intern ("call"), 1, |
|
299 LMMESSAGE2RVAL (message)); |
|
300 |
|
301 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
302 } |
|
303 |
|
304 VALUE |
|
305 conn_add_msg_handler (int argc, VALUE *argv, VALUE self) |
|
306 { |
|
307 LmConnection *conn = rb_lm_connection_from_ruby_object (self); |
|
308 VALUE type, func; |
|
309 LmMessageHandler *handler; |
|
310 |
|
311 rb_scan_args (argc, argv, "1&", &type, &func); |
|
312 if (NIL_P (func)) { |
|
313 func = rb_block_proc (); |
|
314 } |
|
315 |
|
316 handler = lm_message_handler_new (msg_handler_cb, (gpointer) func, NULL); |
|
317 |
|
318 lm_connection_register_message_handler (conn, handler, |
|
319 rb_lm_message_type_from_ruby_object (type), |
|
320 LM_HANDLER_PRIORITY_NORMAL); |
|
321 lm_message_handler_unref (handler); |
|
322 |
|
323 return Qnil; |
|
324 } |
|
325 |
|
326 void |
|
327 Init_lm_connection (VALUE lm_mLM) |
|
328 { |
|
329 lm_cConnection = rb_define_class_under (lm_mLM, "Connection", |
|
330 rb_cObject); |
|
331 |
|
332 rb_define_alloc_func (lm_cConnection, conn_allocate); |
|
333 |
|
334 rb_define_method (lm_cConnection, "initialize", conn_initialize, -1); |
|
335 rb_define_method (lm_cConnection, "open", conn_open, -1); |
|
336 rb_define_method (lm_cConnection, "close", conn_close, 0); |
|
337 rb_define_method (lm_cConnection, "authenticate", conn_auth, -1); |
|
338 rb_define_method (lm_cConnection, "keep_alive_rate=", conn_set_keep_alive_rate, 1); |
|
339 /* rb_define_method (lm_cConnection, "keep_alive_rate", conn_get_keep_alive_rate, 0); */ |
|
340 rb_define_method (lm_cConnection, "open?", conn_is_open, 0); |
|
341 rb_define_method (lm_cConnection, "authenticated?", conn_is_authenticated, 0); |
|
342 rb_define_method (lm_cConnection, "server", conn_get_server, 0); |
|
343 rb_define_method (lm_cConnection, "server=", conn_set_server, 1); |
|
344 rb_define_method (lm_cConnection, "jid", conn_get_jid, 0); |
|
345 rb_define_method (lm_cConnection, "jid=", conn_set_jid, 1); |
|
346 rb_define_method (lm_cConnection, "port", conn_get_port, 0); |
|
347 rb_define_method (lm_cConnection, "port=", conn_set_port, 1); |
|
348 |
|
349 rb_define_method (lm_cConnection, "ssl", conn_get_ssl, 0); |
|
350 rb_define_method (lm_cConnection, "ssl=", conn_set_ssl, 1); |
|
351 rb_define_method (lm_cConnection, "proxy", conn_get_proxy, 0); |
|
352 rb_define_method (lm_cConnection, "proxy=", conn_set_proxy, 1); |
|
353 |
|
354 /* Use one send message and check if there is a block passed? */ |
|
355 |
|
356 rb_define_method (lm_cConnection, "send", conn_send, 1); |
|
357 /* |
|
358 rb_define_method (lm_cConnection, "send_with_reply", conn_send_with_reply, -1); |
|
359 rb_define_method (lm_cConnection, "send_raw", conn_send_raw, 1); |
|
360 */ |
|
361 |
|
362 rb_define_method (lm_cConnection, "state", conn_get_state, 0); |
|
363 rb_define_method (lm_cConnection, "add_message_handler", conn_add_msg_handler, -1); |
|
364 } |
|