author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 28 Nov 2012 01:56:15 +0200 | |
changeset 54 | 6bef2082e5f9 |
parent 38 | 34a2b880615c |
permissions | -rw-r--r-- |
23 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This file is part of lua-lm. |
|
5 |
||
6 |
lua-lm is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 2 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
||
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 18 |
|
19 |
#include <lua.h> |
|
20 |
#include <lauxlib.h> |
|
21 |
#include <glib.h> |
|
22 |
#include <loudmouth/loudmouth.h> |
|
23 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
24 |
#include "config.h" |
0 | 25 |
#include "lm_types.h" |
26 |
#include "util.h" |
|
27 |
||
28 |
/// lm.message_handler |
|
29 |
/// Module, representing message handling functions. |
|
30 |
||
31 |
/// message handler callback function |
|
32 |
/// Callback function, used on incoming messages. |
|
33 |
/// Should return true if message has been fully handled and no more |
|
34 |
/// handlers should be called. |
|
35 |
/// A: lm connection object, lm message object |
|
36 |
/// R: boolean |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
37 |
LmHandlerResult callback_lm_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, llm_callback_t *cb) |
0 | 38 |
{ |
39 |
int ret; |
|
40 |
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
|
41 |
bless_lm_connection (cb->L, connection); |
0 | 42 |
// XXX lm_connection_unref (connection); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
43 |
bless_lm_message (cb->L, message); |
0 | 44 |
// XXX lm_message_unref (message); |
45 |
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
|
46 |
W ("Message handler callback error: %s", lua_tostring (cb->L, -1)); |
0 | 47 |
lua_pop (cb->L, 1); |
48 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
49 |
} |
|
50 |
ret = lua_toboolean (cb->L, -1); |
|
51 |
lua_pop (cb->L, 1); |
|
52 |
if (ret) |
|
53 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
54 |
else |
|
55 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
56 |
} |
|
57 |
||
58 |
/// lm.message_handler.new |
|
59 |
/// Creates new message handler object. |
|
60 |
/// A: message handler callback function |
|
61 |
/// R: lm message handler object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
62 |
static int new_lm_handler (lua_State *L) |
0 | 63 |
{ |
64 |
llm_callback_t *cb; |
|
65 |
LmMessageHandler *handler; |
|
66 |
luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected"); |
|
67 |
||
68 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
69 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
70 |
cb->L = L; |
|
71 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
72 |
handler = lm_message_handler_new ((LmHandleMessageFunction)callback_lm_handler, |
0 | 73 |
cb, (GDestroyNotify)llm_callback_destroy); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
74 |
bless_lm_handler (L, handler); |
0 | 75 |
lm_message_handler_unref (handler); // XXX |
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
76 |
D ("Message handler %p created", handler); |
0 | 77 |
return 1; |
78 |
} |
|
79 |
||
80 |
/// lm.message_handler.bless |
|
81 |
/// Blesses given pointer to lm message handler object. |
|
82 |
/// A: lightuserdata (C lm message handler object) |
|
83 |
/// R: lm message handler object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
84 |
static int bless_lua_lm_handler (lua_State *L) |
0 | 85 |
{ |
86 |
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
|
87 |
bless_lm_handler (L, lua_touserdata (L, 1)); |
0 | 88 |
return 1; |
89 |
} |
|
90 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
91 |
// XXX: merge with next? |
0 | 92 |
/// message_handler:invalidate |
93 |
/// Invalidates handler. |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
94 |
/// R: lm message handler object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
95 |
static int invalidate_lm_handler (lua_State *L) |
0 | 96 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
97 |
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
|
98 |
lm_message_handler_invalidate (object->handler); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
99 |
return 1; |
0 | 100 |
} |
101 |
||
102 |
/// message_handler:valid |
|
103 |
/// Returns true if message handler is still valid. |
|
104 |
/// R: boolean |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
105 |
static int valid_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); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
108 |
lua_pushboolean (L, lm_message_handler_is_valid (object->handler)); |
0 | 109 |
return 1; |
110 |
} |
|
111 |
||
112 |
/// message_handler:pointer |
|
113 |
/// Returns pointer to underlying C loudmouth structure. |
|
114 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
115 |
static int pointer_lm_handler (lua_State *L) |
0 | 116 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
117 |
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
|
118 |
lua_pushlightuserdata (L, object->handler); |
0 | 119 |
return 1; |
120 |
} |
|
121 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
122 |
static int gc_lm_handler (lua_State *L) |
0 | 123 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
124 |
llm_handler_t *object = luaL_checklm_handler (L, 1); |
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
125 |
D ("Message handler %p gc called", object); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
126 |
lm_message_handler_unref (object->handler); |
0 | 127 |
return 0; |
128 |
} |
|
129 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
130 |
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
|
131 |
{ "new", new_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
132 |
{ "bless", bless_lua_lm_handler }, |
12 | 133 |
{ NULL, NULL }, |
0 | 134 |
}; |
135 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
136 |
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
|
137 |
{ "invalidate", invalidate_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
138 |
{ "valid", valid_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
139 |
{ "pointer", pointer_lm_handler }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
140 |
{ "__gc", gc_lm_handler }, |
12 | 141 |
{ NULL, NULL }, |
0 | 142 |
}; |
143 |
||
144 |
int luaopen_lm_message_handler (lua_State *L) |
|
145 |
{ |
|
146 |
luaL_newmetatable (L, "loudmouth.message_handler"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
147 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
148 |
lua_setfield (L, -2, "__index"); |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
149 |
luaL_setfuncs (L, reg_m_lm_handler, 0); |
0 | 150 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
151 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
152 |
luaL_setfuncs (L, reg_f_lm_handler, 0); |
0 | 153 |
return 1; |
154 |
} |
|
155 |