gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r28869 - in gnunet: . src src/include src/psycstore


From: gnunet
Subject: [GNUnet-SVN] r28869 - in gnunet: . src src/include src/psycstore
Date: Tue, 27 Aug 2013 09:29:59 +0200

Author: tg
Date: 2013-08-27 09:29:59 +0200 (Tue, 27 Aug 2013)
New Revision: 28869

Added:
   gnunet/src/psycstore/
   gnunet/src/psycstore/Makefile.am
   gnunet/src/psycstore/gnunet-service-psycstore.c
   gnunet/src/psycstore/psycstore.conf
   gnunet/src/psycstore/psycstore.h
   gnunet/src/psycstore/psycstore_api.c
   gnunet/src/psycstore/test_psycstore.c
   gnunet/src/psycstore/test_psycstore.conf
Modified:
   gnunet/configure.ac
   gnunet/src/Makefile.am
   gnunet/src/include/gnunet_protocols.h
   gnunet/src/include/gnunet_psycstore_service.h
Log:
psycstore service skeleton

Modified: gnunet/configure.ac
===================================================================
--- gnunet/configure.ac 2013-08-27 07:29:54 UTC (rev 28868)
+++ gnunet/configure.ac 2013-08-27 07:29:59 UTC (rev 28869)
@@ -1281,6 +1281,7 @@
 src/hello/Makefile
 src/identity/Makefile
 src/identity/identity.conf
+src/psycstore/Makefile
 src/include/Makefile
 src/include/gnunet_directories.h
 src/integration-tests/Makefile

Modified: gnunet/src/Makefile.am
===================================================================
--- gnunet/src/Makefile.am      2013-08-27 07:29:54 UTC (rev 28868)
+++ gnunet/src/Makefile.am      2013-08-27 07:29:59 UTC (rev 28869)
@@ -60,6 +60,7 @@
   namestore \
   vpn \
   gns \
+  psycstore \
   fs \
   exit \
   pt \

Modified: gnunet/src/include/gnunet_protocols.h
===================================================================
--- gnunet/src/include/gnunet_protocols.h       2013-08-27 07:29:54 UTC (rev 
28868)
+++ gnunet/src/include/gnunet_protocols.h       2013-08-27 07:29:59 UTC (rev 
28869)
@@ -1935,12 +1935,22 @@
 #define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT 644
 
 
+/*******************************************************************************
+ * PSYCSTORE message types
+ 
******************************************************************************/
 
 /**
- * Next available: 650
+ * Generic response from PSYCstore service with success and/or error message.
  */
+#define GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE 650
 
 
+/**
+ * Next available: 670
+ */
+
+
+
 /* WIP: no numbers assigned yet */
 
 
