// Called from lua to stop listening to a particular event static int addon_output_event_listen_stop(lua_State *L) { // Args should be : // 1) self // 2) event name // Find the event const char *evt_name = luaL_checkstring(L, 2); struct event_reg *evt = event_find(evt_name); if (!evt) luaL_error(L, "Event %s does not exists", evt_name); // Get the output struct addon_instance_priv *p = addon_output_get_priv(L, 1); if (event_listener_unregister(evt, p) != POM_OK) luaL_error(L, "Error while unregistering event listener"); // Forget about listening to the event lua_pushlightuserdata(L, evt); lua_pushnil(L); lua_settable(L, 1); return 0; }
// Called from lua to start listening to files static int addon_output_pload_listen_start(lua_State *L) { // Args should be : // 1) self // 2) open function // 3) write function // 4) close function // Push nill if additional functions are missing while (lua_gettop(L) < 4) lua_pushnil(L); // Stack : instance, read_func, write_func, close_func // Get the output struct addon_instance_priv *p = addon_output_get_priv(L, 1); if (!lua_isfunction(L, 2) && !lua_isfunction(L, 3) && !lua_isfunction(L, 4)) luaL_error(L, "At least one function should be provided to pload_listen_start()"); // Check if we are already listening or not lua_getfield(L, 1, "__pload_listener"); if (!lua_isnil(L, -1)) luaL_error(L, "The output is already listening for payloads"); if (pload_listen_start(p, NULL, NULL, addon_output_pload_open, addon_output_pload_write, addon_output_pload_close) != POM_OK) luaL_error(L, "Error while registering the payload listener"); // Create table to track pload listener functions lua_pushliteral(L, "__pload_listener"); lua_newtable(L); if (!lua_isnil(L, 2)) { lua_pushliteral(L, "open"); lua_pushvalue(L, 2); lua_settable(L, -3); } if (!lua_isnil(L, 3)) { lua_pushliteral(L, "write"); lua_pushvalue(L, 3); lua_settable(L, -3); } if (!lua_isnil(L, 4)) { lua_pushliteral(L, "close"); lua_pushvalue(L, 4); lua_settable(L, -3); } lua_settable(L, 1); return 0; }
// Called from lua to listen to a new event from an instance static int addon_output_event_listen_start(lua_State *L) { // Args should be : // 1) self // 2) event name // 3) process_begin // 4) process_end // Find the event const char *evt_name = luaL_checkstring(L, 2); struct event_reg *evt = event_find(evt_name); if (!evt) luaL_error(L, "Event %s does not exists", evt_name); // Check which function we should register int (*process_begin) (struct event *evt, void *obj, struct proto_process_stack *stack, unsigned int stack_index) = NULL; int (*process_end) (struct event *evt, void *obj) = NULL; if (lua_isfunction(L, 3)) process_begin = addon_event_process_begin; if (lua_isfunction(L, 4)) process_end = addon_event_process_end; // Get the output struct addon_instance_priv *p = addon_output_get_priv(L, 1); if (event_listener_register(evt, p, process_begin, process_end) != POM_OK) luaL_error(L, "Error while listening to event %s", evt_name); // Add a table to self for the processing functions of this event lua_newtable(L); lua_pushlightuserdata(L, evt); lua_pushvalue(L, -2); lua_settable(L, 1); // Add the processing function if (process_begin) { lua_pushliteral(L, "begin"); lua_pushvalue(L, 3); lua_settable(L, -3); } if (process_end) { lua_pushliteral(L, "end"); lua_pushvalue(L, 4); lua_settable(L, -3); } pomlog(POMLOG_DEBUG "Output listening to event %s", evt_name); return 0; }
// Called from lua to get a parameter value static int addon_output_param_get(lua_State *L) { struct addon_instance_priv *p = addon_output_get_priv(L, 1); const char *name = luaL_checkstring(L, 2); struct addon_param *tmp; for (tmp = p->params; tmp && strcmp(tmp->name, name); tmp = tmp->next); if (!tmp) luaL_error(L, "No such parameter %s", name); addon_ptype_push(L, tmp->value); return 1; }
// Called from lua to stop listening to a pload static int addon_output_pload_listen_stop(lua_State *L) { // Args should be : // 1) self // Get the output struct addon_instance_priv *p = addon_output_get_priv(L, 1); // Stack : instance // Get the listening table lua_getfield(L, 1, "__pload_listener"); // Stack : instance, __pload_listener if (lua_isnil(L, 1)) luaL_error(L, "The output is not listening for payloads"); if (pload_listen_stop(p, NULL) != POM_OK) luaL_error(L, "Error while stopping payload listening"); lua_pushnil(L); // Stack : instance, nil lua_setfield(L, 1, "__pload_listener"); // Stack : instance lua_pop(L, 1); // Stack : empty return 0; }