Commits

Daniel Poelzleithner  committed b364691

work un lua, uci, node config
add node:write_file and node:read_file

  • Participants
  • Parent commits 1098f78

Comments (0)

Files changed (9)

     option 'addr' '0.0.0.0'
     option 'proto' 'ipv4'
     option 'port' '2342'
-    option 'suffix' '003242'
-    option 'city' '000000'
+    option 'suffix' '013242'
+    option 'city' '123456'
     option 'pool' '01'
     option 'region' '0000'
     option 'storage_path' '/tmp'
     option 'port' '54'
 
 config 'plugin' 'http'
-    option 'enabled' 'on'
+    option 'enabled' 'off'
     option 'port' '54'
 
 config 'backend' 'tests'
-    option 'enabled' 'off'
+    option 'enabled' 'on'

File src/backends/static.lua

-print("runnig static")
-print(uci)
-print(dssd_server)
-
-print("meta"..tostring(getmetatable(dssd_server.uci_context)))
-dssd_lst = dssd_server.uci_context:get_all('dssd.basic')
-
-function test(section)
-    print('jo:'..tostring(section))
-    for i,v in pairs(section) do
-        print("--")
-        print(tostring(i)..":"..tostring(v))
-    end
-end
-
-
-uci.foreach(dssd_server.uci_context, 'dssd', 'backend', test)
-
 local conf_lst = {['ipv4']={}, ['ipv6']={}}
 
 function get_or_create(proto, hostname)
     end
 end
 
-print(uci.get(dssd_server.uci_context, 'dssd.static.sleep'))
-
 while true do
     local delay = tonumber(uci.get(dssd_server.uci_context, 'dssd.static.delay'))
     if delay and delay > 0 then
     }
 }
 
+int dssd_hex2bin(char unsigned *out, const char *data, int len) {
+    int i;
+	for (i=0; i < len; i++) {
+		char c = data[2*i];
+		if (c >= '0' && c <= '9') {
+			out[i] = (c - '0') << 4;
+		} else if (c >= 'a' && c <= 'f') {
+			out[i] = (c - 'a' + 10) << 4;
+		} else if (data[2*i] >= 'A' && c <= 'F') {
+			out[i] = (c - 'A' + 10) << 4;
+		} else {
+            return 1;
+		}
+
+		c = data[2*i+1];
+		if (c >= '0' && c <= '9') {
+			out[i] += c - '0';
+		} else if (c >= 'a' && c <= 'f') {
+			out[i] += c - 'a' + 10;
+		} else if (c >= 'A' && c <= 'F') {
+			out[i] += c - 'A' + 10;
+		} else {
+            return 1;
+		}
+	}
+    return 0;
+}
+
+
+unsigned char *dssd_create_id(struct uci_basic_config *basic) {
+    unsigned char *id;
+    char *tmp;
+    
+    id = malloc(sizeof(unsigned char) * DNET_ID_SIZE);
+    memset(id, 0, sizeof(unsigned char) * DNET_ID_SIZE);
+
+    dssd_hex2bin(id, basic->city, 3);
+    dssd_hex2bin(id+3, basic->pool, 1);
+    dssd_hex2bin(id+4, basic->suffix, DNET_ID_SIZE-4);
+
+    return id;
+}
+
 
 int main(int argc, char *argv[])
 {
     struct uci_basic_config *ucfg;
 	char *logfile = NULL, *readf = NULL, *writef = NULL, *cmd = NULL, *lookup = NULL;
 	char *historyf = NULL, *root = NULL, *removef = NULL, *config_path = NULL;
+    char *tmp = NULL;
     char *config_name = NULL;
 	unsigned char trans_id[DNET_ID_SIZE], *id = NULL;
     dssd_do_exit = 0;
 	dssd_server_config->proto = IPPROTO_TCP;
 	dssd_server_config->wait_timeout = 60*60;
 	dssd_server_config->log_mask = ~0;
+    dssd_server_config->join = 1;
 	dssd_server_config->merge_strategy = DNET_MERGE_PREFER_NETWORK;
 
 	dssd_uci_context = uci_alloc_context();
     ucfg = list_first_entry(&dssd_config, struct uci_basic_config, list);
  
     config_node(dssd_server_config, ucfg);
+    id = dssd_create_id(ucfg);
+    memcpy(&dssd_server_config->id, id, DNET_ID_SIZE);
+    free(id);
     dssd_server_node = dnet_node_create(dssd_server_config);
 	if (!dssd_server_node)
 		dssd_fatal("Can't create dnet node\n");
 
+    
+    for(i = 0; i < 5; i++) {
+        tmp = malloc(10);
+        e = malloc(sizeof(struct dnet_crypto_engine));
+        memset(e, 0, sizeof(struct dnet_crypto_engine));
+        snprintf(tmp, 10, "dssd_%d", i);
+        dnet_crypto_engine_init(e, tmp);
+		err = dnet_add_transform(dssd_server_node, e, e->name,
+                                 e->init, e->update, e->final);
+        if (err)
+            dssd_fatal("Can't add transformation %d\n", i);
+        free(tmp);
+    }
 
 	if (daemon)
 		dnet_background();
 
     start_all_plugins();
+
+    //sleep(3);
+    //return 0;
     start_lua_threads();
 
     while (!dssd_do_exit)
 #include <sys/time.h>
 #include <sys/syscall.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "dnet/packet.h"
 #include "dnet/interface.h"
 #include <lua.h>
 #include <pthread.h>
 
+#ifndef __DSSD_H
+#define __DSSD_H
+
 
 struct list_head dssd_config;
 struct list_head dssd_config_plugin;
 };
 
 
