Commits

Gabriel AHTUNE  committed f595f92

added secsgem/abc.py

  • Participants
  • Parent commits fbd8dab

Comments (0)

Files changed (1)

File secsgem/abc.py

+'''
+Package SECSGEM
+
+Created on 3 juin 2012
+
+'''
+
+__author__ = 'Gabriel AHTUNE'
+__version__ = '1.0'
+
+import time
+import threading
+from abc import ABCMeta, abstractmethod
+
+
+class SECSGEMEntity(threading.Thread, metaclass=ABCMeta):
+    """
+    Base class for a secsgem communication entity
+    """
+
+    def __init__(self, transport, sessionid):
+        threading.Thread.__init__(self)
+        self.daemon = True
+        self.transport = transport(sessionid=sessionid)
+        self.listeners = []
+        self.statelisteners = []
+        self._connected = False
+        self.getconnection = threading.Event()
+
+    @property
+    def connected(self):
+        return self._connected
+
+    @connected.setter
+    def connected(self, value):
+        self._connected = value
+        if value:
+            self.getconnection.set()
+        else:
+            self.getconnection.clear()
+
+    @connected.getter
+    def connected(self):
+        return self._connected
+
+    def addlistener(self, listener):
+        """
+        Add a listener to the secsgem message
+        """
+
+        self.listeners.append(listener)
+
+    def removelistener(self, listener):
+        """
+        Remove a listener to the secsgem message list of listeners
+        """
+
+        self.listeners.remove(listener)
+
+    def addstatelistener(self, listener):
+        """
+        Add a listener to the server state connection
+        """
+        self.statelisteners.append(listener)
+
+    def feed(self, msg):
+        """
+        Feed the listener of messages
+        """
+        for listener in self.listeners:
+            listener(msg)
+
+    def feedstate(self, msg):
+        """
+        Feed a message through the state channel
+        """
+        for listener in self.statelisteners:
+            listener(msg)
+        self.logger.info('connection state: %s' % str(msg))
+
+    def wait_connection(self):
+        """
+        Wait the entity to be connected
+        """
+        self.getconnection.wait()
+        time.sleep(0.1)
+
+    @abstractmethod
+    def connect(self):
+        raise NotImplementedError
+
+    @abstractmethod
+    def close(self):
+        raise NotImplementedError
+
+    @abstractmethod
+    def send(self, msg):
+        raise NotImplementedError