From 2529c2d238e6c29e031ccbced9c3c33f38a4a873 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 2 Apr 2017 20:10:59 +0000 Subject: [PATCH] Rewrite session and connection A session and connection now have a room assigned to them for their lifetime. You can't connect a session to another room. The launch() function must only be called once. --- yaboli/connection.py | 51 +++++++++++++++++--------------------------- yaboli/session.py | 49 +++++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/yaboli/connection.py b/yaboli/connection.py index ec36aee..690b66a 100644 --- a/yaboli/connection.py +++ b/yaboli/connection.py @@ -14,7 +14,7 @@ SSLOPT = {"ca_certs": ssl.get_default_verify_paths().cafile} ROOM_FORMAT = "wss://euphoria.io/room/{}/ws" logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) +#logger.setLevel(logging.INFO) class Connection(): """ @@ -30,18 +30,20 @@ class Connection(): - "stop" """ - def __init__(self, url_format=ROOM_FORMAT, tries=10, delay=30): + def __init__(self, room, url_format=ROOM_FORMAT, tries=10, delay=30): """ url_format - url the bot will connect to, where the room name is represented by {} tries - how often to try to reconnect when connection is lost (-1 - try forever) delay - time (in seconds) to wait between tries """ - self.room = None + self.room = room self.tries = tries self.delay = delay self.url_format = url_format + self.start_time = None + self._stopping = True self._ws = None @@ -102,22 +104,6 @@ class Connection(): return False - def _launch(self): - """ - _launch() -> Thread - - Connect to the room and spawn a new thread. - """ - - self._stopping = False - self._thread = threading.Thread( - target=self._run, - name="{}-{}".format(int(time.time()), self.room) - ) - logger.debug("Launching new thread: {}".format(self._thread.name)) - self._thread.start() - return self._thread - def _run(self): """ _run() -> None @@ -182,26 +168,27 @@ class Connection(): except WSException: self.disconnect() - def connected(self): - return self._ws - - def connect_to(self, room): + def launch(self): """ - connect_to(room) -> bool + launch() -> bool - Attempts to connect to the room and spawns a new thread if it's successful. - Returns True if connection was sucessful, else False. + Connect to the room and spawn a new thread. + Returns True if connecting was successful and a new thread was spawned. """ - self.stop() - - logger.debug("Connecting to &{}.".format(room)) - self.room = room if self._connect(1): - self._launch() + self.start_time = time.time() + self._stopping = False + + self._thread = threading.Thread( + target=self._run, + name="{}-{}".format(int(self.start_time), self.room) + ) + + logger.debug("Launching new thread: {}".format(self._thread.name)) + self._thread.start() return True else: - logger.warn("Could not connect to &{}.".format(room)) return False def disconnect(self): diff --git a/yaboli/session.py b/yaboli/session.py index dea1613..6931c02 100644 --- a/yaboli/session.py +++ b/yaboli/session.py @@ -28,14 +28,17 @@ class Session(): """ - def __init__(self, name=None): + def __init__(self, room, password=None, name=None, timeout=10): + self.password = password + self.real_name = name + self._room_accessible = False self._room_accessible_event = threading.Event() - self._room_accessible_timeout = None + self._room_accessible_timeout = threading.Timer(timeout, self.stop) - self._connection = Connection() + self._connection = Connection(room) self._connection.subscribe("disconnect", self._reset_variables) - + # and now the packet types self._connection.subscribe("bounce-event", self._handle_bounce_event) self._connection.subscribe("disconnect-event", self._handle_disconnect_event) self._connection.subscribe("hello-event", self._handle_hello_event) @@ -53,9 +56,6 @@ class Session(): self._callbacks = Callbacks() self.subscribe("enter", self._on_enter) - self.password = None - self.real_name = name - #self._hello_event_completed = False #self._snapshot_event_completed = False #self._ready = False @@ -66,30 +66,12 @@ class Session(): self._reset_variables() - def switch_to(self, room, password=None, timeout=10): - logger.info("Switching to &{}.".format(room)) - self.password = password - - if self._room_accessible_timeout: - self._room_accessible_timeout.cancel() - self._room_accessible_timeout = threading.Timer(timeout, self.stop) - self._room_accessible_timeout.start() - - if self._connection.connect_to(room): - logger.debug("Connection established. Waiting for correct events") - self._room_accessible_event.wait() - return self._room_accessible - else: - logger.warn("Could not connect to room url.") - return False - def _reset_variables(self): logger.debug("Resetting room-related variables") self._room_accessible = False self.my_session = SessionView(None, None, None, None, None) self.sessions = {} - self._room_accessible_event.clear() self._hello_event_completed = False self._snapshot_event_completed = False @@ -114,6 +96,19 @@ class Session(): if self.real_name: self._set_name(self.real_name) + def launch(self, timeout=10): + logger.info("Launching session &{}.".format(room)) + + self._room_accessible_timeout.start() + + if self._connection.launch(room): + logger.debug("Connection established. Waiting for correct events") + self._room_accessible_event.wait() + return self._room_accessible + else: + logger.warn("Could not connect to room url.") + return False + def launch(self): return self._connection.launch() @@ -153,6 +148,10 @@ class Session(): def room(self): return self._connection.room + @property + def start_time(self): + return self._connection.start_time + def refresh_sessions(self): logger.debug("Refreshing sessions") self._connection.send_packet("who")