Ejemplo n.º 1
0
def main(Data,daemon,ns):
    LogicalProcess.nextLPID = 0
    
    #
    # initialization
    #
    print 'Starting run'
    
    random.seed(Data.seedNum)

    # Create Urban network/map
    Map = GenMap(Data.mapX,Data.mapY)
    Map.map(Data.numStreets,Data.Nuisance)
    randNodes=[]
    for i in range(Data.numTargets):
        randNodes.append(Map.RandNode())
        
    print '############################## MAP ENTRY PT'    
    print Map.MapEntryPt

    # Create HMINT, will be separate process started by Controller
    hmint = initHMINT(randNodes,Data)
    hmintInQ = LPInputQueue()
    hmintInQ.setLocalTime(0)
    hmintInQ.setLPID(hmint.LPID)
    hmintInQ_uri = daemon.register(hmintInQ)
    hmintInQ_uri = daemon.uriFor(hmintInQ)
    
    ns.register("inputqueue.hmint", hmintInQ_uri)        
    
    # Create CAOC, will be separate process started by Controller
    caoc = initCAOC(Data,Map.MapEntryPt)
    caocInQ = LPInputQueue()
    caocInQ.setLocalTime(0)
    caocInQ.setLPID(caoc.LPID)
    caocInQ_uri = daemon.register(caocInQ)
    caocInQ_uri = daemon.uriFor(caocInQ)
    ns.register("inputqueue.caoc", caocInQ_uri)    
    
    # Create IMINT, will be separate process started by Controller
    imint = initIMINT(Data.heuristic,Data.numTargets)
    imintInQ = LPInputQueue()
    imintInQ.setLocalTime(0)
    imintInQ.setLPID(imint.LPID)
    imintInQ_uri = daemon.register(imintInQ)
    imintInQ_uri = daemon.uriFor(imintInQ)
    ns.register("inputqueue.imint", imintInQ_uri)    
    
    # Create Simulation Controller
    controller = DroneSimController(hmint, caoc, imint)
    controller.setConnectionParams(PYRO_HOST, PYRO_PORT)
    controllerInQ = LPInputQueue()
    controllerInQ.setLocalTime(0)
    controllerInQ_uri = daemon.register(controllerInQ)
    controllerInQ_uri = daemon.uriFor(controllerInQ)
    ns.register("inputqueue.controller", controllerInQ_uri)
    
    # Create drone entities, each will be separate process started by Controller
    droneInQs = DroneInputQueueContainer()
    drones = []
    for i in range(Data.numDrones):
        dronename = i
        print 'dronename is %s' % dronename
        drone = createNewDrone(dronename, Data.typeOfDrone,Data.heuristic,Data.Legs)
        drones.append(drone)
        controller.addDrone(drone)
        droneInQs.addDroneInputQueue(dronename)
    droneInQs.setLPIDs(drones)
    droneInQs_uri = daemon.register(droneInQs)
    droneInQs_uri = daemon.uriFor(droneInQs)
    ns.register("inputqueue.drones", droneInQs_uri)    

    #Shared loop controler
    loopInQs=Loops()
    loopInQs_uri = daemon.register(loopInQs)
    loopInQs_uri = daemon.uriFor(loopInQs)
    ns.register("inL.loop", loopInQs_uri)

    # Start Controller process
    pController = Process(group=None, target=controller, name='Drone Sim Controller Process')
    print 'starting controller'
    pController.start()
    while not(pController.is_alive):
        time.sleep(0.1)
    print 'Controller process is alive'
    sys.stdout.flush()
    
    # HMINT
    print 'starting hmint'
    pHMINT = Process(group=None, target=hmint, name='HMINT Process')
    pHMINT.start()
    while not(pHMINT.is_alive):
        time.sleep(0.1) 
    print 'HMINT process is alive'
    
    # CAOC
    print 'starting caoc'
    pCAOC = Process(group=None, target=caoc, name='CAOC Process')
    pCAOC.start()
    while not(pCAOC.is_alive):
        time.sleep(0.1) 
    print 'CAOC process is alive'    
    
    # Drones
    print 'starting drones'
    pDrones = []
    for i in range(0, len(drones)):
        pDrone = Process(group=None, target=drones[i], name='drone'+str(drones[i].uid), args=(Map.MapEntryPt,)) 
        pDrones.append(pDrone)
        pDrone.start()
        while not(pDrone.is_alive):
            time.sleep(0.1) 
        print 'Drone %d process is alive'  % drones[i].uid       

    # IMINT
    print 'starting imint'
    pIMINT = Process(group=None, target=imint, name='IMINT Process')
    pIMINT.start()
    while not(pIMINT.is_alive):
        time.sleep(0.1) 
    print 'IMINT process is alive'    
    
    # Run shared object requests loop
    print 'starting shared objects request loop'
    daemon.requestLoop(loopCondition=lambda:loopInQs.loopC())

    # Request loop exited, simulation complete, release resources
    daemon.unregister("inL.loop")
    daemon.unregister("inputqueue.drones")
    daemon.unregister("inputqueue.controller")
    daemon.unregister("inputqueue.caoc")
    daemon.unregister("inputqueue.caoc")
    daemon.unregister("inputqueue.hmint")

    try:
        del hmint
        print "Deleted hmint!"
        del hmintInQ
        print "Deleted hmintInq!"
        del caoc
        print "Deleted caoc!"
        del caocInQ
        print "Deleted caocInQ!"
        del drones
        print "Deleted drones!"
        del droneInQs
        print "Deleted dronesInQs!"
        del imint
        print "Deleted imint!"
        del imintInQ
        print "Deleted imint!"
        del controller
        print "Deleted controller!"
        del controllerInQ
        print "Deleted controller!"
        print "Deleted all variables!"

    except:
        print "Failed to delete all variables"

    daemon.close()
    print "\n\n\n"
