social cli
authorGabor X Toth <*@tg-x.net>
Thu, 5 May 2016 13:02:18 +0000 (13:02 +0000)
committerGabor X Toth <*@tg-x.net>
Thu, 5 May 2016 13:02:18 +0000 (13:02 +0000)
src/social/Makefile.am
src/social/gnunet-social.c
src/social/social_api.c

index 2de7226e13c669181002c7ce8b49ab14f4ce81ce..b0749e9e6bea467467e6b92c15a230816c5c2dbc 100644 (file)
@@ -40,6 +40,7 @@ gnunet_social_SOURCES = \
   gnunet-social.c
 gnunet_social_LDADD = \
   libgnunetsocial.la \
+  $(top_builddir)/src/core/libgnunetcore.la \
   $(top_builddir)/src/util/libgnunetutil.la
 
 gnunet_service_social_SOURCES = \
index e0b5273acfa670a6e8cba65672e764d452219691..b59aab77ca97806607d1e5638c200a2c34fbade2 100644 (file)
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_social_service.h"
+#include "gnunet_core_service.h"
 
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
+#define DATA2ARG(data) data, sizeof (data)
+
 /* operations corresponding to API calls */
 
 /** --host-enter */
@@ -76,6 +79,9 @@ static char *opt_place;
 /** --ego */
 static char *opt_ego;
 
+/** --peer */
+static char *opt_peer;
+
 /** --follow */
 static int opt_follow;
 
@@ -130,13 +136,17 @@ struct GNUNET_SOCIAL_Guest *gst;
 struct GNUNET_SOCIAL_Place *plc;
 
 
+/* DISCONNECT */
+
+
 static void
-cleanup ()
+disconnect ()
 {
-
+  GNUNET_SOCIAL_app_disconnect (app);
+  GNUNET_CORE_disconnect (core);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
-
 /**
  * Terminate the test case (failure).
  *
@@ -145,75 +155,216 @@ cleanup ()
 static void
 timeout (void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
-  cleanup ();
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout.\n");
+  disconnect ();
+}
+
+static void
+schedule_end (void *cls)
+{
+  ret = 0;
+  disconnect ();
 }
 
 
 static void
-host_leave (struct GNUNET_SOCIAL_Host *host)
+end ()
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The end.\n");
 
+  if (timeout_task != NULL)
+  {
+    GNUNET_SCHEDULER_cancel (timeout_task);
+    timeout_task = NULL;
+  }
+  GNUNET_SCHEDULER_add_now (&schedule_end, NULL);
 }
 
 
+/* LEAVE */
+
+
 static void
-host_announce (struct GNUNET_SOCIAL_Host *host,
-               const char *method,
-               const char *data)
+host_left ()
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "The host has left the place.\n");
+  end ();
+}
+
 
+static void
+host_leave ()
+{
+  GNUNET_SOCIAL_host_leave (hst, NULL, &host_left, NULL);
+  hst = NULL;
+  plc = NULL;
 }
 
 
 static void
-guest_leave (struct GNUNET_SOCIAL_Guest *guest)
+guest_left (void *cls)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "The guest has left the place.\n");
+}
+
 
+static void
+guest_leave ()
+{
+  struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
+  GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
+                       "_notice_place_leave", DATA2ARG ("Leaving."));
+  GNUNET_SOCIAL_guest_leave (gst, env, &guest_left, NULL);
+  GNUNET_PSYC_env_destroy (env);
+  gst = NULL;
+  plc = NULL;
+}
+
+
+/* ANNOUNCE / TALK */
+
+
+struct TransmitClosure
+{
+  const char *data;
+  size_t size;
+} tmit;
+
+
+static int
+notify_data (void *cls, uint16_t *data_size, void *data)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transmit notify data: %u bytes available\n",
+              *data_size);
+
+  struct TransmitClosure *tmit = cls;
+  uint16_t size = tmit->size < *data_size ? tmit->size : *data_size;
+  *data_size = size;
+  memcpy (data, tmit->data, size);
+
+  tmit->size -= size;
+  tmit->data += size;
+
+  return 0 == tmit->size ? GNUNET_NO : GNUNET_YES;
 }
 
 
 static void
