[Telepathy-commits] [telepathy-gabble/master] conn-olpc: send "subscribed" presence in response to "subscribe" presence instead of automatically send it when Publish is called

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Fri Sep 26 10:02:55 PDT 2008


20080801135457-7fe3f-6a5b34ad3ce5200e7037e9124090f33f404916e8.gz
---
 src/conn-olpc.c                      |   67 +++++++++++++++++++++++++++++-----
 src/conn-olpc.h                      |    3 ++
 src/connection.c                     |   15 ++++++++
 src/connection.h                     |    1 +
 tests/twisted/olpc/gadget-publish.py |   23 +++++++++++-
 5 files changed, 98 insertions(+), 11 deletions(-)

diff --git a/src/conn-olpc.c b/src/conn-olpc.c
index ee4a3b5..7abc5ae 100644
--- a/src/conn-olpc.c
+++ b/src/conn-olpc.c
@@ -3696,6 +3696,7 @@ conn_olpc_activity_properties_init (GabbleConnection *conn)
 
   conn->olpc_gadget_buddy = NULL;
   conn->olpc_gadget_activity = NULL;
+  conn->olpc_gadget_publish = FALSE;
 
   g_signal_connect (conn, "status-changed",
       G_CALLBACK (connection_status_changed_cb), NULL);
