Refactoring

This commit is contained in:
Uriziel 2012-07-10 14:39:25 +02:00
parent d9928e644a
commit 18681538e2
2 changed files with 31 additions and 11 deletions

View File

@ -11,10 +11,7 @@ from .network_utils import (
arg_count,
CommandProtocol,
)
from .utils import (
format_time,
parse_state,
)
from .utils import format_time
class SyncClientProtocol(CommandProtocol):
@ -59,7 +56,7 @@ class SyncClientProtocol(CommandProtocol):
@arg_count(4, 5)
def handle_connected_state(self, args):
args = parse_state(args)
args = self.__parseState(args)
if not args:
self.drop_with_error('Malformed state attributes')
return
@ -143,6 +140,32 @@ class SyncClientProtocol(CommandProtocol):
)
initial_state = 'init'
def __parseState(self, args):
if len(args) == 4:
counter, ctime, state, position = args
who_changed_state = None
elif len(args) == 5:
counter, ctime, state, position, who_changed_state = args
else:
return
if not state in ('paused', 'playing'):
return
paused = state == 'paused'
try:
counter = int(counter)
ctime = int(ctime)
position = int(position)
except ValueError:
return
ctime /= 1000.0
position /= 1000.0
return counter, ctime, paused, position, who_changed_state
class SyncClientFactory(ClientFactory):
def __init__(self, manager):
self.manager = manager

View File

@ -16,10 +16,7 @@ from twisted.web.iweb import IBodyProducer
from zope.interface import implements
from .utils import (
join_args,
split_args,
)
from .utils import ArgumentParser
def arg_count(minimum, maximum=None):
@ -44,7 +41,7 @@ class CommandProtocol(LineReceiver):
if not line:
return
args = split_args(line)
args = ArgumentParser.splitArguments(line)
if not args:
self.drop_with_error('Malformed line')
return
@ -73,7 +70,7 @@ class CommandProtocol(LineReceiver):
self._state = new_state
def send_message(self, *args):
line = join_args(args)
line = ArgumentParser.joinArguments(args)
#if args[0] not in ['ping', 'pong']:
# print '<<<', line
self.sendLine(line)