Exemple #1
0
def randomLocationInCircle(center, radius):
    orientationRad = random.uniform(0, 2.0 *  math.pi)
    distanceFromCenter = random.uniform(0, radius)
    randomLocation = Location()
    randomLocation.x = int(center.x + radius * math.sin(orientationRad))
    randomLocation.y = int(center.y + radius * math.cos(orientationRad))
    # Bound the new location within the world
    randomLocation.x = max(0, min(config.WORLD_SIZE[0], randomLocation.x))
    randomLocation.y = max(0, min(config.WORLD_SIZE[1], randomLocation.y))
    return randomLocation
def updateLocThread():
    global IS_BUS_START
    while True:
        if IS_BUS_START == True:
            Location.moveOneStop()
            LOGGER.info("Update Location")
            sleep(10)       # take 10 seconds to move to next stop
            # if Location reaches the end - 29, turn off 
            if Location.getLocation() == 29:
                os._exit(0)
Exemple #3
0
def updateLocThread():
    global IS_BUS_START
    while True:
        if IS_BUS_START == True:
            Location.moveOneStop()
            LOGGER.info("Update Location")
            sleep(10)  # take 10 seconds to move to next stop
            # if Location reaches the end - 29, turn off
            if Location.getLocation() == 29:
                os._exit(0)
 def next(self, input):
     action = map(DriverAction.DriverAction, [input["action"]])[0]
     if action == DriverAction.recvLocReq:
         # TODO: response the current location
         # check if the rsn is my RSN, route matched?
         global ROUTE_NO, DIRECTION, RSN_ADDR, LOCAL_ADDR, BUS_ID, WATCHDOG
         LOGGER.info("received RSN location request: %s" % input)
         
         # pet the watch dog
         WATCHDOG.petWatchdog()
         
         loc_message = {
                        "SM" : "RSN_SM",
                        "action" : "recvDriverLoc",
                        "requestNo" : input["requestNo"],
                        "route" : ROUTE_NO,
                        "direction" : DIRECTION,
                        "busId" : BUS_ID,
                        "location" : Location.getLocation(), 
                        "busIP" : LOCAL_ADDR.ip,
                        "busPort" : LOCAL_ADDR.port
                        }
         # TODO: TEST ONLY; rsn should be modified
         MessagePasser.directSend(RSN_ADDR.ip, RSN_ADDR.port, loc_message)
         
         return DriverSM.Ready
     elif action == DriverAction.timeout:
         # don't hear from RSN for 15 secs
         # send re-elect message to GSN
         global ROUTE_NO, DIRECTION, GSN_ADDR, LOCAL_ADDR, BUS_ID, WATCHDOG
         LOGGER.info("send re-elect request to GSN")
         RSN_elect_message = {
                              "SM" : "GSN_SM",
                              "action" : "recvElecReq",
                              "busId" : BUS_ID,
                              "route" : ROUTE_NO,
                              "direction" : DIRECTION,
                              "location" : Location.getLocation(),
                              "busIP" : LOCAL_ADDR.ip,
                              "busPort" : LOCAL_ADDR.port
                              }
         MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, RSN_elect_message)
         WATCHDOG.petWatchdog()
         return DriverSM.Hold
     elif action == DriverAction.turnOff:
         # TODO: do something to shut-down
         RESEND_ELECT_NUM = 0
         WATCHDOG.StopWatchdog()
         return DriverSM.Off
     else:
         # for other illegal action
         # assert 0, "Ready: invalid action: %s" % str(input)
         pass
    def next(self, input):
        action = map(DriverAction.DriverAction, [input["action"]])[0]
        if action == DriverAction.start:
            global LOCAL_ADDR, RSN_ADDR, ROUTE_NO, DIRECTION, BUS_ID, WATCHDOG
            
            ROUTE_NO = input["route"]
            DIRECTION = input["direction"]
            Location.setLocation(int(input["location"]))
            
            # TODO: ping RSN to add into the group
            add_message = {
                           "SM" : "GSN_SM",
                           "action" : "recvBusReq",
                           "type" : "add",
                           "route" : ROUTE_NO,
                           "direction" : DIRECTION,
                           "busId" : BUS_ID,
                           "location" : Location.getLocation(), 
                           "busIP" : LOCAL_ADDR.ip,
                           "busPort" : LOCAL_ADDR.port
                           }
            # TODO: should use real gsn 
            MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, add_message)
            
            # start the watchdog
            WATCHDOG.startWatchdog()
            
            return DriverSM.Init_Waiting
        elif action == DriverAction.timeout:
            # TODO: re-ping
            return DriverSM.Idle
        elif action == DriverAction.turnOff:
            # TODO: do something to shut-down
            RESEND_ELECT_NUM = 0
            WATCHDOG.stopWatchdog()

            return DriverSM.Off
        else:
            # for other illegal action
            # assert 0, "Idle: invalid action: %s" % str(input)
            pass
