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, arg_count,
CommandProtocol, CommandProtocol,
) )
from .utils import ( from .utils import format_time
format_time,
parse_state,
)
class SyncClientProtocol(CommandProtocol): class SyncClientProtocol(CommandProtocol):
@ -59,7 +56,7 @@ class SyncClientProtocol(CommandProtocol):
@arg_count(4, 5) @arg_count(4, 5)
def handle_connected_state(self, args): def handle_connected_state(self, args):
args = parse_state(args) args = self.__parseState(args)
if not args: if not args:
self.drop_with_error('Malformed state attributes') self.drop_with_error('Malformed state attributes')
return return
@ -143,6 +140,32 @@ class SyncClientProtocol(CommandProtocol):
) )
initial_state = 'init' 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): class SyncClientFactory(ClientFactory):
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager

View File

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