-guest_talk (struct GNUNET_SOCIAL_Guest *guest,
-            const char *method,
-            const char *data)
+host_announce (const char *method, const char *data, size_t data_size)
 {
+  struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
+  GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
+                       "_foo", DATA2ARG ("bar baz"));
+
+  tmit = (struct TransmitClosure) {};
+  tmit.data = data;
+  tmit.size = data_size;
 
+  GNUNET_SOCIAL_host_announce (hst, method, env,
+                               &notify_data, &tmit,
+                               GNUNET_SOCIAL_ANNOUNCE_NONE);
 }
 
 
 static void
-history_replay (struct GNUNET_SOCIAL_Place *place,
-                uint64_t start, uint64_t end, const char *prefix)
+guest_talk (const char *method,
+            const char *data, size_t data_size)
 {
+  struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
+  GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
+                       "_foo", DATA2ARG ("bar baz"));
+
+  tmit = (struct TransmitClosure) {};
+  tmit.data = data;
+  tmit.size = data_size;
 
+  GNUNET_SOCIAL_guest_talk (gst, method, env,
+                            &notify_data, &tmit,
+                            GNUNET_SOCIAL_TALK_NONE);
 }
 
 
+/* HISTORY REPLAY */
+
+
 static void
-history_replay_latest (struct GNUNET_SOCIAL_Place *place,
-                       uint64_t limit, const char *prefix)
+recv_history_replay_result (void *cls, int64_t result,
+                            const void *data, uint16_t data_size)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Guest received history replay result: %" PRId64 "\n"
+              "%.*s\n",
+              result, data_size, (const char *) data);
+}
 
+
+static void
+history_replay (uint64_t start, uint64_t end, const char *prefix)
+{
+  GNUNET_SOCIAL_place_history_replay (plc, start, end, prefix,
+                                      GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
+                                      slicer,
+                                      &recv_history_replay_result,
+                                      NULL);
 }
 
 
 static void
-look_at (struct GNUNET_SOCIAL_Place *place,
-         const char *name)
+history_replay_latest (uint64_t limit, const char *prefix)
+{
+  GNUNET_SOCIAL_place_history_replay_latest (plc, limit, prefix,
+                                             GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
+                                             slicer,
+                                             &recv_history_replay_result,
+                                             NULL);
+}
+
+
+/* LOOK AT/FOR */
+
+
+static void
+look_result (void *cls, int64_t result_code,
+             const void *data, uint16_t data_size)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "look_result: %" PRId64 "\n", result_code);
 
 }
 
 
 static void
-look_for (struct GNUNET_SOCIAL_Place *place,
-          const char *name)
+look_var (void *cls,
+          const struct GNUNET_MessageHeader *mod,
+          const char *name,
+          const void *value,
+          uint32_t value_size,
+          uint32_t full_value_size)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "guest_look_at_var: %s\n%.*s\n",
+              name, value_size, (const char *) value);
+}
+
 
+static void
+look_at (const char *name)
+{
+  GNUNET_SOCIAL_place_look_at (plc, name, look_var, look_result, NULL);
 }
 
-/* SLICER + CALLBACKS */
+
+static void
+look_for (const char *name)
+{
+  GNUNET_SOCIAL_place_look_for (plc, name, look_var, look_result, NULL);
+}
+
+
+/* SLICER */
 
 
 static void
@@ -244,7 +395,7 @@ slicer_recv_modifier (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Received modifier for message ID %" PRIu64 ":\n"
               "%c%s: %.*s (size: %u)\n",
-              message_id, oper, name, value_size, value, value_size);
+              message_id, oper, name, value_size, (const char *) value, value_size);
 }
 
 
@@ -259,7 +410,7 @@ slicer_recv_data (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Received data for message ID %" PRIu64 ":\n"
               "%.*s\n",
-              message_id, data_size, data);
+              message_id, data_size, (const char *) data);
 }
 
 
@@ -290,7 +441,7 @@ slicer_create ()
 }
 
 
-/* GUEST ENTER + CALLBACKS */
+/* GUEST ENTER */
 
 
 static void
@@ -315,7 +466,7 @@ guest_recv_entry_decision (void *cls,
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s\n%.*s\n",
-                method_name, data_size, data);
+                method_name, data_size, (const char *) data);
   }
 }
 
@@ -338,7 +489,7 @@ guest_enter_msg_create ()
   const char *method_name = "_request_enter";
   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