@@ -4309,6 +4310,8 @@ olpc_gadget_publish (GabbleSvcOLPCGadget *iface,
   if (!check_gadget_buddy (conn, context))
     return;
 
+  conn->olpc_gadget_publish = publish;
+
   if (publish)
     {
       /* FIXME: we should check if we are already registered before */
@@ -4320,14 +4323,6 @@ olpc_gadget_publish (GabbleSvcOLPCGadget *iface,
           g_error_free (error);
           return;
         }
-
-      if (!send_presence_to_gadget (conn, LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED,
-            &error))
-        {
-          dbus_g_method_return_error (context, error);
-          g_error_free (error);
-          return;
-        }
     }
   else
     {
@@ -4340,7 +4335,7 @@ olpc_gadget_publish (GabbleSvcOLPCGadget *iface,
           return;
         }
 
-      if (!send_presence_to_gadget (conn, LM_MESSAGE_SUB_TYPE_SUBSCRIBED,
+      if (!send_presence_to_gadget (conn, LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED,
             &error))
         {
           dbus_g_method_return_error (context, error);
@@ -4352,6 +4347,60 @@ olpc_gadget_publish (GabbleSvcOLPCGadget *iface,
   gabble_svc_olpc_gadget_return_from_publish (context);
 }
 
+LmHandlerResult
+conn_olpc_presence_cb (LmMessageHandler *handler,
+                       LmConnection *connection,
+                       LmMessage *presence,
+                       gpointer user_data)
+{
+  GabbleConnection *conn = GABBLE_CONNECTION (user_data);
+  LmMessageNode *pres_node;
+  const gchar *from;
+  LmMessageSubType sub_type;
+  GError *error = NULL;
+
+  pres_node = lm_message_get_node (presence);
+  from = lm_message_node_get_attribute (pres_node, "from");
+  if (from == NULL)
+    return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+
+  /* We are only interested about presence from Gadget */
+  if (tp_strdiff (from, conn->olpc_gadget_buddy))
+    return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+
+  sub_type = lm_message_get_sub_type (presence);
+
+  if (sub_type == LM_MESSAGE_SUB_TYPE_SUBSCRIBE)
+    {
+      if (conn->olpc_gadget_publish)
+        {
+          DEBUG ("accept Gadget subscribe request");
+
+          if (!send_presence_to_gadget (conn, LM_MESSAGE_SUB_TYPE_SUBSCRIBED,
+                &error))
+            {
+              DEBUG ("failed to send subscribed presence to Gadget: %s",
+                  error->message);
+              g_error_free (error);
+            }
+        }
+      else
+        {
+          DEBUG ("decline Gadget subscribe request");
+
+          if (!send_presence_to_gadget (conn, LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED,
+                &error))
+            {
+              DEBUG ("failed to send subscribed presence to Gadget: %s",
+                  error->message);
+              g_error_free (error);
+            }
+        }
+    }
+
+  return LM_HANDLER_RESULT_REMOVE_MESSAGE;
+}
+
 void
 olpc_gadget_iface_init (gpointer g_iface,
                         gpointer iface_data)
diff --git a/src/conn-olpc.h b/src/conn-olpc.h
index 821535b..19428f8 100644
--- a/src/conn-olpc.h
+++ b/src/conn-olpc.h
@@ -61,6 +61,9 @@ gboolean conn_olpc_process_activity_uninvite_message (GabbleConnection *conn,
 LmHandlerResult conn_olpc_msg_cb (LmMessageHandler *handler,
     LmConnection *connection, LmMessage *message, gpointer user_data);
 
+LmHandlerResult conn_olpc_presence_cb (LmMessageHandler *handler,
+    LmConnection *connection, LmMessage *presence, gpointer user_data);
+
 void olpc_gadget_iface_init (gpointer g_iface, gpointer iface_data);
 
 #endif /* __CONN_OLPC_H__ */
diff --git a/src/connection.c b/src/connection.c
index 5a786e4..62d35e1 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -140,6 +140,7 @@ struct _GabbleConnectionPrivate
   LmMessageHandler *stream_error_cb;
   LmMessageHandler *pubsub_msg_cb;
   LmMessageHandler *olpc_msg_cb;
+  LmMessageHandler *olpc_presence_cb;
 
   /* connection properties */
   gchar *connect_server;
@@ -764,6 +765,7 @@ gabble_connection_dispose (GObject *object)
   g_assert (priv->stream_error_cb == NULL);
   g_assert (priv->pubsub_msg_cb == NULL);
   g_assert (priv->olpc_msg_cb == NULL);
+  g_assert (priv->olpc_presence_cb == NULL);
 
   /*
    * The Loudmouth connection can't be unref'd immediately because this
@@ -1077,6 +1079,7 @@ connect_callbacks (TpBaseConnection *base)
   g_assert (priv->stream_error_cb == NULL);
   g_assert (priv->pubsub_msg_cb == NULL);
   g_assert (priv->olpc_msg_cb == NULL);
+  g_assert (priv->olpc_presence_cb == NULL);
 
   priv->iq_disco_cb = lm_message_handler_new (connection_iq_disco_cb,
                                               conn, NULL);
@@ -1107,6 +1110,12 @@ connect_callbacks (TpBaseConnection *base)
   lm_connection_register_message_handler (conn->lmconn, priv->olpc_msg_cb,
                                           LM_MESSAGE_TYPE_MESSAGE,
                                           LM_HANDLER_PRIORITY_FIRST);
+
+  priv->olpc_presence_cb = lm_message_handler_new (conn_olpc_presence_cb,
+      conn, NULL);
+  lm_connection_register_message_handler (conn->lmconn, priv->olpc_presence_cb,
+                                          LM_MESSAGE_TYPE_PRESENCE,
+                                          LM_HANDLER_PRIORITY_NORMAL);
 }
 
 static void
@@ -1120,6 +1129,7 @@ disconnect_callbacks (TpBaseConnection *base)
   g_assert (priv->stream_error_cb != NULL);
   g_assert (priv->pubsub_msg_cb != NULL);
   g_assert (priv->olpc_msg_cb != NULL);
+  g_assert (priv->olpc_presence_cb != NULL);
 
   lm_connection_unregister_message_handler (conn->lmconn, priv->iq_disco_cb,
                                             LM_MESSAGE_TYPE_IQ);
@@ -1145,6 +1155,11 @@ disconnect_callbacks (TpBaseConnection *base)
                                             LM_MESSAGE_TYPE_MESSAGE);
   lm_message_handler_unref (priv->olpc_msg_cb);
   priv->olpc_msg_cb = NULL;
+
+  lm_connection_unregister_message_handler (conn->lmconn,
+      priv->olpc_presence_cb, LM_MESSAGE_TYPE_MESSAGE);
+  lm_message_handler_unref (priv->olpc_presence_cb);
+  priv->olpc_presence_cb = NULL;
 }
 
 /**
diff --git a/src/connection.h b/src/connection.h
index 07a7dfa..5394475 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -146,6 +146,7 @@ struct _GabbleConnection {
     /* OLPC services */
     const gchar *olpc_gadget_buddy;
     const gchar *olpc_gadget_activity;
+    gboolean olpc_gadget_publish;
 
     /* bytestream factory */
     GabbleBytestreamFactory *bytestream_factory;
diff --git a/tests/twisted/olpc/gadget-publish.py b/tests/twisted/olpc/gadget-publish.py
index d221596..ff66062 100644
--- a/tests/twisted/olpc/gadget-publish.py
+++ b/tests/twisted/olpc/gadget-publish.py
@@ -5,7 +5,8 @@ test OLPC search activity
 import dbus
 
 from servicetest import call_async, EventPattern
-from gabbletest import exec_test, make_result_iq, acknowledge_iq, sync_stream
+from gabbletest import exec_test, make_result_iq, acknowledge_iq, sync_stream, \
+    elem
 
 from twisted.words.xish import domish, xpath
 from twisted.words.protocols.jabber.client import IQ
@@ -47,9 +48,20 @@ def test(q, bus, conn, stream):
 
     q.expect_many(
             EventPattern('stream-presence', presence_type='subscribe'),
-            EventPattern('stream-presence', presence_type='subscribed'),
             EventPattern('dbus-return', method='Publish'))
 
+    # accept the request
+    presence = elem('presence', to='test at localhost', from_='gadget.localhost',
+        type='subscribed')
+    stream.send(presence)
+
+    # send a subscribe request
+    presence = elem('presence', to='test at localhost', from_='gadget.localhost',
+        type='subscribe')
+    stream.send(presence)
+
+    q.expect('stream-presence', presence_type='subscribed'),
+
     call_async(q, gadget_iface, 'Publish', False)
 
     q.expect_many(
@@ -57,5 +69,12 @@ def test(q, bus, conn, stream):
             EventPattern('stream-presence', presence_type='unsubscribed'),
             EventPattern('dbus-return', method='Publish'))
 
+    # Gadget tries to subscribe but is refused now
+    presence = elem('presence', to='test at localhost', from_='gadget.localhost',
+        type='subscribe')
+    stream.send(presence)
+
+    q.expect('stream-presence', presence_type='unsubscribed'),
+
 if __name__ == '__main__':
     exec_test(test)
-- 
1.5.6.5




More information about the Telepathy-commits mailing list