def recv(self, n=1): """Receive data from the socket. The return value is a string representing the data received.""" message = self.queue.get() op, params = networking.parse_message(message) if not op == 'RECEIVED': if op == 'DISCONN': self.close() else: print '[ParrotSocket %s] Unexpected message in recv(), got: %s' % (self.id, message) return None return params['payload']
def recv(self, n=1): """Receive data from the socket. The return value is a string representing the data received.""" message = self.queue.get() op, params = networking.parse_message(message) if not op == 'RECEIVED': if op == 'DISCONN': self.close() else: print '[ParrotSocket %s] Unexpected message in recv(), got: %s' % ( self.id, message) return None return params['payload']
def connect(self, address): """Connect to an address where `address` is a tuple of (ip, port).""" self.name = self.interface_lookup(address[0]); message = networking.build_message(networking.CONNECT, id=self.id, ip=address[0], port=address[1]) # print '[ParrotSocket %s] connect: %s (%s)' % (self.id, message, self.name) self.comm_chan.send_cmd(message, self) # Block here waiting for ACCEPT from server message = self.queue.get() op, params = networking.parse_message(message) if op != networking.ACCEPTED or params['id'] != self.id: raise SocketException("Failed in connecting")
def accept(self): """Accept a connection. The socket must be bound to an address and listening for connections. The return value is a new socket object usable to send and receive data on the connection. """ message = self.queue.get() op, params = networking.parse_message(message) if not op == networking.NEW_CONN: # print '[ParrotSocket %s] accept: Expected NEW_CONN, got: %s' % (self.id, message) return None return StreamSocket(self.node, family=self.family, id=params['new_id'], iface_name=self.name)
def relay_handler(self, data, sender): """sender is an Interface.""" cmd, params = networking.parse_message(data) handlers = { networking.LISTEN: self.listen, networking.CONNECT: self.connect, networking.ACCEPT: self.accept, networking.SEND: self.send, networking.SENDTO: self.sendto, networking.RECVFROM: self.recvfrom, networking.DISCONN: self.disconnect } status = handlers.get(cmd, self.bad_command)(sender, **params)
def connect(self, address): """Connect to an address where `address` is a tuple of (ip, port).""" self.name = self.interface_lookup(address[0]) message = networking.build_message(networking.CONNECT, id=self.id, ip=address[0], port=address[1]) # print '[ParrotSocket %s] connect: %s (%s)' % (self.id, message, self.name) self.comm_chan.send_cmd(message, self) # Block here waiting for ACCEPT from server message = self.queue.get() op, params = networking.parse_message(message) if op != networking.ACCEPTED or params['id'] != self.id: raise SocketException("Failed in connecting")
def accept(self): """Accept a connection. The socket must be bound to an address and listening for connections. The return value is a new socket object usable to send and receive data on the connection. """ message = self.queue.get() op, params = networking.parse_message(message) if not op == networking.NEW_CONN: # print '[ParrotSocket %s] accept: Expected NEW_CONN, got: %s' % (self.id, message) return None return StreamSocket(self.node, family=self.family, id=params['new_id'], iface_name=self.name)
def recvfrom(self, dummy): """Receive data from the socket. The return value is a pair (data, address) where data is data received and address is the tuple of (ip, port) of the socket sending the data. The parameter `dummy` is currently unused. """ self._become_active_listener() message = self.queue.get() op, params = networking.parse_message(message) if not op == 'RECDFROM': if op == 'DISCONN': self.comm_chan.unregister_handler_for_interface(self.id) else: print '[ParrotSocket %s] Unexpected message in recv(), got: %s' % (self.id, message) return None return params['payload'], (params['src_ip'], params['src_port'])
def recvfrom(self, dummy): """Receive data from the socket. The return value is a pair (data, address) where data is data received and address is the tuple of (ip, port) of the socket sending the data. The parameter `dummy` is currently unused. """ self._become_active_listener() message = self.queue.get() op, params = networking.parse_message(message) if not op == 'RECDFROM': if op == 'DISCONN': self.comm_chan.unregister_handler_for_interface(self.id) else: print '[ParrotSocket %s] Unexpected message in recv(), got: %s' % ( self.id, message) return None return params['payload'], (params['src_ip'], params['src_port'])