-                       "_foo", "bar", sizeof ("bar"));
+                       "_foo", DATA2ARG ("bar"));
   void *data = "let me in";
   uint16_t data_size = strlen (data) + 1;
 
@@ -347,8 +498,7 @@ guest_enter_msg_create ()
 
 
 static void
-guest_enter (const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
-             struct GNUNET_PeerIdentity *peer)
+guest_enter (struct GNUNET_PeerIdentity *peer)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Entering to place as guest.\n");
@@ -377,8 +527,7 @@ guest_enter_by_name (const char *gns_name)
 }
 
 
-
-/* HOST ENTER + CALLBACKS */
+/* HOST ENTER */
 
 
 static void
@@ -442,22 +591,23 @@ host_enter ()
 }
 
 
-/* RECONNECT CALLBACKS */
+/* PLACE RECONNECT */
+
 
 static void
 place_reconnected ()
 {
   if (op_history_replay) {
-    history_replay (plc, opt_start, opt_end, opt_method);
+    history_replay (opt_start, opt_end, opt_method);
   }
   else if (op_history_replay_latest) {
-    history_replay_latest (plc, opt_limit, opt_method);
+    history_replay_latest (opt_limit, opt_method);
   }
   else if (op_look_at) {
-    look_at (plc, opt_name);
+    look_at (opt_name);
   }
   else if (op_look_for) {
-    look_for (plc, opt_name);
+    look_for (opt_name);
   }
 }
 
@@ -471,10 +621,10 @@ host_reconnected (void *cls, int result,
               "Host reconnected\n");
 
   if (op_host_leave) {
-    host_leave (hst);
+    host_leave ();
   }
   else if (op_host_announce) {
-    host_announce (hst, opt_method, opt_data);
+    host_announce (opt_method, opt_data, strlen (opt_data));
   }
   else {
     place_reconnected ();
@@ -491,10 +641,10 @@ guest_reconnected (void *cls, int result,
               "Guest reconnected\n");
 
   if (op_guest_leave) {
-    guest_leave (gst);
+    guest_leave ();
   }
   else if (op_guest_talk) {
-    guest_talk (gst, opt_method, opt_data);
+    guest_talk (opt_method, opt_data, strlen (opt_data));
   }
   else {
     place_reconnected ();
@@ -502,7 +652,7 @@ guest_reconnected (void *cls, int result,
 }
 
 
-/* APP CALLBACKS */
+/* APP */
 
 
 static void
@@ -515,7 +665,8 @@ app_connected (void *cls)
     host_enter ();
   }
   else if (op_guest_enter) {
-    guest_enter (&place_pub_key);
+    guest_enter (&peer);
+    // FIXME: guest_enter_by_name
   }
 }
 
@@ -530,7 +681,7 @@ app_recv_host (void *cls,
   struct GNUNET_HashCode host_pub_hash;
   GNUNET_CRYPTO_hash (host_pub_key, sizeof (*host_pub_key), &host_pub_hash);
   char *
-    host_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (guest_pub_key);
+    host_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (host_pub_key);
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Host: %s (%s)\n",
@@ -556,7 +707,7 @@ app_recv_guest (void *cls,
   struct GNUNET_HashCode guest_pub_hash;
   GNUNET_CRYPTO_hash (guest_pub_key, sizeof (*guest_pub_key), &guest_pub_hash);
   char *
-    guest_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (guest_pub_key);
+    guest_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (guest_pub_key);
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Guest: %s (%s)\n",
@@ -595,7 +746,8 @@ app_connect ()
                                    NULL);
 }
 
-/* CORE CALLBACKS */
+
+/* CORE */
 
 
 static void
@@ -606,6 +758,9 @@ core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
 }
 
 
