[gnome-games/sudoku-tube] Be able to send/receive updates
- From: Zhang Sen <zhangsen src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games/sudoku-tube] Be able to send/receive updates
- Date: Fri, 17 Jul 2009 11:04:56 +0000 (UTC)
commit 99338dd2dc420b0919c0b6ce63319e6f5ee288a2
Author: Zhang Sen <zh jesse gmail com>
Date: Fri Jul 17 09:22:04 2009 +0800
Be able to send/receive updates
gnome-sudoku/src/lib/gsudoku.py | 3 ++
gnome-sudoku/src/lib/main.py | 42 +++++++++++--------------
gnome-sudoku/src/lib/networking.py | 60 +++++++++++++++++++++++++++++------
3 files changed, 70 insertions(+), 35 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/gsudoku.py b/gnome-sudoku/src/lib/gsudoku.py
index 48f4a2c..1e99a75 100644
--- a/gnome-sudoku/src/lib/gsudoku.py
+++ b/gnome-sudoku/src/lib/gsudoku.py
@@ -93,6 +93,9 @@ class SudokuNumberGrid (gtk.AspectFrame):
def set_color(self, x, y, color):
self.__entries__[(x, y)].set_color(color)
+ def set_background_color(self, x, y, color):
+ self.__entries__[(x, y)].set_background_color(color)
+
def set_focus(self, x, y):
self.table.set_focus_child(self.__entries__[x, y])
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 71cd047..f02a619 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -52,7 +52,7 @@ class UI (gconf_wrapper.GConfWrapper):
'auto_save_interval':60 # auto-save interval in seconds...
}
tube_service = 'org.gnome.Sudoku'
- _server_obj_path = "/Game"
+ _remote_view_obj_path = "/ViewProxy"
def __init__ (self):
gconf_wrapper.GConfWrapper.__init__(self,
@@ -201,9 +201,15 @@ class UI (gconf_wrapper.GConfWrapper):
self.tracker_ui.hide()
tracker_ui_container.pack_start(self.tracker_ui)
- self._side_gsd = gsudoku.SudokuView(group_size=9)
- self._side_gsd.hide()
- side_grid_container.add(self._side_gsd)
+ self._setup_side_view()
+ side_grid_container.add(self._side_grid_vew)
+
+ def _setup_side_view(self):
+ self._side_grid_vew = gsudoku.SudokuView(group_size=9)
+ for x in range(9):
+ for y in range(9):
+ self._side_grid_vew.set_readonly_appearance(x, y, True)
+ self._side_grid_vew.hide()
def setup_color (self):
# setup background colors
@@ -343,25 +349,11 @@ class UI (gconf_wrapper.GConfWrapper):
sublabel = sublabel
)
- def _get_server(self, bus):
- return bus.get_object(object_path=self._server_obj_path)
-
- def get_puzzle(self):
- """Return the puzzle as a string
-
- so a remote peer can use it to construct their own game"""
- return self._puzzle # should be a string
-
def _tube_received_cb(self, bus):
- game_server = self._get_server(bus)
- puzzle = str(game_server.get_puzzle())
- logger.info("get puzzle from server")
- logger.debug("puzzle: %s" % puzzle)
- self._open_side_puzzle(puzzle)
-
- def _open_side_puzzle(self, puzzle):
- self._side_gsd.change_grid(puzzle, 9)
- self._side_gsd.show()
+ remote_view_proxy = bus.get_object(object_path=self._remote_view_obj_path)
+ logger.info("got view from server")
+ local_view_proxy = networking.LocalViewProxy(remote_view_proxy)
+ local_view_proxy.connect_to_model(self._main_model)
def new_cb (self, *args):
if self._close_current_game():
@@ -381,8 +373,10 @@ class UI (gconf_wrapper.GConfWrapper):
def _tube_offered_cb(self, bus):
"""Called when the peer accepts our tube"""
- server = networking.GameServer(bus, self._server_obj_path, self)
- logger.info("server created")
+ proxy_of_self = networking.RemoteViewProxy(bus,
+ self._remote_view_obj_path, self._side_grid_vew)
+ logger.info("created the mirror of myself")
+ self._side_grid_vew.show()
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 d9dcb34..91a195c 100644
--- a/gnome-sudoku/src/lib/networking.py
+++ b/gnome-sudoku/src/lib/networking.py
@@ -4,19 +4,57 @@ import logging
import dbus
-logger = logging.getLogger("main.server")
+logger = logging.getLogger("main.network")
-class GameServer(dbus.service.Object):
+class RemoteViewProxy(dbus.service.Object):
"""Expose the game on dbus, so the others can listen to it"""
- server_interface = "org.gnome.Sudoku.server"
+ view_interface = "org.gnome.Sudoku.view"
- def __init__(self, bus, obj_path, game):
- super(GameServer, self).__init__(bus, obj_path)
- self.game = game
+ 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=server_interface,
- in_signature="", out_signature="s")
- def get_puzzle(self):
- logger.info("client is asking for the puzzle")
- return self.game.get_puzzle()
+ @dbus.service.method(dbus_interface=view_interface)
+ def set_value(self, x, y, value):
+ self._view.set_value(x, y, value)
+
+ @dbus.service.method(dbus_interface=view_interface)
+ def set_invisible(self, x, y):
+ self._view.set_background_color(x, y, (0,0,0))
+
+ @dbus.service.method(dbus_interface=view_interface)
+ def set_empty(self, x, y):
+ self._view.set_value(x, y, 0)
+ self._view.set_background_color(x, y, None)
+
+ @dbus.service.method(dbus_interface=view_interface,
+ in_signature="", out_signature="")
+ def puzzle_finished_cb(self):
+ self.puzzle_finished_cb()
+
+class LocalViewProxy:
+
+ def __init__(self, remote_proxy):
+ self._remote_proxy = remote_proxy
+
+ def update(self, changes):
+ for box in changes:
+ x, y, value, conflict = box
+ if value is None:
+ pass
+ elif value == 0:
+ self._remote_proxy.set_empty(x, y)
+ else:
+ self._remote_proxy.set_invisible(x, y)
+
+ def puzzle_finished_cb(self):
+ self._remote_proxy.puzzle_finished_cb()
+
+ def connect_to_model(self, model):
+ model.add_observer(self)
+ for x in range(9):
+ for y in range(9):
+ val = model.grid._get_(x, y)
+ if val:
+ self._remote_proxy.set_value(x,y, val)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]