Exemple #1
0
    def connect(self, address, timeout=1.0, name=''):
        """ connect(self, address, timeout=1.0, name='')
        
        Setup a connection with another context, by connection to a 
        hosting context. An error is raised when the connection could
        not be made.
        
        Returns a Connection instance that represents the
        connection to the other context. These connection objects 
        can also be obtained via the Context.connections property.
        
        Parameters
        ----------
        address : str
            Should be of the shape hostname:port. The port should be an
            integer number between 1024 and 2**16. If port does not 
            represent a number, a valid port number is created using a 
            hash function.
        max_tries : int
            The number of ports to try; starting from the given port, 
            subsequent ports are tried until a free port is available. 
            The final port can be obtained using the 'port' property of
            the returned Connection instance.
        name : string
            The name for the created Connection instance. It can
            be used as a key in the connections property.
        
        Notes on hostname
        -----------------
        The hostname can be:
          * The IP address, or the string hostname of this computer. 
          * 'localhost': the connection is only visible from this computer. 
            Also some low level networking layers are bypassed, which results
            in a faster connection. The other context should also host as
            'localhost'.
          * 'publichost': the connection is visible by other computers on the 
            same network. Optionally an integer index can be appended if
            the machine has multiple IP addresses (see socket.gethostbyname_ex).
        
        """

        # Trigger cleanup of closed connections
        self.connections

        # Split address in protocol, real hostname and port number
        protocol, hostname, port = split_address(address)

        # Based on protocol, instantiate connection class (currently only tcp)
        if False:  #protocol == 'itc':
            connection = ItcConnection(self, name)
        else:
            connection = TcpConnection(self, name)

        # Create new connection and connect it
        connection._connect(hostname, port, timeout)

        # Save connection instance
        self._connections_lock.acquire()
        try:
            # Push packages from startup queue
            while self._startupQueue:
                connection._inject_package(self._startupQueue.pop())
            # Add connection object to list of connections
            self._connections.append(connection)
        finally:
            self._connections_lock.release()

        # Send message in the network to signal a new connection
        bb = 'NEW_CONNECTION'.encode('utf-8')
        p = Package(bb, SLOT_CONTEXT, self._id, 0, 0, 0, 0)
        self._send_package(p)

        # Return Connection instance
        return connection
Exemple #2
0
 def connect(self, address, timeout=1.0, name=''):
     """ connect(self, address, timeout=1.0, name='')
     
     Setup a connection with another context, by connection to a 
     hosting context. An error is raised when the connection could
     not be made.
     
     Returns a Connection instance that represents the
     connection to the other context. These connection objects 
     can also be obtained via the Context.connections property.
     
     Parameters
     ----------
     address : str
         Should be of the shape hostname:port. The port should be an
         integer number between 1024 and 2**16. If port does not 
         represent a number, a valid port number is created using a 
         hash function.
     max_tries : int
         The number of ports to try; starting from the given port, 
         subsequent ports are tried until a free port is available. 
         The final port can be obtained using the 'port' property of
         the returned Connection instance.
     name : string
         The name for the created Connection instance. It can
         be used as a key in the connections property.
     
     Notes on hostname
     -----------------
     The hostname can be:
       * The IP address, or the string hostname of this computer. 
       * 'localhost': the connection is only visible from this computer. 
         Also some low level networking layers are bypassed, which results
         in a faster connection. The other context should also host as
         'localhost'.
       * 'publichost': the connection is visible by other computers on the 
         same network. Optionally an integer index can be appended if
         the machine has multiple IP addresses (see socket.gethostbyname_ex).
     
     """
     
     # Trigger cleanup of closed connections
     self.connections
     
     # Split address in protocol, real hostname and port number
     protocol, hostname, port = split_address(address)
     
     # Based on protocol, instantiate connection class (currently only tcp)
     if False:#protocol == 'itc':
         connection = ItcConnection(self, name)
     else:
         connection = TcpConnection(self, name)
     
     # Create new connection and connect it
     connection._connect(hostname, port, timeout)
     
     # Save connection instance
     self._connections_lock.acquire()
     try:
         # Push packages from startup queue
         while self._startupQueue:
             connection._inject_package(self._startupQueue.pop())
         # Add connection object to list of connections
         self._connections.append(connection)
     finally:
         self._connections_lock.release()
     
     # Send message in the network to signal a new connection
     bb = 'NEW_CONNECTION'.encode('utf-8')
     p = Package(bb, SLOT_CONTEXT, self._id, 0,0,0,0)
     self._send_package(p)
     
     # Return Connection instance
     return connection
