[glibmm] DBus Peer Example: Correct some code to complete the example.



commit d3c900253ea83f4461bbec21d77eb91834e5775b
Author: José Alburquerque <jaalburqu svn gnome org>
Date:   Wed Jan 12 22:59:21 2011 -0500

    DBus Peer Example: Correct some code to complete the example.
    
    	* examples/dbus/peer.cc (keep_connection): renamed to curr_connection.
    	(on_method_call): Place the return value of the method in a tuple
    	which is then returned.
    	(curr_connection): Use this variable to manage incoming connections.
    	The C API docs says that when handling ther DBusServer's
    	signal_new_connection() a reference to the connection should be kept
    	and the handler should return true if the connection should be kept.
    	This variable keeps a reference to the current connection.  When the
    	caller finishes, it closes the connection thus allowing the server to
    	accept another connection.
    	(try/catches): Add return statements in the catches that fail so that
    	execution does not continue.
    
    	The example runs successfully with the previous commit and the patch
    	in bug #639391 filed in the glib bugzilla product page.  Hopefully the
    	request will be accepted.

 ChangeLog             |   21 +++++++++++++++++++++
 examples/dbus/peer.cc |   32 ++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 828ec6b..919d06d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,26 @@
 2011-01-12  José Alburquerque  <jaalburqu svn gnome org>
 
+	DBus Peer Example: Correct some code to complete the example.
+
+	* examples/dbus/peer.cc (keep_connection): renamed to curr_connection.
+	(on_method_call): Place the return value of the method in a tuple
+	which is then returned.
+	(curr_connection): Use this variable to manage incoming connections.
+	The C API docs says that when handling ther DBusServer's
+	signal_new_connection() a reference to the connection should be kept
+	and the handler should return true if the connection should be kept.
+	This variable keeps a reference to the current connection.  When the
+	caller finishes, it closes the connection thus allowing the server to
+	accept another connection.
+	(try/catches): Add return statements in the catches that fail so that
+	execution does not continue.
+
+	The example runs successfully with the previous commit and the patch
+	in bug #639391 filed in the glib bugzilla product page.  Hopefully the
+	request will be accepted.
+
+2011-01-12  José Alburquerque  <jaalburqu svn gnome org>
+
 	DBusConnection: Check for NULL bus name in the MethodCall callback.
 
 	* gio/src/dbusconnection.ccg
diff --git a/examples/dbus/peer.cc b/examples/dbus/peer.cc
index ea04b2a..be25ebf 100644
--- a/examples/dbus/peer.cc
+++ b/examples/dbus/peer.cc
@@ -35,8 +35,9 @@ static Glib::ustring introspection_xml =
   "  </interface>"
   "</node>";
 
-// This variable is used to keep an incoming connection active.
-Glib::RefPtr<Gio::DBusConnection> keep_connection;
+// This variable is used to keep an incoming connection active until it is
+// closed.
+static Glib::RefPtr<Gio::DBusConnection> curr_connection;
 
 static void on_method_call(const Glib::RefPtr<Gio::DBusConnection>&,
   const Glib::ustring& /* sender */, const Glib::ustring& /* object_path */,
@@ -60,7 +61,13 @@ static void on_method_call(const Glib::RefPtr<Gio::DBusConnection>&,
     Glib::Variant<Glib::ustring> answer =
       Glib::Variant<Glib::ustring>::create(response);
 
-    invocation->return_value(answer);
+    std::vector<Glib::VariantBase> var_array;
+    var_array.push_back(answer);
+
+    Glib::VariantContainerBase ret =
+      Glib::VariantContainerBase::create_tuple(var_array);
+
+    invocation->return_value(ret);
 
     std::cout << "Client said '" << param.get() << "'." << std::endl;
   }
@@ -87,9 +94,20 @@ bool on_new_connection(const Glib::RefPtr<Gio::DBusConnection>& connection)
     "Peer credentials: " << credentials_str << std::endl <<
     "Negotiated capabilities: unix-fd-passing=" << (connection->get_capabilities() & Gio::DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING) << std::endl;
 
+  // If there is already an active connection, do not accept this new one.
+  // There may be a better way to decide how to keep current incoming
+  // connections.
+  if(curr_connection && !curr_connection->is_closed())
+  {
+    std::cerr << "Unable to accept new incoming connection because one is "
+      "already active." << std::endl;
+
+    return false;
+  }
+
   // In order for the connection to stay active the reference to the
   // connection must be kept so store the connection in a global variable.
-  keep_connection = connection;
+  curr_connection = connection;
 
   guint reg_id = connection->register_object("/org/glibmm/GDBus/TestObject",
     introspection_data->lookup_interface("org.glibmm.GDBus.TestPeerInterface"),
@@ -123,6 +141,7 @@ void run_as_server(Glib::ustring address, bool allow_anonymous)
   {
     std::cerr << "Error creating server at address: " << address <<
       ": " << ex.what() << "." << std::endl;
+    return;
   }
 
   server->start();
@@ -149,6 +168,7 @@ void run_as_client(Glib::ustring address)
   {
     std::cerr << "Error connecting to D-Bus address " << address << ": " <<
       ex.what() << "." << std::endl;
+    return;
   }
 
   std::cout << "Connected. " << std::endl <<
@@ -183,11 +203,14 @@ void run_as_client(Glib::ustring address)
     result.get(child);
 
     std::cout << "The server said: " << child.get() << "." << std::endl;
+
+    connection->close_sync();
   }
   catch(const Glib::Error& ex)
   {
     std::cerr << "Error calling the server's method: " << ex.what() << "." <<
       std::endl;
+    return;
   }
 }
 
@@ -247,6 +270,7 @@ int main(int argc, char** argv)
   {
     std::cerr << "Unable to create introspection data: " << ex.what() <<
       "." << std::endl;
+    return 1;
   }
 
   if(opt_server)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]