lm_connection.c
changeset 30 21547232c875
parent 23 13f03e604c8a
child 31 afcdbbce5002
equal deleted inserted replaced
29:e86ee1f4227d 30:21547232c875
   126 /// R: lm connection object or nil, string (error message)
   126 /// R: lm connection object or nil, string (error message)
   127 static int open_lm_connection (lua_State *L)
   127 static int open_lm_connection (lua_State *L)
   128 {
   128 {
   129 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   129 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   130 	llm_callback_t *cb;
   130 	llm_callback_t *cb;
       
   131 	GError *err = NULL;
   131 	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
   132 	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
   132 	GError *err;
       
   133 	
   133 	
   134 	cb = luaL_malloc (L, sizeof (llm_callback_t));
   134 	cb = luaL_malloc (L, sizeof (llm_callback_t));
   135 	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   135 	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   136 	cb->L         = L;
   136 	cb->L         = L;
   137 
   137 
   139 				 cb, (GDestroyNotify) llm_callback_destroy, &err))
   139 				 cb, (GDestroyNotify) llm_callback_destroy, &err))
   140 		return 1;
   140 		return 1;
   141 	else {
   141 	else {
   142 		lua_pushnil (L);
   142 		lua_pushnil (L);
   143 		lua_pushstring (L, err->message);
   143 		lua_pushstring (L, err->message);
   144 		I ("Connection opening failed");
   144 		I ("Connection opening failed: %s", err -> message);
       
   145 		g_error_free (err);
   145 		return 2;
   146 		return 2;
   146 	}
   147 	}
   147 }
   148 }
   148 
   149 
   149 /// connection:authenticate
   150 /// connection:authenticate
   172 		return 1;
   173 		return 1;
   173 	} else {
   174 	} else {
   174 		lua_pushnil (L);
   175 		lua_pushnil (L);
   175 		lua_pushstring (L, err->message);
   176 		lua_pushstring (L, err->message);
   176 		I ("Connection authentication failed: %s", err->message);
   177 		I ("Connection authentication failed: %s", err->message);
       
   178 		g_error_free (err);
   177 		return 2;
   179 		return 2;
   178 	}
   180 	}
   179 }
   181 }
   180 
   182 
   181 /// connection:port
   183 /// connection:port
   309 		return 1;
   311 		return 1;
   310 	else {
   312 	else {
   311 		I ("Connection close failed: %s", err->message);
   313 		I ("Connection close failed: %s", err->message);
   312 		lua_pushnil (L);
   314 		lua_pushnil (L);
   313 		lua_pushstring (L, err->message);
   315 		lua_pushstring (L, err->message);
       
   316 		g_error_free (err);
   314 		return 2;
   317 		return 2;
   315 	}
   318 	}
   316 }
   319 }
   317 
   320 
   318 /// connection:status
   321 /// connection:status
   374 		return 1;
   377 		return 1;
   375 	else {
   378 	else {
   376 		I ("Message sending failed: %s", err->message);
   379 		I ("Message sending failed: %s", err->message);
   377 		lua_pushnil (L);
   380 		lua_pushnil (L);
   378 		lua_pushstring (L, err->message);
   381 		lua_pushstring (L, err->message);
       
   382 		g_error_free (err);
   379 		return 2;
   383 		return 2;
   380 	}
   384 	}
   381 }
   385 }
   382 
   386 
   383 /// connection:handler
   387 /// connection:handler
   384 /// Registers or unregisters handler function for a given type of messages.
   388 /// Registers or unregisters handler function for a given type of messages.
   385 /// To unregister handler, omit the priority argument.
   389 /// To unregister handler, omit the priority argument.
       
   390 /// To unregister reply handler, omit message type argument too.
   386 /// Handler function can be specified as plain function or message handler object.
   391 /// Handler function can be specified as plain function or message handler object.
   387 /// Though, you can unregister only a message handler object.
   392 /// A: message handler callback function or lm message handler object, message type (optional), handler priority (optional)
   388 /// A: message handler callback function or lm message handler object, message type, handler priority (optional)
       
   389 /// R: lm connection object
   393 /// R: lm connection object
   390 static int handler_lm_connection (lua_State *L)
   394 static int handler_lm_connection (lua_State *L)
   391 {
   395 {
   392 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   396 	llm_connection_t *object = luaL_checklm_connection (L, 1);
   393 	int type = luaL_checkenum (L, 3, type_lm_message);
   397 	int top  = lua_gettop (L);
   394 
   398 	if (top > 2) { // Have message type
   395 	if (lua_gettop (L) > 3) { // Register
   399 		int type = luaL_checkenum (L, 3, type_lm_message);
   396 		int priority = luaL_checkenum (L, 4, priority_lm_handler);
   400 
   397 
   401 		if (lua_gettop (L) > 3) { // Register
   398 		if (lua_isfunction (L, 2)) { // Function
   402 			int priority = luaL_checkenum (L, 4, priority_lm_handler);
   399 			LmMessageHandler *handler;
   403 
   400 			llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
   404 			if (lua_isfunction (L, 2)) { // Function
   401 			lua_pushvalue (L, 2);
   405 				LmMessageHandler *handler;
   402 			cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   406 				llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
   403 			cb->L         = L;
   407 				lua_pushvalue (L, 2);
   404 
   408 				cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   405 			handler = lm_message_handler_new (
   409 				cb->L         = L;
   406 					(LmHandleMessageFunction)callback_lm_handler,
   410 
   407 					cb, (GDestroyNotify)llm_callback_destroy);
   411 				handler = lm_message_handler_new (
   408 			lm_connection_register_message_handler (object->connection,
   412 						(LmHandleMessageFunction)callback_lm_handler,
   409 								handler, type, priority);
   413 						cb, (GDestroyNotify)llm_callback_destroy);
   410 			lm_message_handler_unref (handler);
   414 				lm_connection_register_message_handler (object->connection,
   411 		} else { // Object
   415 									handler, type, priority);
       
   416 				lm_message_handler_unref (handler);
       
   417 			} else { // Object
       
   418 				llm_handler_t *handler = luaL_checklm_handler (L, 2);
       
   419 				lm_connection_register_message_handler (object->connection,
       
   420 									handler->handler,
       
   421 									type, priority);
       
   422 			}
       
   423 			lua_pop (L, 1);
       
   424 		} else { // Unregister message handler
   412 			llm_handler_t *handler = luaL_checklm_handler (L, 2);
   425 			llm_handler_t *handler = luaL_checklm_handler (L, 2);
   413 			lm_connection_register_message_handler (object->connection,
   426 			lm_connection_unregister_message_handler (object->connection,
   414 								handler->handler,
   427 								  handler->handler,
   415 								type, priority);
   428 								  type);
   416 		}
   429 		}
   417 		lua_pop (L, 1);
   430 		lua_pop (L, 1);
   418 	} else { // Unregister
   431 	} else { // Unregister reply handler
       
   432 #if 0
   419 		llm_handler_t *handler = luaL_checklm_handler (L, 2);
   433 		llm_handler_t *handler = luaL_checklm_handler (L, 2);
   420 		lm_connection_unregister_message_handler (object->connection,
   434 		lm_connection_unregister_reply_handler (object -> connection,
   421 							  handler->handler,
   435 		                                        handler -> handler);
   422 							  type);
   436 #else
   423 	}
   437 		lua_pushnil (L);
   424 	lua_pop (L, 2);
   438 		lua_pushliteral (L, "Sorry, lm_connection_unregister_reply_handler is not yet supported");
       
   439 		return 2;
       
   440 #endif
       
   441 	}
       
   442 	lua_pop (L, 1);
   425 	return 1;
   443 	return 1;
   426 }
   444 }
   427 
   445 
   428 /// disconnect callback function
   446 /// disconnect callback function
   429 /// Function, that will be called when disconnection occurs.
   447 /// Function, that will be called when disconnection occurs.
   471 		return 1;
   489 		return 1;
   472 	else {
   490 	else {
   473 		I ("Synchronous connection opening failed: %s", err->message);
   491 		I ("Synchronous connection opening failed: %s", err->message);
   474 		lua_pushnil (L);
   492 		lua_pushnil (L);
   475 		lua_pushstring (L, err->message);
   493 		lua_pushstring (L, err->message);
       
   494 		g_error_free (err);
   476 		return 2;
   495 		return 2;
   477 	}
   496 	}
   478 }
   497 }
   479 
   498 
   480 /// connection:authenticate_wait
   499 /// connection:authenticate_wait
   493 		return 1;
   512 		return 1;
   494 	else {
   513 	else {
   495 		I ("Synchronous authentication failed: %s", err->message);
   514 		I ("Synchronous authentication failed: %s", err->message);
   496 		lua_pushnil (L);
   515 		lua_pushnil (L);
   497 		lua_pushstring (L, err->message);
   516 		lua_pushstring (L, err->message);
       
   517 		g_error_free (err);
   498 		return 2;
   518 		return 2;
   499 	}
   519 	}
   500 }
   520 }
   501 
   521 
   502 /// connection:send_wait
   522 /// connection:send_wait
   512 									message->message, &err);
   532 									message->message, &err);
   513 	if (!new) {
   533 	if (!new) {
   514 		I ("Synchronous message sending failed: %s", err->message);
   534 		I ("Synchronous message sending failed: %s", err->message);
   515 		lua_pushnil (L);
   535 		lua_pushnil (L);
   516 		lua_pushstring (L, err->message);
   536 		lua_pushstring (L, err->message);
       
   537 		g_error_free (err);
   517 		return 2;
   538 		return 2;
   518 	} else {
   539 	} else {
   519 		bless_lm_message (L, new);
   540 		bless_lm_message (L, new);
   520 		lm_message_unref (new); // XXX
   541 		lm_message_unref (new); // XXX
   521 		return 1;
   542 		return 1;