def _send(self, frame): try: self.sock.send(dumps(frame)) except socket.error, s: # Just "lose" the sent packet as any lost packet but remember that # we are a disconnected host self._handle_socket_error(s)
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
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 stop_connecting(self): self._is_connecting = False if self._awaiting_connection: unwelcome = 'NOTCONNECTING!', () self._awaiting_connection.sock.send(dumps(unwelcome)) # If the packet is lost, he'll keep on hammering me, but I # don't care... self._listener_sock.close() del self._listener_sock del self._awaiting_connection del self._already_connected self.update = self._sync_actions
def _send_listening_on_ports(self): ports = [sock.getsockname()[1] for sock in self._listener_socks] self._socket_to_host.send(dumps(('listening_on', ports)))