author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 14 Mar 2009 02:41:26 +0200 | |
changeset 12 | 63f06a23c235 |
parent 11 | a8c6460d612b |
child 16 | 09b375e9ce32 |
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 |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
20 |
LmHandlerResult callback_lm_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, llm_callback_t *cb) |
0 | 21 |
{ |
22 |
int ret; |
|
23 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
24 |
bless_lm_connection (cb->L, connection); |
0 | 25 |
// XXX lm_connection_unref (connection); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
26 |
bless_lm_message (cb->L, message); |
0 | 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 |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
45 |
static int new_lm_handler (lua_State *L) |
0 | 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 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
55 |
handler = lm_message_handler_new ((LmHandleMessageFunction)callback_lm_handler, |
0 | 56 |
cb, (GDestroyNotify)llm_callback_destroy); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
57 |
bless_lm_handler (L, handler); |
0 | 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 |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
67 |
static int bless_lua_lm_handler (lua_State *L) |
0 | 68 |
{ |
69 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth message handler pointer expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
70 |
bless_lm_handler (L, lua_touserdata (L, 1)); |
0 | 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 |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
78 |
static int invalidate_lm_handler (lua_State *L) |
0 | 79 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
80 |
llm_handler_t *object = luaL_checklm_handler (L, 1); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
81 |
lm_message_handler_invalidate (object->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 |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
88 |
static int valid_lm_handler (lua_State *L) |
0 | 89 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
90 |
llm_handler_t *object = luaL_checklm_handler (L, 1); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
91 |
lua_pushboolean (L, lm_message_handler_is_valid (object->handler)); |
0 | 92 |
return 1; |
93 |
} |
|
94 |
||
95 |
/// message_handler:pointer |
|
96 |
/// Returns pointer to underlying C loudmouth structure. |
|
97 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
98 |
static int pointer_lm_handler (lua_State *L) |
0 | 99 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
100 |
llm_handler_t *object = luaL_checklm_handler (L, 1); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
101 |
lua_pushlightuserdata (L, object->handler); |
0 | 102 |
return 1; |
103 |
} |
|
104 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
105 |
static int gc_lm_handler (lua_State *L) |
0 | 106 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
107 |
llm_handler_t *object = luaL_checklm_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); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
109 |
lm_message_handler_unref (object->handler); |
0 | 110 |
return 0; |
111 |
} |
|
112 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
113 |
static const luaL_Reg reg_f_lm_handler[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
114 |
{ "new", new_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
115 |
{ "bless", bless_lua_lm_handler }, |
12 | 116 |
{ NULL, NULL }, |
0 | 117 |
}; |
118 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
119 |
static const luaL_Reg reg_m_lm_handler[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
120 |
{ "invalidate", invalidate_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
121 |
{ "valid", valid_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
122 |
{ "pointer", pointer_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
123 |
{ "__gc", gc_lm_handler }, |
12 | 124 |
{ NULL, NULL }, |
0 | 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); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
133 |
luaL_register (L, NULL, reg_m_lm_handler); |
0 | 134 |
lua_pop (L, 1); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
135 |
luaL_register (L, "lm.message_handler", reg_f_lm_handler); |
0 | 136 |
return 1; |
137 |
} |
|
138 |