def new_agent(id, ip, port): global n, partners_list, event global f_received, Eta_received, SOC_received, V_received, Q_received, count_received global f_meas, P_meas, SOC_meas, V_meas, Q_meas with new_agent_lock: logging.info("new Agent " + str(id) + " is detected ") print("agent ", id, " connecting...") print("reconfigurating to add 1 partner to list...") partners_list[id] = [ip, port] channel = grpc.insecure_channel(ip + ":" + str(port)) stub = finite_consensus_pb2_grpc.AgentStub(channel) partners_list[id] += [ stub ] # store the rpc stub in the config.partners collection n += 1 f_received[id] = f_meas Eta_received[id] = P_meas SOC_received[id] = SOC_meas V_received[id] = V_meas Q_received[id] = Q_meas count_received[id] = 1 print(count_received) # event = True if sum(count_received.values()) == n: all_values_event.set() print("set!!!") # all_values_event.set() # add communication link to new agent print("finish reconfiguring")
def agent_online(self, request, context): try: if request.agent_id not in agents_wired: agents_wired[request.agent_id] = ["localhost", 8001] channel = grpc.insecure_channel("localhost" + ":" + str(8001)) stub = finite_consensus_pb2_grpc.AgentStub(channel) agents_wired[request.agent_id] += [stub] print "Agent" + str(request.agent_id) + "online" return admin_pb2.CommReply( status=admin_pb2.OperationStatus.Value('SUCCESS')) except Exception as exc: logging.critical(exc.message) return admin_pb2.CommReply( status=admin_pb2.OperationStatus.Value('FAILED'), message=exc.message)
logging.basicConfig( level=logging.DEBUG, filename=log, filemode="w", format='%(asctime)s (%(threadName)-9s) %(levelname)s: %(message)s') logging.info("Reading the configuration file") # read the configuration of the agent # print(opts.jsonFile) config = ReadConfiguration(opts.jsonFile) logging.info("Opening communication channels to neighbours") # open communication channels towards the neighbours for p in config.partners: channel = grpc.insecure_channel(p[1] + ":" + str(p[2])) stub = finite_consensus_pb2_grpc.AgentStub(channel) p += [stub] # store the rpc stub in the config.partners collection logging.info("Opening the communication channels to the admin") # open the communication channel to the admin admin_channel = grpc.insecure_channel(admin_ip + ":" + str(admin_port)) admin_stub = admin_pb2_grpc.adminStub(channel=admin_channel) logging.info("Starting the measurement thread") # configure and start the program threads meas_thread = threading.Thread(name='measurement-thread', target=pool_opal, args=(start_event, )) meas_thread.daemon = True meas_thread.start() # start the measurement thread