/*******************************************************************************

Modified: gnunet/src/include/gnunet_psycstore_service.h
===================================================================
--- gnunet/src/include/gnunet_psycstore_service.h       2013-08-27 07:29:54 UTC 
(rev 28868)
+++ gnunet/src/include/gnunet_psycstore_service.h       2013-08-27 07:29:59 UTC 
(rev 28869)
@@ -123,7 +123,8 @@
  */
 typedef void
 (*GNUNET_PSYCSTORE_ResultCallback) (void *cls,
-                                    int result);
+                                    int result,
+                                    const char *err_msg);
 
 
 /** 

Added: gnunet/src/psycstore/Makefile.am
===================================================================
--- gnunet/src/psycstore/Makefile.am                            (rev 0)
+++ gnunet/src/psycstore/Makefile.am    2013-08-27 07:29:59 UTC (rev 28869)
@@ -0,0 +1,72 @@
+INCLUDES = -I$(top_srcdir)/src/include
+
+if MINGW
+ WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols 
+endif
+
+if USE_COVERAGE
+  AM_CFLAGS = --coverage -O0
+  XLIB = -lgcov
+endif
+
+pkgcfgdir= $(pkgdatadir)/config.d/
+
+libexecdir= $(pkglibdir)/libexec/
+
+pkgcfg_DATA = \
+  psycstore.conf
+
+
+lib_LTLIBRARIES = libgnunetpsycstore.la
+
+libgnunetpsycstore_la_SOURCES = \
+  psycstore_api.c \
+  psycstore.h
+libgnunetpsycstore_la_LIBADD = \
+  $(top_builddir)/src/util/libgnunetutil.la \
+  $(GN_LIBINTL) $(XLIB)
+libgnunetpsycstore_la_LDFLAGS = \
+  $(GN_LIB_LDFLAGS)  $(WINFLAGS) \
+  -version-info 0:0:0
+libgnunetpsycstore_la_DEPENDENCIES = \
+  $(top_builddir)/src/util/libgnunetutil.la 
+
+bin_PROGRAMS =
+
+libexec_PROGRAMS = \
+ gnunet-service-psycstore 
+
+gnunet_service_psycstore_SOURCES = \
+ gnunet-service-psycstore.c         
+gnunet_service_psycstore_LDADD = \
+  $(top_builddir)/src/statistics/libgnunetstatistics.la \
+  $(top_builddir)/src/util/libgnunetutil.la \
+  $(GN_LIBINTL)
+gnunet_service_psycstore_DEPENDENCIES = \
+  $(top_builddir)/src/statistics/libgnunetstatistics.la \
+  $(top_builddir)/src/util/libgnunetutil.la 
+
+if HAVE_TESTING
+check_PROGRAMS = \
+ test_psycstore
+endif
+
+if ENABLE_TEST_RUN
+TESTS = $(check_PROGRAMS) 
+endif
+
+test_psycstore_SOURCES = \
+ test_psycstore.c
+test_psycstore_LDADD = \
+  libgnunetpsycstore.la \
+  $(top_builddir)/src/testing/libgnunettesting.la \
+  $(top_builddir)/src/util/libgnunetutil.la 
+test_psycstore_DEPENDENCIES = \
+  libgnunetpsycstore.la \
+  $(top_builddir)/src/testing/libgnunettesting.la \
+  $(top_builddir)/src/util/libgnunetutil.la 
+
+EXTRA_DIST = \
+  test_psycstore.conf 
+
+

Added: gnunet/src/psycstore/gnunet-service-psycstore.c
===================================================================
--- gnunet/src/psycstore/gnunet-service-psycstore.c                             
(rev 0)
+++ gnunet/src/psycstore/gnunet-service-psycstore.c     2013-08-27 07:29:59 UTC 
(rev 28869)
@@ -0,0 +1,169 @@
+/*
+  This file is part of GNUnet.
+  (C) 2013 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 3, 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 psycstore/gnunet-service-psycstore.c
+ * @brief PSYCstore service
+ * @author Gabor X Toth
+ * @author Christian Grothoff
+ *
+ * The purpose of this service is to manage private keys that
+ * represent the various egos/pseudonyms/identities of a GNUnet user.
+ *
+ */
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_constants.h"
+#include "gnunet_protocols.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_psycstore_service.h"
+#include "psycstore.h"
+
+
+/**
+ * Handle to our current configuration.
+ */
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+/**
+ * Handle to the statistics service.
+ */
+static struct GNUNET_STATISTICS_Handle *stats;
+
+/**
+ * Notification context, simplifies client broadcasts.
+ */
+static struct GNUNET_SERVER_NotificationContext *nc;
+
+/**
+ * Database file.
+ */
+static char *db_file;
+
+
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (NULL != nc)
+  {
+    GNUNET_SERVER_notification_context_destroy (nc);
+    nc = NULL;
+  }
+  if (NULL != stats)
+  {
+    GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
+    stats = NULL;
+  }
+  GNUNET_free (db_file);
+  db_file = NULL;
+}
+
+
+/**
+ * Send a result code back to the client.
+ *
+ * @param client client that should receive the result code
+ * @param result_code code to transmit
+ * @param emsg error message to include (or NULL for none)
+ */
+static void
+send_result_code (struct GNUNET_SERVER_Client *client,
+                 uint32_t result_code,
+                 const char *emsg)
+{
+  struct GNUNET_PSYCSTORE_ResultCodeMessage *rcm;
+  size_t elen;
+
+  if (NULL == emsg)
+    elen = 0;
+  else
+    elen = strlen (emsg) + 1;
+  rcm = GNUNET_malloc (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) + 
elen);
+  rcm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
+  rcm->header.size = htons (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) 
+ elen);
+  rcm->result_code = htonl (result_code);
+  memcpy (&rcm[1], emsg, elen);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Sending result %d (%s) to client\n",
+             (int) result_code,
+             emsg);
+  GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, 
GNUNET_NO);
+  GNUNET_free (rcm);
+}
+
+
+/**
+ * Handle PSYCstore clients.
+ *
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
+ */
+static void
+run (void *cls, 
+     struct GNUNET_SERVER_Handle *server,
+     const struct GNUNET_CONFIGURATION_Handle *c)
+{
+  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
+    {NULL, NULL, 0, 0}
+  };
+
+  cfg = c;
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_filename (cfg, "psycstore",
+                                              "DB_FILE",
+                                              &db_file))
+  {
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "psycstore", 
"DB_FILE");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  stats = GNUNET_STATISTICS_create ("psycstore", cfg);
+  GNUNET_SERVER_add_handlers (server, handlers);
+  nc = GNUNET_SERVER_notification_context_create (server, 1);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+                                NULL);
+}
+
+
+/**
+ * The main function for the network size estimation service.
+ *
+ * @param argc number of arguments from the command line
+ * @param argv command line arguments
+ * @return 0 ok, 1 on error
+ */
+int
+main (int argc, char *const *argv)
+{
+  return (GNUNET_OK ==
+          GNUNET_SERVICE_run (argc, argv, "psycstore", 
+                             GNUNET_SERVICE_OPTION_NONE,
+                              &run, NULL)) ? 0 : 1;
+}
+
+
+/* end of gnunet-service-psycstore.c */

