Show joins, lefts, and present people after join

This commit is contained in:
Tomasz Fluxid Kowalczyk 2012-02-04 18:07:54 +01:00
parent eaaa661099
commit 3c0b8154ac
2 changed files with 59 additions and 3 deletions

View File

@ -29,6 +29,21 @@ class SyncClientProtocol(CommandProtocol):
self.manager.stop()
CommandProtocol.handle_error(self, args)
@arg_count(0)
def handle_init_hello(self, args):
self.change_state('connected')
@arg_count(1, 2)
def handle_init_present(self, args):
if len(args) == 2:
who, what = args
else:
who, what = args[0], None
if what:
print '%s is present and is playing %s' % (who, what)
else:
print '%s is present' % who
@arg_count(3, 4)
def handle_connected_state(self, args):
args = parse_state(args)
@ -47,7 +62,16 @@ class SyncClientProtocol(CommandProtocol):
@arg_count(2)
def handle_connected_playing(self, args):
who, what = args
print '%s plays %s' % (who, what)
print '%s is playing %s' % (who, what)
@arg_count(1)
def handle_connected_joined(self, args):
print '%s joined' % args[0]
@arg_count(1)
def handle_connected_left(self, args):
print '%s left' % args[0]
def send_state(self, counter, paused, position):
self.send_message('state', counter, ('paused' if paused else 'playing'), int(position*1000))
@ -57,14 +81,20 @@ class SyncClientProtocol(CommandProtocol):
states = dict(
init = dict(
present = 'handle_init_present',
hello = 'handle_init_hello',
),
connected = dict(
state = 'handle_connected_state',
seek = 'handle_connected_seek',
ping = 'handle_connected_ping',
playing = 'handle_connected_playing',
joined = 'handle_connected_joined',
left = 'handle_connected_left',
),
)
initial_state = 'connected'
initial_state = 'init'
class SyncClientFactory(ClientFactory):
def __init__(self, manager):

View File

@ -89,6 +89,21 @@ class SyncServerProtocol(CommandProtocol):
def send_playing(self, who, what):
self.send_message('playing', who, what)
def send_present(self, who, what):
if what:
self.send_message('present', who, what)
else:
self.send_message('present', who)
def send_joined(self, who):
self.send_message('joined', who)
def send_left(self, who):
self.send_message('left', who)
def send_hello(self):
self.send_message('hello')
states = dict(
init = dict(
@ -110,6 +125,7 @@ class WatcherInfo(object):
self.name = name
self.position = 0
self.filename = None
self.max_position = 0
self.last_update = None
self.last_update_sent = None
@ -139,12 +155,20 @@ class SyncFactory(Factory):
def add_watcher(self, watcher_proto, name):
watcher = WatcherInfo(watcher_proto, name)
self.watchers[watcher_proto] = watcher
for receiver in self.watchers.itervalues():
if receiver == watcher:
continue
receiver.watcher_proto.send_joined(name)
watcher_proto.send_present(receiver.name, receiver.filename)
watcher_proto.send_hello()
self.send_state_to(watcher)
self.schedule_send_ping(watcher_proto)
# send info someone joined
def remove_watcher(self, watcher_proto):
watcher = self.watchers.pop(watcher_proto, None)
for receiver in self.watchers.itervalues():
if receiver != watcher:
receiver.watcher_proto.send_left(watcher.name)
if self.pause_change_by == watcher:
self.pause_change_time = None
self.pause_change_by = None
@ -251,6 +275,8 @@ class SyncFactory(Factory):
if not watcher:
return
watcher.filename = filename
for receiver in self.watchers.itervalues():
if receiver != watcher:
receiver.watcher_proto.send_playing(watcher.name, filename)