reg_phase.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import numpy as np
  2. import simpy
  3. from classes.Client import *
  4. from classes.Net import *
  5. from classes.Utilities import *
  6. from classes.Publisher import *
  7. from datetime import datetime
  8. def setup_env(conf):
  9. env = simpy.Environment()
  10. env.stop_sim_event = env.event()
  11. env.phase_transitions = [False, False]
  12. env.stop_first_phase = env.event()
  13. env.message_ctr = 0
  14. env.total_messages_sent = 0
  15. env.total_messages_received = 0
  16. env.finished = False
  17. env.entropy = np.zeros(int(conf["misc"]["num_target_packets"]))
  18. return env
  19. def flush_logs(loggers):
  20. for l in loggers:
  21. for h in l.handlers:
  22. h.flush()
  23. def get_loggers(log_dir, conf):
  24. packet_logger = setup_logger('simulation.packet', os.path.join(log_dir, 'packet_log.csv'))
  25. packet_logger.info(StructuredMessage(metadata=("Type", "CurrentTime", "ClientID", "PacketID", "PacketType", "MessageID", "PacketTimeQueued", "PacketTimeSent", "PacketTimeDelivered", "TotalFragments", "PrOthers", "PrSenderA", "PrSenderB", "RealSenderLabel", "Route", "PoolSizes")))
  26. message_logger = setup_logger('simulation.messages', os.path.join(log_dir, 'message_log.csv'))
  27. message_logger.info(StructuredMessage(metadata=("Type", "CurrentTime", "ClientID", "MessageID", "NumPackets", "MsgTimeQueued", "MsgTimeSent", "MsgTimeDelivered", "MsgTransitTime", "MsgSize", "MsgRealSender","MsgRealSenderID")))
  28. return (packet_logger, message_logger, None)
  29. def run(log_dir, conf):
  30. # Setup environment
  31. env = setup_env(conf)
  32. # Initialize components
  33. type = conf["network"]["topology"]
  34. loggers = get_loggers(log_dir, conf)
  35. net = Network(env, type, conf, loggers)
  36. publisher = Publisher(env, conf, net, loggers)
  37. env.process(publisher.set_start_logs())
  38. clients = net.clients
  39. print("Number of active clients: ", len(clients))
  40. for c in clients:
  41. env.process(c.start_no_dummy())
  42. print("---------" + str(datetime.now()) + "---------")
  43. print("> Running the system for %s ticks to prepare it for measurment." % (100))
  44. print("> Finished the preparation")
  45. for p in net.mixnodes:
  46. p.mixlogging = True
  47. # Add a registration request to each client's queue
  48. for c in clients:
  49. msg = Message.random(conf=c.conf, net=c.net, sender=c, dest=publisher) # New Message
  50. c.schedule_message(msg)
  51. env.message_ctr += 1
  52. env.finished = True
  53. time_started = env.now
  54. env.run(until=env.stop_first_phase) # Run until publisher receives all requests
  55. print("> Publisher received all messages.")
  56. gr_start = env.now
  57. print("> Start sending out confirmation.")
  58. for c in clients:
  59. msg = Message(conf=conf, net=net, payload="GID", real_sender=publisher, dest=c)
  60. publisher.schedule_message(msg)
  61. env.message_ctr += 1
  62. env.process(c.set_start_logs())
  63. while len(publisher.pkt_buffer_out) > 0: # Flush all the replies into the network
  64. tmp_pkt = publisher.pkt_buffer_out.pop(0)
  65. publisher.send_packet(tmp_pkt)
  66. env.total_messages_sent += 1
  67. env.run(until=env.stop_sim_event) # Run until all clients received confirmation
  68. time_finished = env.now
  69. print("> Total Simulation Time [in ticks]: " + str(time_finished - time_started))
  70. flush_logs(loggers[:2])
  71. return gr_start