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
def start_connecting(self): self._is_connecting = True self._listener_sock = socketutils.udp_listener_on(self._port) self._awaiting_connection = None self._already_connected = [] self.update = self._attempt_listening