clean up for configs
[oweals/gnunet.git] / src / mesh / gnunet-mesh.c
index a771f69484dd376681c4227a90ea14d71299c2dd..fa19a09e951a448bef6c21d9da19be94e952aa60 100644 (file)
@@ -68,6 +68,10 @@ static char *target_id;
  */
 static uint32_t target_port;
 
+/**
+ * Data pending in netcat mode.
+ */
+size_t data_size;
 
 
 /**
@@ -75,6 +79,9 @@ static uint32_t target_port;
  */
 static struct GNUNET_MESH_Handle *mh;
 
+/**
+ * Channel handle.
+ */
 static struct GNUNET_MESH_Channel *ch;
 
 /**
@@ -82,6 +89,13 @@ static struct GNUNET_MESH_Channel *ch;
  */
 GNUNET_SCHEDULER_TaskIdentifier sd;
 
+
+
+static void
+listen_stdio (void);
+
+
+
 /**
  * Task run in monitor mode when the user presses CTRL-C to abort.
  * Stops monitoring activity.
@@ -106,6 +120,96 @@ shutdown_task (void *cls,
 }
 
 
+/**
+ * Function called to notify a client about the connection
+ * begin ready to queue more data.  "buf" will be
+ * NULL and "size" zero if the connection was closed for
+ * writing in the meantime.
+ *
+ * FIXME
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+size_t
+data_ready (void *cls, size_t size, void *buf)
+{
+  struct GNUNET_MessageHeader *msg;
+  size_t total_size;
+
+  if (NULL == buf || 0 == size)
+  {
+    GNUNET_SCHEDULER_shutdown();
+    return 0;
+  }
+
+  total_size = data_size + sizeof (struct GNUNET_MessageHeader);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
+  GNUNET_assert (size >= total_size);
+
+  msg = buf;
+  msg->size = ntohs (total_size);
+  msg->type = ntohs (GNUNET_MESSAGE_TYPE_MESH_CLI);
+  memcpy (&msg[1], cls, data_size);
+  listen_stdio ();
+
+  return total_size;
+}
+
+
+/**
+ * Task run in monitor mode when the user presses CTRL-C to abort.
+ * Stops monitoring activity.
+ *
+ * @param cls Closure (unused).
+ * @param tc scheduler context
+ */
+static void
+read_stdio (void *cls,
+            const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  char buf[60000];
+
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    return;
+  }
+
+  data_size = read (0, buf, 60000);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
+  if (data_size < 1)
+  {
+    GNUNET_SCHEDULER_shutdown();
+    return;
+  }
+  GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
+                                     GNUNET_TIME_UNIT_FOREVER_REL,
+                                     data_size
+                                     + sizeof (struct GNUNET_MessageHeader),
+                                     &data_ready, buf);
+}
+
+
+/**
+ * Start listening to stdin
+ */
+static void
+listen_stdio (void)
+{
+  struct GNUNET_NETWORK_FDSet *rs;
+
+  rs = GNUNET_NETWORK_fdset_create ();
+  GNUNET_NETWORK_fdset_set_native (rs, 0);
+  GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                               GNUNET_TIME_UNIT_FOREVER_REL,
+                               rs, NULL,
+                               &read_stdio, NULL);
+  GNUNET_NETWORK_fdset_destroy (rs);
+}
+
+
 /**
  * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
@@ -147,13 +251,15 @@ channel_ended (void *cls,
  * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
-void *
+static void *
 channel_incoming (void *cls,
                   struct GNUNET_MESH_Channel * channel,
                   const struct GNUNET_PeerIdentity * initiator,
-                  uint32_t port, enum MeshOption options)
+                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Incoming channel %p on port %u\n", channel, port);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Incoming channel %p on port %u\n",
+              channel, port);
   if (NULL != ch)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
@@ -165,6 +271,7 @@ channel_incoming (void *cls,
     return NULL;
   }
   ch = channel;
+  listen_stdio ();
   return NULL;
 }
 
@@ -197,6 +304,7 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
   ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port,
                                    GNUNET_MESH_OPTION_DEFAULT);
+  listen_stdio ();
 }
 
 
@@ -214,7 +322,7 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return #GNUNET_OK to keep the channel open,
  *         #GNUNET_SYSERR to close it (signal serious error).
  */
-int
+static int
 data_callback (void *cls,
                struct GNUNET_MESH_Channel *channel,
                void **channel_ctx,
@@ -224,6 +332,7 @@ data_callback (void *cls,
   GNUNET_break (ch == channel);
 
   len = ntohs (message->size) - sizeof (*message);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
   FPRINTF (stdout, "%.*s", len, (char *) &message[1]);
   return GNUNET_OK;
 }
@@ -444,6 +553,7 @@ int
 main (int argc, char *const *argv)
 {
   int res;
+  const char helpstr[] = "Create channels and retreive info about meshs status.";
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
      gettext_noop ("provide information about a particular channel"),
@@ -470,9 +580,8 @@ main (int argc, char *const *argv)
     return 2;
 
   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
-                      gettext_noop
-                      ("Create channels and retreive info about meshs status."),
-                      options, &run, NULL);
+                            gettext_noop (helpstr),
+                            options, &run, NULL);
 
   GNUNET_free ((void *) argv);