Beispiel #1
0
    def getFreeAddresses(self):
        import socket
        s = socket.socket()
        s.bind(('', 0))
        port0 = s.getsockname()[1]

        s1 = socket.socket()
        s1.bind(('', 0))
        port1 = s1.getsockname()[1]
        s.close()
        s1.close()

        if port0 <= 0 or port1 <= 0:
            #This happens in Jython...
            from java.net import ServerSocket
            s0 = ServerSocket(0)
            port0 = s0.getLocalPort()

            s1 = ServerSocket(0)
            port1 = s1.getLocalPort()

            s0.close()
            s1.close()

        assert port0 != port1
        assert port0 > 0
        assert port1 > 0

        return port0, port1
Beispiel #2
0
    def getFreeAddresses(self):
        import socket
        s = socket.socket()
        s.bind(('',0))
        port0 = s.getsockname()[1]
        
        s1 = socket.socket()
        s1.bind(('',0))
        port1 = s1.getsockname()[1]
        s.close()
        s1.close()
        
        if port0 <= 0 or port1 <= 0:
            #This happens in Jython...
            from java.net import ServerSocket
            s0 = ServerSocket(0)
            port0 = s0.getLocalPort()
            
            s1 = ServerSocket(0)
            port1 = s1.getLocalPort()
            
            s0.close()
            s1.close()

        assert port0 != port1
        assert port0 > 0
        assert port1 > 0
            
        return port0, port1
class NetworkListener(Thread):
    """Use a new thread to listen to a network port and place each message
    sent on a new line to the message queue"""
    
    def __init__(self):
        self._serverSocket = ServerSocket(PORT)
        self._isListening = True
        Thread.__init__(self)
        log("Started server on " + str(PORT))
        
    def run(self):
        """Start thread listening to the socket"""
        while(self._isListening):
            self._clientSocket = self._serverSocket.accept()
            self._handleConnection(self._clientSocket)
        
    def _handleConnection(self, socket):
        reader = BufferedReader(InputStreamReader(socket.getInputStream()))

        try:
            line = reader.readLine();
    
            while(line is not None and not self._serverSocket.closed):
                self._handleMessage(line)
                line = reader.readLine();
        except SocketException:
            if self._isListening:
                raise SocketException
            else:
                log("Ignoring socket exception during shutdown")

        socket.close()

    def _handleMessage(self, message):
        """Called for each message received."""
        lock.acquire()
        messageQueue.append(message)
        lock.notifyAll()
        lock.release()

    def stop(self):
        """Stop the server socket and close any open client socket"""
        self._isListening = False

        if(self._serverSocket is not None):
            self._serverSocket.close()

        if(self._clientSocket is not None):
            self._clientSocket.close()