aboutsummaryrefslogtreecommitdiffstats
path: root/server.py
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-23 14:01:46 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-23 14:01:46 -0400
commit70cd632270083acc2bb835069d6e00f07a728cb3 (patch)
tree37298ef15ceed88a147ec668ce6977a9f1a5b942 /server.py
parentf44c44324c24aa667bfd56bb7b9ee2e96ac93bd4 (diff)
downloadpython-termcast-server-70cd632270083acc2bb835069d6e00f07a728cb3.tar.gz
python-termcast-server-70cd632270083acc2bb835069d6e00f07a728cb3.zip
reorganize
Diffstat (limited to 'server.py')
-rw-r--r--server.py82
1 files changed, 0 insertions, 82 deletions
diff --git a/server.py b/server.py
deleted file mode 100644
index c406e92..0000000
--- a/server.py
+++ /dev/null
@@ -1,82 +0,0 @@
-import signal
-import socket
-import sys
-import threading
-import uuid
-
-import pubsub
-import ssh
-import termcast
-
-class Server(object):
- def __init__(self, keyfile):
- self.publisher = pubsub.Publisher()
- self.keyfile = keyfile
-
- def listen(self):
- ssh_sock = self._open_socket(2200)
- termcast_sock = self._open_socket(2201)
-
- threading.Thread(
- target=lambda: self.wait_for_ssh_connection(ssh_sock)
- ).start()
- threading.Thread(
- target=lambda: self.wait_for_termcast_connection(termcast_sock)
- ).start()
-
- def wait_for_ssh_connection(self, sock):
- self._wait_for_connection(
- sock,
- lambda client: self.handle_ssh_connection(client)
- )
-
- def wait_for_termcast_connection(self, sock):
- self._wait_for_connection(
- sock,
- lambda client: self.handle_termcast_connection(client)
- )
-
- def handle_ssh_connection(self, client):
- self._handle_connection(
- client,
- lambda client, connection_id: ssh.Connection(
- client, connection_id, self.publisher, self.keyfile
- )
- )
-
- def handle_termcast_connection(self, client):
- self._handle_connection(
- client,
- lambda client, connection_id: termcast.Connection(
- client, connection_id, self.publisher
- )
- )
-
- def _wait_for_connection(self, sock, cb):
- while True:
- try:
- sock.listen(100)
- client, addr = sock.accept()
- except Exception as e:
- print('*** Listen/accept failed: ' + str(e))
- traceback.print_exc()
- continue
-
- threading.Thread(target=cb, args=(client,)).start()
-
- def _handle_connection(self, client, cb):
- connection_id = uuid.uuid4().hex
- connection = cb(client, 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)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind(('', port))
- return sock
-
-if __name__ == '__main__':
- server = Server(sys.argv[1])
- server.listen()