def main():
    
    numDrones = 1
    
    # Create PYRO remote object daemon
    daemon = Pyro4.Daemon(host=PYRO_HOST, port=PYRO_PORT)
    ns = Pyro4.locateNS() 
    
    # Create input queue instance to share
    stublp = StubLP()
    stublp.setConnectionParams(PYRO_HOST, PYRO_PORT)    
    stublpInQ = LPInputQueue()
    stublpInQ.setLocalTime(0)
    stublpInQ.setLPID(stublp.LPID)
    uri = daemon.register(stublpInQ)
    ns.register('inputqueue.stublp', uri)   
    
    # Producer Client
    pc1 = ProducerClient()
    pPc1 = Process(group=None, target=pc1, name='Test Message Producer Process')
    pPc1.start() 
    
    # Consumer Client (StubLP)
    pStubLP = Process(group=None, target=stublp, name='StubLP Process')
    pStubLP.start()
    
    # Create Simulation Controller
    controller = StubController(stublp)
    controller.setConnectionParams(PYRO_HOST, PYRO_PORT)
    controllerInQ = LPInputQueue()
    controllerInQ.setLocalTime(0)
    controllerInQ_uri = daemon.register(controllerInQ)
    ns.register("inputqueue.stubcontroller", controllerInQ_uri)          
    
    # Stub drones
    # Create drone entities, each will be separate process started by Controller
    droneInQs = DroneInputQueueContainer()
    drones = []
    for i in range(numDrones):
        dronename = i
        print 'dronename is %s' % dronename
        drone = createNewDrone(dronename)
        drones.append(drone)
        controller.addDrone(drone)
        droneInQs.addDroneInputQueue(dronename)
    droneInQs.setLPIDs(drones)
    droneInQs_uri = daemon.register(droneInQs)
    ns.register("inputqueue.stubdrones", droneInQs_uri)  
    
    # Start Controller process
    pController = Process(group=None, target=controller, name='Stub Controller Process')
    print 'starting controller'
    pController.start()     
    
    # Drones
    print 'starting drones'
    pDrones = []
    for i in range(0, len(drones)):
        pDrone = Process(group=None, target=drones[i], name='drone'+str(drones[i].uid)) 
        pDrones.append(pDrone)
        pDrone.start()  
        print i, pDrone.is_alive()    
    
    # Run shared object requests loop
    print 'starting shared objects request loop'
    daemon.requestLoop()