+/* RUN */
+
+
 /**
  * Main function run by the scheduler.
  *
@@ -628,7 +783,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   if (op_host_enter && NULL != opt_place)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _ ("--place must not be specified when using --host-enter\n"));
+                _("--place must not be specified when using --host-enter\n"));
     return;
   }
 
@@ -638,7 +793,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                                                                   &place_pub_key))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _ ("--place missing or invalid.\n"));
+                _("--place missing or invalid.\n"));
     return;
   }
 
@@ -674,77 +829,81 @@ main (int argc, char *const *argv)
     /* operations */
 
     { 'E', "host-enter", NULL,
-      _ ("create a place for nyms to join"),
+      gettext_noop ("create a place for nyms to join"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_enter },
 
     { 'L', "host-leave", NULL,
-      _ ("destroy a place we were hosting"),
+      gettext_noop ("destroy a place we were hosting"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_leave },
 
     { 'A', "host-announce", NULL,
-      _ ("publish something to a place we are hosting"),
+      gettext_noop ("publish something to a place we are hosting"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_announce },
 
     { 'e', "guest-enter", NULL,
-      _ ("join somebody else's place"),
+      gettext_noop ("join somebody else's place"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_enter },
 
     { 'l', "guest-leave", NULL,
-      _ ("leave somebody else's place"),
+      gettext_noop ("leave somebody else's place"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_leave },
 
     { 't', "guest-talk", NULL,
-      _ ("submit something to somebody's place"),
+      gettext_noop ("submit something to somebody's place"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_talk },
 
     { 'R', "history-replay", NULL,
-      _ ("replay history of messages between message IDs --start and --end"),
+      gettext_noop ("replay history of messages between message IDs --start and --end"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_history_replay },
 
     { 'r', "history-replay-latest", NULL,
-      _ ("replay history of latest messages up to the given --limit"),
+      gettext_noop ("replay history of latest messages up to the given --limit"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_history_replay_latest },
 
     /* options */
 
     { 'A', "app", "application ID",
-      _ ("application ID to use when connecting"),
+      gettext_noop ("application ID to use when connecting"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_app },
 
     { 'p', "place", "PUBKEY",
-      _ ("public key of place"),
+      gettext_noop ("public key of place"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_place },
 
+    { 'P', "peer", "PEER_ID",
+      gettext_noop ("peer ID for --guest-enter"),
+      GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_peer },
+
     { 'g', "ego", "PUBKEY",
-      _ ("public key of ego"),
+      gettext_noop ("public key of ego"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_place },
 
     { 'f', "follow", NULL,
-      _ ("wait for incoming messages"),
+      gettext_noop ("wait for incoming messages"),
       GNUNET_NO, &GNUNET_GETOPT_set_one, &opt_follow },
 
     { 'm', "method", "METHOD_NAME",
-      _ ("method name"),
+      gettext_noop ("method name"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_method },
 
     { 'd', "data", "DATA",
-      _ ("message body to transmit"),
+      gettext_noop ("message body to transmit"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_data },
 
     { 'n', "name", "VAR_NAME",
-      _ ("state var name to query"),
+      gettext_noop ("state var name to query"),
       GNUNET_NO, &GNUNET_GETOPT_set_string, &opt_name },
 
     { 'a', "start", NULL,
-      _ ("start message ID for history replay"),
+      gettext_noop ("start message ID for history replay"),
       GNUNET_NO, &GNUNET_GETOPT_set_ulong, &opt_start },
 
     { 'z', "end", NULL,
-      _ ("end message ID for history replay"),
+      gettext_noop ("end message ID for history replay"),
       GNUNET_NO, &GNUNET_GETOPT_set_ulong, &opt_end },
 
     { 'n', "limit", NULL,
-      _ ("number of messages to replay from history"),
+      gettext_noop ("number of messages to replay from history"),
       GNUNET_NO, &GNUNET_GETOPT_set_ulong, &opt_limit },
 
     GNUNET_GETOPT_OPTION_END
@@ -754,7 +913,7 @@ main (int argc, char *const *argv)
     return 2;
 
   const char *help =
-    _ ("interact with the social service: enter/leave, send/receive messages, access history and state")m;
+    _ ("interact with the social service: enter/leave, send/receive messages, access history and state");
   const char *usage =
     "gnunet-social --host-enter --ego <name or pubkey> [--listen]\n"
     "gnunet-social --host-leave --place <pubkey>\n"
index d2428893df680fbd261030ef9a81189a4a31a406..77ea3ec0d54c8c80d55f15f84c042fc45037b2fa 100644 (file)
@@ -432,7 +432,7 @@ host_recv_notice_place_leave_modifier (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
               "%c%s: %.*s\n",
-              message_id, oper, name, value_size, value);
+              message_id, oper, name, value_size, (const char *) value);
 
   /* skip _nym, it's added later in eom() */
   if (0 == memcmp (name, "_nym", sizeof ("_nym"))