Added: gnunet/src/psycstore/psycstore.conf
===================================================================
--- gnunet/src/psycstore/psycstore.conf                         (rev 0)
+++ gnunet/src/psycstore/psycstore.conf 2013-08-27 07:29:59 UTC (rev 28869)
@@ -0,0 +1,10 @@
+[psycstore]
+AUTOSTART = YES
+HOME = $SERVICEHOME
+BINARY = gnunet-service-psycstore
+UNIXPATH = /tmp/gnunet-service-psycstore.unix
+UNIX_MATCH_UID = NO
+UNIX_MATCH_GID = YES
+
+# Path to the SQLite database
+DB_FILE = $SERVICEHOME/psycstore.sqlite

Added: gnunet/src/psycstore/psycstore.h
===================================================================
--- gnunet/src/psycstore/psycstore.h                            (rev 0)
+++ gnunet/src/psycstore/psycstore.h    2013-08-27 07:29:59 UTC (rev 28869)
@@ -0,0 +1,57 @@
+/*
+     This file is part of GNUnet.
+     (C) 2013 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 Liceidentity as published
+     by the Free Software Foundation; either version 3, 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 Liceidentity for more details.
+
+     You should have received a copy of the GNU General Public Liceidentity
+     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 psycstore/psycstore.h
+ * @brief Common type definitions for the PSYCstore service and API.
+ * @author Gabor X Toth
+ */
+
+#ifndef PSYCSTORE_H
+#define PSYCSTORE_H
+
+#include "gnunet_common.h"
+
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Answer from service to client about last operation.
+ */
+struct GNUNET_PSYCSTORE_ResultCodeMessage
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Status code for the last operation, in NBO.
+   * (currently not used).
+   */
+  uint32_t result_code GNUNET_PACKED;
+
+  /* followed by 0-terminated error message (on error) */
+
+};
+
+GNUNET_NETWORK_STRUCT_END
+
+#endif

