- fix ACK direction
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_local.c
index 6ee23e6e0140c84cd958fb156e888fa11b897d3f..9b868124a1d96b3f2d5c4dfbafd9151d2ed6386d 100644 (file)
@@ -179,6 +179,7 @@ handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
 {
   struct MeshClient *c;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
   if (NULL == client)
     return;
   c = GNUNET_new (struct MeshClient);
@@ -191,6 +192,31 @@ handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
 }
 
 
+/**
+ * Iterator for deleting each channel whose client endpoint disconnected.
+ *
+ * @param cls Closure (client that has disconnected).
+ * @param key The local channel id (used to access the hashmap).
+ * @param value The value stored at the key (channel to destroy).
+ *
+ * @return GNUNET_OK, keep iterating.
+ */
+static int
+channel_destroy_iterator (void *cls,
+                          uint32_t key,
+                          void *value)
+{
+  struct MeshChannel *ch = value;
+  struct MeshClient *c = cls;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+              " Channel %s destroy, due to client %s shutdown.\n",
+              GMCH_2s (ch), GML_2s (c));
+
+  GMCH_handle_local_destroy (ch, c);
+  return GNUNET_OK;
+}
+
 /**
  * Handler for client disconnection
  *
@@ -210,7 +236,7 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     return;
   }
 
-  c = client_get (client);
+  c = GML_client_get (client);
   if (NULL != c)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
@@ -328,17 +354,12 @@ static void
 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
                        const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_MESH_ChannelMessage *msg;
-  struct MeshPeer *peer;
-  struct MeshTunnel2 *t;
-  struct MeshChannel *ch;
   struct MeshClient *c;
-  MESH_ChannelNumber chid;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
 
   /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
+  if (NULL == (c = GML_client_get (client)))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -354,66 +375,13 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  msg = (struct GNUNET_MESH_ChannelMessage *) message;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
-              GNUNET_i2s (&msg->peer), ntohl (msg->port));
-  chid = ntohl (msg->channel_id);
-
-  /* Sanity check for duplicate channel IDs */
-  if (NULL != channel_get_by_local_id (c, chid))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  peer = peer_get (&msg->peer);
-  if (NULL == peer->tunnel)
-  {
-    peer->tunnel = tunnel_new ();
-    peer->tunnel->peer = peer;
-    if (peer->id == myid)
-    {
-      tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
-    }
-    else
-    {
-      peer_connect (peer);
-    }
-  }
-  t = peer->tunnel;
-
-  /* Create channel */
-  ch = channel_new (t, c, chid);
-  if (NULL == ch)
+  if (GNUNET_OK !=
+      GMCH_handle_local_create (c,
+                                (struct GNUNET_MESH_ChannelMessage *) message))
   {
-    GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  ch->port = ntohl (msg->port);
-  channel_set_options (ch, ntohl (msg->opt));
-
-  /* In unreliable channels, we'll use the DLL to buffer BCK data */
-  ch->root_rel = GNUNET_new (struct MeshChannelReliability);
-  ch->root_rel->ch = ch;
-  ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
-              peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
-
-  /* Send create channel */
-  {
-    struct GNUNET_MESH_ChannelCreate msgcc;
-
-    msgcc.header.size = htons (sizeof (msgcc));
-    msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
-    msgcc.chid = htonl (ch->gid);
-    msgcc.port = msg->port;
-    msgcc.opt = msg->opt;
-
-    GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
-  }
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   return;
@@ -469,7 +437,7 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  GMCH_handle_local_destroy (ch, c, chid);
+  GMCH_handle_local_destroy (ch, c);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   return;
@@ -587,8 +555,8 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  /* If client is root, the ACK is going FWD, therefore this is "BCK". */
-  /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
+  /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
+  /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
   fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
 
   GMCH_handle_local_ack (ch, fwd);
@@ -758,6 +726,7 @@ static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
 void
 GML_init (struct GNUNET_SERVER_Handle *handle)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
   server_handle = handle;
   GNUNET_SERVER_suspend (server_handle);
   ports = GNUNET_CONTAINER_multihashmap32_create (32);