Added ruby bindings code for LmMessage
authorMikael Hallendal <micke@imendio.com>
Wed, 09 Apr 2008 19:59:35 +0200
changeset 361 8a820e5155eb
parent 360 b9d0543a18bc
child 362 586b54d0f21e
Added ruby bindings code for LmMessage
bindings/ruby/rlm-connection.c
bindings/ruby/rlm-message.c
bindings/ruby/rloudmouth.c
--- a/bindings/ruby/rlm-connection.c	Tue Apr 08 22:21:20 2008 +0200
+++ b/bindings/ruby/rlm-connection.c	Wed Apr 09 19:59:35 2008 +0200
@@ -227,41 +227,41 @@
 void
 Init_lm_connection (VALUE lm_mLM)
 {
-	VALUE lm_mConnection;
+	VALUE lm_cConnection;
 	
-	lm_mConnection = rb_define_class_under (lm_mLM, "Connection", 
+	lm_cConnection = rb_define_class_under (lm_mLM, "Connection", 
 						rb_cObject);
 
-	rb_define_alloc_func (lm_mConnection, conn_allocate);
+	rb_define_alloc_func (lm_cConnection, conn_allocate);
 
-	rb_define_method (lm_mConnection, "initialize", conn_initialize, 1);
-	rb_define_method (lm_mConnection, "open", conn_open, -1);
-	rb_define_method (lm_mConnection, "close", conn_close, 0);
-	rb_define_method (lm_mConnection, "authenticate", conn_auth, -1);
-	rb_define_method (lm_mConnection, "keep_alive_rate=", conn_set_keep_alive_rate, 1);
-	/* rb_define_method (lm_mConnection, "keep_alive_rate", conn_get_keep_alive_rate, 0); */
-	rb_define_method (lm_mConnection, "open?", conn_is_open, 0);
-	rb_define_method (lm_mConnection, "authenticated?", conn_is_authenticated, 0);
-	rb_define_method (lm_mConnection, "server", conn_get_server, 0);
-	rb_define_method (lm_mConnection, "server=", conn_set_server, 1);
-	rb_define_method (lm_mConnection, "jid", conn_get_jid, 0);
-	rb_define_method (lm_mConnection, "jid=", conn_set_jid, 1);
-	rb_define_method (lm_mConnection, "port", conn_get_port, 0);
-	rb_define_method (lm_mConnection, "port=", conn_set_port, 1);
+	rb_define_method (lm_cConnection, "initialize", conn_initialize, 1);
+	rb_define_method (lm_cConnection, "open", conn_open, -1);
+	rb_define_method (lm_cConnection, "close", conn_close, 0);
+	rb_define_method (lm_cConnection, "authenticate", conn_auth, -1);
+	rb_define_method (lm_cConnection, "keep_alive_rate=", conn_set_keep_alive_rate, 1);
+	/* rb_define_method (lm_cConnection, "keep_alive_rate", conn_get_keep_alive_rate, 0); */
+	rb_define_method (lm_cConnection, "open?", conn_is_open, 0);
+	rb_define_method (lm_cConnection, "authenticated?", conn_is_authenticated, 0);
+	rb_define_method (lm_cConnection, "server", conn_get_server, 0);
+	rb_define_method (lm_cConnection, "server=", conn_set_server, 1);
+	rb_define_method (lm_cConnection, "jid", conn_get_jid, 0);
+	rb_define_method (lm_cConnection, "jid=", conn_set_jid, 1);
+	rb_define_method (lm_cConnection, "port", conn_get_port, 0);
+	rb_define_method (lm_cConnection, "port=", conn_set_port, 1);
 
 	/*
-	rb_define_method (lm_mConnection, "ssl", conn_get_ssl, 0);
-	rb_define_method (lm_mConnection, "ssl=", conn_set_ssl, 1);
-	rb_define_method (lm_mConnection, "proxy", conn_get_proxy, 0);
-	rb_define_method (lm_mConnection, "proxy=", conn_set_proxy, 1);
+	rb_define_method (lm_cConnection, "ssl", conn_get_ssl, 0);
+	rb_define_method (lm_cConnection, "ssl=", conn_set_ssl, 1);
+	rb_define_method (lm_cConnection, "proxy", conn_get_proxy, 0);
+	rb_define_method (lm_cConnection, "proxy=", conn_set_proxy, 1);
 	*/
 
 	/* Use one send message and check if there is a block passed? */
 	/*
-	rb_define_method (lm_mConnection, "send", conn_send, 1);
-	rb_define_method (lm_mConnection, "send_with_reply", conn_send_with_reply, -1);
-	rb_define_method (lm_mConnection, "send_raw", conn_send_raw, 1);
+	rb_define_method (lm_cConnection, "send", conn_send, 1);
+	rb_define_method (lm_cConnection, "send_with_reply", conn_send_with_reply, -1);
+	rb_define_method (lm_cConnection, "send_raw", conn_send_raw, 1);
 	*/
 
-	rb_define_method (lm_mConnection, "state", conn_get_state, 0);
+	rb_define_method (lm_cConnection, "state", conn_get_state, 0);
 }
--- a/bindings/ruby/rlm-message.c	Tue Apr 08 22:21:20 2008 +0200
+++ b/bindings/ruby/rlm-message.c	Wed Apr 09 19:59:35 2008 +0200
@@ -1,20 +1,118 @@
 #include "rloudmouth.h"
 
+typedef struct {
+	LmMessage *message;
+} MsgWrapper;
+
+void
+msg_wrapper_mark (MsgWrapper *wrapper)
+{
+	/* Do nothing here */
+}
+
+void
+msg_wrapper_free (MsgWrapper *wrapper)
+{
+	if (wrapper->message) {
+		lm_message_unref (wrapper->message);
+	}
+
+	g_slice_free (MsgWrapper, wrapper);
+}
+
 VALUE
 msg_allocate (VALUE klass)
 {
+	MsgWrapper *wrapper;
+
+	wrapper = g_slice_new0 (MsgWrapper);
+
+	Data_Wrap_Struct (klass, msg_wrapper_mark, msg_wrapper_free, wrapper);
+}
+
+VALUE
+msg_initialize (int argc, VALUE *argv, VALUE self)
+{
+	MsgWrapper *wrapper;
+	VALUE       to, type, sub_type;
+	char       *to_str = NULL;
+
+	Data_Get_Struct (self, MsgWrapper, wrapper);
+
+	rb_scan_args (argc, argv, "21", &to, &type, &sub_type);
+
+	/* To can be nil */
+	if (!NIL_P (to)) {
+		if (!rb_respond_to (to, rb_intern ("to_s"))) {
+			rb_raise (rb_eArgError, "to should respond to to_s");
+		} else {
+			VALUE str_val = rb_funcall (to, rb_intern ("to_s"), 0);
+			to_str = StringValuePtr (str_val);
+		}
+	} 
+
+	if (NIL_P (sub_type)) {
+		/* Without sub_type */
+		wrapper->message = lm_message_new (to_str, NUM2INT (type));
+	} else {
+		wrapper->message = 
+			lm_message_new_with_sub_type (to_str,
+						      NUM2INT (type),
+						      NUM2INT (sub_type));
+	}
+
+	return self;
+}
+
+VALUE
+msg_get_type (VALUE self)
+{
+	MsgWrapper *wrapper;
+
+	Data_Get_Struct (self, MsgWrapper, wrapper);
+
+	return INT2NUM (lm_message_get_type (wrapper->message));
+}
+
+VALUE
+msg_get_sub_type (VALUE self)
+{
+	MsgWrapper *wrapper;
+
+	Data_Get_Struct (self, MsgWrapper, wrapper);
+
+	return INT2NUM (lm_message_get_sub_type (wrapper->message));
+}
+
+VALUE
+msg_get_root_node (VALUE self)
+{
+	/* How to? */
+	return Qnil;
+}
+
+LmMessage *
+rlm_message_from_value (VALUE self)
+{
+	MsgWrapper *wrapper;
+
+	Data_Get_Struct (self, MsgWrapper, wrapper);
+
+	return wrapper->message;
 }
 
 extern void 
 Init_lm_message (VALUE lm_mLM)
 {
-	VALUE lm_mConnection;
+	VALUE lm_cMessage;
+
+	lm_cMessage = rb_define_class_under (lm_mLM, "Message", rb_cObject);
 
-	lm_mMessage = rb_define_class_under (lm_mLM, "Message", 
-					     rb_cObject);
-
-	rb_define_alloc_func (lm_mMessage, msg_allocate);
+	rb_define_alloc_func (lm_cMessage, msg_allocate);
 	
-	rb_define_method (lm_mMessage, "initialize", msg_initialize, 1);
+	rb_define_method (lm_cMessage, "initialize", msg_initialize, -1);
+	rb_define_method (lm_cMessage, "type", msg_get_type, 0);
+	rb_define_method (lm_cMessage, "sub_type", msg_get_sub_type, 0);
+	rb_define_method (lm_cMessage, "root_node", msg_get_root_node, 0);
 }
 
--- a/bindings/ruby/rloudmouth.c	Tue Apr 08 22:21:20 2008 +0200
+++ b/bindings/ruby/rloudmouth.c	Wed Apr 09 19:59:35 2008 +0200
@@ -8,5 +8,6 @@
 	lm_mLM = rb_define_module ("LM");
 
 	Init_lm_connection (lm_mLM);
+	Init_lm_message (lm_mLM);
 }