author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 23 Feb 2009 15:42:07 +0200 | |
changeset 9 | 50f55d494efb |
parent 6 | 90073cbb535d |
child 11 | a8c6460d612b |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <lua.h> |
|
3 |
#include <lauxlib.h> |
|
4 |
#include <glib.h> |
|
5 |
#include <loudmouth/loudmouth.h> |
|
6 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
7 |
#include "config.h" |
0 | 8 |
#include "lm_types.h" |
9 |
#include "util.h" |
|
10 |
||
11 |
/// lm.message_handler |
|
12 |
/// Module, representing message handling functions. |
|
13 |
||
14 |
/// message handler callback function |
|
15 |
/// Callback function, used on incoming messages. |
|
16 |
/// Should return true if message has been fully handled and no more |
|
17 |
/// handlers should be called. |
|
18 |
/// A: lm connection object, lm message object |
|
19 |
/// R: boolean |
|
20 |
LmHandlerResult llm_message_handler_callback (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, llm_callback_t *cb) |
|
21 |
{ |
|
22 |
int ret; |
|
23 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
24 |
llm_connection_bless (cb->L, connection); |
|
25 |
// XXX lm_connection_unref (connection); |
|
26 |
llm_message_bless (cb->L, message); |
|
27 |
// XXX lm_message_unref (message); |
|
28 |
if (lua_pcall (cb->L, 2, 1, 0)) { |
|
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
29 |
W ("Message handler callback error: %s", lua_tostring (cb->L, -1)); |
0 | 30 |
lua_pop (cb->L, 1); |
31 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
32 |
} |
|
33 |
ret = lua_toboolean (cb->L, -1); |
|
34 |
lua_pop (cb->L, 1); |
|
35 |
if (ret) |
|
36 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
37 |
else |
|
38 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
39 |
} |
|
40 |
||
41 |
/// lm.message_handler.new |
|
42 |
/// Creates new message handler object. |
|
43 |
/// A: message handler callback function |
|
44 |
/// R: lm message handler object |
|
45 |
static int llm_message_handler_new (lua_State *L) |
|
46 |
{ |
|
47 |
llm_callback_t *cb; |
|
48 |
LmMessageHandler *handler; |
|
49 |
luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected"); |
|
50 |
||
51 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
52 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
53 |
cb->L = L; |
|
54 |
||
55 |
handler = lm_message_handler_new ((LmHandleMessageFunction)llm_message_handler_callback, |
|
56 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
57 |
llm_message_handler_bless (L, handler); |
|
58 |
lm_message_handler_unref (handler); // XXX |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
59 |
D ("Message handler %X created", (int) handler); |
0 | 60 |
return 1; |
61 |
} |
|
62 |
||
63 |
/// lm.message_handler.bless |
|
64 |
/// Blesses given pointer to lm message handler object. |
|
65 |
/// A: lightuserdata (C lm message handler object) |
|
66 |
/// R: lm message handler object |
|
67 |
static int llm_message_handler_bless_lua (lua_State *L) |
|
68 |
{ |
|
69 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth message handler pointer expected"); |
|
70 |
llm_message_handler_bless (L, lua_touserdata (L, 1)); |
|
71 |
return 1; |
|
72 |
} |
|
73 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
74 |
// XXX: merge with next? |
0 | 75 |
/// message_handler:invalidate |
76 |
/// Invalidates handler. |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
77 |
/// R: lm message handler object |
0 | 78 |
static int llm_message_handler_invalidate (lua_State *L) |
79 |
{ |
|
80 |
llm_message_handler_t *object = luaL_checklm_message_handler (L, 1); |
|
81 |
lm_message_handler_invalidate (object->message_handler); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
82 |
return 1; |
0 | 83 |
} |
84 |
||
85 |
/// message_handler:valid |
|
86 |
/// Returns true if message handler is still valid. |
|
87 |
/// R: boolean |
|
88 |
static int llm_message_handler_valid (lua_State *L) |
|
89 |
{ |
|
90 |
llm_message_handler_t *object = luaL_checklm_message_handler (L, 1); |
|
91 |
lua_pushboolean (L, lm_message_handler_is_valid (object->message_handler)); |
|
92 |
return 1; |
|
93 |
} |
|
94 |
||
95 |
/// message_handler:pointer |
|
96 |
/// Returns pointer to underlying C loudmouth structure. |
|
97 |
/// R: lightuserdata |
|
98 |
static int llm_message_handler_pointer (lua_State *L) |
|
99 |
{ |
|
100 |
llm_message_handler_t *object = luaL_checklm_message_handler (L, 1); |
|
101 |
lua_pushlightuserdata (L, object->message_handler); |
|
102 |
return 1; |
|
103 |
} |
|
104 |
||
105 |
static int llm_message_handler_gc (lua_State *L) |
|
106 |
{ |
|
107 |
llm_message_handler_t *object = luaL_checklm_message_handler (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
108 |
D ("Message handler %X gc called", (int) object); |
0 | 109 |
lm_message_handler_unref (object->message_handler); |
110 |
return 0; |
|
111 |
} |
|
112 |
||
113 |
static const luaL_Reg llm_message_handler_reg_f[] = { |
|
114 |
{ "new", llm_message_handler_new }, |
|
115 |
{ "bless", llm_message_handler_bless_lua }, |
|
116 |
{ NULL, NULL }, |
|
117 |
}; |
|
118 |
||
119 |
static const luaL_Reg llm_message_handler_reg_m[] = { |
|
120 |
{ "invalidate", llm_message_handler_invalidate }, |
|
121 |
{ "valid", llm_message_handler_valid }, |
|
122 |
{ "pointer", llm_message_handler_pointer }, |
|
123 |
{ "__gc", llm_message_handler_gc }, |
|
124 |
{ NULL, NULL }, |
|
125 |
}; |
|
126 |
||
127 |
int luaopen_lm_message_handler (lua_State *L) |
|
128 |
{ |
|
129 |
luaL_newmetatable (L, "loudmouth.message_handler"); |
|
130 |
lua_pushstring (L, "__index"); |
|
131 |
lua_pushvalue (L, -2); |
|
132 |
lua_settable (L, -3); |
|
133 |
luaL_register (L, NULL, llm_message_handler_reg_m); |
|
134 |
lua_pop (L, 1); |
|
135 |
luaL_register (L, "lm.message_handler", llm_message_handler_reg_f); |
|
136 |
return 1; |
|
137 |
} |
|
138 |