Exemple #3
0
    def bind(self, address, max_tries=1, name=''):
        """ bind(address, max_tries=1, name='')
        
        Setup a connection with another Context, by being the host.
        This method starts a thread that waits for incoming connections.
        Error messages are printed when an attemped connect fails. the
        thread keeps trying until a successful connection is made, or until
        the connection is closed.
        
        Returns a Connection instance that represents the
        connection to the other context. These connection objects 
        can also be obtained via the Context.connections property.
        
        Parameters
        ----------
        address : str
            Should be of the shape hostname:port. The port should be an
            integer number between 1024 and 2**16. If port does not 
            represent a number, a valid port number is created using a 
            hash function.
        max_tries : int
            The number of ports to try; starting from the given port, 
            subsequent ports are tried until a free port is available. 
            The final port can be obtained using the 'port' property of
            the returned Connection instance.
        name : string
            The name for the created Connection instance. It can
            be used as a key in the connections property.
        
        Notes on hostname
        -----------------
        The hostname can be:
          * The IP address, or the string hostname of this computer. 
          * 'localhost': the connections is only visible from this computer. 
            Also some low level networking layers are bypassed, which results
            in a faster connection. The other context should also connect to
            'localhost'.
          * 'publichost': the connection is visible by other computers on the 
            same network. Optionally an integer index can be appended if
            the machine has multiple IP addresses (see socket.gethostbyname_ex).
        
        """

        # Trigger cleanup of closed connections
        self.connections

        # Split address in protocol, real hostname and port number
        protocol, hostname, port = split_address(address)

        # Based on protocol, instantiate connection class (currently only tcp)
        if False:  #protocol == 'itc':
            connection = ItcConnection(self, name)
        else:
            connection = TcpConnection(self, name)

        # Bind connection
        connection._bind(hostname, port, max_tries)

        # Save connection instance
        self._connections_lock.acquire()
        try:
            # Push packages from startup queue
            while len(self._startupQueue):
                connection._inject_package(self._startupQueue.pop())
            # Add connection object to list of connections
            self._connections.append(connection)
        finally:
            self._connections_lock.release()

        # Return Connection instance
        return connection
Exemple #4
0
 def bind(self, address, max_tries=1, name=''):
     """ bind(address, max_tries=1, name='')
     
     Setup a connection with another Context, by being the host.
     This method starts a thread that waits for incoming connections.
     Error messages are printed when an attemped connect fails. the
     thread keeps trying until a successful connection is made, or until
     the connection is closed.
     
     Returns a Connection instance that represents the
     connection to the other context. These connection objects 
     can also be obtained via the Context.connections property.
     
     Parameters
     ----------
     address : str
         Should be of the shape hostname:port. The port should be an
         integer number between 1024 and 2**16. If port does not 
         represent a number, a valid port number is created using a 
         hash function.
     max_tries : int
         The number of ports to try; starting from the given port, 
         subsequent ports are tried until a free port is available. 
         The final port can be obtained using the 'port' property of
         the returned Connection instance.
     name : string
         The name for the created Connection instance. It can
         be used as a key in the connections property.
     
     Notes on hostname
     -----------------
     The hostname can be:
       * The IP address, or the string hostname of this computer. 
       * 'localhost': the connections is only visible from this computer. 
         Also some low level networking layers are bypassed, which results
         in a faster connection. The other context should also connect to
         'localhost'.
       * 'publichost': the connection is visible by other computers on the 
         same network. Optionally an integer index can be appended if
         the machine has multiple IP addresses (see socket.gethostbyname_ex).
     
     """ 
     
     # Trigger cleanup of closed connections
     self.connections
     
     # Split address in protocol, real hostname and port number
     protocol, hostname, port = split_address(address)
     
     # Based on protocol, instantiate connection class (currently only tcp)
     if False:#protocol == 'itc':
         connection = ItcConnection(self, name)
     else:
         connection = TcpConnection(self, name)
     
     # Bind connection
     connection._bind(hostname, port, max_tries)
     
     # Save connection instance
     self._connections_lock.acquire()
     try:
         # Push packages from startup queue
         while len(self._startupQueue):
             connection._inject_package(self._startupQueue.pop())
         # Add connection object to list of connections
         self._connections.append(connection)
     finally:
         self._connections_lock.release()
     
     # Return Connection instance
     return connection