Isolated room server is now using inheritance properly

This commit is contained in:
Uriziel 2012-12-13 22:51:31 +01:00
parent 06fd73f6a4
commit 56ea29af72
2 changed files with 31 additions and 24 deletions

View File

@ -8,14 +8,13 @@ from syncplay.protocols import SyncServerProtocol
import time import time
class SyncFactory(Factory): class SyncFactory(Factory):
def __init__(self, password = '', isolateRooms = False): def __init__(self, password = ''):
print "Welcome to Syncplay server, ver. {0}".format(syncplay.version) print "Welcome to Syncplay server, ver. {0}".format(syncplay.version)
if(password): if(password):
password = hashlib.md5(password).hexdigest() password = hashlib.md5(password).hexdigest()
self.password = password self.password = password
self._rooms = {} self._rooms = {}
self._roomStates = {} self._roomStates = {}
self.isolateRooms = isolateRooms
self._usersCheckupTimer = task.LoopingCall(self._checkUsers) self._usersCheckupTimer = task.LoopingCall(self._checkUsers)
self._usersCheckupTimer.start(4, True) self._usersCheckupTimer.start(4, True)
@ -53,18 +52,11 @@ class SyncFactory(Factory):
return room[watcherProtocol] return room[watcherProtocol]
def getAllWatchers(self, watcherProtocol): def getAllWatchers(self, watcherProtocol):
if(self.isolateRooms): watchers = {}
room = self.getWatcher(watcherProtocol).room for room in self._rooms.itervalues():
if(self._rooms.has_key(room)): for watcher in room.itervalues():
return self._rooms[room] watchers[watcher.watcherProtocol] = watcher
else: return watchers
return {}
else:
watchers = {}
for room in self._rooms.itervalues():
for watcher in room.itervalues():
watchers[watcher.watcherProtocol] = watcher
return watchers
def _removeWatcherFromTheRoom(self, watcherProtocol): def _removeWatcherFromTheRoom(self, watcherProtocol):
for room in self._rooms.itervalues(): for room in self._rooms.itervalues():
@ -173,9 +165,6 @@ class SyncFactory(Factory):
self._roomStates[room]["setBy"] = watcher.name self._roomStates[room]["setBy"] = watcher.name
self._roomStates[room]["lastUpdate"] = time.time() self._roomStates[room]["lastUpdate"] = time.time()
self._deleteRoomIfEmpty(oldRoom) 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 watcher.room = room
l = lambda w: w.sendUserSetting(watcher.name, watcher.room, watcher.file, None) l = lambda w: w.sendUserSetting(watcher.name, watcher.room, watcher.file, None)
self.broadcast(watcherProtocol, l) self.broadcast(watcherProtocol, l)
@ -193,9 +182,6 @@ class SyncFactory(Factory):
what(receiver) what(receiver)
def broadcast(self, sender, what): def broadcast(self, sender, what):
if(self.isolateRooms):
self.broadcastRoom(sender, what)
return
for room in self._rooms.itervalues(): for room in self._rooms.itervalues():
for receiver in room: for receiver in room:
what(receiver) what(receiver)
@ -208,8 +194,26 @@ class SyncFactory(Factory):
self.removeWatcher(watcher.watcherProtocol) self.removeWatcher(watcher.watcherProtocol)
self._checkUsers() #restart self._checkUsers() #restart
return #end loop 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): class Watcher(object):
def __init__(self, factory, watcherProtocol, name, room): def __init__(self, factory, watcherProtocol, name, room):
self.factory = factory self.factory = factory

View File

@ -2,10 +2,13 @@
from twisted.internet import reactor from twisted.internet import reactor
from syncplay.server import SyncFactory from syncplay.server import SyncFactory, SyncIsolatedFactory
from syncplay.ui.ConfigurationGetter import ServerConfigurationGetter from syncplay.ui.ConfigurationGetter import ServerConfigurationGetter
argsGetter = ServerConfigurationGetter() argsGetter = ServerConfigurationGetter()
args = argsGetter.getConfiguration() 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() reactor.run()