Commits

Daniel Poelzleithner committed 7b1065a

better setup of lua environment

Comments (0)

Files changed (7)

 
 config 'mapping' 'dns'
     option 'server' 'basic'
-	option 'basic' 'basic'
+    option 'basic' 'basic'
     list mappings '00:5'
     list mappings '01:2'
 
+
 config 'mapping' 'conf'
     list mappings '00:10'
     list mappings '01:4'

src/CMakeLists.txt

 IF (CMAKE_C_FLAGS_DEBUG)
     add_definitions(-DDEBUG)
 ENDIF (CMAKE_C_FLAGS_DEBUG)
-
+add_definitions(-DINSTALL_PREFIX=${CMAKE_HOME_DIRECTORY} -DSOURCE_PREFIX=${CMAKE_SOURCE_DIR})
 
 add_executable (dssd dssd.c hash.c file_backend.c common.c lua.c uci.c)
 

src/backends/static.lua

+print("runnig static")
+print(uci)
+print(dssd_config)
 
 int main(int argc, char *argv[])
 {
-	struct uci_context *ctx;
 	struct uci_package *pkg;
     struct list_head *p;
 	int rv;
 	cfg.log_mask = ~0;
 	cfg.merge_strategy = DNET_MERGE_PREFER_NETWORK;
 
-	ctx = uci_alloc_context();
+	dssd_uci_context = uci_alloc_context();
 	ucimap_init(&dssd_map);
 
 
 
     // load config file
     if(config_path)
-        uci_set_confdir(ctx, config_path);
-	rv = uci_load(ctx, config_name ? config_name : "dssd", &pkg);
+        uci_set_confdir(dssd_uci_context, config_path);
+	rv = uci_load(dssd_uci_context, config_name ? config_name : "dssd", &pkg);
     if (rv)
         dssd_fatal("Can't load config\n");
 	ucimap_parse(&dssd_map, pkg);
     //}
 
     ucfg = list_first_entry(&dssd_config, struct uci_basic_config, list);
-
+ 
     config_node(&cfg, ucfg);
     node = dnet_node_create(&cfg);
 	if (!node)
 	if (daemon)
 		dnet_background();
 
-
     start_lua_threads();
 
     //dnet_give_up_control(node);
     sleep(1);
 
 	ucimap_cleanup(&dssd_map);
-	uci_free_context(ctx);
+	uci_free_context(dssd_uci_context);
 
 	return 0;
 }
 struct list_head dssd_pool_config;
 extern struct uci_map dssd_map;
 void *dssd_log_priv;
+struct uci_context *dssd_uci_context;
 
 
 struct uci_basic_config {
 #define DNET_META "dnet"
 #define DNET_META_CONFIG "dnet_config"
 #define DNET_META_NODE "dnet_node"
-//#define DNET_META_IO "dnet_io_control"
+#define _QUOTE(x) #x
+#define QUOTE(x) _QUOTE(x)
+
 
 #define DNET_PUSH_CONSTANT(x) \
 	lua_pushinteger(L, x); \
 void *start_lua_thread (void *ptr)
 {
     struct lua_thread_param *param = ptr;
+    struct uci_context **u;
+    int exec_len = strlen(param->lua_file)+30;
+    char *exec = malloc(exec_len);
     lua_State *L = param->lua_state;
+
+    snprintf(exec, exec_len, "require(\"%s\")", param->lua_file);
+
+    luaL_openlibs (L);
+    // adjust package path
+    
+    // adjust package path
+    //lua_getglobal (L, "package.path");
+
+    lua_getglobal(L, "package");
+    if (LUA_TTABLE != lua_type(L, 1)) {
+        return;
+    }
+    lua_getfield(L, 1, "path");
+    if (LUA_TSTRING != lua_type(L, 2)) {
+        lua_pop(L, 1);
+        return;
+    }
+    lua_pushliteral(L, ";");
+    lua_pushliteral(L, QUOTE(SOURCE_PREFIX));
+    lua_pushliteral(L, "/src/backends/?.lua;");
+    lua_pushliteral(L, QUOTE(SOURCE_PREFIX));
+    lua_pushliteral(L, "/src/plugins/?.lua;");
+    lua_pushliteral(L, QUOTE(INSTALL_PREFIX));
+    lua_pushliteral(L, "/lib/dssd/backends/?.lua;");
+    lua_pushliteral(L, QUOTE(INSTALL_PREFIX));
+    lua_pushliteral(L, "/lib/dssd/plugin/?.lua;");
+    lua_concat (L, 10);
+    //lua_setglobal (L, "package.path");
+
+    lua_setfield(L, -2, "path");  
+    //lua_pop(L, 1);
+    luaL_dostring (L, "require(\"uci\")");
     
     luaopen_libdnet_lua(L);
     
+    // import uci module and prepare for
+    u = lua_newuserdata(L, sizeof(struct uci_context *));
+	luaL_getmetatable(L, "uci.meta");
+	lua_setmetatable(L, -2);
+
+	*u = dssd_uci_context;
+    
+    lua_setglobal(L, "dssd_config");
+    
 /*    luaL_newmetatable(L, "dssd");
 
 	lua_pushvalue(L, -1);
     //lua_remove(L, -1);
 */
     dssd_log(DNET_LOG_NOTICE, "run: %s\n", param->lua_file);
-    if(luaL_dofile(L, param->lua_file)) {
+
+    lua_getglobal(L, "require");
+    lua_pushstring(L, param->lua_file);
+    
+
+    if(lua_pcall(L, 1, 0, 0) == LUA_ERRRUN) {
         dssd_log(DNET_LOG_ERROR, "error running %s: %s\n", param->lua_file, 
                  luaL_checkstring(L, -1));
-        stackDump(L, 0);
         }
+free:
+    free(param->lua_file);
+    free(param->lua_state);
     free(param);
+    free(exec);
 	return NULL;
 }
 
 	struct uci_plugins_config *plugin;
     struct uci_backend_config *backend;
     struct lua_thread_param *param;
-
+    
 	list_for_each(p, &dssd_config_backend) {
         backend = list_entry(p, struct uci_backend_config, list);
         dssd_log(DNET_LOG_INFO, "load backend: %s\n", backend->name);
 static struct uci_optmap dssd_mapping_options[] = {
 	{
 		UCIMAP_OPTION(struct uci_mapping, server),
-        .name = "server",
 		.type = UCIMAP_SECTION,
 		.data.sm = &dssd_basic_config
 	}
             cfg->thread_num);
         printf("map: %d\n", cfg->mappings->n_items);
 		for (i = 0; i < cfg->mappings->n_items; i++) {
-			mapping = cfg->mappings->item[i].section;
+            mapping = cfg->mappings->item[i].ptr;
             printf("jo\n");
 			//printf("%s\n", mapping->name);
 		}