Merge send_raw and send methods
authorMyhailo Danylenko <isbear@ukrpost.net>
Mon, 23 Feb 2009 19:19:13 +0200
changeset 10 aed141accdd9
parent 9 50f55d494efb
child 11 a8c6460d612b
Merge send_raw and send methods
TODO
lm_connection.c
--- a/TODO	Mon Feb 23 15:42:07 2009 +0200
+++ b/TODO	Mon Feb 23 19:19:13 2009 +0200
@@ -1,5 +1,4 @@
 
 Verify refcounts of lm objects. Need a decent test script for that.
 Some additional lua functions?
-Merge sending functions, add enum to specify blocking and raw modes
 
--- a/lm_connection.c	Mon Feb 23 15:42:07 2009 +0200
+++ b/lm_connection.c	Mon Feb 23 19:19:13 2009 +0200
@@ -309,25 +309,36 @@
 }
 
 /// connection:send
-/// Sends message (object). If specified, handler function will be called upon
-/// receiving of response to that message.
-/// Handler function can be either message handler object or just a function.
-/// A: lm message object, message handler callback function or lm message handler object (optional)
+/// Sends message. If specified, handler function will be called upon receiving of response to this message.
+/// Handler function can be either a message handler object or just a message handler function.
+/// Message can be raw xml string. However, you cannot use it with handler function.
+/// In short:
+/// * connection, errmsg = connection:send ( "raw xml" )
+/// * connection, errmsg = connection:send ( message )
+/// * connection, errmsg = connection:send ( message, function ( connection, message ) end )
+/// * connection, errmsg = connection:send ( message, handler )
+/// If connection is nil, errmsg contains error message.
+/// A: lm message object/string, message handler callback function/lm message handler object (optional)
 /// R: lm connection object or nil, string (error message)
 static int llm_connection_send (lua_State *L)
 {
 	llm_connection_t *object = luaL_checklm_connection (L, 1);
-	llm_message_t *message = luaL_checklm_message (L, 2);
 	int status;
 	GError *err = NULL;
-	if (lua_gettop (L) < 3) // Send
-		status = lm_connection_send (object->connection, message->message, &err);
-	else if (lua_isfunction (L, 3)) { // Send w/reply, func
-		llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
+	if (lua_gettop (L) < 3) { // Send
+		if (lua_type (L, 2) == LUA_TSTRING)
+			status = lm_connection_send_raw (object->connection, lua_tostring (L, 2), &err);
+		else {
+			llm_message_t *message = luaL_checklm_message (L, 2);
+			status = lm_connection_send (object->connection, message->message, &err);
+		}
+	} else if (lua_isfunction (L, 3)) { // Send w/reply, func
+		llm_message_t    *message = luaL_checklm_message (L, 2);
+		llm_callback_t   *cb      = luaL_malloc (L, sizeof (llm_callback_t));
 		LmMessageHandler *handler;
 
 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
-		cb->L = L;
+		cb->L         = L;
 		handler = lm_message_handler_new (
 					(LmHandleMessageFunction)llm_message_handler_callback,
 					cb, (GDestroyNotify)llm_callback_destroy);
@@ -335,6 +346,7 @@
 							message->message, handler, &err);
 		lm_message_handler_unref (handler);
 	} else { // Send w/reply, handler
+		llm_message_t *message = luaL_checklm_message (L, 2);
 		llm_message_handler_t *handler = luaL_checklm_message_handler (L, 3);
 		status = lm_connection_send_with_reply (object->connection, message->message,
 							handler->message_handler, &err);
@@ -351,26 +363,6 @@
 	}
 }
 
-/// connection:send_raw
-/// Sends arbitrary string to opened connection.
-/// A: string
-/// R: lm connection object or nil, string (error message)
-static int llm_connection_send_raw (lua_State *L)
-{
-	llm_connection_t *object = luaL_checklm_connection (L, 1);
-	const char *string = luaL_checkstring (L, 2);
-	GError *err = NULL;
-	if (lm_connection_send_raw (object->connection, string, NULL)) {
-		lua_pop (L, 1);
-		return 1;
-	} else {
-		I ("Raw message sending failed: %s", err->message);
-		lua_pushnil (L);
-		lua_pushstring (L, err->message);
-		return 2;
-	}
-}
-
 /// connection:handler
 /// Registers or unregisters handler function for a given type of messages.
 /// To unregister handler, omit the priority argument.
@@ -550,7 +542,6 @@
 	{ "proxy",               llm_connection_proxy               },
 	{ "ssl",                 llm_connection_ssl                 },
 	{ "send",                llm_connection_send                },
-	{ "send_raw",            llm_connection_send_raw            },
 	{ "handler",             llm_connection_handler             },
 	{ "ondisconnect",        llm_connection_ondisconnect        },
 	{ "open_wait",           llm_connection_open_wait           },