summaryrefslogtreecommitdiffhomepage
path: root/plop.c
diff options
context:
space:
mode:
Diffstat (limited to 'plop.c')
-rw-r--r--plop.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/plop.c b/plop.c
index fb62b01..d676c4f 100644
--- a/plop.c
+++ b/plop.c
@@ -73,12 +73,13 @@ int make_server(const char * node, const char * service)
/// Progresses the connection for a client and shifts it back in the array.
/// `pfd` must be at least `shift_by + 1`-th element of the array.
+/// \param L Server's Lua state
/// \param pfd Element in array processed by poll
/// \param data Pointer to data associated with the client
/// \param shift_by Element will be moved by in array this many indices
/// \return Shift value for next handler
/// \see poll(2)
-int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
+int handle_client(lua_State * L, struct pollfd * pfd, char ** data, const int shift_by)
{
if (0 == pfd->revents)
{
@@ -95,7 +96,17 @@ int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
return -1; // TODO: Handle errors properly
}
- respond_with_body(pfd->fd, STATUS_OK, *data);
+ lua_getglobal(L, "Handler");
+ lua_pushstring(L, *data);
+ lua_call(L, 1, 1);
+
+ size_t length;
+ const char * body = lua_tolstring(L, -1, &length);
+
+ if (NULL != body)
+ {
+ respond_with_body(pfd->fd, STATUS_OK, body, (int) length);
+ }
close(pfd->fd);
(pfd - shift_by)->fd = -1;
@@ -111,7 +122,7 @@ int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
/// \param fdv Array of descriptors for poll
/// \param fdc Number of valid descriptors in the array including the server
/// \param size Size limit for the array
-/// \param Number of valid descriptors in the array or -1 if an error occurred.
+/// \return Number of valid descriptors in the array or -1 if an error occurred.
/// \see poll(2)
int handle_server(struct pollfd * fdv, int fdc, const int size)
{
@@ -143,6 +154,11 @@ int main(int argc, char ** argv)
lua_State * L = luaL_newstate();
luaL_openlibs(L);
+ if (LUA_OK != luaL_dofile(L, "default.lua"))
+ {
+ return 2; // TODO: Document error codes/print usage information
+ }
+
if (2 != argc)
{
return 1; // TODO: Document error codes/print usage information
@@ -167,7 +183,7 @@ int main(int argc, char ** argv)
for (int i = 1; i < fdc; ++i)
{
- shift_by = handle_client(&fdv[i], &data[i], shift_by);
+ shift_by = handle_client(L, &fdv[i], &data[i], shift_by);
}
fdc = handle_server(fdv, fdc - shift_by, max_clients);