Added: gnunet/src/psycstore/psycstore_api.c
===================================================================
--- gnunet/src/psycstore/psycstore_api.c                                (rev 0)
+++ gnunet/src/psycstore/psycstore_api.c        2013-08-27 07:29:59 UTC (rev 
28869)
@@ -0,0 +1,451 @@
+/*
+     This file is part of GNUnet.
+     (C) 2013 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 Liceidentity as published
+     by the Free Software Foundation; either version 3, 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 Liceidentity for more details.
+
+     You should have received a copy of the GNU General Public Liceidentity
+     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 psycstore/psycstore_api.c
+ * @brief API to interact with the PSYCstore service
+ * @author Gabor X Toth
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_constants.h"
+#include "gnunet_protocols.h"
+#include "gnunet_psycstore_service.h"
+#include "psycstore.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
+
+
+/** 
+ * Handle for an operation with the PSYCstore service.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle
+{
+
+  /**
+   * Main PSYCstore handle.
+   */
+  struct GNUNET_PSYCSTORE_Handle *h;
+  
+  /**
+   * We keep operations in a DLL.
+   */
+  struct GNUNET_PSYCSTORE_OperationHandle *next;
+
+  /**
+   * We keep operations in a DLL.
+   */
+  struct GNUNET_PSYCSTORE_OperationHandle *prev;
+
+  /**
+   * Message to send to the PSYCstore service.
+   * Allocated at the end of this struct.
+   */
+  const struct GNUNET_MessageHeader *msg;
+
+  /**
+   * Continuation to invoke with the result of an operation.
+   */
+  GNUNET_PSYCSTORE_ResultCallback res_cb;
+
+  /**
+   * Continuation to invoke with the result of an operation returning a 
fragment.
+   */
+  GNUNET_PSYCSTORE_FragmentCallback frag_cb;
+
+  /**
+   * Continuation to invoke with the result of an operation returning a state 
variable.
+   */
+  GNUNET_PSYCSTORE_StateCallback state_cb;
+
+  /**
+   * Closure for the callbacks.
+   */
+  void *cls;
+
+};
+
+
+/**
+ * Handle for the service.
+ */
+struct GNUNET_PSYCSTORE_Handle
+{
+  /**
+   * Configuration to use.
+   */
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+  /**
+   * Socket (if available).
+   */
+  struct GNUNET_CLIENT_Connection *client;
+
+  /**
+   * Head of active operations.
+   */ 
+  struct GNUNET_PSYCSTORE_OperationHandle *op_head;
+
+  /**
+   * Tail of active operations.
+   */ 
+  struct GNUNET_PSYCSTORE_OperationHandle *op_tail;
+
+  /**
+   * Currently pending transmission request, or NULL for none.
+   */
+  struct GNUNET_CLIENT_TransmitHandle *th;
+
+  /**
+   * Task doing exponential back-off trying to reconnect.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+
+  /**
+   * Time for next connect retry.
+   */
+  struct GNUNET_TIME_Relative reconnect_delay;
+
+  /**
+   * Are we polling for incoming messages right now?
+   */
+  int in_receive;
+
+};
+
+
+/**
+ * Try again to connect to the PSYCstore service.
+ *
+ * @param cls handle to the PSYCstore service.
+ * @param tc scheduler context
+ */
+static void
+reconnect (void *cls,
+          const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Reschedule a connect attempt to the service.
+ *
+ * @param h transport service to reconnect
+ */
+static void
+reschedule_connect (struct GNUNET_PSYCSTORE_Handle *h)
+{
+  GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
+
+  if (NULL != h->th)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+    h->th = NULL;
+  }
+  if (NULL != h->client)
+  {
+    GNUNET_CLIENT_disconnect (h->client);
+    h->client = NULL;
+  }
+  h->in_receive = GNUNET_NO;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Scheduling task to reconnect to PSYCstore service in %s.\n",
+       GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, 
GNUNET_YES));
+  h->reconnect_task =
+      GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
+  h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
+}
+
+
+/**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+message_handler (void *cls, 
+                const struct GNUNET_MessageHeader *msg)
+{
+  struct GNUNET_PSYCSTORE_Handle *h = cls;
+  struct GNUNET_PSYCSTORE_OperationHandle *op;
+  const struct GNUNET_PSYCSTORE_ResultCodeMessage *rcm;
+  const char *str;
+  uint16_t size;
+
+  if (NULL == msg)
+  {
+    reschedule_connect (h);
+    return;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received message of type %d from PSYCstore service\n",
+       ntohs (msg->type));
+  size = ntohs (msg->size);
+  switch (ntohs (msg->type))
+  {
+  case GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE:
+    if (size < sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage))
+    {
+      GNUNET_break (0);
+      reschedule_connect (h);
+      return;
+    }
+    rcm = (const struct GNUNET_PSYCSTORE_ResultCodeMessage *) msg;
+    str = (const char *) &rcm[1];
+    if ( (size > sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage)) &&
+        ('\0' != str[size - sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) 
- 1]) )
+    {
+      GNUNET_break (0);
+      reschedule_connect (h);
+      return;
+    }
+    if (size == sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage))
+      str = NULL;
+
+    op = h->op_head;
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                h->op_tail,
+                                op);
+    GNUNET_CLIENT_receive (h->client, &message_handler, h,
+                          GNUNET_TIME_UNIT_FOREVER_REL);
+    if (NULL != op->res_cb)
+      op->res_cb (op->cls, rcm->result_code , str);
+    GNUNET_free (op);
+    break;
+  default:
+    GNUNET_break (0);
+    reschedule_connect (h);
+    return;
+  }
+}
+
+
+/**
+ * Schedule transmission of the next message from our queue.
+ *
+ * @param h PSYCstore handle
+ */
+static void
+transmit_next (struct GNUNET_PSYCSTORE_Handle *h);
+
+
+/**
+ * Transmit next message to service.
+ *
+ * @param cls the 'struct GNUNET_PSYCSTORE_Handle'.
+ * @param size number of bytes available in buf
+ * @param buf where to copy the message
+ * @return number of bytes copied to buf
+ */
+static size_t
+send_next_message (void *cls, 
+                  size_t size, 
+                  void *buf)
+{
+  struct GNUNET_PSYCSTORE_Handle *h = cls;
+  struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head;
+  size_t ret;
+  
+  h->th = NULL;
+  if (NULL == op)
+    return 0;
+  ret = ntohs (op->msg->size);
+  if (ret > size)
+  {
+    reschedule_connect (h);
+    return 0;
+  }  
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Sending message of type %d to PSYCstore service\n",
+       ntohs (op->msg->type));
+  memcpy (buf, op->msg, ret);
+  if ( (NULL == op->res_cb) &&
+       (NULL == op->frag_cb) &&
+       (NULL == op->state_cb))
+  {
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                h->op_tail,
+                                op);
+    GNUNET_free (op);
+    transmit_next (h);
+  }
+  if (GNUNET_NO == h->in_receive)
+  {
+    h->in_receive = GNUNET_YES;
+    GNUNET_CLIENT_receive (h->client,
+                          &message_handler, h,
+                          GNUNET_TIME_UNIT_FOREVER_REL);
+  }
+  return ret;
+}
+
+
+/**
+ * Schedule transmission of the next message from our queue.
+ *
+ * @param h PSYCstore handle
+ */
+static void
+transmit_next (struct GNUNET_PSYCSTORE_Handle *h)
+{
+  struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head;
+
+  GNUNET_assert (NULL == h->th);
+  if (NULL == op)
+    return;
+  if (NULL == h->client)
+    return;
+  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
+                                              ntohs (op->msg->size),
+                                              GNUNET_TIME_UNIT_FOREVER_REL,
+                                              GNUNET_NO,
+                                              &send_next_message,
+                                              h);
+}
+
+
+/**
+ * Try again to connect to the PSYCstore service.
+ *
+ * @param cls the handle to the PSYCstore service
+ * @param tc scheduler context
+ */
+static void
+reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_PSYCSTORE_Handle *h = cls;
+
+  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Connecting to PSYCstore service.\n");
+  GNUNET_assert (NULL == h->client);
+  h->client = GNUNET_CLIENT_connect ("psycstore", h->cfg);
+  GNUNET_assert (NULL != h->client);
+/*
+  struct GNUNET_PSYCSTORE_OperationHandle *op;
+  struct GNUNET_MessageHeader msg;
+  op = GNUNET_malloc (sizeof (struct GNUNET_PSYCSTORE_OperationHandle) + 
+                     sizeof (struct GNUNET_MessageHeader));
+  op->h = h;
+  op->msg = (const struct GNUNET_MessageHeader *) &op[1];
+  msg.size = htons (sizeof (msg));
+  msg.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_START);
+  memcpy (&op[1], &msg, sizeof (msg));
+  GNUNET_CONTAINER_DLL_insert (h->op_head,
+                              h->op_tail,
+                              op);
+  transmit_next (h);
+  GNUNET_assert (NULL != h->th);
+*/
+}
+
+
+/**
+ * Connect to the PSYCstore service.
+ *
+ * @param cfg the configuration to use
+ * @return handle to use
+ */
+struct GNUNET_PSYCSTORE_Handle *
+GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  struct GNUNET_PSYCSTORE_Handle *h;
+
+  h = GNUNET_new (struct GNUNET_PSYCSTORE_Handle);
+  h->cfg = cfg;
+  h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
+  h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
+  return h;
+}
+
+
+/**
+ * Cancel a PSYCstore operation. Note that the operation MAY still
+ * be executed; this merely cancels the continuation; if the request
+ * was already transmitted, the service may still choose to complete
+ * the operation.
+ *
+ * @param op operation to cancel
+ */
+void
+GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op)
+{
+  struct GNUNET_PSYCSTORE_Handle *h = op->h;
+
+  if ( (h->op_head != op) ||
+       (NULL == h->client) )
+  {
+    /* request not active, can simply remove */
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                h->op_tail,
+                                op);
+    GNUNET_free (op);
+    return;
+  }
+  if (NULL != h->th)
+  {
+    /* request active but not yet with service, can still abort */
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+    h->th = NULL;
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                h->op_tail,
+                                op);
+    GNUNET_free (op);
+    transmit_next (h);
+    return;
+  }
+  /* request active with service, simply ensure continuations are not called */
+  op->res_cb = NULL;
+  op->frag_cb = NULL;
+  op->state_cb = NULL;
+}
+
+
+/**
+ * Disconnect from PSYCstore service
+ *
+ * @param h handle to destroy
+ */
+void
+GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h)
+{
+  GNUNET_assert (NULL != h);
+  GNUNET_assert (h->op_head == h->op_tail);
+  if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+  {
+    GNUNET_SCHEDULER_cancel (h->reconnect_task);
+    h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (NULL != h->th)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+    h->th = NULL;
+  }
+  if (NULL != h->client)
+  {
+    GNUNET_CLIENT_disconnect (h->client);
+    h->client = NULL;
+  }
+  GNUNET_free (h);
+}
+
+/* end of psycstore_api.c */

