summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-11-19 12:09:13 +0000
committerFreeArtMan <dos21h@gmail.com>2017-11-19 12:09:13 +0000
commit4b86ab3af6e64c0c4bb533766c284507f0d3ee62 (patch)
tree88d02d1122a77f90878b0633c68a2002245cc052 /cmd
parentd23ebff6250c7816bd3b15df06b35d4b2bb97874 (diff)
downloadagni-4b86ab3af6e64c0c4bb533766c284507f0d3ee62.tar.gz
agni-4b86ab3af6e64c0c4bb533766c284507f0d3ee62.zip
Now lua commands supported
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cmd_lua.c263
-rw-r--r--cmd/cmd_lua.h6
2 files changed, 242 insertions, 27 deletions
diff --git a/cmd/cmd_lua.c b/cmd/cmd_lua.c
index a4d7573..105cb47 100644
--- a/cmd/cmd_lua.c
+++ b/cmd/cmd_lua.c
@@ -1,46 +1,255 @@
#include "cmd_lua.h"
-#define BILLION 1000000000L
+void fatal_error(lua_State *L, char *msg)
+{
+ fprintf(stderr, "\n FATAL ERROR:\n %s %s\n\n", msg, lua_tostring(L,-1));
+}
+
+int check_table_key_string(lua_State *L, const char *key, const char **value, size_t *size)
+{
+ const char *result;
+ size_t result_sz;
+ int t;
+
+ lua_pushstring(L, key);
+ lua_gettable(L,1);
+ t = lua_type(L,-1);
+ if (t == LUA_TSTRING)
+ {
+ result = luaL_checklstring(L,-1,&result_sz);
+ *value = result;
+ *size = result_sz;
+ return 0;
+ } else
+ {
+ *value = NULL;
+ *size = 0;
+ return -1;
+ }
+}
+
+int check_table_key_integer(lua_State *L, const char *key, int *value)
+{
+ int result;
+ int t;
+
+ lua_pushstring(L, key);
+ lua_gettable(L,1);
+ if ((t = lua_type(L, -1)) == LUA_TNUMBER)
+ {
+ result = luaL_checkinteger(L,-1);
+ *value = result;
+ return 0;
+ }
+
+ return -1;
+}
+
+int verify_name(const char *lua_name)
+{
+ int i;
+
+ //check if all symbols are alphanumeric
+ for (i=0;i<strlen(lua_name);i++)
+ {
+ if (!isdigit(lua_name[i]) && !isalpha(lua_name[i]))
+ {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int lua_excute(const char *fname, rpc_call_request *req, rpc_call_response **resp)
+{
+ lua_State *L;
+ sds lua_full_path = sdsempty();
+
+ L = luaL_newstate();
+ luaL_openlibs(L);
+
+ if (verify_name(fname) != 0)
+ {
+ return -1;
+ }
+
+ lua_full_path = sdscat(lua_full_path, "lua/");
+ lua_full_path = sdscat(lua_full_path, fname);
+ lua_full_path = sdscat(lua_full_path, ".lua");
+
+ if (luaL_loadfile(L,lua_full_path))
+ {
+ fatal_error(L, "load file failed");
+ sdsfree(lua_full_path);
+ return -1;
+ }
+ sdsfree(lua_full_path);
+
+ if (lua_pcall(L,0,0,0))
+ {
+ fatal_error(L, "lua_pcall failed");
+ return -1;
+ }
+
+ lua_getglobal(L, "callback");
+ lua_newtable(L);
+
+ lua_pushstring(L,"method");
+ lua_pushstring(L, req->method);
+ lua_settable(L,-3);
+
+ lua_pushstring(L,"params");
+ lua_pushstring(L, req->params);
+ lua_settable(L,-3);
+
+ lua_pushstring(L,"id");
+ lua_pushinteger(L, req->id);
+ lua_settable(L,-3);
+
+ lua_pushstring(L,"user");
+ lua_pushstring(L, req->user);
+ lua_settable(L,-3);
+
+ lua_pushstring(L,"mask");
+ lua_pushstring(L, req->mask);
+ lua_settable(L,-3);
+
+ lua_pushstring(L,"server");
+ lua_pushstring(L, req->server);
+ lua_settable(L,-3);
+
+ if (lua_pcall(L,1,1,0))
+ {
+ fatal_error(L, "cant call callback function from file");
+ return -1;
+ }
+
+ {
+ int t;
+ int top = lua_gettop(L);
+
+ if (top == 1)
+ {
+ t = lua_type(L,1);
+ if (t == LUA_TTABLE)
+ {
+ int j;
+ int len = lua_rawlen(L,1);
+ const char *r_result;
+ const char *r_error;
+ int r_id;
+ size_t sz;
+
+ check_table_key_string(L, "result", &r_result, &sz);
+ //resp->result = alloc_new_str(result);
+ printf("RESULT:%s\n", r_result);
+ check_table_key_integer(L, "id", &r_id);
+ //resp->id = result_i;
+ printf("ID:%d\n", r_id);
+ check_table_key_string(L, "error", &r_error, &sz);
+ //resp->error = alloc_new_str(result);
+ printf("ERROR:%s\n", r_error);
+
+ *resp = rpc_call_resp_new(alloc_new_str(r_result), alloc_new_str(r_error), 1);
+ (*resp)->user = alloc_new_str(" ");
+ (*resp)->server = alloc_new_str(" ");
+ (*resp)->mask = alloc_new_str(" ");
+
+ }
+ } else
+ {
+ fatal_error(L,"Some unexpected argumetns");
+ return -1;
+ }
+ }
+
+ lua_close(L);
+ return 0;
+}
void *cmd_lua(void *data)
{
char *ret = NULL;
- int fret=-1;
+ char *req_data = NULL;
+ int fret;
+ int rc;
+ int i;
- const int buf_size = 128;
- char buf[buf_size+1];
+ int count;
+ sds params = sdsempty();
+ sds out_result = sdsempty(), out_lua = sdsempty();
+ sds *tokens;
- struct timespec start;
- struct stat file_stat;
- uint64_t proc_sec;
+ rpc_call_request *req = NULL;
+ rpc_call_response *resp = NULL;
+ netbyte_store *nb_req = NULL, *nb_resp=NULL;
+ char *nb_buf = NULL;
- printf("BOTU\n");
+ PRINT("LUA\n");
- stat("/proc/self",&file_stat);
+ req_data = (char *)data;
+ if (!req_data)
+ {
+ PERM();
+ return NULL;
+ }
- /*
- CHECK PREDIFINED MACROSES IF SUCH FUNCTIONALITY EXCISTS OR NOT
- */
- #if _POSIX_C_SOURCE < 199309L
- ERROR("Dont have functionality\n");
- #endif
+ //----------------------------------------------------------------------------
+ //prepare request
+ nb_req = malloc(sizeof(netbyte_store));
+ nb_init(nb_req);
+ nb_load(nb_req, req_data);
- fret = clock_gettime(CLOCK_REALTIME, &start);
- if (fret<0)
+ if (nb_req == NULL)
{
- perror("clock gettime");
- ret = alloc_new_str("Can get clock thread uptime\n");
- return ret;
+ ERROR("implement response\n");
+ return NULL;
}
- proc_sec = start.tv_sec - file_stat.st_ctim.tv_sec;
+ fret = rpc_call_req_unmarsh(nb_req, &req);
+ if (fret != 0)
+ {
+ ERROR("Invalid request format\n");
+ return NULL;
+ }
+
+ //----------------------------------------------------------------------------
+ //main code
+
+
+ params = sdsnew(req->params);
+ tokens = sdssplitargs(params, &count);
+
+
+
+ if (-1 == lua_excute(tokens[1], req, &resp))
+ {
+ resp = rpc_call_resp_new(out_result,"None",1);
+ resp->user = alloc_new_str(" ");
+ resp->server = alloc_new_str(" ");
+ resp->mask = alloc_new_str(" ");
+ }
+
+ sdsfree(params);
+ sdsfreesplitres(tokens, count);
+
+ //----------------------------------------------------------------------------
+ //prepare response
+ nb_resp = malloc(sizeof(netbyte_store));
+ nb_init(nb_resp);
+
+ PNL();
+ rpc_call_resp_marsh(resp, &nb_resp);
+
+ PNL();
+ nb_buf = (char *)nb_create(nb_resp);
+ if (nb_buf)
+ {
+ ret = nb_buf;
+ }
- snprintf(buf, buf_size, "%lud %luh %lum %lus",
- (proc_sec/(3600*24)),
- (proc_sec/(3600))%24,
- (proc_sec/60)%60,
- proc_sec%60);
- ret = alloc_new_str(buf);
+ nb_free(nb_resp);
+ rpc_call_resp_free(resp);
return ret;
}
diff --git a/cmd/cmd_lua.h b/cmd/cmd_lua.h
index e2a0c4d..7050946 100644
--- a/cmd/cmd_lua.h
+++ b/cmd/cmd_lua.h
@@ -9,8 +9,14 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
#include "util.h"
#include "debug.h"
+#include "nbrpc_call.h"
+#include "sds.h"
void *cmd_lua(void *data);