-
-
 #define dssd_fatal(...) {  \
     printf(__VA_ARGS__); \
     exit(1); }
 void dssd_log(int mask, char *format, ...);
 void dssd_log_raw(int mask, char *msg);
 
+static const char bin2hex_map[16] = {
+'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+};
+
+int dssd_hex2bin(char unsigned *out, const char *data, int len);
+#endif
   return dcp;
 }
 
-const char bin2hex_map[16] = {
-'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
-};
-
 static int bin2hex(lua_State *L) {
 	size_t len, lenout, i;
 	luaL_checktype(L, -1, LUA_TSTRING);
 		return luaL_error(L, "out of mem");
 	}
 
-	for (i=0; i < lenout; i++) {
-		char c = data[2*i];
-		if (c >= '0' && c <= '9') {
-			out[i] = (c - '0') << 4;
-		} else if (c >= 'a' && c <= 'f') {
-			out[i] = (c - 'a' + 10) << 4;
-		} else if (data[2*i] >= 'A' && c <= 'F') {
-			out[i] = (c - 'A' + 10) << 4;
-		} else {
-			free(out);
-            return luaL_error(L, "wrong value");
-		}
-
-		c = data[2*i+1];
-		if (c >= '0' && c <= '9') {
-			out[i] += c - '0';
-		} else if (c >= 'a' && c <= 'f') {
-			out[i] += c - 'a' + 10;
-		} else if (c >= 'A' && c <= 'F') {
-			out[i] += c - 'A' + 10;
-		} else {
-			free(out);
-            return luaL_error(L, "wrong value");
-		}
-	}
+    if(dssd_hex2bin(out, data, lenout)) {
+        free(out);
+        return luaL_error(L, "wrong value");
+    }
 
 	lua_pushlstring(L, out, lenout);
 	free(out);
 
 
 
