Author: jerome
Date: 2009-12-08 16:05:44 +0100 (Tue, 08 Dec 2009)
New Revision: 5958

Added:
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNClient.py
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNServer.py
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNSimpleTest.py
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNStressTest.py
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/__init__.py
   
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/version.py
Log:
* Added IPN package.

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNClient.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNClient.py
                          (rev 0)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNClient.py
  2009-12-08 15:05:44 UTC (rev 5958)
@@ -0,0 +1,354 @@
+# -*- coding: utf-8 -*-
+
+import version
+__author__ = version.author
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyright (C) 2009 Kysoh Sa
+#    Remi Jocaille <remi.jocai...@kysoh.com>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import socket
+import threading
+import time
+
+# 
==============================================================================
+# Public class
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Interprocess Notifier Client Class.
+# 
------------------------------------------------------------------------------
+class IPNClient(object):
+    """Interprocess Notifier Client Class.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, host = '127.0.0.1', port = 271):
+        """Constructor.
+        @param host: Host address of the server.
+        @param port: Host port of the server.
+        """
+        self.__host = host
+        self.__port = port
+        self.__socket = None
+        self.__run = False
+        self.__runThread = None
+        self.__runMutex = threading.Lock()
+        self.__onNotification = None
+        self.__onConnected = None
+        self.__onDisconnected = None
+        self.__notifyThreadsList = []
+        self.__ntlMutex = threading.Lock()
+        self.__id = "0"
+        self.__msgStack = IPNClientStack()
+
+    # 
--------------------------------------------------------------------------
+    # Get the indentifier of the client.
+    # 
--------------------------------------------------------------------------
+    def getId(self):
+        """Get the indentifier of the client.
+        @return: The identifier if connected ortherwise '0' as string.
+        """
+        return self.__id
+
+    # 
--------------------------------------------------------------------------
+    # Register a callback function to the "On notification" event.
+    # 
--------------------------------------------------------------------------
+    def registerOnNotificationCallBack(self, funct):
+        """Register a callback function to the "On notification" event.
+        @param funct: Function pointer. The function must accept one parameter.
+                      Example :
+                      def onNotification(message):
+                          print message
+        """
+        self.__onNotification = funct
+
+    # 
--------------------------------------------------------------------------
+    # Register a callback function to the "On connected" event.
+    # 
--------------------------------------------------------------------------
+    def registerOnConnectedCallBack(self, funct):
+        """Register a callback function to the "On connected" event.
+        @param funct: Function pointer. The function must accept one parameter.
+                      Example :
+                      def onConnected(identifier):
+                          print "Client connected with identifier :", 
identifier
+        """
+        self.__onConnected = funct
+
+    # 
--------------------------------------------------------------------------
+    #
+    # 
--------------------------------------------------------------------------
+    def registerOnDisconnectedCallBack(self, funct):
+        """Register a callback function to the "On disconnected" event.
+        @param funct: Function pointer.
+                      Example :
+                      def onDisconnected():
+                          print "Client disconnected"
+        """
+        self.__onDisconnected = funct
+
+    # 
--------------------------------------------------------------------------
+    # Start the client.
+    # 
--------------------------------------------------------------------------
+    def start(self):
+        """Start the client.
+        @return: The success of the client start.
+        """
+        # Exit the function if the client is already started
+        if self.__getRun():
+            return True
+        # Create the client socket
+        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        # Set the socket to blocking mode before to connect it to the server
+        self.__socket.setblocking(1)
+        try:
+            # Connect the client to the server
+            self.__socket.connect((self.__host, self.__port))
+            # Read my client identifier
+            self.__id = self.__socket.recv(128).split('\n')[0]
+        except socket.timeout:
+            self.__setRun(False)
+            self.__socket.setblocking(0)
+            # Failed to connect the client to the server
+            return False
+        except socket.error:
+            self.__setRun(False)
+            self.__socket.setblocking(0)
+            # Failed to connect the client to the server
+            return False
+        # Set the socket to unblocking mode
+        self.__socket.setblocking(0)
+        # Set the socket timeout to 100 msec
+        self.__socket.settimeout(0.1)
+        # Call the "On connected" event
+        if self.__onConnected != None:
+            self.__onConnected(self.__id)
+        # Start the message listening loop
+        self.__runThread = threading.Thread(target = self.__runLoop)
+        self.__runThread.start()
+        time.sleep(0.1)
+        # The client is successfuly connected to the server
+        return True
+
+    # 
--------------------------------------------------------------------------
+    # Stop the client.
+    # 
--------------------------------------------------------------------------
+    def stop(self):
+        """Stop the client.
+        """
+        # Exit the function is the client is not started
+        if not self.__getRun():
+            return
+        # Stop the message listening loop
+        self.__setRun(False)
+        # Ensure that the thread of the message listening loop has been closed
+        if self.__runThread.isAlive():
+            if not self.__runThread.join(5.0):
+                self.__runThread._Thread__stop()
+        # Ensure that the notifications thread has been closed.
+        self.__stopNotifyThreadList()
+
+    # 
--------------------------------------------------------------------------
+    # Notify a message to the server.
+    # 
--------------------------------------------------------------------------
+    def notify(self, message):
+        """Notify a message to the server.
+        """
+        # Exit the function is the client is not started
+        if not self.__getRun():
+            return
+        # Regularize the message length (0 > correct size < 124)
+        if len(message) > 123:
+            message = message[:122]
+        self.__msgStack.push(message)
+
+    # 
--------------------------------------------------------------------------
+    # Add thread in the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __addNotifyThread(self, thread):
+        """Add thread in the threaded messages list.
+        @param thread: Thread to be added.
+        """
+        self.__ntlMutex.acquire()
+        self.__notifyThreadsList.append(thread)
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Wait that the client has stopped.
+    # 
--------------------------------------------------------------------------
+    def waitStop(self):
+        """Wait that the client has stopped.
+        """
+        while self.__getRun():
+            time.sleep(0.5)
+        time.sleep(0.5)
+
+    # 
--------------------------------------------------------------------------
+    # Clean the closed thread from the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __cleanNotifyThreadList(self):
+        """Clean the closed thread from the threaded messages list in order to
+        avoiding a memory leak issue.
+        """
+        self.__ntlMutex.acquire()
+        newLst = []
+        for t in self.__notifyThreadsList:
+            if t.isAlive():
+                newLst.append(t)
+        self.__notifyThreadsList = newLst
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Stop all threads from the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __stopNotifyThreadList(self):
+        """Stop all threads from the threaded messages list.
+        """
+        self.__ntlMutex.acquire()
+        for t in self.__notifyThreadsList:
+            if t.isAlive():
+                # Wait for a hypothetical self closing of the thread
+                if not t.join(0.1):
+                    # Otherwise, kill it
+                    t._Thread__stop()
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Get the connection state of the client.
+    # 
--------------------------------------------------------------------------
+    def __getRun(self):
+        """Get the connection state of the client.
+        @return: True or False.
+        """
+        self.__runMutex.acquire()
+        result = self.__run
+        self.__runMutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Set the connection state of the client.
+    # 
--------------------------------------------------------------------------
+    def __setRun(self, value = True):
+        """Set the connection state of the client.
+        @param value: New value (True or False)
+        """
+        self.__runMutex.acquire()
+        self.__run = value
+        self.__runMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Loop listening message.
+    # 
--------------------------------------------------------------------------
+    def __runLoop(self):
+        """Loop listening message.
+        """
+        self.__setRun(True)
+        while self.__getRun():
+            # Remove the closed threads from the threads list (garbage 
cleaning)
+            self.__cleanNotifyThreadList()
+            try:
+                # Wait a message from the server. (timeout at 100msec, defined
+                # in the function "start()")
+                data = self.__socket.recv(128)
+                # Extract the message from the frame
+                data = data.split('\n')[0]
+                # If the message is valid
+                if len(data) != 0:
+                    # It's a PING
+                    if data == "PING":
+                        # Responding to the server
+                        self.__socket.send("PONG")
+                        time.sleep(0.01)
+                        continue
+                    # It's a notification request message
+                    elif data == "GETNOTIFICATION":
+                        # Responding to the server
+                        message = self.__msgStack.pop()
+                        if message == None:
+                            self.__socket.send("PONG")
+                        else:
+                            self.__socket.send("RET:%s" % message)
+                        time.sleep(0.01)
+                        continue
+                    # It's a notification message
+                    else:
+                        if self.__onNotification != None:
+                            # Call the "On notification" event through a thread
+                            # Store the thread in the threads list in order to
+                            # stop all threads at the client closure
+                            t = threading.Thread(target = 
self.__onNotification,
+                                args = (data,))
+                            self.__addNotifyThread(t)
+                            t.start()
+                        time.sleep(0.01)
+                        continue
+            except socket.timeout:
+                time.sleep(0.01)
+                # No message from the server ...
+                continue
+            except socket.error:
+                time.sleep(0.01)
+                # Server connection was broken, exit the loop !
+                break
+            except:
+                time.sleep(0.01)
+                # Unexpected error, should never happen ...
+                continue
+        # The client must be disconnected
+        try:
+            self.__socket.close()
+        except:
+            pass
+        # Call the "On disconnected" event and reset the client identifier
+        if self.__id != "0":
+            if self.__onDisconnected != None:
+                self.__onDisconnected()
+            self.__id = "0"
+
+# 
------------------------------------------------------------------------------
+# Interprocess Notifier Client Stack Class.
+# 
------------------------------------------------------------------------------
+class IPNClientStack(object):
+    """Interprocess Notifier Client Stack Class.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor.
+    # 
--------------------------------------------------------------------------
+    def __init__(self):
+        """Constructor.
+        """
+        self.__stack = []
+        self.__mutex = threading.Lock()
+
+    # 
--------------------------------------------------------------------------
+    # Push a message to the stack.
+    # 
--------------------------------------------------------------------------
+    def push(self, message):
+        """Push a message to the stack.
+        @param message: Message to push.
+        """
+        self.__mutex.acquire()
+        self.__stack.insert(len(self.__stack), message)
+        self.__mutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Pop a message from the stack.
+    # 
--------------------------------------------------------------------------
+    def pop(self):
+        """Pop a message from the stack.
+        @return: A message or None.
+        """
+        result = None
+        self.__mutex.acquire()
+        if len(self.__stack) > 0:
+            result = self.__stack.pop(0)
+        self.__mutex.release()
+        return result

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNServer.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNServer.py
                          (rev 0)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNServer.py
  2009-12-08 15:05:44 UTC (rev 5958)
