Add a new function that find the best transport method for an app.
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Wed, 21 Jul 2010 01:39:42 +0200
changeset 63 b56853071a09
parent 62 e7915504da33
child 64 63d4f434abbc
Add a new function that find the best transport method for an app.
jingle-filetransfer/filetransfer.c
jingle-ibb/ibb.c
jingle/register.c
jingle/register.h
jingle/sessions.c
--- a/jingle-filetransfer/filetransfer.c	Wed Jul 21 00:24:38 2010 +0200
+++ b/jingle-filetransfer/filetransfer.c	Wed Jul 21 01:39:42 2010 +0200
@@ -197,7 +197,7 @@
 
 static void jingle_ft_init(void)
 {
-  jingle_register_app(NS_JINGLE_APP_FT, &funcs, JINGLE_TRANS_TCP);
+  jingle_register_app(NS_JINGLE_APP_FT, &funcs, JINGLE_TRANSPORT_STREAMING);
   xmpp_add_feature(NS_JINGLE_APP_FT);
 }
 
--- a/jingle-ibb/ibb.c	Wed Jul 21 00:24:38 2010 +0200
+++ b/jingle-ibb/ibb.c	Wed Jul 21 01:39:42 2010 +0200
@@ -162,7 +162,9 @@
 static void jingle_ibb_init(void)
 {
   jingle_ibb_handler = lm_message_handler_new(jingle_ibb_handle_iq, NULL, NULL);
-  jingle_register_transport(NS_JINGLE_TRANSPORT_IBB, &funcs, JINGLE_TRANS_IN_BAND, JINGLE_TRANS_TCP);
+  jingle_register_transport(NS_JINGLE_TRANSPORT_IBB, &funcs,
+                            JINGLE_TRANSPORT_STREAMING,
+                            JINGLE_TRANSPORT_LOW);
   xmpp_add_feature(NS_JINGLE_TRANSPORT_IBB);
 }
 
--- a/jingle/register.c	Wed Jul 21 00:24:38 2010 +0200
+++ b/jingle/register.c	Wed Jul 21 01:39:42 2010 +0200
@@ -30,14 +30,14 @@
 typedef struct {
   gchar *xmlns;
   JingleAppFuncs *funcs;
-  JingleTransMethod method;
+  JingleTransportType transtype;
 } AppHandlerEntry;
 
 typedef struct {
   gchar *xmlns;
   JingleTransportFuncs *funcs;
-  JingleTransType type;
-  JingleTransMethod method;
+  JingleTransportType transtype;
+  JingleTransportPriority priority;
 } TransportHandlerEntry;
 
 
@@ -49,32 +49,42 @@
 GSList *jingle_transport_handlers = NULL;
 
 
+/**
+ * Register a new supported application.
+ * type is the type of transport the data shall be sent over.
+ */
 void jingle_register_app(const gchar *xmlns, JingleAppFuncs *funcs,
-                         JingleTransMethod method)
+                         JingleTransportType type)
 {
   if (!g_str_has_prefix(xmlns, NS_JINGLE_APP_PREFIX)) return;
 
   AppHandlerEntry *h = g_new(AppHandlerEntry, 1);
 
-  h->xmlns  = g_strdup(xmlns);
-  h->funcs  = funcs;
-  h->method = method;
-  
+  h->xmlns       = g_strdup(xmlns);
+  h->funcs       = funcs;
+  h->transtype   = type;
+
   jingle_app_handlers = g_slist_append(jingle_app_handlers, h);
 }
 
-void jingle_register_transport(const gchar *xmlns, JingleTransportFuncs *funcs,
-                               JingleTransType type, JingleTransMethod method)
+/**
+ * Register a new supported transport.
+ * type is the type of transport.
+ */
+void jingle_register_transport(const gchar *xmlns,
+                               JingleTransportFuncs *funcs,
+                               JingleTransportType type,
+                               JingleTransportPriority prio)
 {
   if (!g_str_has_prefix(xmlns, NS_JINGLE_TRANSPORT_PREFIX)) return;
 
   TransportHandlerEntry *h = g_new(TransportHandlerEntry, 1);
 
-  h->xmlns  = g_strdup(xmlns);
-  h->funcs  = funcs;
-  h->method = method;
-  h->type = type;
-  
+  h->xmlns     = g_strdup(xmlns);
+  h->funcs     = funcs;
+  h->transtype = type;
+  h->priority  = prio;
+
   jingle_transport_handlers = g_slist_append(jingle_transport_handlers, h);
 }
 
