From c6633dae0e1886ac415dcb74fa0b2bde1ea48e66 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 14 Sep 2014 23:37:56 -0400 Subject: introduce a pubsub system for communication --- server.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'server.py') diff --git a/server.py b/server.py index 3df5c92..8eb5a7e 100644 --- a/server.py +++ b/server.py @@ -2,13 +2,13 @@ import socket import threading import uuid +import pubsub import ssh import termcast class Server(object): def __init__(self): - self.termcast_connections = {} - self.ssh_connections = {} + self.publisher = pubsub.Publisher() def listen(self): ssh_sock = self._open_socket(2200) @@ -36,17 +36,13 @@ class Server(object): def handle_ssh_connection(self, client): self._handle_connection( client, - self.ssh_connections, - self.termcast_connections, - lambda client, connection_id: ssh.Connection(client, connection_id) + lambda client, connection_id: ssh.Connection(client, connection_id, self.publisher) ) def handle_termcast_connection(self, client): self._handle_connection( client, - self.termcast_connections, - self.ssh_connections, - lambda client, connection_id: termcast.Connection(client, connection_id) + lambda client, connection_id: termcast.Connection(client, connection_id, self.publisher) ) def _wait_for_connection(self, sock, cb): @@ -61,12 +57,12 @@ class Server(object): threading.Thread(target=cb, args=(client,)).start() - def _handle_connection(self, client, connection_store, other_store, cb): + def _handle_connection(self, client, cb): connection_id = uuid.uuid4().hex connection = cb(client, connection_id) - connection_store[connection_id] = connection - connection.run(other_store) - del connection_store[connection_id] + self.publisher.subscribe(connection) + connection.run() + self.publisher.unsubscribe(connection) def _open_socket(self, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -- cgit v1.2.3-54-g00ecf