lm_connection.c
changeset 10 aed141accdd9
parent 9 50f55d494efb
child 11 a8c6460d612b
equal deleted inserted replaced
9:50f55d494efb 10:aed141accdd9
   307 	luaL_pushenum (L, lm_connection_get_state (connection->connection), llm_connection_state);
   307 	luaL_pushenum (L, lm_connection_get_state (connection->connection), llm_connection_state);
   308 	return 1;
   308 	return 1;
   309 }
   309 }
   310 
   310 
   311 /// connection:send
   311 /// connection:send
   312 /// Sends message (object). If specified, handler function will be called upon
   312 /// Sends message. If specified, handler function will be called upon receiving of response to this message.
   313 /// receiving of response to that message.
   313 /// Handler function can be either a message handler object or just a message handler function.
   314 /// Handler function can be either message handler object or just a function.
   314 /// Message can be raw xml string. However, you cannot use it with handler function.
   315 /// A: lm message object, message handler callback function or lm message handler object (optional)
   315 /// In short:
       
   316 /// * connection, errmsg = connection:send ( "raw xml" )
       
   317 /// * connection, errmsg = connection:send ( message )
       
   318 /// * connection, errmsg = connection:send ( message, function ( connection, message ) end )
       
   319 /// * connection, errmsg = connection:send ( message, handler )
       
   320 /// If connection is nil, errmsg contains error message.
       
   321 /// A: lm message object/string, message handler callback function/lm message handler object (optional)
   316 /// R: lm connection object or nil, string (error message)
   322 /// R: lm connection object or nil, string (error message)
   317 static int llm_connection_send (lua_State *L)
   323 static int llm_connection_send (lua_State *L)
   318 {
   324 {
   319 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   325 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   320 	llm_message_t *message = luaL_checklm_message (L, 2);
       
   321 	int status;
   326 	int status;
   322 	GError *err = NULL;
   327 	GError *err = NULL;
   323 	if (lua_gettop (L) < 3) // Send
   328 	if (lua_gettop (L) < 3) { // Send
   324 		status = lm_connection_send (object->connection, message->message, &err);
   329 		if (lua_type (L, 2) == LUA_TSTRING)
   325 	else if (lua_isfunction (L, 3)) { // Send w/reply, func
   330 			status = lm_connection_send_raw (object->connection, lua_tostring (L, 2), &err);
   326 		llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
   331 		else {
       
   332 			llm_message_t *message = luaL_checklm_message (L, 2);
       
   333 			status = lm_connection_send (object->connection, message->message, &err);
       
   334 		}
       
   335 	} else if (lua_isfunction (L, 3)) { // Send w/reply, func
       
   336 		llm_message_t    *message = luaL_checklm_message (L, 2);
       
   337 		llm_callback_t   *cb      = luaL_malloc (L, sizeof (llm_callback_t));
   327 		LmMessageHandler *handler;
   338 		LmMessageHandler *handler;
   328 
   339 
   329 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   340 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   330 		cb->L = L;
   341 		cb->L         = L;
   331 		handler = lm_message_handler_new (
   342 		handler = lm_message_handler_new (
   332 					(LmHandleMessageFunction)llm_message_handler_callback,
   343 					(LmHandleMessageFunction)llm_message_handler_callback,
   333 					cb, (GDestroyNotify)llm_callback_destroy);
   344 					cb, (GDestroyNotify)llm_callback_destroy);
   334 		status = lm_connection_send_with_reply (object->connection,
   345 		status = lm_connection_send_with_reply (object->connection,
   335 							message->message, handler, &err);
   346 							message->message, handler, &err);
   336 		lm_message_handler_unref (handler);
   347 		lm_message_handler_unref (handler);
   337 	} else { // Send w/reply, handler
   348 	} else { // Send w/reply, handler
       
   349 		llm_message_t *message = luaL_checklm_message (L, 2);
   338 		llm_message_handler_t *handler = luaL_checklm_message_handler (L, 3);
   350 		llm_message_handler_t *handler = luaL_checklm_message_handler (L, 3);
   339 		status = lm_connection_send_with_reply (object->connection, message->message,
   351 		status = lm_connection_send_with_reply (object->connection, message->message,
   340 							handler->message_handler, &err);
   352 							handler->message_handler, &err);
   341 		lua_pop (L, 1);
   353 		lua_pop (L, 1);
   342 	};
   354 	};
   343 	lua_pop (L, 1);
   355 	lua_pop (L, 1);
   344 	if (status)
   356 	if (status)
   345 		return 1;
   357 		return 1;
   346 	else {
   358 	else {
   347 		I ("Message sending failed: %s", err->message);
   359 		I ("Message sending failed: %s", err->message);
   348 		lua_pushnil (L);
       
   349 		lua_pushstring (L, err->message);
       
   350 		return 2;
       
   351 	}
       
   352 }
       
   353 
       
   354 /// connection:send_raw
       
   355 /// Sends arbitrary string to opened connection.
       
   356 /// A: string
       
   357 /// R: lm connection object or nil, string (error message)
       
   358 static int llm_connection_send_raw (lua_State *L)
       
   359 {
       
   360 	llm_connection_t *object = luaL_checklm_connection (L, 1);
       
   361 	const char *string = luaL_checkstring (L, 2);
       
   362 	GError *err = NULL;
       
   363 	if (lm_connection_send_raw (object->connection, string, NULL)) {
       
   364 		lua_pop (L, 1);
       
   365 		return 1;
       
   366 	} else {
       
   367 		I ("Raw message sending failed: %s", err->message);
       
   368 		lua_pushnil (L);
   360 		lua_pushnil (L);
   369 		lua_pushstring (L, err->message);
   361 		lua_pushstring (L, err->message);
   370 		return 2;
   362 		return 2;
   371 	}
   363 	}
   372 }
   364 }
   548 	{ "keep_alive_rate",     llm_connection_keep_alive_rate     },
   540 	{ "keep_alive_rate",     llm_connection_keep_alive_rate     },
   549 	{ "state",               llm_connection_status              },
   541 	{ "state",               llm_connection_status              },
   550 	{ "proxy",               llm_connection_proxy               },
   542 	{ "proxy",               llm_connection_proxy               },
   551 	{ "ssl",                 llm_connection_ssl                 },
   543 	{ "ssl",                 llm_connection_ssl                 },
   552 	{ "send",                llm_connection_send                },
   544 	{ "send",                llm_connection_send                },
   553 	{ "send_raw",            llm_connection_send_raw            },
       
   554 	{ "handler",             llm_connection_handler             },
   545 	{ "handler",             llm_connection_handler             },
   555 	{ "ondisconnect",        llm_connection_ondisconnect        },
   546 	{ "ondisconnect",        llm_connection_ondisconnect        },
   556 	{ "open_wait",           llm_connection_open_wait           },
   547 	{ "open_wait",           llm_connection_open_wait           },
   557 	{ "authenticate_wait",   llm_connection_authenticate_wait   },
   548 	{ "authenticate_wait",   llm_connection_authenticate_wait   },
   558 	{ "send_wait",           llm_connection_send_wait           },
   549 	{ "send_wait",           llm_connection_send_wait           },