def runContainer(): # Creating container contains the home agents # and prediction agents RC = aiomas.Container.create(('localhost', 5555)) # Create the DB engine # db_engine = create_engine("mysql+pymysql://{}@{}/{}".format(CF.DB_USER, CF.DB_HOST, CF.DB_NAME)) db_engine = CF.get_db_engine() # Initiate the blockchain agent blockChainAgent = BlockchainAgent(container=RC, ) # Dump the blockchain agent address logger.info("Blcokchain agent initiated at {}".format( blockChainAgent.addr)) # Record this agent to DB status = recordAgent(agent_addr=blockChainAgent.addr, agent_type='blockchain', db_engine=db_engine) # Run the event loop try: logger.info( "Running the event loop. The blockchain agent is open to be connected!" ) aiomas.run() except KeyboardInterrupt: logging.info("Keyboard Interrupted") except Exception as e: traceback.print_exc(file=sys.stdout) # Shutting donw the controller and thereby cleaning # all agents try: logger.info("Shutting down the root container...") RC.shutdown() logger.info("Done.") logger.info("Killing Blockchain agent") status = killAgent(agent_addr=blockChainAgent.addr, agent_type='blockchain', db_engine=db_engine) if status: logger.info("Done.") else: logger.info("Couldnot kill the agent!") except Exception as e: logger.info("Failed to shutdown the root container") traceback.print_exc(file=sys.stdout)
def main(session_id, agent_id, port): """ """ # DB engine db_engine = CF.get_db_engine() agent_addr = getAgentAddress(agent_id=int(agent_id), session_id=session_id, db_engine=db_engine) if agent_addr is None: logging.info("Agent address couldn't be retreived. Make sure to provide correct session ID.") return logging.info("Agent's address: {}".format(agent_addr)) try: # Create the container the host trigger agent c = aiomas.Container.create(('localhost', int(port))) # Host the trigger agent trigger_agent = TriggerAgent(container=c) # Kick the home agent by trigger agent aiomas.run(until=trigger_agent.run(agent_addr)) except OSError: logger.info("Probably the provided port is already in use or the home agent is dead!") return except ConnectionResetError: logger.info("Probably the home agent died.") except Exception as e: logger.info("Failed to open/create container or run the triggering agent!") traceback.print_exc(file=sys.stdout) # Shutting down the container logger.info("Shutting down the triggering agents container.") c.shutdown()
def runContainer(): # Creating container contains the home agents # and prediction agents HC = aiomas.Container.create(('localhost', 5556), clock=CLOCK) # HC = aiomas.Container.create(('localhost', 5556), clock=CLOCK) # Set the clcok # t_clock_setter = asyncio.async(clock_setter()) # List of Homes homes = [9019, 9981] # 7881, 100237, 7850, 980, 9981,] # Create the DB engine # db_engine = create_engine("mysql+pymysql://{}@{}/{}".format(CF.DB_USER, CF.DB_HOST, CF.DB_NAME)) db_engine = CF.get_db_engine() # Initiate the agents into HC homeAgents = [ HomeAgent( container=HC, agent_id=home, db_engine=db_engine, ) for home in homes ] # Creating the session session_id = createSession(agents=homeAgents, db_engine=db_engine) # Address of the blockchain agent # Later, it will be retreived from the Agent Server bc_address = getActiveBlockchainAddress(db_engine=db_engine) if bc_address is None: logging.info("Blockchain is not initiated.") else: # Bind the blockchain with home agents for agent in homeAgents: agent.setBlockchainAddress(bc_address=bc_address) # Run the event loop try: logger.info( "Running the event loop. One of the home agents is trying to connect with BC agent!" ) logger.info("Session ID:{}".format(session_id)) # Run the even loop aiomas.run() except KeyboardInterrupt: logger.info("Stopping the event loop") # Try to stop the event loop except Exception as e: traceback.print_exc(file=sys.stdout) finally: # Killing the current session killSession(session_id=session_id, db_engine=db_engine) # Shutting donw the controller and thereby cleaning # all agents try: logger.info( "Shutting down the home container...and cancelling the clock") HC.shutdown() # t_clock_setter.cancel() logger.info("Done.") except Exception as e: logger.info("Failed to shutdown the home container") traceback.print_exc(file=sys.stdout)