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 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.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
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
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
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