Removed locks - this is not threaded app
This commit is contained in:
parent
e993b2ef25
commit
a9325de8f9
@ -6,8 +6,6 @@ from twisted.internet.protocol import Factory
|
|||||||
import syncplay
|
import syncplay
|
||||||
from syncplay.protocols import SyncServerProtocol
|
from syncplay.protocols import SyncServerProtocol
|
||||||
import time
|
import time
|
||||||
import threading
|
|
||||||
|
|
||||||
|
|
||||||
class SyncFactory(Factory):
|
class SyncFactory(Factory):
|
||||||
def __init__(self, password = '', banlist = None , isolateRooms = False):
|
def __init__(self, password = '', banlist = None , isolateRooms = False):
|
||||||
@ -18,8 +16,6 @@ class SyncFactory(Factory):
|
|||||||
self._rooms = {}
|
self._rooms = {}
|
||||||
self._roomStates = {}
|
self._roomStates = {}
|
||||||
self.isolateRooms = isolateRooms
|
self.isolateRooms = isolateRooms
|
||||||
self.roomUpdateLock = threading.RLock()
|
|
||||||
self.roomUsersLock = threading.RLock()
|
|
||||||
|
|
||||||
def buildProtocol(self, addr):
|
def buildProtocol(self, addr):
|
||||||
return SyncServerProtocol(self)
|
return SyncServerProtocol(self)
|
||||||
@ -50,35 +46,30 @@ class SyncFactory(Factory):
|
|||||||
self.broadcast(watcherProtocol, l)
|
self.broadcast(watcherProtocol, l)
|
||||||
|
|
||||||
def getWatcher(self, watcherProtocol):
|
def getWatcher(self, watcherProtocol):
|
||||||
with self.roomUsersLock:
|
for room in self._rooms.itervalues():
|
||||||
for room in self._rooms.itervalues():
|
if(room.has_key(watcherProtocol)):
|
||||||
if(room.has_key(watcherProtocol)):
|
return room[watcherProtocol]
|
||||||
return room[watcherProtocol]
|
|
||||||
|
|
||||||
def getAllWatchers(self, watcherProtocol):
|
def getAllWatchers(self, watcherProtocol):
|
||||||
with self.roomUsersLock:
|
if(self.isolateRooms):
|
||||||
if(self.isolateRooms):
|
room = self.getWatcher(watcherProtocol).room
|
||||||
room = self.getWatcher(watcherProtocol).room
|
return self._rooms[room]
|
||||||
return self._rooms[room]
|
else:
|
||||||
else:
|
watchers = {}
|
||||||
watchers = {}
|
for room in self._rooms.itervalues():
|
||||||
for room in self._rooms.itervalues():
|
for watcher in room.itervalues():
|
||||||
for watcher in room.itervalues():
|
watchers[watcher.watcherProtocol] = watcher
|
||||||
watchers[watcher.watcherProtocol] = watcher
|
return watchers
|
||||||
return watchers
|
|
||||||
|
|
||||||
def _removeWatcherFromTheRoom(self, watcherProtocol):
|
def _removeWatcherFromTheRoom(self, watcherProtocol):
|
||||||
with self.roomUsersLock:
|
for room in self._rooms.itervalues():
|
||||||
for room in self._rooms.itervalues():
|
watcher = room.pop(watcherProtocol, None)
|
||||||
watcher = room.pop(watcherProtocol, None)
|
return watcher
|
||||||
return watcher
|
|
||||||
|
|
||||||
def _deleteRoomIfEmpty(self, room):
|
def _deleteRoomIfEmpty(self, room):
|
||||||
with self.roomUpdateLock:
|
if (self._rooms[room] == {}):
|
||||||
with self.roomUsersLock:
|
self._rooms.pop(room)
|
||||||
if (self._rooms[room] == {}):
|
self._roomStates.pop(room)
|
||||||
self._rooms.pop(room)
|
|
||||||
self._roomStates.pop(room)
|
|
||||||
|
|
||||||
def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False):
|
def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False):
|
||||||
watcher = self.getWatcher(watcherProtocol)
|
watcher = self.getWatcher(watcherProtocol)
|
||||||
@ -132,28 +123,26 @@ class SyncFactory(Factory):
|
|||||||
watcher = self.getWatcher(watcherProtocol)
|
watcher = self.getWatcher(watcherProtocol)
|
||||||
self.__updateWatcherPing(latencyCalculation, watcher)
|
self.__updateWatcherPing(latencyCalculation, watcher)
|
||||||
watcher.lastUpdate = time.time()
|
watcher.lastUpdate = time.time()
|
||||||
with self.roomUpdateLock:
|
if(watcher.file):
|
||||||
if(watcher.file):
|
if(position is not None):
|
||||||
if(position is not None):
|
self.__updatePositionState(position, doSeek, watcher)
|
||||||
self.__updatePositionState(position, doSeek, watcher)
|
pauseChanged = False
|
||||||
pauseChanged = False
|
if(paused is not None):
|
||||||
if(paused is not None):
|
pauseChanged = self.__updatePausedState(paused, watcher)
|
||||||
pauseChanged = self.__updatePausedState(paused, watcher)
|
forceUpdate = self.__shouldServerForceUpdateOnRoom(pauseChanged, doSeek)
|
||||||
forceUpdate = self.__shouldServerForceUpdateOnRoom(pauseChanged, doSeek)
|
if(forceUpdate):
|
||||||
if(forceUpdate):
|
l = lambda w: self.sendState(w, doSeek, watcher.latency, forceUpdate)
|
||||||
l = lambda w: self.sendState(w, doSeek, watcher.latency, forceUpdate)
|
self.broadcastRoom(watcher, l)
|
||||||
self.broadcastRoom(watcher, l)
|
|
||||||
|
|
||||||
def removeWatcher(self, watcherProtocol):
|
def removeWatcher(self, watcherProtocol):
|
||||||
with self.roomUsersLock:
|
watcher = self._removeWatcherFromTheRoom(watcherProtocol)
|
||||||
watcher = self._removeWatcherFromTheRoom(watcherProtocol)
|
if(not watcher):
|
||||||
if(not watcher):
|
return
|
||||||
return
|
watcher.deactivate()
|
||||||
watcher.deactivate()
|
print "{0} left server".format(watcher.name)
|
||||||
print "{0} left server".format(watcher.name)
|
self._deleteRoomIfEmpty(watcher.room)
|
||||||
self._deleteRoomIfEmpty(watcher.room)
|
l = lambda w: w.sendUserSetting(watcher.name, watcher.room, None, {"left": True})
|
||||||
l = lambda w: w.sendUserSetting(watcher.name, watcher.room, None, {"left": True})
|
self.broadcast(watcherProtocol, l)
|
||||||
self.broadcast(watcherProtocol, l)
|
|
||||||
|
|
||||||
def watcherGetUsername(self, watcherProtocol):
|
def watcherGetUsername(self, watcherProtocol):
|
||||||
return self.getWatcher(watcherProtocol).name
|
return self.getWatcher(watcherProtocol).name
|
||||||
@ -162,21 +151,20 @@ class SyncFactory(Factory):
|
|||||||
return self.getWatcher(watcherProtocol).room
|
return self.getWatcher(watcherProtocol).room
|
||||||
|
|
||||||
def watcherSetRoom(self, watcherProtocol, room):
|
def watcherSetRoom(self, watcherProtocol, room):
|
||||||
with self.roomUsersLock:
|
watcher = self._removeWatcherFromTheRoom(watcherProtocol)
|
||||||
watcher = self._removeWatcherFromTheRoom(watcherProtocol)
|
if(not watcher):
|
||||||
if(not watcher):
|
return
|
||||||
return
|
watcher.resetStateTimer()
|
||||||
watcher.resetStateTimer()
|
oldRoom = watcher.room
|
||||||
oldRoom = watcher.room
|
self._createRoomIfDoesntExist(room)
|
||||||
self._createRoomIfDoesntExist(room)
|
self._rooms[room][watcherProtocol] = watcher
|
||||||
self._rooms[room][watcherProtocol] = watcher
|
self._deleteRoomIfEmpty(oldRoom)
|
||||||
self._deleteRoomIfEmpty(oldRoom)
|
if(self.isolateRooms): #this is trick to inform old room about leaving
|
||||||
if(self.isolateRooms): #this is trick to inform old room about leaving
|
l = lambda w: w.sendUserSetting(watcher.name, room, watcher.file, None)
|
||||||
l = lambda w: w.sendUserSetting(watcher.name, room, watcher.file, None)
|
|
||||||
self.broadcast(watcherProtocol, l)
|
|
||||||
watcher.room = room
|
|
||||||
l = lambda w: w.sendUserSetting(watcher.name, watcher.room, watcher.file, None)
|
|
||||||
self.broadcast(watcherProtocol, l)
|
self.broadcast(watcherProtocol, l)
|
||||||
|
watcher.room = room
|
||||||
|
l = lambda w: w.sendUserSetting(watcher.name, watcher.room, watcher.file, None)
|
||||||
|
self.broadcast(watcherProtocol, l)
|
||||||
|
|
||||||
def watcherSetFile(self, watcherProtocol, file_):
|
def watcherSetFile(self, watcherProtocol, file_):
|
||||||
watcher = self.getWatcher(watcherProtocol)
|
watcher = self.getWatcher(watcherProtocol)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user