-const char dnet__bin2hex[16] = {
-'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
-};
-
 char *id_to_hexstring(struct dnet_config *dc) {
     /*
     convert a dnet_config id into hex to be passed into
     char *rv = malloc(DNET_ID_SIZE*2+1);
     memset(rv, 0, DNET_ID_SIZE*2+1);
     for(i = 0; i < DNET_ID_SIZE; i++) {
-        rv[2*i]   = dnet__bin2hex[((dc->id)[i] & 0xf0) >> 4];
-        rv[2*i+1] = dnet__bin2hex[((dc->id)[i] & 0x0f)];
+        rv[2*i]   = bin2hex_map[((dc->id)[i] & 0xf0) >> 4];
+        rv[2*i+1] = bin2hex_map[((dc->id)[i] & 0x0f)];
     }
     return rv;
 
 }
 
 
+static int dnetL_node_write_file(lua_State *L)
+{
+    // test for optional options trough tables
+    struct dnet_node *dn = check_dnet_node(L, 1);
+    const char *file;
+    const unsigned char *id;
+    uint64_t offset, size;
+    unsigned int flags;
+
+    file = luaL_checkstring(L, 2);
+    if(lua_isnil(L, 3))
+        id = NULL;
+    else
+        id = luaL_checkstring(L, 3);
+    offset = luaL_checkinteger(L, 4);
+    size = luaL_checkinteger(L, 5);
+    flags = luaL_checkinteger(L, 6);
+
+    lua_pushinteger(L, dnet_write_file(dn, (char *)file, (unsigned char *)id, offset, size, flags));
+
+    return 1;
+}
+
+static int dnetL_node_read_file(lua_State *L)
+{
+    // test for optional options trough tables
+    struct dnet_node *dn = check_dnet_node(L, 1);
+    const char *file;
+    const unsigned char *id;
+    uint64_t offset, size;
+    unsigned int hist;
+
+    file = luaL_checkstring(L, 2);
+    if(lua_isnil(L, 3))
+        id = NULL;
+    else
+        id = luaL_checkstring(L, 3);
+    offset = luaL_checkinteger(L, 4);
+    size = luaL_checkinteger(L, 5);
+    hist = luaL_checkinteger(L, 6);
+    
+    lua_pushinteger(L, dnet_read_file(dn, (char *)file, (unsigned char *)id, offset, size, hist));
+    return 1;
+}
+
 
 /****************************************************
  *  dnet_config
     {"add_state", dnetL_node_add_state},
     {"join", dnetL_node_join},
     {"destroy", dnetL_node_gc},
+    {"write_file",  dnetL_node_write_file},
+    {"read_file",  dnetL_node_read_file},
     {0,0}
 };
 

File src/plugins/CMakeLists.txt

 add_library(http SHARED http.c)
 INCLUDE_DIRECTORIES(${CMAKE_HOME_DIRECTORY}/src)
 
-target_link_libraries(http uci ${LUA_LIBRARIES} event)
+target_link_libraries(http dnet uci ${LUA_LIBRARIES} event)

File src/plugins/http.c

+#include "dssd.h"
+#include <time.h>
+
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/queue.h>
 #include <stdlib.h>
-#include "dssd.h"
 
 #include <err.h>
 #include <event.h>
         if (buf == NULL)
             err(1, "failed to create response buffer");
 
-        evbuffer_add_printf(buf, "Requested: %sn", evhttp_request_uri(req));
+        evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
         evhttp_send_reply(req, HTTP_OK, "OK", buf);
 }
 
 int dssd_plugin(struct uci_plugins_config *plugin)
 {
     struct evhttp *httpd;
+    char *host;
+    int port;
 
     event_init();
-    httpd = evhttp_start("0.0.0.0", 8080);
+    
+    host = "0.0.0.0";
+    port = 8080;
+    
+    httpd = evhttp_start(host, port);
 
+    dssd_log(DNET_LOG_INFO, "http listening on: %s:%d\n", host, port);
     printf("running http\n");
 
     /* Set a callback for requests to "/specific". */
 	return 0;
 }
 
