def encode(sender, receiver, protocol, payload=None, payload_type='', sequence_num=-1, await_ack=False): """ Encodes the data with the sender, receiver, protocol, payload type and sequence number and forms the packet with data and the header. Args: sender(str): ID of the sender receiver(str): ID of the receiver protocol(str): ID of the protocol of which the packet should be processed. payload : The message that is intended to send with the packet. Type of payload depends on the protocol. payload_type(str): Type of the payload. sequence_num(int): Sequence number of the packet. await_ack(bool): If the sender should await an ACK Returns: (Packet): Encoded packet """ packet = Packet(sender, receiver, protocol, payload_type, payload, sequence_number=sequence_num, await_ack=await_ack) return packet
def _send_broadcast(packet): sender = packet.sender message = packet.payload host_sender = network.get_host(sender) for host in network.ARP.keys(): if sender != host: seq_num = host_sender.get_next_sequence_number(host) message.seq_num = seq_num new_packet = Packet(sender=sender, receiver=host, protocol=Constants.REC_CLASSICAL, payload=message, payload_type=Constants.CLASSICAL, sequence_number=seq_num, await_ack=False) network.send(new_packet)
def _send_w(packet): """ Gets W qubits and distributes the to all hosts. One qubit is stored in own storage. Args: packet (Packet): The incoming packet """ host_list = packet.payload[Constants.HOSTS] qubits = packet.payload[Constants.QUBITS] sender = packet.sender await_ack = packet.await_ack seq_num_list = packet.seq_num for host, qubit, seq_num in zip(host_list, qubits, seq_num_list): new_packet = Packet(sender=sender, receiver=host, protocol=Constants.REC_W, payload=qubit, payload_type=Constants.QUANTUM, sequence_number=seq_num, await_ack=await_ack) network.send(new_packet)