[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r3853 - in GNUnet/src/applications/dht: module tools
From: |
grothoff |
Subject: |
[GNUnet-SVN] r3853 - in GNUnet/src/applications/dht: module tools |
Date: |
Sat, 2 Dec 2006 17:31:57 -0800 (PST) |
Author: grothoff
Date: 2006-12-02 17:31:53 -0800 (Sat, 02 Dec 2006)
New Revision: 3853
Added:
GNUnet/src/applications/dht/module/dstore.c
GNUnet/src/applications/dht/module/dstore.h
GNUnet/src/applications/dht/module/routing.c
GNUnet/src/applications/dht/module/routing.h
Removed:
GNUnet/src/applications/dht/tools/dht-join.c
Modified:
GNUnet/src/applications/dht/module/Makefile.am
GNUnet/src/applications/dht/tools/dht-query.c
Log:
fix
Modified: GNUnet/src/applications/dht/module/Makefile.am
===================================================================
--- GNUnet/src/applications/dht/module/Makefile.am 2006-12-02 23:03:38 UTC
(rev 3852)
+++ GNUnet/src/applications/dht/module/Makefile.am 2006-12-03 01:31:53 UTC
(rev 3853)
@@ -6,7 +6,9 @@
libgnunetmodule_dht.la
libgnunetmodule_dht_la_SOURCES = \
- table.c dstore.c
+ table.c table.h \
+ dstore.c dstore.h \
+ routing.c routing.h
libgnunetmodule_dht_la_LIBADD = \
$(top_builddir)/src/util/crypto/libgnunetutil_crypto.la \
$(top_builddir)/src/util/containers/libgnunetutil_containers.la \
Added: GNUnet/src/applications/dht/module/dstore.c
===================================================================
--- GNUnet/src/applications/dht/module/dstore.c 2006-12-02 23:03:38 UTC (rev
3852)
+++ GNUnet/src/applications/dht/module/dstore.c 2006-12-03 01:31:53 UTC (rev
3853)
@@ -0,0 +1,337 @@
+/*
+ This file is part of GNUnet
+ (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file module/dstore.c
+ * @brief entries in local DHT
+ * @author Simo Viitanen, Christian Grothoff
+ *
+ * TODO:
+ * - store should automatically delete
+ * oldest content when full
+ * - use SQL-based database instead of
+ * in-memory database
+ */
+
+#include "platform.h"
+#include "dstore.h"
+
+
+/**
+ * @brief datastructure for one entry in the table.
+ */
+typedef struct HT_Entry_t {
+ struct HT_Entry_t * next;
+ HashCode512 key;
+ unsigned int count;
+ DataContainer ** values;
+} HT_Entry;
+
+/**
+ * @brief the per-table data
+ */
+typedef struct {
+ struct MUTEX * lock;
+ size_t max_memory;
+ HT_Entry * first;
+} MemoryDatastore;
+
+
+/**
+ * Lookup an item in the datastore.
+ *
+ * @param key the value to lookup
+ * @param maxResults maximum number of results
+ * @param results where to store the result; must point to
+ * an array of maxResuls containers; if the containers
+ * point to allocated memory, it will be used by lookup;
+ * otherwise lookup will allocate the data pointer;
+ * in either case dataLength is adjusted to the actual
+ * size of the data. If not enough space is present to
+ * accomodate the data the data will be truncated.
+ * @return number of results, SYSERR on error
+ */
+static int ds_lookup(void * closure,
+ unsigned int type,
+ unsigned int prio,
+ unsigned int keyCount,
+ const HashCode512 * keys,
+ DataProcessor resultCallback,
+ void * resCallbackClosure) {
+ MemoryDatastore * ds = (MemoryDatastore*) closure;
+ HT_Entry * pos;
+ int i;
+
+ if ( (ds == NULL) || (keyCount != 1) )
+ return SYSERR;
+ MUTEX_LOCK(ds->lock);
+ pos = ds->first;
+ while (pos != NULL) {
+ if (0 == memcmp(&keys[0],
+ &pos->key,
+ sizeof(HashCode512))) {
+ for (i=0;i<pos->count;i++)
+ if (OK != resultCallback(&pos->key,
+ pos->values[i],
+ resCallbackClosure)) {
+ MUTEX_UNLOCK(ds->lock);
+ return SYSERR;
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return pos->count;
+ }
+ pos = pos->next;
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return 0;
+}
+
+/**
+ * Store an item in the datastore.
+ *
+ * @param key the key of the item
+ * @param value the value to store
+ * for other entries under the same key (if key already exists)
+ * @return OK if the value could be stored, DHT_ERRORCODE or SYSERR if not
(i.e. out of space)
+ */
+static int ds_store(void * closure,
+ const HashCode512 * key,
+ const DataContainer * value,
+ unsigned int prio) {
+ MemoryDatastore * ds = closure;
+ HT_Entry * pos;
+ unsigned int size;
+
+ if (ds == NULL)
+ return SYSERR;
+
+ size = ntohl(value->size);
+ MUTEX_LOCK(ds->lock);
+ pos = ds->first;
+ while (pos != NULL) {
+ if (0 == memcmp(key,
+ &pos->key,
+ sizeof(HashCode512))) {
+ if (ds->max_memory < size) {
+ MUTEX_UNLOCK(ds->lock);
+ return NO;
+ }
+ ds->max_memory -= size;
+ GROW(pos->values,
+ pos->count,
+ pos->count+1);
+ pos->values[pos->count-1]
+ = MALLOC(size);
+ memcpy(pos->values[pos->count-1],
+ value,
+ size);
+ MUTEX_UNLOCK(ds->lock);
+ return OK;
+ } /* end key match */
+ pos = pos->next;
+ }
+ /* no key matched, create fresh entry */
+ if (ds->max_memory < sizeof(HT_Entry) + size) {
+ MUTEX_UNLOCK(ds->lock);
+ return NO;
+ }
+ ds->max_memory -= sizeof(HT_Entry) + size;
+ pos = MALLOC(sizeof(HT_Entry));
+ pos->key = *key;
+ pos->count = 1;
+ pos->values = MALLOC(sizeof(DataContainer*));
+ pos->values[0] = MALLOC(size);
+ memcpy(pos->values[0],
+ value,
+ size);
+ pos->next = ds->first;
+ ds->first = pos;
+ MUTEX_UNLOCK(ds->lock);
+ return OK;
+}
+
+
+
+/**
+ * Remove an item from the datastore.
+ *
+ * @param key the key of the item
+ * @param value the value to remove, NULL for all values of the key
+ * @return OK if the value could be removed, SYSERR if not (i.e. not present)
+ */
+static int ds_remove(void * closure,
+ const HashCode512 * key,
+ const DataContainer * value) {
+ MemoryDatastore * ds = closure;
+ HT_Entry * pos;
+ HT_Entry * prev;
+ int i;
+ unsigned int size;
+
+ if (ds == NULL)
+ return SYSERR;
+ size = ntohl(value->size);
+ MUTEX_LOCK(ds->lock);
+ prev = NULL;
+ pos = ds->first;
+ while (pos != NULL) {
+ if (0 == memcmp(key,
+ &pos->key,
+ sizeof(HashCode512))) {
+ if (value != NULL) {
+ for (i=0;i<pos->count;i++) {
+ if ( (pos->values[i]->size == value->size) &&
+ (0 == memcmp(pos->values[i],
+ value,
+ size)) ) {
+ FREE(pos->values[i]);
+ ds->max_memory += size;
+ pos->values[i] = pos->values[pos->count-1];
+ GROW(pos->values,
+ pos->count,
+ pos->count-1);
+ if (pos->count == 0) {
+ if (prev == NULL)
+ ds->first = pos->next;
+ else
+ prev->next = pos->next;
+ FREE(pos);
+ ds->max_memory += sizeof(HT_Entry);
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return OK;
+ }
+ }
+ } else {
+ /* remove entire link */
+ if (prev == NULL)
+ ds->first = pos->next;
+ else
+ prev->next = pos->next;
+
+ for (i=0;i<pos->count;i++) {
+ ds->max_memory += ntohl(pos->values[i]->size);
+ FREE(pos->values[i]);
+ }
+ GROW(pos->values,
+ pos->count,
+ 0);
+ FREE(pos);
+ ds->max_memory += sizeof(HT_Entry);
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return OK;
+ }
+ prev = pos;
+ pos = pos->next;
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return SYSERR; /* not found */
+}
+
+/**
+ * Iterate over all keys in the local datastore
+ *
+ * @param processor function to call on each item
+ * @param cls argument to processor
+ * @return number of results, SYSERR on error
+ */
+static int ds_iterate(void * closure,
+ DataProcessor processor,
+ void * cls) {
+ MemoryDatastore * ds = closure;
+ int ret;
+ HT_Entry * pos;
+ int i;
+
+ if (ds == NULL)
+ return SYSERR;
+
+ MUTEX_LOCK(ds->lock);
+ pos = ds->first;
+ ret = 0;
+ while (pos != NULL) {
+ for (i=0;i<pos->count;i++) {
+ ret++;
+ if (processor != NULL)
+ if (OK != processor(&pos->key,
+ pos->values[i],
+ cls)) {
+ MUTEX_UNLOCK(ds->lock);
+ return ret;
+ }
+ }
+ pos = pos->next;
+ }
+ MUTEX_UNLOCK(ds->lock);
+ return SYSERR;
+}
+
+/**
+ * Initialize dstore DHT component.
+ *
+ * @param capi the core API
+ * @return OK on success
+ */
+Blockstore * init_dht_store(size_t max_size,
+ CoreAPIForApplication * capi) {
+ Blockstore * res;
+ MemoryDatastore * md;
+
+ md = MALLOC(sizeof(MemoryDatastore));
+ md->max_memory = max_size;
+ md->first = NULL;
+ md->lock = MUTEX_CREATE(YES);
+ res = MALLOC(sizeof(Blockstore));
+ res->get = &ds_lookup;
+ res->put = &ds_store;
+ res->del = &ds_remove;
+ res->iterate = &ds_iterate;
+ res->closure = md;
+ return res;
+}
+
+/**
+ * Shutdown dstore DHT component.
+ *
+ * @return OK on success
+ */
+int done_dht_store(Blockstore * ds) {
+ MemoryDatastore * md;
+ HT_Entry * pos;
+ HT_Entry * next;
+ unsigned int i;
+
+ md = ds->closure;
+ pos = md->first;
+ while (pos != NULL) {
+ next = pos->next;
+ for (i=0;i<pos->count;i++)
+ FREENONNULL(pos->values[i]);
+ FREE(pos->values);
+ FREE(pos);
+ pos = next;
+ }
+ MUTEX_DESTROY(md->lock);
+ FREE(md);
+ FREE(ds);
+ return OK;
+}
+
Added: GNUnet/src/applications/dht/module/dstore.h
===================================================================
--- GNUnet/src/applications/dht/module/dstore.h 2006-12-02 23:03:38 UTC (rev
3852)
+++ GNUnet/src/applications/dht/module/dstore.h 2006-12-03 01:31:53 UTC (rev
3853)
@@ -0,0 +1,50 @@
+/*
+ This file is part of GNUnet
+ (C) 2006 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file module/dstore.h
+ * @brief entries in local DHT
+ * @author Christian Grothoff
+ */
+
+#ifndef DHT_DSTORE_H
+#define DHT_DSTORE_H
+
+#include "gnunet_util.h"
+#include "gnunet_core.h"
+#include "gnunet_blockstore.h"
+
+/**
+ * Initialize dstore DHT component.
+ *
+ * @param capi the core API
+ * @return NULL on error
+ */
+Blockstore * init_dht_store(size_t max_size,
+ CoreAPIForApplication * capi);
+
+/**
+ * Shutdown dstore DHT component.
+ *
+ * @return OK on success
+ */
+int done_dht_store(Blockstore * store);
+
+#endif
Added: GNUnet/src/applications/dht/module/routing.c
===================================================================
--- GNUnet/src/applications/dht/module/routing.c 2006-12-02 23:03:38 UTC
(rev 3852)
+++ GNUnet/src/applications/dht/module/routing.c 2006-12-03 01:31:53 UTC
(rev 3853)
@@ -0,0 +1,56 @@
+/*
+ This file is part of GNUnet
+ (C) 2006 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file module/routing.c
+ * @brief state for active DHT routing operations
+ * @author Christian Grothoff
+ *
+ * TODO:
+ * - tracking of get/put opertations
+ * - retry
+ * - reply handling
+ * - register handlers (get, put, result)
+ */
+
+#include "platform.h"
+#include "routing.h"
+#include "table.h"
+
+/**
+ * Initialize routing DHT component.
+ *
+ * @param capi the core API
+ * @return OK on success
+ */
+int init_dht_routing(CoreAPIForApplication * capi) {
+ return OK;
+}
+
+/**
+ * Shutdown routing DHT component.
+ *
+ * @return OK on success
+ */
+int done_dht_routing() {
+ return OK;
+}
+
+
Added: GNUnet/src/applications/dht/module/routing.h
===================================================================
--- GNUnet/src/applications/dht/module/routing.h 2006-12-02 23:03:38 UTC
(rev 3852)
+++ GNUnet/src/applications/dht/module/routing.h 2006-12-03 01:31:53 UTC
(rev 3853)
@@ -0,0 +1,48 @@
+/*
+ This file is part of GNUnet
+ (C) 2006 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file module/routing.h
+ * @brief state for active DHT routing operations
+ * @author Christian Grothoff
+ */
+
+#ifndef DHT_ROUTING_H
+#define DHT_ROUTING_H
+
+#include "gnunet_util.h"
+#include "gnunet_core.h"
+
+/**
+ * Initialize routing DHT component.
+ *
+ * @param capi the core API
+ * @return OK on success
+ */
+int init_dht_routing(CoreAPIForApplication * capi);
+
+/**
+ * Shutdown routing DHT component.
+ *
+ * @return OK on success
+ */
+int done_dht_routing(void);
+
+#endif
Deleted: GNUnet/src/applications/dht/tools/dht-join.c
===================================================================
--- GNUnet/src/applications/dht/tools/dht-join.c 2006-12-02 23:03:38 UTC
(rev 3852)
+++ GNUnet/src/applications/dht/tools/dht-join.c 2006-12-03 01:31:53 UTC
(rev 3853)
@@ -1,216 +0,0 @@
-/*
- This file is part of GNUnet
- (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
-
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
- option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
- */
-
-
-/**
- * @file dht-join.c
- * @brief join table and provide client store
- * @author Christian Grothoff
- *
- * Todo:
- * - test
- */
-
-#include "platform.h"
-#include "gnunet_util.h"
-#include "gnunet_util_boot.h"
-#include "gnunet_dht_lib.h"
-#include "gnunet_dht_datastore_memory.h"
-
-static unsigned int memory = 64 * 1024;
-
-static char * table_id;
-
-static char * cfgFilename;
-
-static int verbose;
-
-static struct GE_Context * ectx;
-
-/**
- * All gnunet-dht-join command line options
- */
-static struct CommandLineOption gnunetjoinOptions[] = {
- COMMAND_LINE_OPTION_CFG_FILE(&cfgFilename), /* -c */
- COMMAND_LINE_OPTION_HELP(gettext_noop("Join a DHT.")), /* -h */
- COMMAND_LINE_OPTION_HOSTNAME, /* -H */
- COMMAND_LINE_OPTION_LOGGING, /* -L */
- { 'm', "memory", "SIZE",
- gettext_noop("allow SIZE bytes of memory for the local table"),
- 1, &gnunet_getopt_configure_set_uint, &memory },
- { 't', "table", "NAME",
- gettext_noop("join table called NAME"),
- 1, &gnunet_getopt_configure_set_string, &table_id },
- COMMAND_LINE_OPTION_VERSION(PACKAGE_VERSION), /* -v */
- COMMAND_LINE_OPTION_VERBOSE,
- COMMAND_LINE_OPTION_END,
-};
-
-static void dump(const char * fmt,
- ...) {
- va_list ap;
- if (verbose > 0) {
- va_start(ap, fmt);
- vfprintf(stdout,
- fmt,
- ap);
- va_end(ap);
- }
-}
-
-#define LOGRET(ret) dump(_("Call to `%s' returns %d.\n"), __FUNCTION__, ret)
-#define LOGKEY(key) do { EncName kn; hash2enc(key, &kn); dump(_("Call to `%s'
with key `%s'.\n"), __FUNCTION__, &kn); } while (0)
-#define LOGVAL(val) dump(_("Call to `%s' with value '%.*s' (%d bytes).\n"),
__FUNCTION__, (val == NULL) ? 0 : &val[1], (val == NULL) ? NULL : &val[1], (val
== NULL) ? 0 : (ntohl(val->size) - sizeof(DataContainer)))
-
-static int lookup(void * closure,
- unsigned int type,
- unsigned int prio,
- unsigned int keyCount,
- const HashCode512 * keys,
- DataProcessor processor,
- void * pclosure) {
- int ret;
- Blockstore * cls = closure;
- LOGKEY(&keys[0]);
- ret = cls->get(cls->closure,
- type,
- prio,
- keyCount,
- keys,
- processor,
- pclosure);
- LOGRET(ret);
- return ret;
-}
-
-static int store(void * closure,
- const HashCode512 * key,
- const DataContainer * value,
- unsigned int prio) {
- int ret;
- Blockstore * cls = closure;
- LOGKEY(key);
- LOGVAL(value);
- ret = cls->put(cls->closure,
- key,
- value,
- prio);
- LOGRET(ret);
- return ret;
-}
-
-static int removeDS(void * closure,
- const HashCode512 * key,
- const DataContainer * value) {
- int ret;
- Blockstore * cls = closure;
- LOGKEY(key);
- LOGVAL(value);
- ret = cls->del(cls->closure,
- key,
- value);
- LOGRET(ret);
- return ret;
-}
-
-static int iterate(void * closure,
- DataProcessor processor,
- void * parg) {
- int ret;
- Blockstore * cls = closure;
- ret = cls->iterate(cls->closure,
- processor,
- parg);
- LOGRET(ret);
- return ret;
-}
-
-int main(int argc,
- char * const * argv) {
- int i;
- HashCode512 table;
- struct GC_Configuration * cfg;
- Blockstore myStore;
-
-
- i = GNUNET_init(argc,
- argv,
- "gnunet-dht-join",
- &cfgFilename,
- gnunetjoinOptions,
- &ectx,
- &cfg);
- if (i == -1) {
- GNUNET_fini(ectx, cfg);
- return -1;
- }
- if (table_id == NULL) {
- printf(_("No table name specified, using `%s'.\n"),
- "test");
- table_id = STRDUP("test");
- }
- if (OK != enc2hash(table_id,
- &table)) {
- hash(table_id,
- strlen(table_id),
- &table);
- }
- FREE(table_id);
- table_id = NULL;
- myStore.closure = create_blockstore_memory(memory);
- myStore.get = &lookup;
- myStore.put = &store;
- myStore.del = &removeDS;
- myStore.iterate = &iterate;
-
- if (OK != DHT_LIB_join(&myStore,
- cfg,
- ectx,
- &table)) {
- GE_LOG(ectx,
- GE_WARNING | GE_BULK | GE_USER,
- _("Error joining DHT.\n"));
- destroy_blockstore_memory((Blockstore*)myStore.closure);
- GC_free(cfg);
- GE_free_context(ectx);
- return 1;
- }
-
- printf(_("Joined DHT. Press CTRL-C to leave.\n"));
- GNUNET_SHUTDOWN_WAITFOR();
-
- i = OK;
- /* shutdown */
- if (OK != DHT_LIB_leave(&table)) {
- i = SYSERR;
- GE_LOG(ectx,
- GE_WARNING | GE_BULK | GE_USER,
- _("Error leaving DHT.\n"));
- }
-
- destroy_blockstore_memory((Blockstore*)myStore.closure);
- GC_free(cfg);
- GE_free_context(ectx);
- if (i != OK)
- return 1;
- return 0;
-}
-
-/* end of dht-join.c */
Modified: GNUnet/src/applications/dht/tools/dht-query.c
===================================================================
--- GNUnet/src/applications/dht/tools/dht-query.c 2006-12-02 23:03:38 UTC
(rev 3852)
+++ GNUnet/src/applications/dht/tools/dht-query.c 2006-12-03 01:31:53 UTC
(rev 3853)
@@ -31,7 +31,6 @@
#include "gnunet_dht_lib.h"
#include "gnunet_util_boot.h"
#include "gnunet_util_network_client.h"
-#include "gnunet_dht_datastore_memory.h"
static DHT_TableId table;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r3853 - in GNUnet/src/applications/dht: module tools,
grothoff <=