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 |
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. |