cleaning up set handlers, eliminating 2nd level demultiplexing and improving use...
[oweals/gnunet.git] / src / include / gnunet_cadet_service.h
index 8d10c3d8d5ab4048399acce06cab41e6dfd80ee1..fd838df8d63d998911901478f8c22754c50fd532 100644 (file)
@@ -456,7 +456,7 @@ typedef void
                         int tunnel,
                         int neighbor,
                         unsigned int n_paths,
-                        struct GNUNET_PeerIdentity *paths);
+                        const struct GNUNET_PeerIdentity *paths);
 
 
 /**
@@ -697,152 +697,133 @@ GC_u2h (uint32_t port);
 /******************************************************************************/
 
 /**
- * Function called after #GNUNET_CADET_connecT has succeeded (or failed
- * for good). Implementations of this function must not call
- * #GNUNET_CADET_disconnecT (other than by scheduling a new task to
- * do this later).
+ * Method called whenever a peer connects to a port in MQ-based CADET.
  *
- * @param cls closure
- * @param connected #GNUNET_YES if successfully connected, #GNUNET_NO otherwise.
- */
-typedef void
-(*GNUNET_CADET_StartupCallback) (void *cls, int connected);
-
-
-/**
- * Method called whenever a given peer connects in mq-based CADET.
- *
- * @param cls Closure given to @a GNUNET_CADET_connecT.
+ * @param cls Closure from #GNUNET_CADET_open_porT.
  * @param channel New handle to the channel.
- * @param channel_cls Closure given to @a GNUNET_CADET_open_porT.
- *        NOTE: do we need two cls? I'd get rid of this one.
- * @param peer Peer that started this channel.
- *
- * NOTE: to keep symmetry between incoming and outgoing channels, this call
- *       does not provide the *mq, since we cannot cleanly return an mq
- *       from @a GNUNET_CADET_channel_create.
- *       The client must always call @a GNUNET_CADET_get_mq to the *mq
- *       Alternatively, we can provide the mq here and add and out **mq
- *       to @a GNUNET_CADET_channel_create
- *
- * @return initial channel context for the channel
- *         (can be NULL -- that's not an error)
+ * @param source Peer that started this channel.
+ * @return Closure for the incoming @a channel. It's given to:
+ *         - The #GNUNET_CADET_DisconnectEventHandler (given to
+ *           #GNUNET_CADET_open_porT) when the channel dies.
+ *         - Each the #GNUNET_MQ_MessageCallback handlers for each message
+ *           received on the @a channel.
  */
 typedef void *
 (*GNUNET_CADET_ConnectEventHandler) (void *cls,
                                      struct GNUNET_CADET_Channel *channel,
-                                     const struct GNUNET_PeerIdentity *peer);
+                                     const struct GNUNET_PeerIdentity *source);
+
 
 /**
- * Function called whenever an mq-channel is destroyed.  Should clean up
- * any associated state, including cancelling any pending transmission on this
- * channel.
+ * Function called whenever an MQ-channel is destroyed, even if the destruction
+ * was requested by #GNUNET_CADET_channel_destroy.
+ * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
  *
- * It must NOT call @a GNUNET_CADET_channel_destroy on the channel.
+ * It should clean up any associated state, including cancelling any pending
+ * transmission on this channel.
  *
- * @param cls Closure (set from @a GNUNET_CADET_connecT).
+ * @param cls Channel closure.
  * @param channel Connection to the other end (henceforth invalid).
- * @param channel_ctx Context (set from @a GNUNET_CADET_ConnectEventHandler).
  */
 typedef void
-(GNUNET_CADET_DisconnectEventHandler) (void *cls,
-                                       const struct GNUNET_CADET_Channel *channel,
-                                       void *channel_ctx);
+(*GNUNET_CADET_DisconnectEventHandler) (void *cls,
+                                        const struct GNUNET_CADET_Channel *channel);
+
 
 /**
- * Connect to the mq-based cadet service.
+ * Function called whenever an MQ-channel's transmission window size changes.
  *
- * NOTE: it would be more elegant to provide a separate @a handlers and
- *       @a disconnects for each port, giving them to @a GNUNET_CADET_open_porT,
- *       but how do we handle *incoming* channels?
+ * The first callback in an outgoing channel will be with a non-zero value
+ * and will mean the channel is connected to the destination.
  *
- * @param cfg Configuration to use.
- * @param cls Closure for the various callbacks that follow (including
- *            handlers in the handlers array).
- * @param init callback to call once we have successfully connected
- *             to the cadet service
- * @param disconnects Function called when a channel is destroyed.
- *                    It is called immediately if the channel is destroyed by
- *                    calling @a GNUNET_CADET_channel_destroy.
- * @param handlers Callbacks for messages we care about, NULL-terminated.
- *                 Messages of a type that is not in the handlers array
- *                 are ignored if received.
+ * For an incoming channel it will be called immediately after the
+ * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
  *
- * @return handle to the cadet service NULL on error
- *         (in this case, init is never called)
+ * @param cls Channel closure.
+ * @param channel Connection to the other end --- FIXME: drop?
+ * @param window_size New window size. If the is more messages than buffer size
+ *                    this value will be negative. -- FIXME: make unsigned, we never call negative?
  */
