[telepathy-gabble/telepathy-gabble-0.8] Use (part of) D-Bus machine ID as default resource.

Will Thompson will.thompson at collabora.co.uk
Mon Nov 9 03:32:36 PST 2009


Previously, Gabble generated a random resource for each connection if no
resource was explicitly specified. This meant that you could connect to
the same account with two Telepathy-powered clients at the same time
(say, your N900 and Empathy on the desktop) without having to change
your resource on one of them.

However, this is too random: if you lose your network connection without
telling the server you're leaving, and then reconnect with a new
resource, the server may still think the old resource is connected
(until it decides it's timed out) and send messages there rather than to
your new connection. Ditto your contacts, if they're sending to full
JIDs. By using the D-Bus machine ID as the default resource, you'll get
the same resource each time you connect from a particular device, with a
reasonably good chance that it'll be different on each of your devices.

This has the downside that you can't make two connections to the same
account on the same bus. I don't think this is a major problem: MC
should be managing your connections anyway. :-) However, it does mean I
had to remove the test which checks that you can do that...
---
 src/connection.c                                   |   23 +++++++--
 tests/twisted/Makefile.am                          |    1 -
 .../connect/multiple-times-to-same-account.py      |   50 --------------------
 3 files changed, 19 insertions(+), 55 deletions(-)
 delete mode 100644 tests/twisted/connect/multiple-times-to-same-account.py

diff --git a/src/connection.c b/src/connection.c
index c17ab3e..da87522 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -77,8 +77,6 @@
 static guint disco_reply_timeout = 5;
 static guint connect_timeout = 60;
 
-#define DEFAULT_RESOURCE_FORMAT "Telepathy.%x"
-
 static void conn_service_iface_init (gpointer, gpointer);
 static void capabilities_service_iface_init (gpointer, gpointer);
 static void gabble_conn_contact_caps_iface_init (gpointer, gpointer);
@@ -322,6 +320,24 @@ gabble_connection_constructor (GType type,
   return (GObject *) self;
 }
 
+static gchar *
+get_machine_id (void)
+{
+  /* This is a once-per-process leak. */
+  static char *local_machine_id = NULL;
+
+  if (G_UNLIKELY (local_machine_id == NULL))
+    {
+      local_machine_id = dbus_get_local_machine_id ();
+
+      if (local_machine_id == NULL)
+        g_error ("Out of memory getting local machine ID");
+    }
+
+  /* 32 bytes of hex. The first 8 should be unique enough... */
+  return g_strndup (local_machine_id, 8);
+}
+
 static void
 gabble_connection_constructed (GObject *object)
 {
@@ -335,8 +351,7 @@ gabble_connection_constructed (GObject *object)
 
   if (priv->resource == NULL)
     {
-      priv->resource = g_strdup_printf (DEFAULT_RESOURCE_FORMAT,
-          g_random_int ());
+      priv->resource = get_machine_id ();
       DEBUG ("defaulted resource to %s", priv->resource);
     }
 
diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am
index 4ef1d25..e39c043 100644
--- a/tests/twisted/Makefile.am
+++ b/tests/twisted/Makefile.am
@@ -131,7 +131,6 @@ TWISTED_TESTS = \
 	file-transfer/test-send-file-to-unknown-contact.py \
 	file-transfer/test-send-file-wait-to-provide.py \
 	test-debug.py \
-	connect/multiple-times-to-same-account.py \
 	connect/test-fail.py \
 	connect/test-success.py \
 	connect/test-twice.py \
diff --git a/tests/twisted/connect/multiple-times-to-same-account.py b/tests/twisted/connect/multiple-times-to-same-account.py
deleted file mode 100644
index ed8a346..0000000
--- a/tests/twisted/connect/multiple-times-to-same-account.py
+++ /dev/null
@@ -1,50 +0,0 @@
-"""
-Tests making two connections to the same account, to see if Gabble correctly
-picks random resources when none has been specified. (If they get the same
-resource, the second call to RequestConnection will fail, since they'll try to
-have the same object path.
-"""
-
-import dbus
-
-from servicetest import tp_name_prefix, tp_path_prefix, wrap_connection
-
-if __name__ == '__main__':
-    bus = dbus.SessionBus()
-
-    name = 'gabble'
-    proto = 'jabber'
-
-    cm = bus.get_object(
-        tp_name_prefix + '.ConnectionManager.%s' % name,
-        tp_path_prefix + '/ConnectionManager/%s' % name)
-    cm_iface = dbus.Interface(cm, tp_name_prefix + '.ConnectionManager')
-
-    params = {
-        'account': 'test at localhost',
-        'password': 'pass',
-        'server': 'localhost',
-        'port': dbus.UInt32(4242),
-        }
-
-    # Create two connections with the same account and no specified resource.
-    connection_name, connection_path = cm_iface.RequestConnection(
-        proto, params)
-    conn1 = wrap_connection(bus.get_object(connection_name, connection_path))
-
-    connection_name, connection_path = cm_iface.RequestConnection(
-        proto, params)
-    conn2 = wrap_connection(bus.get_object(connection_name, connection_path))
-
-    # Cool. Let's get rid of them.
-    try:
-        conn1.Connect()
-        conn1.Disconnect()
-    except dbus.DBusException, e:
-        pass
-
-    try:
-        conn2.Connect()
-        conn2.Disconnect()
-    except dbus.DBusException, e:
-        pass
-- 
1.5.6.5




More information about the telepathy-commits mailing list