Packet.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from classes.Utilities import random_string
  2. import numpy
  3. class Packet():
  4. ''' This module implements the Packet object, which is the data structure responsible for
  5. transporting message blocks among clients.
  6. '''
  7. __slots__ = ['conf', 'id', 'route', 'payload', 'real_sender', 'dest', 'msg_id', 'message', 'fragments', 'type', 'pool_logs', 'dropped', 'current_node', 'times_transmitted',
  8. 'ACK_Received', 'time_queued', 'time_sent', 'time_delivered', 'sender_estimates', 'probability_mass','order']
  9. def __init__(self, conf, route, payload, sender, dest, type, packet_id = None, msg_id="DUMMY", order=1, num=1, message=None):
  10. self.conf = conf
  11. self.id = packet_id or random_string(32)
  12. self.route = route
  13. self.payload = payload
  14. self.real_sender = sender
  15. self.dest = dest
  16. self.order = order
  17. self.msg_id = msg_id
  18. self.message = message
  19. self.fragments = num
  20. self.type = type
  21. self.pool_logs = []
  22. # State
  23. self.dropped = False
  24. self.current_node = -1
  25. self.times_transmitted = 0
  26. self.ACK_Received = False
  27. self.time_queued = None
  28. self.time_sent = None
  29. self.time_delivered = None
  30. # Measurements
  31. self.sender_estimates = numpy.array([0.0, 0.0, 0.0]) #Other, A, B
  32. self.sender_estimates = numpy.zeros(self.conf["clients"]["number"])
  33. self.sender_estimates[self.real_sender.label] = 1.0
  34. self.probability_mass = numpy.zeros(self.conf["misc"]["num_target_packets"])
  35. if self.type=="REAL":
  36. self.message.reconstruct.add(self.id)
  37. @classmethod
  38. def new(cls, conf, net, dest, payload, sender, type, num, msg_id):
  39. '''Method used for constructing a new Packet where
  40. the content is defined by the client but the route is generated on the constructor.'''
  41. rand_route = net.select_random_route()
  42. rand_route = rand_route + [dest]
  43. return cls(conf=conf, route=rand_route, payload=payload, sender=sender, dest=dest, type=type, num=num, msg_id=msg_id)
  44. @classmethod
  45. def ack(cls, conf, net, dest, sender, packet_id, msg_id):
  46. ''' The class method used for creating an ack Packet. '''
  47. payload = random_string(conf["packet"]["packet_size"])
  48. rand_route = net.select_random_route()
  49. rand_route = rand_route + [dest]
  50. return cls(conf=conf, route=rand_route, payload=payload, sender=sender, dest=dest, packet_id=packet_id, msg_id=msg_id, type="ACK")
  51. @classmethod
  52. def dummy(cls, conf, net, dest, sender):
  53. ''' The class method used for creating a dummy Packet. '''
  54. payload = random_string(conf["packet"]["packet_size"])
  55. rand_route = net.select_random_route()
  56. rand_route = rand_route + [dest]
  57. return cls(conf=conf, route=rand_route, payload=payload, sender=sender, dest=dest, type="DUMMY", msg_id="-")
  58. @classmethod
  59. def dummy_ack(cls, conf, net, dest, sender):
  60. payload = random_string(conf["packet"]["ack_packet_size"])
  61. rand_route = net.select_random_route()
  62. rand_route = rand_route + [dest]
  63. return cls(conf=conf, route=rand_route, payload=payload, sender=sender, dest=dest, type="DUMMY_ACK", msg_id="DUMMY_ACK")
  64. def output(self):
  65. ''' Function prints the information about the packet'''
  66. if not self.conf["debug"]["enabled"]:
  67. return
  68. print("=====================")
  69. print("Packet ID : " + str(self.id))
  70. print("Packet Type : " + str(self.type))
  71. print("Sender : " + str(self.real_sender))
  72. print("Labels : " + str(self.sender_estimates))
  73. print("Time Added to Queue : " + str(self.time_queued))
  74. print("Time Sent : " + str(self.time_sent))
  75. print("Time Delivered : " + str(self.time_delivered))
  76. print("ACK Received : " + str(self.ACK_Received))
  77. print("Route : " + str(self.route))
  78. print("Current Hop : " + str(self.current_node))
  79. print("Times Transmitted : " + str(self.times_transmitted))
  80. print("=====================")