@@ -91,6 +101,32 @@
 }
 
 /**
+ * Determine which transport is better suited for a given app.
+ */
+JingleTransportFuncs *jingle_transport_for_app(const gchar *appxmlns)
+{
+  AppHandlerEntry *app = jingle_find_app(appxmlns);
+  GSList *entry;
+  TransportHandlerEntry *thistransport, *besttransport;
+  JingleTransportPriority bestprio;
+  JingleTransportType requestedtype;
+
+  if (entry == NULL)
+    return NULL;
+
+  requestedtype = app->transtype;
+  for (entry = jingle_transport_handlers; entry; entry = entry->next) {
+    thistransport = (TransportHandlerEntry *) entry->data;
+    if (thistransport->priority > bestprio) {
+      bestprio = thistransport->priority;
+      besttransport = thistransport;
+    }
+  }
+
+  return besttransport->funcs;
+}
+
+/**
  * This function should work with AppHandlerEntry and
  * TransportHandlerEntry as long as both start with xmlns.
  */
--- a/jingle/register.h	Wed Jul 21 00:24:38 2010 +0200
+++ b/jingle/register.h	Wed Jul 21 01:39:42 2010 +0200
@@ -7,15 +7,32 @@
 #define NS_JINGLE_APP_PREFIX       "urn:xmpp:jingle:apps:"
 #define NS_JINGLE_TRANSPORT_PREFIX "urn:xmpp:jingle:transports:"
 
-typedef enum {
-  JINGLE_TRANS_IN_BAND,
-  JINGLE_TRANS_OUT_BAND,
-} JingleTransType;
 
 typedef enum {
-  JINGLE_TRANS_TCP,
-  JINGLE_TRANS_UDP,
-} JingleTransMethod;
+  /* A datagram transport has one or more components with which to exchange
+   * packets with UDP-like behavior. Packets might be of arbitrary length,
+   * might be received out of order, and might not be received at all
+   * (i.e., the transport is lossy). */
+  JINGLE_TRANSPORT_STREAMING,
+  
+  /* A streaming transport has one or more components with which to exchange
+   * bidirectional bytestreams with TCP-like behavior. Bytes are received
+   * reliably and in order, and applications MUST NOT rely on a stream being
+   * chunked in any specific way. */
+  JINGLE_TRANSPORT_DATAGRAM
+} JingleTransportType;
+
+/**
+ * We need to rank transports to determine which one to choose.
+ * With this system, In-Band Bytestreams could have a low priority, SOCKS5
+ * Bytestream a normal priority, and some stream transport method that allow
+ * direct connection would have a high priority, since it would be the fastest.
+ */
+typedef enum {
+  JINGLE_TRANSPORT_LOW,
+  JINGLE_TRANSPORT_NORMAL,
+  JINGLE_TRANSPORT_HIGH
+} JingleTransportPriority;
 
 typedef gconstpointer (*JingleAppCheck) (JingleContent *cn, GError **err);
 typedef void (*JingleAppHandle) (gconstpointer data, LmMessageNode *node);
@@ -49,9 +66,11 @@
 
 
 void jingle_register_app(const gchar *xmlns, JingleAppFuncs *funcs,
-                         JingleTransMethod method);
-void jingle_register_transport(const gchar *xmlns, JingleTransportFuncs *funcs,
-                               JingleTransType type, JingleTransMethod method);
+                         JingleTransportType type);
+void jingle_register_transport(const gchar *xmlns,
+                               JingleTransportFuncs *funcs,
+                               JingleTransportType type,
+                               JingleTransportPriority prio);
 JingleAppFuncs *jingle_get_appfuncs(const gchar *xmlns);
 JingleTransportFuncs *jingle_get_transportfuncs(const gchar *xmlns);
 void jingle_unregister_app(const gchar *xmlns);
--- a/jingle/sessions.c	Wed Jul 21 00:24:38 2010 +0200
+++ b/jingle/sessions.c	Wed Jul 21 01:39:42 2010 +0200
@@ -137,7 +137,6 @@
     sc->state = state;
 }
 
-
 /**
  * Remove a session from the linked list and free it.
  */