Added: gnunet/src/psycstore/test_psycstore.c
===================================================================
--- gnunet/src/psycstore/test_psycstore.c                               (rev 0)
+++ gnunet/src/psycstore/test_psycstore.c       2013-08-27 07:29:59 UTC (rev 
28869)
@@ -0,0 +1,158 @@
+/*
+     This file is part of GNUnet.
+     (C) 2013 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 3, 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 psycstore/test_psycstore.c
+ * @brief Testcase for the PSYCstore service
+ * @author Gabor X Toth
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include "gnunet_common.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_psycstore_service.h"
+#include "gnunet_testing_lib.h"
+
+
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
+
+
+/**
+ * Return value from 'main'.
+ */
+static int res;
+
+/**
+ * Handle to PSYCstore service.
+ */
+static struct GNUNET_PSYCSTORE_Handle *h;
+
+/**
+ * Handle to PSYCstore operation.
+ */
+static struct GNUNET_PSYCSTORE_Operation *op;
+
+/**
+ * Handle for task for timeout termination.
+ */ 
+static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
+
+
+/**
+ * Clean up all resources used.
+ */
+static void
+cleanup ()
+{
+  if (NULL != op)
+  {
+    GNUNET_PSYCSTORE_operation_cancel (op);
+    op = NULL;
+  }
+  if (NULL != h)
+  {
+    GNUNET_PSYCSTORE_disconnect (h);
+    h = NULL;
+  }
+  GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
+ * Termiante the testcase (failure).
+ *
+ * @param cls NULL
+ * @param tc scheduler context
+ */
+static void
+endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  cleanup ();
+  res = 1;
+}
+
+
+/**
+ * Termiante the testcase (success).
+ *
+ * @param cls NULL
+ * @param tc scheduler context
+ */
+static void
+end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  cleanup ();
+  res = 0;
+}
+
+
+/**
+ * Finish the testcase (successfully).
+ */
+static void 
+end ()
+{
+  if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
+  {
+    GNUNET_SCHEDULER_cancel (endbadly_task);
+    endbadly_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
+                               &end_normally, NULL);
+}
+
+
+/**
+ * Main function of the test, run from scheduler.
+ *
+ * @param cls NULL
+ * @param cfg configuration we use (also to connect to PSYCstore service)
+ * @param peer handle to access more of the peer (not used)
+ */
+static void
+run (void *cls, 
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_TESTING_Peer *peer)
+{
+  endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, 
+                                               &endbadly, NULL); 
+  h = GNUNET_PSYCSTORE_connect (cfg);
+  GNUNET_assert (NULL != h);
+  end ();
+}
+
+
+int
+main (int argc, char *argv[])
+{
+  res = 1;
+  if (0 != 
+      GNUNET_TESTING_service_run ("test-psycstore",
+                                 "psycstore",
+                                 "test_psycstore.conf",
+                                 &run,
+                                 NULL))
+    return 1;
+  return res;
+}
+
+
+/* end of test_psycstore.c */

Added: gnunet/src/psycstore/test_psycstore.conf
===================================================================
--- gnunet/src/psycstore/test_psycstore.conf                            (rev 0)
+++ gnunet/src/psycstore/test_psycstore.conf    2013-08-27 07:29:59 UTC (rev 
28869)
@@ -0,0 +1,6 @@
+[arm]
+DEFAULTSERVICES = psycstore
+UNIXPATH = /tmp/test-psycstore-service-arm.sock
+
+[psycstore]
+DBFILE = /tmp/test-psycstore-service.sqlite




reply via email to

[Prev in Thread] Current Thread [Next in Thread]