bindings/ruby/rblm-connection.c
changeset 364 71e6639a924d
child 365 64b1c1c32d56
equal deleted inserted replaced
363:6d53af6c3227 364:71e6639a924d
       
     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 static LmConnection *
       
     9 rb_lm_connection_from_ruby_object (VALUE obj)
       
    10 {
       
    11 	LmConnection *conn;
       
    12 
       
    13 	if (!rb_lm__is_kind_of (obj, lm_cConnection)) {
       
    14 		rb_raise (rb_eTypeError, "not a LmConnection");
       
    15 	}
       
    16 
       
    17 	Data_Get_Struct (obj, LmConnection, conn);
       
    18 
       
    19 	return conn;
       
    20 }
       
    21 
       
    22 void
       
    23 conn_mark (LmConnection *self)
       
    24 {
       
    25 }
       
    26 
       
    27 void
       
    28 conn_free (LmConnection *self)
       
    29 {
       
    30 	lm_connection_unref (self);
       
    31 }
       
    32 
       
    33 VALUE
       
    34 conn_allocate(VALUE klass)
       
    35 {
       
    36 	LmConnection *conn = lm_connection_new (NULL);
       
    37 
       
    38 	return Data_Wrap_Struct (klass, conn_mark, conn_free, conn);
       
    39 }
       
    40 
       
    41 VALUE
       
    42 conn_initialize (VALUE self, VALUE server, VALUE context)
       
    43 {
       
    44 	LmConnection *conn;
       
    45 
       
    46 	Data_Get_Struct (self, LmConnection, conn);
       
    47 
       
    48 	conn_set_server (self, server);
       
    49 
       
    50 	/* Set context */
       
    51 
       
    52 	return self;
       
    53 }
       
    54 
       
    55 static void
       
    56 open_callback (LmConnection *conn, gboolean success, gpointer user_data)
       
    57 {
       
    58 	rb_funcall((VALUE)user_data, rb_intern ("call"), 1,
       
    59 		   GBOOL2RVAL (success));
       
    60 }
       
    61 
       
    62 VALUE
       
    63 conn_open (int argc, VALUE *argv, VALUE self)
       
    64 {
       
    65 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
    66 	VALUE         func;
       
    67 
       
    68 	rb_scan_args (argc, argv, "0&", &func);
       
    69 	if (NIL_P (func)) {
       
    70 		func = rb_block_proc ();
       
    71 	}
       
    72 
       
    73 	return GBOOL2RVAL (lm_connection_open (conn, open_callback, 
       
    74 					       (gpointer) func, NULL, NULL));
       
    75 }
       
    76 
       
    77 VALUE
       
    78 conn_close (VALUE self)
       
    79 {
       
    80 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
    81 
       
    82 	return GBOOL2RVAL (lm_connection_close (conn, NULL));
       
    83 }
       
    84 
       
    85 static void
       
    86 auth_callback (LmConnection *conn, gboolean success, gpointer user_data)
       
    87 {
       
    88 	rb_funcall((VALUE)user_data, rb_intern ("call"), 1,
       
    89 		   GBOOL2RVAL (success));
       
    90 }
       
    91 
       
    92 VALUE
       
    93 conn_auth (int argc, VALUE *argv, VALUE self)
       
    94 {
       
    95 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
    96 	VALUE         name, password, resource, func; 
       
    97 
       
    98 	rb_scan_args (argc, argv, "21&", &name, &password, &resource, &func);
       
    99 	if (NIL_P (func)) {
       
   100 		func = rb_block_proc ();
       
   101 	}
       
   102 
       
   103 	return GBOOL2RVAL (lm_connection_authenticate (conn, 
       
   104 						       StringValuePtr (name),
       
   105 						       StringValuePtr (password), 
       
   106 						       StringValuePtr (resource),
       
   107 						       auth_callback,
       
   108 						       (gpointer) func, NULL,
       
   109 						       NULL));
       
   110 }
       
   111 
       
   112 VALUE
       
   113 conn_set_keep_alive_rate (VALUE self, VALUE rate)
       
   114 {
       
   115 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   116 
       
   117 	lm_connection_set_keep_alive_rate (conn, NUM2UINT (rate));
       
   118 
       
   119 	return Qnil;
       
   120 }
       
   121 
       
   122 /*
       
   123  * VALUE
       
   124 conn_get_keep_alive_rate (VALUE self)
       
   125 {
       
   126 	LmConnection *connection;
       
   127 } */
       
   128 
       
   129 VALUE
       
   130 conn_is_open (VALUE self)
       
   131 {
       
   132 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   133 
       
   134 	return GBOOL2RVAL (lm_connection_is_open (conn));
       
   135 }
       
   136 
       
   137 VALUE
       
   138 conn_is_authenticated (VALUE self)
       
   139 {
       
   140 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   141 
       
   142 	return GBOOL2RVAL (lm_connection_is_authenticated (conn));
       
   143 }
       
   144 
       
   145 VALUE
       
   146 conn_get_server (VALUE self)
       
   147 {
       
   148 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   149 
       
   150 	return rb_str_new2 (lm_connection_get_server (conn));
       
   151 }
       
   152 
       
   153 VALUE
       
   154 conn_set_server (VALUE self, VALUE server)
       
   155 {
       
   156 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   157 
       
   158 	if (!rb_respond_to (server, rb_intern ("to_s"))) {
       
   159 		rb_raise (rb_eArgError, "server should respond to to_s");
       
   160 	} else {
       
   161 		VALUE str_val = rb_funcall (server, rb_intern ("to_s"), 0);
       
   162 		lm_connection_set_server (conn, StringValuePtr (str_val));
       
   163 	}
       
   164 
       
   165 	return Qnil;
       
   166 }
       
   167 
       
   168 VALUE
       
   169 conn_get_jid (VALUE self)
       
   170 {
       
   171 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   172 
       
   173 	return rb_str_new2 (lm_connection_get_jid (conn));
       
   174 }
       
   175 
       
   176 VALUE
       
   177 conn_set_jid (VALUE self, VALUE jid)
       
   178 {
       
   179 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   180 
       
   181 	if (!rb_respond_to (jid, rb_intern ("to_s"))) {
       
   182 		rb_raise (rb_eArgError, "jid should respond to to_s");
       
   183 	} else {
       
   184 		VALUE str_val = rb_funcall (jid, rb_intern ("to_s"), 0);
       
   185 		lm_connection_set_jid (conn, StringValuePtr (str_val));
       
   186 	}
       
   187 
       
   188 	return Qnil;
       
   189 }
       
   190 
       
   191 VALUE
       
   192 conn_get_port (VALUE self)
       
   193 {
       
   194 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   195 
       
   196 	return UINT2NUM (lm_connection_get_port (conn));
       
   197 }
       
   198 
       
   199 VALUE
       
   200 conn_set_port (VALUE self, VALUE port)
       
   201 {
       
   202 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   203 
       
   204 	lm_connection_set_port (conn, NUM2UINT (port));
       
   205 
       
   206 	return Qnil;
       
   207 }
       
   208 
       
   209 VALUE
       
   210 conn_get_state (VALUE self)
       
   211 {
       
   212 	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
       
   213 
       
   214 	/* TODO: FIXME */
       
   215 	return Qnil;
       
   216 }
       
   217 
       
   218 void
       
   219 Init_lm_connection (VALUE lm_mLM)
       
   220 {
       
   221 	lm_cConnection = rb_define_class_under (lm_mLM, "Connection", 
       
   222 						rb_cObject);
       
   223 
       
   224 	rb_define_alloc_func (lm_cConnection, conn_allocate);
       
   225 
       
   226 	rb_define_method (lm_cConnection, "initialize", conn_initialize, 1);
       
   227 	rb_define_method (lm_cConnection, "open", conn_open, -1);
       
   228 	rb_define_method (lm_cConnection, "close", conn_close, 0);
       
   229 	rb_define_method (lm_cConnection, "authenticate", conn_auth, -1);
       
   230 	rb_define_method (lm_cConnection, "keep_alive_rate=", conn_set_keep_alive_rate, 1);
       
   231 	/* rb_define_method (lm_cConnection, "keep_alive_rate", conn_get_keep_alive_rate, 0); */
       
   232 	rb_define_method (lm_cConnection, "open?", conn_is_open, 0);
       
   233 	rb_define_method (lm_cConnection, "authenticated?", conn_is_authenticated, 0);
       
   234 	rb_define_method (lm_cConnection, "server", conn_get_server, 0);
       
   235 	rb_define_method (lm_cConnection, "server=", conn_set_server, 1);
       
   236 	rb_define_method (lm_cConnection, "jid", conn_get_jid, 0);
       
   237 	rb_define_method (lm_cConnection, "jid=", conn_set_jid, 1);
       
   238 	rb_define_method (lm_cConnection, "port", conn_get_port, 0);
       
   239 	rb_define_method (lm_cConnection, "port=", conn_set_port, 1);
       
   240 
       
   241 	/*
       
   242 	rb_define_method (lm_cConnection, "ssl", conn_get_ssl, 0);
       
   243 	rb_define_method (lm_cConnection, "ssl=", conn_set_ssl, 1);
       
   244 	rb_define_method (lm_cConnection, "proxy", conn_get_proxy, 0);
       
   245 	rb_define_method (lm_cConnection, "proxy=", conn_set_proxy, 1);
       
   246 	*/
       
   247 
       
   248 	/* Use one send message and check if there is a block passed? */
       
   249 	/*
       
   250 	rb_define_method (lm_cConnection, "send", conn_send, 1);
       
   251 	rb_define_method (lm_cConnection, "send_with_reply", conn_send_with_reply, -1);
       
   252 	rb_define_method (lm_cConnection, "send_raw", conn_send_raw, 1);
       
   253 	*/
       
   254 
       
   255 	rb_define_method (lm_cConnection, "state", conn_get_state, 0);
       
   256 }