+#define fix_string(name, len) \
+static int \
+uci_parse_##name(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str) \
+{ \
+	data->s = malloc(len+1); \
+    memset(data->s, 0, len+1); \
+    strncpy(data->s, str, len); \
+	return 0; \
+} \
+static void \
+uci_free_##name(void *section, struct uci_optmap *om, void *ptr) \
+{ \
+    free(ptr); \
+}
+
+fix_string(city, 6);
+fix_string(pool, 1);
+fix_string(suffix, DNET_ID_SIZE - 7);
+
+#undef fix_string
+
 static int
 uci_parse_proto(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
 {
 	{
 		.map = {
 			UCIMAP_OPTION(struct uci_basic_config, proto),
-			.type = UCIMAP_STRING,
+			.type = UCIMAP_CUSTOM,
 			.name = "proto",
-			.data.s.maxlen = 32,
+			.parse = uci_parse_proto,
+			.format = uci_format_proto,
 		}
 	},
 	{
 	{
 		.map = {
 			UCIMAP_OPTION(struct uci_basic_config, suffix),
-			.type = UCIMAP_STRING,
+			.type = UCIMAP_CUSTOM,
 			.name = "suffix",
+            .parse = uci_parse_suffix,
+            .free = uci_free_suffix,
 		}
 	},
 	{
 		.map = {
 			UCIMAP_OPTION(struct uci_basic_config, city),
-			.type = UCIMAP_STRING,
+			.type = UCIMAP_CUSTOM,
 			.name = "city",
+            .free = uci_free_city,
+            .parse = uci_parse_city,
 		}
 	},
 	{
 		.map = {
 			UCIMAP_OPTION(struct uci_basic_config, pool),
-			.type = UCIMAP_STRING,
+			.type = UCIMAP_CUSTOM,
 			.name = "pool",
-		}
-	},
-	{
-		.map = {
-			UCIMAP_OPTION(struct uci_basic_config, region),
-			.type = UCIMAP_STRING,
-			.name = "region",
+            .parse = uci_parse_pool,
+            .free = uci_free_pool,
 		}
 	},
     // storage

File tests/lua/test_dssd.lua

 -- http://www.keplerproject.org/luafilesystem/manual.html
 require('lfs')
 
+-- FIXME
 dnet = dssd
 
+
+TEMP_DIR = "/tmp/dnet_server/"
+lfs.mkdir(TEMP_DIR)
+FILES_DIR = TEMP_DIR.."files/"
+lfs.mkdir(FILES_DIR)
+
+
 function set(var, key, val)
     var[key] = val
 end
 
 STARTPORT = 5222
 
+
+function create_file(num)
+    fname = FILES_DIR..tostring(num)
+    fp = io.open(fname, "w")
+    i = 0
+    print(fname..tostring(fp))
+    for count = 1,100 do 
+        fp:write(string.rep(tostring(num), count).."\n")
+    end
+    fp:close()
+    print("done")
+    return fname
+end
+
+function hash_file(fname)
+    local run = "md5sum "..fname
+    local fp = io.popen (run, "r")
+    local buf = ""
+    while 1 do
+        local dat = fp:read()
+        if dat == nil then
+            break
+        end
+        buf = buf..dat
+        hash = string.match(buf, '[a-f0-9A-F]+ ')
+        return hash
+    end
+end
+
+
 TestLibDssd = {}
 
 function TestLibDssd:testParseIp()
 
 
 
-
 TestDnet = {} --class
 
 
     end
 
 
+    function TestDnet:test_fileop(ip, port)
+        dc = dnet.new_config()
+        if port == nil then
+            STARTPORT = STARTPORT +1
+        end
+        path = create_file(1)
+        hash = hash_file(path)
+        assertEquals(tostring(dssd_server.node:write_file(path, nil, 0, 10000, 0)), 
+                     "0")
+        os.remove(path)
+        assertEquals(tostring(dssd_server.node:read_file(path, nil, 0, 10000, 0)), 
+                     "0")
+        assertEquals(hash_file(path), hash)
+        
+
+    end
+
+
 -- class TestDnet
 
 LuaUnit:run()