Example #1
0
    def _attempt_listening(self):
        self._sync_actions()
        
        if self._awaiting_connection is not None:
            # Cannot handle multiple connection requests at the same time
            return
        try:
            socketutils.wait_for_read([self._listener_sock], timeout=0)
        except socketutils.TimeoutError:
            return
        
        conn = Bunch()
        data, conn.remote_address = self._listener_sock.recvfrom(MAX_PACKET_SIZE)
        remote_hostname, remote_port = conn.remote_address
        remote_version, conn.remote_host_id, conn.remote_host_name = loads(data)
        assert remote_version == self.protocol_version, "Attempt to connect with client of different version"
        if conn.remote_address in self._already_connected:
            # We may be receiving a retransmit of the connection
            # packet from before, anyhow, he is already connected, so
            # ignore it.
            return
        
        self.notify("new_connector1", conn.remote_host_name, conn.remote_address)
        conn.sock=socketutils.new_udp_socket()
        conn.sock.connect(conn.remote_address)
        
        host_ids = [(host.id, host.name) for host in self.hosts]
        welcome_to_send = dumps(('WELCOME!', (self._publicized_data, host_ids)))
        conn._welcome_to_send = welcome_to_send
        conn._welcome_count = 0

        self._awaiting_connection = conn
        self.update = self._send_welcome
Example #2
0
    def start_connecting(self):
        print "Connecting to %s:%d" % self.address
        self._is_connecting = True
        
        self._socket_to_host = socketutils.new_udp_socket()

        for i in xrange(ATTEMPT_COUNT):
            # Send version to identify both our version and the local port of this connection
            self._socket_to_host.sendto(dumps((self.protocol_version,
                                               self.local_host.id,
                                               self.local_host.name)), self.address)
            # Receive hello as ack and to know the remote port of this particular connection
            try:
                socketutils.wait_for_read([self._socket_to_host], timeout=0.5)
            except socketutils.TimeoutError:
                pass
            else:
                request, remote_address = self._socket_to_host.recvfrom(MAX_PACKET_SIZE)
                request_type, request_data = loads(request)
                if request_type == 'NOTCONNECTING!':
                    raise ConnectionFailed("Other host has stopped connecting to us!")
                assert request_type == 'WELCOME!', "Expected WELCOME, received %r" % (request_type,)
                print "Received welcome response"
                self._publicized_data, host_ids = request_data

                # The welcome is sent from a different port (not the listening port)...
                self._socket_to_host.connect(remote_address)

                self._listener_socks = dict([(socketutils.udp_listener_on(0), (id, name))
                                             for id, name in host_ids])
                self._send_listening_on_ports()
                break
        else:
            raise ConnectionFailed("Cannot connect to given host")
        self.update = self._attempt_connecting
Example #3
0
 def _connect_to(self, src_host,
                 remote_host_id, remote_host_name,
                 original_remote_address, address):
     if not self._is_connecting:
         # Too late, his connection is aborted...
         return None
     
     sock = socketutils.new_udp_socket()
     sock.connect(address)
     remote_host = RemoteHost(remote_host_id, remote_host_name, address, sock)
     self._add_remote_host(remote_host)
     return remote_host