@@ -0,0 +1,475 @@
+# -*- coding: utf-8 -*-
+
+import version
+__author__ = version.author
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyright (C) 2009 Kysoh Sa
+#    Remi Jocaille <remi.jocai...@kysoh.com>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import socket
+import threading
+import time
+
+try:
+    from hashlib import md5
+except:
+    from md5 import md5
+
+#from tuxisalive.lib.logger import *
+
+# Formated PING command
+_PING_CMD = "PING\n" + "".join(" " * 123)
+# Formated GETNOTIFICATION command
+_GETNOTIFICATION_CMD = "GETNOTIFICATION\n" + "".join(" " * 112)
+
+# 
==============================================================================
+# Public class
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Interprocess Notifier Server Class.
+# 
------------------------------------------------------------------------------
+class IPNServer(object):
+    """Interprocess Notifier Server Class.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, host = '127.0.0.1', port = 271):
+        """Constructor.
+        @param host: Host IP to listen.
+                     Example : '127.0.0.1' for local loop only.
+                     Example : '192.168.0.1' for local network only.
+                     Example : '' for internet access.
+        @param port: TCP port to listen.
+        """
+        self.__cliLst = []
+        self.__cliMutex = threading.Lock()
+        self.__socket = None
+        self.__host = host
+        self.__port = port
+        self.__runLst = False
+        self.__runLstThread = None
+        self.__runLstMutex = threading.Lock()
+        self.__runPing = False
+        self.__runPingThread = None
+        self.__runPingMutex = threading.Lock()
+        self.__notifyThreadsList = []
+        self.__ntlMutex = threading.Lock()
+        self.__onClientAdded = None
+        self.__onClientRemoved = None
+        self.__onClientNotification = None
+
+    # 
--------------------------------------------------------------------------
+    # Register a callback function to the "On client added" event.
+    # 
--------------------------------------------------------------------------
+    def registerOnClientAddedCallBack(self, funct):
+        """Register a callback function to the "On client added" event.
+        @param funct: Function pointer. The function must accept one parameter.
+                      Example :
+                      def onClientAdded(idClient):
+                          print idClient
+        """
+        self.__onClientAdded = funct
+
+    # 
--------------------------------------------------------------------------
+    # Register a callback function to the "On client removed" event.
+    # 
--------------------------------------------------------------------------
+    def registerOnClientRemovedCallBack(self, funct):
+        """Register a callback function to the "On client removed" event.
+        @param funct: Function pointer. The function must accept one parameter.
+                      Example :
+                      def onClientRemoved(idClient):
+                          print idClient
+        """
+        self.__onClientRemoved = funct
+
+    # 
--------------------------------------------------------------------------
+    # Register a callback function to the "On client notification" event.
+    # 
--------------------------------------------------------------------------
+    def registerOnClientNotificationCallBack(self, funct):
+        """Register a callback function to the "On client notification" event.
+        @param funct: Function pointer. The function must accept one parameter.
+                      Example :
+                      def onClientNotification(idClient, message):
+                          print idClient
+        """
+        self.__onClientNotification = funct
+
+    # 
--------------------------------------------------------------------------
+    # Check if a client exists.
+    # 
--------------------------------------------------------------------------
+    def clientExists(self, id):
+        """Check if a client exists.
+        @param id: Id client.
+        @return: True or False.
+        """
+        self.__cliMutex.acquire()
+        result = False
+        for cli in self.__cliLst:
+            if cli['id'] == id:
+                result = True
+                break
+        self.__cliMutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Start the server.
+    # 
--------------------------------------------------------------------------
+    def start(self):
+        """Start the server.
+        @return: The success of the server start. True or False.
+        """
+        # Exit the function if the server is already started
+        if self.__getRunLst():
+            return True
+        # Create the server socket
+        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        try:
+            # Bind the socket
+            self.__socket.bind((self.__host, self.__port))
+            # Set the socket to listen mode
+            self.__socket.listen(50)
+            #Run the listen loop and the ping loop
+            self.__runLstThread = threading.Thread(target = self.__listenLoop)
+            self.__runLstThread.start()
+            self.__runPingThread = threading.Thread(target = self.__pingLoop)
+            self.__runPingThread.start()
+            time.sleep(0.1)
+            # Server successfuly started
+            return True
+        except socket.timeout:
+            self.__setRunLst(False)
+            # Failed to start the server
+            return False
+        except socket.error:
+            self.__setRunLst(False)
+            # Failed to start the server
+            return False
+        except:
+            self.__setRunLst(False)
+            # Failed to start the server
+            return False
+
+    # 
--------------------------------------------------------------------------
+    # Stop the server.
+    # 
--------------------------------------------------------------------------
+    def stop(self):
+        """Stop the server.
+        """
+        # If the server don't runs then exit the function
+        if not self.__getRunLst():
+            return
+        # Stop the listen loop
+        self.__setRunLst(False)
+        # Stop the ping loop
+        self.__setRunPing(False)
+        # Close the server socket
+        self.__socket.close()
+        time.sleep(0.1)
+        # Ensure that the threads have been stopped
+        if self.__runLstThread.isAlive():
+            self.__runLstThread._Thread__stop()
+        if self.__runPingThread.isAlive():
+            self.__runPingThread.join()
+        self.__stopNotifyThreadList()
+        # Clear the clients list
+        self.__clearClients()
+
+    # 
--------------------------------------------------------------------------
+    # Wait that the server has stopped.
+    # 
--------------------------------------------------------------------------
+    def waitStop(self):
+        """Wait that the server has stopped.
+        """
+        while self.__getRunLst():
+            time.sleep(0.5)
+        time.sleep(0.5)
+
+    # 
--------------------------------------------------------------------------
+    # Add thread in the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __addNotifyThread(self, thread):
+        """Add thread in the threaded messages list.
+        @param thread: Thread to be added.
+        """
+        self.__ntlMutex.acquire()
+        self.__notifyThreadsList.append(thread)
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Clean the closed thread from the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __cleanNotifyThreadList(self):
+        """Clean the closed thread from the threaded messages list in order to
+        avoiding a memory leak issue.
+        """
+        self.__ntlMutex.acquire()
+        newLst = []
+        for t in self.__notifyThreadsList:
+            if t.isAlive():
+                newLst.append(t)
+        self.__notifyThreadsList = newLst
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Stop all threads from the threaded messages list.
+    # 
--------------------------------------------------------------------------
+    def __stopNotifyThreadList(self):
+        """Stop all threads from the threaded messages list.
+        """
+        self.__ntlMutex.acquire()
+        for t in self.__notifyThreadsList:
+            if t.isAlive():
+                # Wait for a hypothetical self closing of the thread
+                if not t.join(0.1):
+                    # Otherwise, kill it
+                    t._Thread__stop()
+        self.__ntlMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Get the state of the listening loop.
+    # 
--------------------------------------------------------------------------
+    def __getRunLst(self):
+        """Get the state of the listening loop.
+        @return: True or False.
+        """
+        self.__runLstMutex.acquire()
+        result = self.__runLst
+        self.__runLstMutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Set the state of the listening loop.
+    # 
--------------------------------------------------------------------------
+    def __setRunLst(self, value = True):
+        """Set the state of the listening loop.
+        @param value: New value (True or False)
+        """
+        self.__runLstMutex.acquire()
+        self.__runLst = value
+        self.__runLstMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Get the state of the ping loop.
+    # 
--------------------------------------------------------------------------
+    def __getRunPing(self):
+        """Get the state of the ping loop.
+        @return: True or False.
+        """
+        self.__runPingMutex.acquire()
+        result = self.__runPing
+        self.__runPingMutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Set the state of the ping loop.
+    # 
--------------------------------------------------------------------------
+    def __setRunPing(self, value = True):
+        """Set the state of the ping loop.
+        @param value: New value (True or False)
+        """
+        self.__runPingMutex.acquire()
+        self.__runPing = value
+        self.__runPingMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Add a new client in the clients list.
+    # 
--------------------------------------------------------------------------
+    def __addClient(self, connection, address):
+        """Add a new client in the clients list.
+        @param connection: Client socket.
+        @param address: Client address.
+        """
+        self.__cliMutex.acquire()
+        # Create a md5 hash of the socket address in order to make an unique
+        # identifier for the client.
+        md5H = md5()
+        md5H.update(str(address[0]) + str(address[1]))
+        id = md5H.hexdigest()
+        # Create a dictionary for the client configuration
+        cliConf = {
+            'connection' : connection,
+            'address' : address,
+            'id' : id,
+        }
+        # Add the client to the list
+        self.__cliLst.append(cliConf)
+        # Create a 128 bytes length string with the id client.
+        idToSend = id + "\n" + "".join(" " * (127 - len(id)))
+        try:
+            # Send the identifer to the client
+            connection.send(idToSend)
+        except:
+            pass
+        self.__cliMutex.release()
+        # Call the "On client added" event
+        if self.__onClientAdded != None:
+            self.__onClientAdded(id)
+
+    # 
--------------------------------------------------------------------------
+    # Remove a client from the clients list.
+    # 
--------------------------------------------------------------------------
+    def __removeClient(self, address):
+        """Remove a client from the clients list.
+        @param address: Client address.
+        """
+        self.__cliMutex.acquire()
+        removedId = None
+        # Search the client address in the registered clients
+        for cli in self.__cliLst:
+            if cli['address'] == address:
+                cli['connection'].close()
+                self.__cliLst.remove(cli)
+                removedId = cli['id']
+                break
+        self.__cliMutex.release()
+        # If the client has been removed then call the "On client removed" 
event
+        if removedId != None:
+            if self.__onClientRemoved != None:
+                self.__onClientRemoved(removedId)
+
+    # 
--------------------------------------------------------------------------
+    # Clear the clients list.
+    # 
--------------------------------------------------------------------------
+    def __clearClients(self):
+        """Clear the clients list.
+        """
+        self.__cliMutex.acquire()
+        self.__cliLst = []
+        self.__cliMutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Socket listening loop.
+    # 
--------------------------------------------------------------------------
+    def __listenLoop(self):
+        """Socket listening loop.
+        """
+        self.__setRunLst(True)
+        while self.__getRunLst():
+            try:
+                # Wait for a new client connection. This function is blocking
+                # the loop. The parent loop must be killed.
+                connection, address = self.__socket.accept()
+                # If the client socket is valid then add it to the clients list
+                if (connection != None) and (address != None):
+                    self.__addClient(connection, address)
+            except:
+                pass
+
+    # 
--------------------------------------------------------------------------
+    # Ping loop.
+    # 
--------------------------------------------------------------------------
+    def __pingLoop(self):
+        """Ping loop.
+        """
+        self.__setRunPing(True)
+        while self.__getRunPing():
+            aClientHasRemoved = False
+            self.__cliMutex.acquire()
+            # Ping all clients
+            for cli in self.__cliLst:
+                try:
+                    # Send the PING command
+                    cli['connection'].send(_PING_CMD)
+                    # Read the client response
+                    data = cli['connection'].recv(128)
+                except:
+                    self.__cliMutex.release()
+                    # If an error occuring during the client ping then remove 
it
+                    # from the clients list
+                    self.__removeClient(cli['address'])
+                    aClientHasRemoved = True
+                    self.__cliMutex.acquire()
+                    break
+                if data != "PONG":
+                    self.__cliMutex.release()
+                    # If the client response is invalid then remove it from the
+                    # clients list
+                    self.__removeClient(cli['address'])
+                    aClientHasRemoved = True
+                    self.__cliMutex.acquire()
+                    break
+            self.__cliMutex.release()
+            if aClientHasRemoved:
+                continue
+            # Read clients notifications (20x).
+            for i in range(20):
+                if not self.__getRunPing():
+                    break
+                # Remove the closed threads from the threads list
+                # (garbage cleaning)
+                self.__cleanNotifyThreadList()
+                self.__cliMutex.acquire()
+                for cli in self.__cliLst:
+                    try:
+                        # Send the GETNOTIFICATION command
+                        cli['connection'].send(_GETNOTIFICATION_CMD)
+                        # Read the client response
+                        data = cli['connection'].recv(128)
+                    except:
+                        self.__cliMutex.release()
+                        # If an error occuring during the client ping then 
remove it
+                        # from the clients list
+                        self.__removeClient(cli['address'])
+                        self.__cliMutex.acquire()
+                        break
+                    if data == "PONG":
+                        pass
+                    elif data.find("RET:") == 0:
+                        if self.__onClientNotification != None:
+                            self.__cliMutex.release()
+                            t = threading.Thread(target = 
self.__onClientNotification,
+                                args = (cli['id'], data[4:]))
+                            self.__addNotifyThread(t)
+                            t.start()
+                            self.__cliMutex.acquire()
+                    else:
+                        self.__cliMutex.release()
+                        # If the client response is invalid then remove it 
from the
+                        # clients list
+                        self.__removeClient(cli['address'])
+                        self.__cliMutex.acquire()
+                        break
+
+                self.__cliMutex.release()
+                time.sleep(0.1)
+
+    # 
--------------------------------------------------------------------------
+    # Send a message to the connected clients.
+    # 
--------------------------------------------------------------------------
+    def notify(self, message, idClient = None):
+        """Send a message to the connected clients.
+        @param message: Message to notify. The maximal size of a message is 127
+                        characters.
+        @param idClient: Id of the client to notify or None for all clients.
+        """
+        # Regularize the message length (0 > correct size < 128)
+        if len(message) > 127:
+            message = message[:126]
+        if len(message) == 0:
+            message = "NOTIFY"
+        message = message + "\n" + "".join(" " * (127 - len(message)))
+        self.__cliMutex.acquire()
+        # Send the message to the clients
+        for cli in self.__cliLst:
+            try:
+                if idClient == None:
+                    cli['connection'].send(message)
+                else:
+                    if cli['id'] == idClient:
+                        cli['connection'].send(message)
+            except:
+                # No special action if the client connection is broken, it will
+                # be removed by the "ping" loop
+                pass
+        # Can't sent another message while 100 msec
+        time.sleep(0.1)
+        self.__cliMutex.release()

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNSimpleTest.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNSimpleTest.py
                              (rev 0)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNSimpleTest.py
      2009-12-08 15:05:44 UTC (rev 5958)
@@ -0,0 +1,106 @@
+# -*- coding: utf-8 -*-
+
+import version
+__author__ = version.author
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyleft (C) 2008 C2ME Sa
+#    Remi Jocaille <remi.jocai...@c2me.be>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import threading
+import time
+
+from IPNServer import IPNServer
+from IPNClient import IPNClient
+
+# 
==============================================================================
+# Server functions
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Callback function on "client added" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientAdded(idClient):
+    print "[Server] New client :", idClient
+
+# 
------------------------------------------------------------------------------
+# Callback function on "client removed" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientRemoved(idClient):
+    print "[Server] Removed client :", idClient
+
+# 
------------------------------------------------------------------------------
+# Callback function on "client notification" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientNotification(idClient, message):
+    print "[Server] Client [%s] Notification (%s)" % (idClient, message)
+
+# 
==============================================================================
+# Client functions
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Callback function on "connected" event.
+# 
------------------------------------------------------------------------------
+def clientOnConnected(id):
+    print "[Client] Connected with id : ", id
+
+# 
------------------------------------------------------------------------------
+# Callback function on "disconnected" event.
+# 
------------------------------------------------------------------------------
+def clientOnDisconnected():
+    print "[Client] Disconnected"
+
+# 
------------------------------------------------------------------------------
+# Callback function on "notification" event.
+# 
------------------------------------------------------------------------------
+def clientOnNotification(message):
+    print "[Client] Message from server :", message
+
+# 
------------------------------------------------------------------------------
+# Test the IPN module.
+# 
------------------------------------------------------------------------------
+def test():
+    """Test the IPN module.
+    """
+    # Create an IPN server
+    serv = IPNServer('127.0.0.1', 48536)
+    serv.registerOnClientAddedCallBack(serverOnClientAdded)
+    serv.registerOnClientRemovedCallBack(serverOnClientRemoved)
+    serv.registerOnClientNotificationCallBack(serverOnClientNotification)
+    
+    # Create an IPN client
+    cli = IPNClient('127.0.0.1', 48536)
+    cli.registerOnNotificationCallBack(clientOnNotification)
+    cli.registerOnConnectedCallBack(clientOnConnected)
+    cli.registerOnDisconnectedCallBack(clientOnDisconnected)
+    
+    # Start the server in thread
+    if not serv.start():
+        print "Error whil starting the server."
+        return
+    
+    # Start the client
+    if not cli.start():
+        print "Error whil starting the client."
+        serv.stop()
+        return
+        
+    # Server send notification to client(s)
+    serv.notify("Hello world from Server")
+    cli.notify("How are you from client")
+    
+    # Finish
+    cli.stop()
+    serv.stop()
+
+# 
------------------------------------------------------------------------------
+# Main
+# 
------------------------------------------------------------------------------
+if __name__ == '__main__':
+    test()

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNStressTest.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNStressTest.py
                              (rev 0)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/IPNStressTest.py
      2009-12-08 15:05:44 UTC (rev 5958)
@@ -0,0 +1,114 @@
+# -*- coding: utf-8 -*-
+
+import version
+__author__ = version.author
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyleft (C) 2008 C2ME Sa
+#    Remi Jocaille <remi.jocai...@c2me.be>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import threading
+import time
+
+from IPNServer import IPNServer
+from IPNClient import IPNClient
+
+# 
==============================================================================
+# Server functions
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Callback function on "client added" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientAdded(idClient):
+    print "[Server] New client :", idClient
+
+# 
------------------------------------------------------------------------------
+# Callback function on "client removed" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientRemoved(idClient):
+    print "[Server] Removed client :", idClient
+    
+# 
------------------------------------------------------------------------------
+# Callback function on "client notification" event.
+# 
------------------------------------------------------------------------------
+def serverOnClientNotification(idClient, message):
+    print "[Server] Client [%s] Notification (%s)" % (idClient, message)
+
+# 
------------------------------------------------------------------------------
+# Function to create a IPN server.
+# 
------------------------------------------------------------------------------
+def serverProcess(timeout = 100.0):
+    serv = IPNServer()
+    serv.registerOnClientAddedCallBack(serverOnClientAdded)
+    serv.registerOnClientRemovedCallBack(serverOnClientRemoved)
+    serv.registerOnClientNotificationCallBack(serverOnClientNotification)
+    if serv.start():
+        for i in range(int(timeout / 0.1)):
+            serv.notify("Hello")
+            time.sleep(0.001)
+        serv.stop()
+
+# 
==============================================================================
+# Client functions
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Callback function on "connected" event.
+# 
------------------------------------------------------------------------------
+def clientOnConnected(id):
+    print "[Client] Connected with id : ", id
+
+# 
------------------------------------------------------------------------------
+# Callback function on "disconnected" event.
+# 
------------------------------------------------------------------------------
+def clientOnDisconnected():
+    print "[Client] Disconnected"
+
+# 
------------------------------------------------------------------------------
+# Callback function on "notification" event.
+# 
------------------------------------------------------------------------------
+def clientOnNotification(message):
+    print "[Client] Message :", message
+
+# 
------------------------------------------------------------------------------
+# Create a IPN client.
+# 
------------------------------------------------------------------------------
+def clientProcess(timeout = 10.0):
+    cli = IPNClient()
+    #cli.registerOnNotificationCallBack(clientOnNotification)
+    cli.registerOnConnectedCallBack(clientOnConnected)
+    cli.registerOnDisconnectedCallBack(clientOnDisconnected)
+    if cli.start():
+        cli.notify("Hello world")
+        time.sleep(timeout)
+        cli.stop()
+    else:
+        print "Client can't be connected"
+
+# 
------------------------------------------------------------------------------
+# Test the IPN module.
+# 
------------------------------------------------------------------------------
+def test():
+    """Test the IPN module.
+    """
+    # Create a IPN server
+    t = threading.Thread(target = serverProcess, args = (15.0,))
+    t.start()
+    # Create 50 IPN clients
+    for i in range(50):
+        t = threading.Thread(target = clientProcess, args = (5.0,))
+        t.start()
+        time.sleep(0.02)
+    time.sleep(14.0)
+
+# 
------------------------------------------------------------------------------
+# Main
+# 
------------------------------------------------------------------------------
+if __name__ == '__main__':
+    test()

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/__init__.py
===================================================================

Added: 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/version.py
===================================================================
--- 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/version.py
                            (rev 0)
+++ 
software_suite_v3/software/plugin/plugin-skype/branches/in_out_plugin/executables/IPN/version.py
    2009-12-08 15:05:44 UTC (rev 5958)
@@ -0,0 +1,4 @@
+version = '1.0.0'
+author = "Remi Jocaille (remi.jocai...@kysoh.com)"
+licence = "GPL"
+date = "December 2009"


------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
Tux-droid-svn mailing list
Tux-droid-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to