Exemple #6
0
def state():
    global DISPATCHERMAP, LOCALNAME

    if "RSN_SM" in DISPATCHERMAP:
        if DISPATCHERMAP["RSN_SM"].state() != str(
                RSNStateMachine.RSN_SM.Ready):
            return None

        state = {}
        state["SM"] = "RSN_SM"
        state["rsnId"] = DISPATCHERMAP["RSN_SM"].RSN_ID
        state["busId"] = DISPATCHERMAP["DRIVER_SM"].BUS_ID
        state["route"] = DISPATCHERMAP["DRIVER_SM"].ROUTE_NO
        state["localName"] = LOCALNAME
        state["state"] = DISPATCHERMAP["RSN_SM"].state()
        state["BUS_TABLE"] = DISPATCHERMAP["RSN_SM"].busTable()
        state["location"] = Location.getLocation()
        return state
    elif "USER_SM" in DISPATCHERMAP:
        user_sm = DISPATCHERMAP["USER_SM"]
        if user_sm.state() != str(UserStateMachine.USER_SM.Ready) or \
            user_sm.IS_REPORTED == True or \
            user_sm.LAST_RESPONSE == None or \
            user_sm.LAST_REQ == None or \
            user_sm.LAST_REQ["requestId"] != user_sm.LAST_RESPONSE["original"]["requestId"]:
            return None

        state = {}
        state["SM"] = "USER_SM"
        state["userId"] = user_sm.USER_ID
        state["localName"] = LOCALNAME
        state["state"] = user_sm.state()
        state["query"] = user_sm.LAST_REQ
        state["response"] = user_sm.LAST_RESPONSE
        state["arriveTime"] = user_sm.ARRIVE_TIME
        user_sm.IS_REPORTED = True
        return state

        return None
Exemple #7
0
def state():
    global DISPATCHERMAP, LOCALNAME
    
    if "RSN_SM" in DISPATCHERMAP:
        if DISPATCHERMAP["RSN_SM"].state() != str(RSNStateMachine.RSN_SM.Ready):
            return None
       
        state = {}
        state["SM"] = "RSN_SM"
        state["rsnId"] = DISPATCHERMAP["RSN_SM"].RSN_ID
        state["busId"] = DISPATCHERMAP["DRIVER_SM"].BUS_ID
        state["route"] = DISPATCHERMAP["DRIVER_SM"].ROUTE_NO
        state["localName"] = LOCALNAME
        state["state"] = DISPATCHERMAP["RSN_SM"].state()
        state["BUS_TABLE"] = DISPATCHERMAP["RSN_SM"].busTable()
        state["location"] = Location.getLocation()
        return state
    elif "USER_SM" in DISPATCHERMAP:
        user_sm = DISPATCHERMAP["USER_SM"]
        if user_sm.state() != str(UserStateMachine.USER_SM.Ready) or \
            user_sm.IS_REPORTED == True or \
            user_sm.LAST_RESPONSE == None or \
            user_sm.LAST_REQ == None or \
            user_sm.LAST_REQ["requestId"] != user_sm.LAST_RESPONSE["original"]["requestId"]:
            return None
        
        state = {}
        state["SM"] = "USER_SM"
        state["userId"] = user_sm.USER_ID
        state["localName"] = LOCALNAME
        state["state"] = user_sm.state()
        state["query"] = user_sm.LAST_REQ
        state["response"] = user_sm.LAST_RESPONSE
        state["arriveTime"] = user_sm.ARRIVE_TIME
        user_sm.IS_REPORTED = True
        return state
        
        return None
Exemple #8
0
 def __init__(self, usr: str, loc: ci.SourceLocation):
     self.usr = usr  # Unified Symbol Resolution (USR) for the function
     self.loc = Location(loc)
