summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
diff options
context:
space:
mode:
authorMaZderMind <github@mazdermind.de>2015-05-14 20:41:03 +0200
committerMaZderMind <github@mazdermind.de>2015-05-14 20:41:03 +0200
commitb81461b9a433b8ac0c3b3a098e42b12faf9e8d27 (patch)
tree86791b5677a00a7e9daade692b99f42528478b8d /voctocore/lib/tcpsingleconnection.py
parentb3b9f5630ecfdb9ff7f4e6955fa875f80ae93f84 (diff)
Extract TCP Connection handling into separate classes
Diffstat (limited to 'voctocore/lib/tcpsingleconnection.py')
-rw-r--r--voctocore/lib/tcpsingleconnection.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/voctocore/lib/tcpsingleconnection.py b/voctocore/lib/tcpsingleconnection.py
new file mode 100644
index 0000000..d6a05ef
--- /dev/null
+++ b/voctocore/lib/tcpsingleconnection.py
@@ -0,0 +1,43 @@
+#!/usr/bin/python3
+import logging, socket
+from gi.repository import GObject
+
+from lib.config import Config
+
+class TCPSingleConnection(object):
+ log = logging.getLogger('TCPSingleConnection')
+
+ port = None
+
+ boundSocket = None
+ currentConnection = None
+
+ def __init__(self, port):
+ self.port = port
+
+ self.log.debug('Binding to Source-Socket on [::]:%u', port)
+ self.boundSocket = socket.socket(socket.AF_INET6)
+ self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
+ self.boundSocket.bind(('::', port))
+ self.boundSocket.listen(1)
+
+ self.log.debug('Setting GObject io-watch on Socket')
+ GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
+
+ def on_connect(self, sock, *args):
+ conn, addr = sock.accept()
+ self.log.info("Incomming Connection from %s", addr)
+
+ if self.currentConnection is not None:
+ self.log.warn("Another Source is already connected")
+ return True
+
+ self.on_accepted(conn, addr)
+ self.currentConnection = conn
+
+ return True
+
+ def close_connection(self):
+ self.currentConnection = None
+ self.log.info('Connection closed')