From 56ea29af72be541c3090d86398805ade0c50da2a Mon Sep 17 00:00:00 2001 From: Uriziel Date: Thu, 13 Dec 2012 22:51:31 +0100 Subject: [PATCH] Isolated room server is now using inheritance properly --- syncplay/server.py | 48 +++++++++++++++++++++++++--------------------- syncplayServer.py | 7 +++++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/syncplay/server.py b/syncplay/server.py index 01628be..68a014f 100644 --- a/syncplay/server.py +++ b/syncplay/server.py @@ -8,14 +8,13 @@ from syncplay.protocols import SyncServerProtocol import time class SyncFactory(Factory): - def __init__(self, password = '', isolateRooms = False): + def __init__(self, password = ''): print "Welcome to Syncplay server, ver. {0}".format(syncplay.version) if(password): password = hashlib.md5(password).hexdigest() self.password = password self._rooms = {} self._roomStates = {} - self.isolateRooms = isolateRooms self._usersCheckupTimer = task.LoopingCall(self._checkUsers) self._usersCheckupTimer.start(4, True) @@ -53,18 +52,11 @@ class SyncFactory(Factory): return room[watcherProtocol] def getAllWatchers(self, watcherProtocol): - if(self.isolateRooms): - room = self.getWatcher(watcherProtocol).room - if(self._rooms.has_key(room)): - return self._rooms[room] - else: - return {} - else: - watchers = {} - for room in self._rooms.itervalues(): - for watcher in room.itervalues(): - watchers[watcher.watcherProtocol] = watcher - return watchers + watchers = {} + for room in self._rooms.itervalues(): + for watcher in room.itervalues(): + watchers[watcher.watcherProtocol] = watcher + return watchers def _removeWatcherFromTheRoom(self, watcherProtocol): for room in self._rooms.itervalues(): @@ -173,9 +165,6 @@ class SyncFactory(Factory): self._roomStates[room]["setBy"] = watcher.name self._roomStates[room]["lastUpdate"] = time.time() self._deleteRoomIfEmpty(oldRoom) - if(self.isolateRooms): #this is trick to inform old room about leaving - l = lambda w: w.sendUserSetting(watcher.name, oldRoom, None, {"left": True}) - self.broadcast(watcherProtocol, l) watcher.room = room l = lambda w: w.sendUserSetting(watcher.name, watcher.room, watcher.file, None) self.broadcast(watcherProtocol, l) @@ -193,9 +182,6 @@ class SyncFactory(Factory): what(receiver) def broadcast(self, sender, what): - if(self.isolateRooms): - self.broadcastRoom(sender, what) - return for room in self._rooms.itervalues(): for receiver in room: what(receiver) @@ -208,8 +194,26 @@ class SyncFactory(Factory): self.removeWatcher(watcher.watcherProtocol) self._checkUsers() #restart return #end loop - - + +class SyncIsolatedFactory(SyncFactory): + def broadcast(self, sender, what): + self.broadcastRoom(sender, what) + + def getAllWatchers(self, watcherProtocol): + room = self.getWatcher(watcherProtocol).room + if(self._rooms.has_key(room)): + return self._rooms[room] + else: + return {} + + def watcherSetRoom(self, watcherProtocol, room): + watcher = self.getWatcher(watcherProtocol) + oldRoom = watcher.room + l = lambda w: w.sendUserSetting(watcher.name, oldRoom, None, {"left": True}) + self.broadcast(watcherProtocol, l) + SyncFactory.watcherSetRoom(self, watcherProtocol, room) + + class Watcher(object): def __init__(self, factory, watcherProtocol, name, room): self.factory = factory diff --git a/syncplayServer.py b/syncplayServer.py index e494399..db96b24 100644 --- a/syncplayServer.py +++ b/syncplayServer.py @@ -2,10 +2,13 @@ from twisted.internet import reactor -from syncplay.server import SyncFactory +from syncplay.server import SyncFactory, SyncIsolatedFactory from syncplay.ui.ConfigurationGetter import ServerConfigurationGetter argsGetter = ServerConfigurationGetter() args = argsGetter.getConfiguration() -reactor.listenTCP(int(args.port), SyncFactory(args.password, args.isolate_rooms)) +if(not args.isolate_rooms): + reactor.listenTCP(int(args.port), SyncFactory(args.password)) +else: + reactor.listenTCP(int(args.port), SyncIsolatedFactory(args.password)) reactor.run()