Exemple #9
0
 def __init__(self, usr: str, context: IndexContext):
     self.usr = usr
     self.loc = Location(context.top_cursor().location)
     self.try_block_stack = context.try_block_stack.copy()
    def next(self, input):
        action = map(DriverAction.DriverAction, [input["action"]])[0]
        if action == DriverAction.recvLocReq:
            # TODO: response the current location
            # check if the rsn is my RSN, route matched?
            global ROUTE_NO, DIRECTION, RSN_ADDR, LOCAL_ADDR, BUS_ID, WATCHDOG
            LOGGER.info("received RSN location request: %s" % input)
            
            # pet the watch dog
            WATCHDOG.petWatchdog()
            
            # check if the RSN has changed
            if RSN_ADDR.ip != input["rsnIP"] or RSN_ADDR.port != input["rsnPort"]:
                LOGGER.info("RSN has changed")
                RSN_ADDR = Addr(input["rsnIP"], input["rsnPort"])
            
            loc_message = {
                           "SM" : "RSN_SM",
                           "action" : "recvDriverLoc",
                           "requestNo" : input["requestNo"],
                           "route" : ROUTE_NO,
                           "direction" : DIRECTION,
                           "busId" : BUS_ID,
                           "location" : Location.getLocation(), 
                           "busIP" : LOCAL_ADDR.ip,
                           "busPort" : LOCAL_ADDR.port
                           }
            # TODO: TEST ONLY; rsn should be modified
            MessagePasser.directSend(RSN_ADDR.ip, RSN_ADDR.port, loc_message)
            
            return DriverSM.Ready
        
        elif action == DriverAction.restart:
            # restart
            global LOCAL_ADDR, RSN_ADDR, ROUTE_NO, DIRECTION, BUS_ID, RESEND_ELECT_NUM

            LOGGER.info("Receive restart message from GSN. Restart now.")
                
                
            # TODO: ping RSN to add into the group
            add_message = {
                           "SM" : "GSN_SM",
                           "action" : "recvBusReq",
                           "type" : "add",
                           "route" : ROUTE_NO,
                           "direction" : DIRECTION,
                           "busId" : BUS_ID,
                           "location" : Location.getLocation(), 
                           "busIP" : LOCAL_ADDR.ip,
                           "busPort" : LOCAL_ADDR.port
                           }
            # TODO: should use real gsn 
            MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, add_message)

            RESEND_ELECT_NUM = 0
            
            # pet the watch dog
            WATCHDOG.petWatchdog()
            
            return DriverSM.Init_Waiting
        elif action == DriverAction.timeout:
            # if reaches the limit of re-elect, reboot
            global RESEND_ELECT_NUM
            if RESEND_ELECT_NUM >= 3:
                LOGGER.info("RESEND_ELECT_NUM reaches the limit, reboot now.")
                RESEND_ELECT_NUM = 0
                # restart
                global LOCAL_ADDR, RSN_ADDR, ROUTE_NO, DIRECTION, BUS_ID

                add_message = {
                               "SM" : "GSN_SM",
                               "action" : "recvBusReq",
                               "type" : "add",
                               "route" : ROUTE_NO,
                               "direction" : DIRECTION,
                               "busId" : BUS_ID,
                               "location" : Location.getLocation(), 
                               "busIP" : LOCAL_ADDR.ip,
                               "busPort" : LOCAL_ADDR.port
                               }
                # TODO: should use real gsn 
                MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, add_message)
                
                # pet the watch dog
                WATCHDOG.petWatchdog()
                
                return DriverSM.Init_Waiting
            else:
                # re-send re-elect msg
                LOGGER.info("send re-elect request to GSN, RESEND_ELECT_NUM: %s" % str(RESEND_ELECT_NUM))
                RESEND_ELECT_NUM = RESEND_ELECT_NUM + 1
                RSN_elect_message = {
                                     "SM" : "GSN_SM",
                                     "action" : "recvElecReq",
                                     "busId" : BUS_ID,
                                     "route" : ROUTE_NO,
                                     "direction" : DIRECTION,
                                     "location" : Location.getLocation(),
                                     "busIP" : LOCAL_ADDR.ip,
                                     "busPort" : LOCAL_ADDR.port
                                     }
                MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, RSN_elect_message)
                
                # pet the watch dog
                WATCHDOG.petWatchdog()
                
                return DriverSM.Hold
        elif action == DriverAction.turnOff:
            # TODO: do something to shut-down
            RESEND_ELECT_NUM = 0
            WATCHDOG.stopWatchdog()
            return DriverSM.Off
        else:
            # for other illegal action
            # assert 0, "Init_Waiting: invalid action: %s" % str(input)
            pass
    def next(self, input):
        action = map(DriverAction.DriverAction, [input["action"]])[0]
        if action == DriverAction.recvRSNAck:
            global MYSELF_ADDR, RSN_ADDR, ROUTE_NO, DIRECTION, BUS_ID

            # if the response route number doesn't match
            if input["route"] != ROUTE_NO:
                return DriverSM.Init_Waiting
            
            RSN_ADDR = Addr(input["rsnIP"], input["rsnPort"])
            
            # pet the watch dog
            WATCHDOG.petWatchdog()

            return DriverSM.Ready
        elif action == DriverAction.timeout:
            # not receive the reponse from RSN for 15 secs
            global RESEND_ELECT_NUM
            if RESEND_ELECT_NUM >= 3:
                LOGGER.info("RESEND_ELECT_NUM reaches the limit, reboot now.")
                RESEND_ELECT_NUM = 0
                # restart
                global LOCAL_ADDR, RSN_ADDR, ROUTE_NO, DIRECTION, BUS_ID

                add_message = {
                               "SM" : "GSN_SM",
                               "action" : "recvBusReq",
                               "type" : "add",
                               "route" : ROUTE_NO,
                               "direction" : DIRECTION,
                               "busId" : BUS_ID,
                               "location" : Location.getLocation(), 
                               "busIP" : LOCAL_ADDR.ip,
                               "busPort" : LOCAL_ADDR.port
                               }
                # TODO: should use real gsn 
                MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, add_message)
                
                # pet the watch dog
                WATCHDOG.petWatchdog()
                
                return DriverSM.Init_Waiting
            else:
                # re-send re-elect msg
                LOGGER.info("send re-elect request to GSN, RESEND_ELECT_NUM: %s" % str(RESEND_ELECT_NUM))
                RESEND_ELECT_NUM = RESEND_ELECT_NUM + 1
                RSN_elect_message = {
                                     "SM" : "GSN_SM",
                                     "action" : "recvElecReq",
                                     "busId" : BUS_ID,
                                     "route" : ROUTE_NO,
                                     "direction" : DIRECTION,
                                     "location" : Location.getLocation(),
                                     "busIP" : LOCAL_ADDR.ip,
                                     "busPort" : LOCAL_ADDR.port
                                     }
                MessagePasser.directSend(GSN_ADDR.ip, GSN_ADDR.port, RSN_elect_message)
                
                # pet the watch dog
                WATCHDOG.petWatchdog()
                
                return DriverSM.Init_Waiting
            
            return DriverSM.Init_Waiting
        elif action == DriverAction.turnOff:
            # TODO: do something to shut-down
            RESEND_ELECT_NUM = 0
            WATCHDOG.stopWatchdog()
            return DriverSM.Off
        else:
            # for other illegal action
            # assert 0, "Init_Waiting: invalid action: %s" % str(input)
            pass
 def __init__(self, context: IndexContext):
     self.loc = Location(context.top_cursor().location)
     self.catchers = []  # List[Catcher]
 def __init__(self, context: IndexContext):
     self.loc = Location(context.top_cursor().location)
     self.exception_type = None  # None means "catch all"
 def __init__(self, context: IndexContext):
     self.fun_usr = context.top_function()  # Containing function USR
     self.loc = Location(context.top_cursor().location)
     self.try_block_stack = context.try_block_stack.copy()
     self.exception_type = None
Exemple #15
0
    def __init__(self, travelTimeData: Queue):
        Process.__init__(self)
        self.travelTimeData = travelTimeData
        self.idealTravelTimes = dict()

        # initialization of ideal grid, with original unrestricted travel space
        grid = [
            [0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0],
            [
                0, 0, 0, 0, 0, 0, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0,
                0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0,
                0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0,
                0, 0
            ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0]
        ]

        floormap = np.array(grid).transpose(1, 0)
        shortestPathfinder = Pathfinder(floormap)
        exitPoints = [Location.getCoords(i)[0] for i in range(1, 9)]
        entryPoints = [Location.getCoords(i)[1] for i in range(1, 9)]

        for exitPoint in exitPoints:
            if exitPoint == None:
                continue
            else:
                for entryPoint in entryPoints:
                    self.idealTravelTimes[(
                        exitPoint, entryPoint)] = shortestPathfinder.search(
                            exitPoint, entryPoint)
        del self.idealTravelTimes[(Coord(19, 8), Coord(19, 8))]

        for path, time in self.idealTravelTimes.items():
            print(f"{path}: {time}")