Make a base class, cleanup. I don' like underscores after all.
This commit is contained in:
parent
e87950716d
commit
6693450b47
49
syncplay/network_utils.py
Normal file
49
syncplay/network_utils.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#coding:utf8
|
||||||
|
|
||||||
|
from twisted.protocols.basic import LineReceiver
|
||||||
|
|
||||||
|
class CommandProtocol(LineReceiver):
|
||||||
|
states = None
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._state = self.initial_state
|
||||||
|
|
||||||
|
def lineReceived(self, line):
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
return
|
||||||
|
line = line.split(None, 1)
|
||||||
|
if len(line) != 2:
|
||||||
|
self.drop_with_error('Malformed line')
|
||||||
|
return
|
||||||
|
command, arg = line
|
||||||
|
|
||||||
|
available_commands = self.states.get(self._state)
|
||||||
|
handler = available_commands.get(command)
|
||||||
|
if handler:
|
||||||
|
handler = getattr(self, handler, None)
|
||||||
|
if not handler:
|
||||||
|
self.drop_with_error('Unknown command: `%s`' % command)
|
||||||
|
return # TODO log it too
|
||||||
|
|
||||||
|
handler(arg)
|
||||||
|
|
||||||
|
def change_state(self, new_state):
|
||||||
|
if new_state not in self.states:
|
||||||
|
raise RuntimeError('Unknown state: %s' % new_state)
|
||||||
|
self._state = new_state
|
||||||
|
|
||||||
|
def send_message(self, *args):
|
||||||
|
self.sendLine(' '.join(
|
||||||
|
(arg if isinstance(arg, basestring) else str(arg))
|
||||||
|
for arg in args
|
||||||
|
if arg is not None
|
||||||
|
))
|
||||||
|
|
||||||
|
def drop(self):
|
||||||
|
self.transport.loseConnection()
|
||||||
|
|
||||||
|
def drop_with_error(self, error):
|
||||||
|
self.send_message('error', error)
|
||||||
|
self.drop()
|
||||||
|
|
||||||
@ -3,80 +3,37 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from twisted.internet.protocol import Factory
|
from twisted.internet.protocol import Factory
|
||||||
from twisted.protocols.basic import LineReceiver
|
|
||||||
|
|
||||||
class SyncProtocol(LineReceiver):
|
from .network_utils import CommandProtocol
|
||||||
|
|
||||||
|
class SyncServerProtocol(CommandProtocol):
|
||||||
def __init__(self, factory):
|
def __init__(self, factory):
|
||||||
self._factory = factory
|
CommandProtocol.__init__(self)
|
||||||
|
|
||||||
self._state = 'init'
|
self.factory = factory
|
||||||
self._active = False
|
|
||||||
|
|
||||||
def connectionMade(self):
|
|
||||||
self._active = True
|
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
def connectionLost(self, reason):
|
||||||
self._active = False
|
self.factory.remove_watcher(self)
|
||||||
self._factory.remove_watcher(self)
|
|
||||||
|
|
||||||
def lineReceived(self, line):
|
def get_ident(self):
|
||||||
line = line.strip()
|
|
||||||
if not line:
|
|
||||||
return
|
|
||||||
line = line.split(None, 1)
|
|
||||||
if len(line) != 2:
|
|
||||||
self._drop_with_error('Malformed line')
|
|
||||||
return
|
|
||||||
command, arg = line
|
|
||||||
|
|
||||||
available_commands = self.states.get(self._state)
|
|
||||||
if not available_commands:
|
|
||||||
return # TODO log it
|
|
||||||
|
|
||||||
handler = available_commands.get(command)
|
|
||||||
if handler:
|
|
||||||
handler = getattr(self, handler, None)
|
|
||||||
if not handler:
|
|
||||||
self._drop_with_error('Unknown command: `%s`' % command)
|
|
||||||
return # TODO log it too
|
|
||||||
|
|
||||||
handler(arg)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_ident(self):
|
|
||||||
return '|'.join((
|
return '|'.join((
|
||||||
self.transport.getPeer().host,
|
self.transport.getPeer().host,
|
||||||
str(id(self)),
|
str(id(self)),
|
||||||
))
|
))
|
||||||
|
|
||||||
def _send(self, *args):
|
def handle_init_iam(self, arg):
|
||||||
self.sendLine(' '.join(
|
self.factory.add_watcher(self, arg.strip())
|
||||||
(arg if isinstance(arg, basestring) else str(arg))
|
self.change_state('connected')
|
||||||
for arg in args
|
|
||||||
if arg is not None
|
|
||||||
))
|
|
||||||
|
|
||||||
def _drop(self):
|
def handle_connected_state(self, arg):
|
||||||
self._active = False
|
|
||||||
self.transport.loseConnection()
|
|
||||||
|
|
||||||
def _drop_with_error(self, error):
|
|
||||||
self._send('error', error)
|
|
||||||
self._drop()
|
|
||||||
|
|
||||||
def _handle_init_iam(self, arg):
|
|
||||||
self._factory.add_watcher(self, arg.strip())
|
|
||||||
self._state = 'connected'
|
|
||||||
|
|
||||||
def _handle_connected_state(self, arg):
|
|
||||||
arg = arg.split(None, 1)
|
arg = arg.split(None, 1)
|
||||||
if len(arg) != 2:
|
if len(arg) != 2:
|
||||||
self._drop_with_error('Malformed state attributes')
|
self.drop_with_error('Malformed state attributes')
|
||||||
return
|
return
|
||||||
state, position = arg
|
state, position = arg
|
||||||
|
|
||||||
if not state in ('paused', 'playing'):
|
if not state in ('paused', 'playing'):
|
||||||
self._drop_with_error('Unknown state')
|
self.drop_with_error('Unknown state')
|
||||||
return
|
return
|
||||||
|
|
||||||
paused = state == 'paused'
|
paused = state == 'paused'
|
||||||
@ -84,43 +41,44 @@ class SyncProtocol(LineReceiver):
|
|||||||
try:
|
try:
|
||||||
position = int(position)
|
position = int(position)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._drop_with_error('Invalid position numeral')
|
self.drop_with_error('Invalid position numeral')
|
||||||
|
|
||||||
position /= 100.0
|
position /= 100.0
|
||||||
|
|
||||||
self._factory.update_state(self, paused, position)
|
self.factory.update_state(self, paused, position)
|
||||||
|
|
||||||
def _handle_connected_seek(self, arg):
|
def handle_connected_seek(self, arg):
|
||||||
try:
|
try:
|
||||||
position = int(arg)
|
position = int(arg)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._drop_with_error('Invalid position numeral')
|
self.drop_with_error('Invalid position numeral')
|
||||||
|
|
||||||
position /= 100.0
|
position /= 100.0
|
||||||
|
|
||||||
self._factory.seek(self, position)
|
self.factory.seek(self, position)
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self._get_ident())
|
return hash(self.get_ident())
|
||||||
|
|
||||||
|
|
||||||
def send_state(self, paused, position, who_last_changed):
|
def send_state(self, paused, position, who_last_changed):
|
||||||
self._send('state', ('paused' if paused else 'playing'), int(position*100), who_last_changed)
|
self.send_message('state', ('paused' if paused else 'playing'), int(position*100), who_last_changed)
|
||||||
|
|
||||||
def send_seek(self, position, who_seeked):
|
def send_seek(self, position, who_seeked):
|
||||||
self._send('seek', int(position*100), who_seeked)
|
self.send_message('seek', int(position*100), who_seeked)
|
||||||
|
|
||||||
|
|
||||||
states = dict(
|
states = dict(
|
||||||
init = dict(
|
init = dict(
|
||||||
iam = '_handle_init_iam',
|
iam = 'handle_init_iam',
|
||||||
),
|
),
|
||||||
connected = dict(
|
connected = dict(
|
||||||
state = '_handle_connected_state',
|
state = 'handle_connected_state',
|
||||||
seek = '_handle_connected_seek',
|
seek = 'handle_connected_seek',
|
||||||
#ping = '_handle_connected_ping',
|
#ping = 'handle_connected_ping',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
initial_state = 'init'
|
||||||
|
|
||||||
|
|
||||||
class WatcherInfo(object):
|
class WatcherInfo(object):
|
||||||
@ -151,13 +109,13 @@ class SyncFactory(Factory):
|
|||||||
self.update_time_limit = update_time_limit
|
self.update_time_limit = update_time_limit
|
||||||
|
|
||||||
def buildProtocol(self, addr):
|
def buildProtocol(self, addr):
|
||||||
return SyncProtocol(self)
|
return SyncServerProtocol(self)
|
||||||
|
|
||||||
|
|
||||||
def add_watcher(self, watcher_proto, name):
|
def add_watcher(self, watcher_proto, name):
|
||||||
watcher = WatcherInfo(watcher_proto, name)
|
watcher = WatcherInfo(watcher_proto, name)
|
||||||
self.watchers[watcher_proto] = watcher
|
self.watchers[watcher_proto] = watcher
|
||||||
self._send_state_to(watcher)
|
self.send_state_to(watcher)
|
||||||
# send info someone joined
|
# send info someone joined
|
||||||
|
|
||||||
def remove_watcher(self, watcher_proto):
|
def remove_watcher(self, watcher_proto):
|
||||||
@ -186,23 +144,23 @@ class SyncFactory(Factory):
|
|||||||
else:
|
else:
|
||||||
pause_changed = False
|
pause_changed = False
|
||||||
|
|
||||||
position = self._find_position()
|
position = self.find_position()
|
||||||
for receiver in self.watchers.itervalues():
|
for receiver in self.watchers.itervalues():
|
||||||
if (
|
if (
|
||||||
receiver == watcher or
|
receiver == watcher or
|
||||||
pause_changed or
|
pause_changed or
|
||||||
(curtime-receiver.last_update_sent) > self.update_time_limit
|
(curtime-receiver.last_update_sent) > self.update_time_limit
|
||||||
):
|
):
|
||||||
self._send_state_to(receiver, position, curtime)
|
self.send_state_to(receiver, position, curtime)
|
||||||
|
|
||||||
def seek(self, watcher_proto, position):
|
def seek(self, watcher_proto, position):
|
||||||
#TODO
|
#TODO
|
||||||
#for receiver in self.watchers.itervalues():
|
#for receiver in self.watchers.itervalues():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _send_state_to(self, watcher, position=None, curtime=None):
|
def send_state_to(self, watcher, position=None, curtime=None):
|
||||||
if position is None:
|
if position is None:
|
||||||
position = self._find_position()
|
position = self.find_position()
|
||||||
if curtime is None:
|
if curtime is None:
|
||||||
curtime = time.time()
|
curtime = time.time()
|
||||||
if self.pause_change_by:
|
if self.pause_change_by:
|
||||||
@ -211,7 +169,7 @@ class SyncFactory(Factory):
|
|||||||
watcher.watcher_proto.send_state(self.paused, position, None)
|
watcher.watcher_proto.send_state(self.paused, position, None)
|
||||||
watcher.last_update_sent = curtime
|
watcher.last_update_sent = curtime
|
||||||
|
|
||||||
def _find_position(self):
|
def find_position(self):
|
||||||
curtime = time.time()
|
curtime = time.time()
|
||||||
try:
|
try:
|
||||||
return min(
|
return min(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user