diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-09-23 02:04:39 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-09-23 02:04:39 -0400 |
commit | 96a8d924c37be6dc8f3d12fdc8eb33111930554d (patch) | |
tree | 10bcf4f7d28e9d195c00b7900ea154765ee35077 /ssh.py | |
parent | d158e292536dc139379f1beb95891892dbd321d0 (diff) | |
download | python-termcast-server-96a8d924c37be6dc8f3d12fdc8eb33111930554d.tar.gz python-termcast-server-96a8d924c37be6dc8f3d12fdc8eb33111930554d.zip |
disconnect watchers when the streamer disconnects
Diffstat (limited to 'ssh.py')
-rw-r--r-- | ssh.py | 57 |
1 files changed, 40 insertions, 17 deletions
@@ -1,4 +1,6 @@ +import multiprocessing import paramiko +import select import threading import time @@ -22,6 +24,8 @@ class Connection(object): self.initialized = False self.watching_id = None + self.rpipe, self.wpipe = multiprocessing.Pipe(False) + def run(self): self.server = Server() self.transport.start_server(server=self.server) @@ -52,24 +56,24 @@ class Connection(object): self.publisher.notify("new_viewer", self.watching_id) while True: - c = self.chan.recv(1) - if c == b'q': - print( - "viewer stopped watching %s (%s)" % ( - streamer["name"], streamer["id"] - ) - ) - self.publisher.notify( - "viewer_disconnect", self.watching_id - ) - self.chan.send( - ("\033[1;%d;1;%dr" - + "\033[m" - + "\033[?9l\033[?1000l" - + "\033[H\033[2J") % ( - self.server.rows, self.server.cols + rout, wout, eout = select.select( + [self.chan, self.rpipe], + [], + [] + ) + if self.chan in rout: + c = self.chan.recv(1) + if c == b'q': + print( + "viewer stopped watching %s (%s)" % ( + streamer["name"], streamer["id"] + ) ) - ) + self._cleanup_watcher() + break + + if self.rpipe in rout: + self._cleanup_watcher() break if self.chan is not None: @@ -112,6 +116,12 @@ class Connection(object): self.chan.send(data) + def msg_streamer_disconnect(self, connection_id): + if self.watching_id != connection_id: + return + + self.wpipe.send("q") + def _display_streamer_screen(self, streamers): self.chan.send("\033[H\033[2JWelcome to Termcast!") self.chan.send( @@ -143,6 +153,19 @@ class Connection(object): row += 1 self.chan.send("\033[%dHChoose a stream: " % (row + 1)) + def _cleanup_watcher(self): + self.publisher.notify( + "viewer_disconnect", self.watching_id + ) + self.chan.send( + ("\033[1;%d;1;%dr" + + "\033[m" + + "\033[?9l\033[?1000l" + + "\033[H\033[2J") % ( + self.server.rows, self.server.cols + ) + ) + class Server(paramiko.ServerInterface): def __init__(self): super() |