[gnome-games/sudoku-tube] Can start network game at both ends



commit a8234bfaf9c83599756b827ce81d87d56a08635b
Author: Zhang Sen <zh jesse gmail com>
Date:   Tue Jul 28 23:03:16 2009 +0800

    Can start network game at both ends

 gnome-sudoku/src/lib/main.py       |   37 +++++++++++++++++++++++++----------
 gnome-sudoku/src/lib/networking.py |   18 +++++++++++++---
 2 files changed, 40 insertions(+), 15 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 579a6ab..9df6230 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -54,7 +54,7 @@ class UI (gconf_wrapper.GConfWrapper):
                      'auto_save_interval':60 # auto-save interval in seconds...
                      }
     _tube_service = 'org.gnome.Sudoku'
-    _remote_view_obj_path = "/ViewProxy"
+    _view_obj_path = "/SideView"
 
     def __init__ (self):
         gconf_wrapper.GConfWrapper.__init__(self,
@@ -390,14 +390,6 @@ class UI (gconf_wrapper.GConfWrapper):
                                    sublabel = sublabel
                                    )
 
-    def _tube_received_cb(self, bus):
-        remote_view_proxy = bus.get_object(object_path=self._remote_view_obj_path)
-        logger.info("got a remote view which we can manipulate")
-        # we issue command on remote_view_handler, which will communicate with
-        # remote_view
-        view_handler = networking.RemoteViewHandler(remote_view_proxy)
-        view_handler.connect_to_model(self._main_model)
-
     def new_cb (self, *args):
         if self._close_current_game():
             self._auto_open_game()
@@ -417,13 +409,36 @@ class UI (gconf_wrapper.GConfWrapper):
         else:
             logger.info("None selected")
 
+    def _tube_received_cb(self, bus):
+        """Called when we receive a tube"""
+        self._tube_open_cb(bus, is_initiator=False)
+
     def _tube_offered_cb(self, bus):
         """Called when the peer accepts our tube"""
-        proxy_of_side_view = networking.SideViewProxy(bus,
-                self._remote_view_obj_path, self._side_grid_vew)
+        self._tube_open_cb(bus, is_initiator=True)
+
+    def _tube_open_cb(self, bus, is_initiator):
+        receiver_view = self._view_obj_path + "/receiver"
+        initiator_view = self._view_obj_path + "/initiator"
+        if is_initiator:
+            our_obj_path, peer_obj_path = initiator_view, receiver_view
+        else:
+            our_obj_path, peer_obj_path = receiver_view, initiator_view
+
+        # First export our own side-grid-view on dbus
+        proxy_of_side_view = networking.SideViewProxy(bus, our_obj_path,
+                 self._side_grid_vew)
         logger.info("exported side-view on dbus")
         self._side_grid_vew.show()
 
+        # Then fetch our peer's side-grid-view
+        remote_view_proxy = bus.get_object(object_path=peer_obj_path)
+        logger.info("got a remote view which we can manipulate")
+        # we issue command on remote_view_handler, which will communicate with
+        # remote_view
+        view_handler = networking.RemoteViewHandler(remote_view_proxy)
+        view_handler.connect_to_model(self._main_model)
+
     def _close_current_game (self):
         """Close current running game
 
diff --git a/gnome-sudoku/src/lib/networking.py b/gnome-sudoku/src/lib/networking.py
index 08354af..e7de6cd 100644
--- a/gnome-sudoku/src/lib/networking.py
+++ b/gnome-sudoku/src/lib/networking.py
@@ -6,17 +6,18 @@ import dbus
 
 logger = logging.getLogger("main.network")
 
+view_interface = "org.gnome.Sudoku.view"
+
 class SideViewProxy(dbus.service.Object):
     """A dbus object, which exports a sudoku game's side-view on dbus
     """
 
-    view_interface = "org.gnome.Sudoku.view"
-
     def __init__(self, bus, obj_path, sudoku_view):
         dbus.service.Object.__init__(self, bus, obj_path)
         self._view = sudoku_view
 
-    @dbus.service.method(dbus_interface=view_interface)
+    @dbus.service.method(dbus_interface=view_interface,
+            in_signature="iii", out_signature="")
     def set_value(self, x, y, value):
         self._view.set_value(x, y, value)
 
@@ -63,4 +64,13 @@ class RemoteViewHandler:
             for y in range(9):
                 val = model.get_virgin_value(x, y)
                 if val:
-                    self._remote_proxy.set_value(x,y, val)
+                    self._remote_proxy.set_value(x,y, val,
+                            dbus_interface=view_interface,
+                            reply_handler=self._handle_reply,
+                            error_handler=self._handle_error)
+
+    def _handle_reply(*args):
+        pass
+
+    def _handle_error(*args):
+        print 'error', args



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