Minor updates and refactoring

This commit is contained in:
Uriziel 2012-09-28 17:08:36 +02:00
parent 14d107c3d2
commit 7ff433304b
2 changed files with 7 additions and 25 deletions

View File

@ -71,7 +71,7 @@ class SyncClientProtocol(CommandProtocol):
who, where, what, duration, size = args[0], args[1], None, None, None
self.__syncplayClient.users.addUser(SyncplayClientManager.SyncplayUser(who, what, where, duration, size))
if what:
message = '%s is present and is playing \'%s\' (%s) in the room: \'%s\'' % (who, what, format_time(float(duration)), where)
message = '%s is present and is playing \'%s\' (%s) in the room: \'%s\'' % (who, what, format_time(float(duration)), where) # TODO: move to "add User"
self.__syncplayClient.ui.showMessage(message)
self.__syncplayClient.checkIfFileMatchesOthers()
else:
@ -114,15 +114,10 @@ class SyncClientProtocol(CommandProtocol):
@argumentCount(5)
def playing(self, args):
who, where, what, duration, size = args
message = '%s is playing \'%s\' (%s) in the room: \'%s\'' % (who, what, format_time(float(duration)), where)
message = '%s is playing \'%s\' (%s) in the room: \'%s\'' % (who, what, format_time(float(duration)), where) # TODO: move to "add User"
self.__syncplayClient.ui.showMessage(message)
self.__syncplayClient.users.addUser(SyncplayClientManager.SyncplayUser(who, what, where, duration, size))
self.__syncplayClient.checkIfFileMatchesOthers()
@argumentCount(1)
def joined(self, args):
message = '%s joined' % args[0]
self.__syncplayClient.ui.showMessage(message)
@argumentCount(2)
def room(self, args):
@ -155,8 +150,7 @@ class SyncClientProtocol(CommandProtocol):
except ValueError:
return
ctime /= 1000.0
position /= 1000.0
position /= 1000.0
return counter, ctime, paused, position, who_changed_state
class _MessagesSender(object):

View File

@ -199,10 +199,7 @@ class SyncServerProtocol(CommandProtocol):
self.__protocol.sendMessage('present', who, where, what, duration, size)
else:
self.__protocol.sendMessage('present', who, where)
def send_joined(self, who):
self.__protocol.sendMessage('joined', who)
def send_left(self, who):
self.__protocol.sendMessage('left', who)
@ -268,7 +265,8 @@ class SyncFactory(Factory):
watcher_proto.sender.send_hello(name)
self.send_state_to(watcher)
self.send_ping_to(watcher)
self.broadcast(watcher, lambda receiver: receiver.watcher_proto.sender.send_room(watcher.name, watcher.room))
def removeWatcher(self, watcher_proto):
watcher = self.watchers.pop(watcher_proto, None)
if not watcher:
@ -353,24 +351,20 @@ class SyncFactory(Factory):
watcherPos = max(watcher.max_position, watcher.position + (0 if self.paused[watcher.room] else curtime-watcher.last_update))
minPos = watcherPos if watcherPos < min else min
minWatcher = watcher
return minPos, minWatcher
return minPos, minWatcher
def pong_received(self, watcher_proto, value, ctime):
watcher = self.watchers.get(watcher_proto)
if not watcher:
return
ping_time = watcher.pings_sent.pop(value, None)
if ping_time is not None:
curtime = time.time()
watcher.last_ping_received = curtime
watcher.ping = ping = (curtime - ping_time)/2
if watcher.time_offset_data is not None:
time_offset = curtime - (ctime + ping)
watcher.time_offset_data.append((ping, time_offset))
if len(watcher.time_offset_data) > 1:
pmin = min(p for p,_ in watcher.time_offset_data)
pmax = max(p for p,_ in watcher.time_offset_data) - pmin
@ -386,27 +380,21 @@ class SyncFactory(Factory):
if len(watcher.time_offset_data) > 20:
watcher.time_offset_data = None
#print watcher.name, 'last ping', watcher.ping, 'time offset %.6f' % watcher.time_offset
def send_ping_to(self, watcher):
if not watcher.active:
return
if (time.time()-watcher.last_update_sent) > 8:
self.removeWatcher(watcher.watcher_proto)
return
chars = None
while not chars or chars in watcher.pings_sent:
chars = random_chars()
curtime = time.time()
if curtime - watcher.last_ping_received > 8:
watcher.watcher_proto.drop()
return
watcher.watcher_proto.sender.send_ping(chars)
watcher.pings_sent[chars] = time.time()
if len(watcher.pings_sent) > 30:
watcher.pings_sent.pop(min((time, key) for key, time in watcher.pings_sent.iteritems())[1])
self.schedule_send_ping(watcher)