-struct GNUNET_CADET_Handle *
-GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                      void *cls,
-                      GNUNET_CADET_StartupCallback init,
-                      GNUNET_CADET_DisconnectEventHandler disconnects,
-                      const struct GNUNET_MQ_MessageHandler *handlers);
+typedef void
+(*GNUNET_CADET_WindowSizeEventHandler) (void *cls,
+                                        const struct GNUNET_CADET_Channel *channel,
+                                        int window_size);
+
 
 /**
- * Disconnect from the mq-based cadet service. All channels will be destroyed.
- * All channel disconnect callbacks will be called on any still connected peers,
- * notifying about their disconnection. The registered inbound channel cleaner
- * will be called should any inbound channels still exist.
+ * Connect to the MQ-based cadet service.
  *
- * @param handle connection to cadet to disconnect
+ * @param cfg Configuration to use.
+ * @return Handle to the cadet service NULL on error.
  */
-void
-GNUNET_CADET_disconnecT (struct GNUNET_CADET_Handle *handle);
+struct GNUNET_CADET_Handle *
+GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg);
+
 
 /**
- * Open a port to receive incomming mq-based channels.
+ * Open a port to receive incomming MQ-based channels.
  *
  * @param h CADET handle.
- * @param port Hash representing the port number.
- * @param new_channel Function called when an channel is received.
- * @param new_channel_cls Closure for @a new_channel.
- *        NOTE: get rid of this cls?
- *
+ * @param port Hash identifying the port.
+ * @param connects Function called when an incoming channel is connected.
+ * @param connects_cls Closure for the @a connects handler.
+ * @param window_changes Function called when the transmit window size changes.
+ *                       Can be NULL.
+ * @param disconnects Function called when a channel is disconnected.
+ * @param handlers Callbacks for messages we care about, NULL-terminated.
  * @return Port handle.
  */
 struct GNUNET_CADET_Port *
 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
-                       const struct GNUNET_HashCode *port,
-                       GNUNET_CADET_ConnectEventHandler new_channel,
-                       void *new_channel_cls);
+                        const struct GNUNET_HashCode *port,
+                        GNUNET_CADET_ConnectEventHandler connects,
+                        void *connects_cls,
+                        GNUNET_CADET_WindowSizeEventHandler window_changes,
+                        GNUNET_CADET_DisconnectEventHandler disconnects,
+                        const struct GNUNET_MQ_MessageHandler *handlers);
 
 /**
- * Close a port opened with @a GNUNET_CADET_open_porT.
- * The @a new_channel callback will no longer be called.
+ * Create a new channel towards a remote peer.
  *
- * @param p Port handle.
+ * If the destination port is not open by any peer or the destination peer
+ * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
+ * for this channel.
+ *
+ * @param h CADET handle.
+ * @param channel_cls Closure for the channel. It's given to:
+ *                    - The management handler @a window_changes.
+ *                    - The disconnect handler @a disconnects
+ *                    - Each message type callback in @a handlers
+ * @param destination Peer identity the channel should go to.
+ * @param port Identification of the destination port.
+ * @param options CadetOption flag field, with all desired option bits set to 1.
+ * @param window_changes Function called when the transmit window size changes.
+ *                       Can be NULL if this data is of no interest.
+ * TODO                  Not yet implemented.
+ * @param disconnects Function called when the channel is disconnected.
+ * @param handlers Callbacks for messages we care about, NULL-terminated.
+ * @return Handle to the channel.
  */
-void
-GNUNET_CADET_close_porT (struct GNUNET_CADET_Port *p);
+struct GNUNET_CADET_Channel *
+GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
+                             void *channel_cls,
+                             const struct GNUNET_PeerIdentity *destination,
+                             const struct GNUNET_HashCode *port,
+                             enum GNUNET_CADET_ChannelOption options,
+                             GNUNET_CADET_WindowSizeEventHandler window_changes,
+                             GNUNET_CADET_DisconnectEventHandler disconnects,
+                             const struct GNUNET_MQ_MessageHandler *handlers);
+
 
 /**
- * Obtain the message queue for a connected peer.
- *
- * @param h the cadet handle
- * @param channel the identity of the peer
+ * Obtain the message queue for a connected channel.
  *
- * @return NULL if @a channel is not yet connected.
- *         NOTE: provide an mq before a channel is connected?
- *               provide a callback to notify a client a channel connected?
+ * @param channel The channel handle from which to get the MQ.
+ * @return The message queue of the channel.
  */
 struct GNUNET_MQ_Handle *
-GNUNET_CADET_get_mq (const struct GNUNET_CADET_Handle *h,
-                    const struct GNUNET_CADET_Channel *channel);
+GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel);
 
-/* NOTE:
- * GNUNET_CADET_channel_create and _destroy can stay the same.
- * Monitor API can stay the same (low-priority).
-
-struct GNUNET_CADET_Channel *
-GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
-                            void *channel_ctx,
-                            const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_HashCode *port,
-                            enum GNUNET_CADET_ChannelOption options);
-void
-GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
-
-*/
 
 /******************************************************************************/
 /******************************* MQ-BASED API *********************************/