Changeset 746:277ecb58fe9b


Ignore:
Timestamp:
01/02/16 12:31:05 (7 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

When so configured, RPC calls are used to communicate with the MAVA website (re #283)

Location:
src/mlx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/gui/gui.py

    r744 r746  
    9595        self._credentialsPassword = None
    9696
    97         self.webHandler = web.Handler()
     97        self.webHandler = web.Handler(config, self._getCredentialsCallback)
    9898        self.webHandler.start()
    9999
  • src/mlx/web.py

    r743 r746  
    22import const
    33import util
     4import rpc
    45import rpccommon
    56
     
    611612#------------------------------------------------------------------------------
    612613
     614class RPCRequest(Request):
     615    """Common base class for RPC requests.
     616
     617    It stores the RPC client received from the handler."""
     618    def __init__(self, client, callback):
     619        """Construct the request."""
     620        super(RPCRequest, self).__init__(callback)
     621        self._client = client
     622
     623#------------------------------------------------------------------------------
     624
    613625class Login(Request):
    614626    """A login request."""
     
    684696#------------------------------------------------------------------------------
    685697
     698class LoginRPC(RPCRequest):
     699    """An RPC-based login request."""
     700    def __init__(self, client, callback, pilotID, password, entranceExam):
     701        """Construct the login request with the given pilot ID and
     702        password."""
     703        super(LoginRPC, self).__init__(client, callback)
     704
     705        self._pilotID = pilotID
     706        self._password = password
     707        self._entranceExam = entranceExam
     708
     709    def run(self):
     710        """Perform the login request."""
     711        result = Result()
     712        # FIXME: handle the entrance exam case
     713        result.entranceExam = self._entranceExam
     714
     715        self._client.setCredentials(self._pilotID, self._password)
     716        pilotName = self._client.login()
     717        result.loggedIn = pilotName is not None
     718        if result.loggedIn:
     719            result.pilotID = self._pilotID
     720            result.pilotName = pilotName
     721            result.password = self._password
     722            result.flights = self._client.getFlights()
     723
     724        return result
     725
     726#------------------------------------------------------------------------------
     727
    686728class GetFleet(Request):
    687729    """Request to get the fleet from the website."""
     
    699741        result.fleet = Fleet(f)
    700742        f.close()
     743
     744        return result
     745
     746#------------------------------------------------------------------------------
     747
     748class GetFleetRPC(RPCRequest):
     749    """Request to get the fleet from the website using RPC."""
     750    def __init__(self, client, callback):
     751        """Construct the request with the given client and callback function."""
     752        super(GetFleetRPC, self).__init__(client, callback)
     753
     754    def run(self):
     755        """Perform the login request."""
     756        result = Result()
     757
     758        result.fleet = self._client.getFleet()
    701759
    702760        return result
     
    730788        result = Result()
    731789        result.success = line == "OK"
     790
     791        return result
     792
     793#------------------------------------------------------------------------------
     794
     795class UpdatePlaneRPC(RPCRequest):
     796    """RPC request to update the status and the position of a plane in the
     797    fleet."""
     798    def __init__(self, client, callback, tailNumber, status, gateNumber = None):
     799        """Construct the request."""
     800        super(UpdatePlaneRPC, self).__init__(client, callback)
     801        self._tailNumber = tailNumber
     802        self._status = status
     803        self._gateNumber = gateNumber
     804
     805    def run(self):
     806        """Perform the plane update."""
     807        self._client.updatePlane(self._tailNumber, self._status, self._gateNumber)
     808
     809        # Otherwise an exception is thrown
     810        result = Result()
     811        result.success = True
    732812
    733813        return result
     
    9201000#------------------------------------------------------------------------------
    9211001
     1002class SendPIREPRPC(RPCRequest):
     1003    """A request to send a PIREP to the MAVA website via the RPC interface."""
     1004
     1005    def __init__(self, client, callback, pirep):
     1006        """Construct the sending of the PIREP."""
     1007        super(SendPIREPRPC, self).__init__(client, callback)
     1008        self._pirep = pirep
     1009
     1010    def run(self):
     1011        """Perform the sending of the PIREP."""
     1012        pirep = self._pirep
     1013        resultCode = self._client.addPIREP(pirep.bookedFlight.id, pirep)
     1014
     1015        result = Result()
     1016        result.success = resultCode==rpc.Client.RESULT_OK
     1017        result.alreadyFlown = resultCode==rpc.Client.RESULT_FLIGHT_ALREADY_REPORTED
     1018        result.notAvailable = resultCode==rpc.Client.RESULT_FLIGHT_NOT_EXISTS
     1019
     1020        return result
     1021
     1022#------------------------------------------------------------------------------
     1023
    9221024class SendACARS(Request):
    9231025    """A request to send an ACARS to the MAVA website."""
     
    9671069#------------------------------------------------------------------------------
    9681070
     1071class SendACARSRPC(RPCRequest):
     1072    """A request to send an ACARS to the MAVA website via JSON-RPC."""
     1073    def __init__(self, client, callback, acars):
     1074        """Construct the request for the given PIREP."""
     1075        super(SendACARSRPC, self).__init__(client, callback)
     1076        self._acars = acars
     1077
     1078    def run(self):
     1079        """Perform the sending of the ACARS."""
     1080        print "Sending the online ACARS via JSON-RPC"
     1081
     1082        self._client.updateOnlineACARS(self._acars)
     1083        return Result()
     1084
     1085#------------------------------------------------------------------------------
     1086
    9691087class SendBugReport(Request):
    9701088    """A request to send a bug report to the project homepage."""
     
    10031121    It can process one request at a time. The results are passed to a callback
    10041122    function."""
    1005     def __init__(self):
     1123    def __init__(self, config, getCredentialsFn):
    10061124        """Construct the handler."""
    10071125        super(Handler, self).__init__()
     
    10111129
    10121130        self.daemon = True
     1131        self._config = config
     1132        self._rpcClient = rpc.Client(getCredentialsFn)
     1133        if config.rememberPassword:
     1134            self._rpcClient.setCredentials(config.pilotID, config.password)
    10131135
    10141136    def login(self, callback, pilotID, password, entranceExam = False):
    10151137        """Enqueue a login request."""
    1016         self._addRequest(Login(callback, pilotID, password, entranceExam))
     1138        request = \
     1139          LoginRPC(self._rpcClient, callback, pilotID, password, entranceExam) \
     1140          if self._config.useRPC and not entranceExam \
     1141          else Login(callback, pilotID, password, entranceExam)
     1142
     1143        self._addRequest(request)
    10171144
    10181145    def getFleet(self, callback):
    10191146        """Enqueue a fleet retrieval request."""
    1020         self._addRequest(GetFleet(callback))
     1147        request = \
     1148          GetFleetRPC(self._rpcClient, callback,) if self._config.useRPC \
     1149          else GetFleet(callback)
     1150        self._addRequest(request)
    10211151
    10221152    def updatePlane(self, callback, tailNumber, status, gateNumber = None):
    10231153        """Update the status of the given plane."""
    1024         self._addRequest(UpdatePlane(callback, tailNumber, status, gateNumber))
     1154        request = \
     1155          UpdatePlaneRPC(self._rpcClient, callback,
     1156                         tailNumber, status, gateNumber) \
     1157          if self._config.useRPC \
     1158          else UpdatePlane(callback, tailNumber, status, gateNumber)
     1159        self._addRequest(request)
    10251160
    10261161    def getNOTAMs(self, callback, departureICAO, arrivalICAO):
     
    10341169    def sendPIREP(self, callback, pirep):
    10351170        """Send the given PIREP."""
    1036         self._addRequest(SendPIREP(callback, pirep))
     1171        request = \
     1172          SendPIREPRPC(self._rpcClient, callback, pirep) if self._config.useRPC \
     1173          else SendPIREP(callback, pirep)
     1174        self._addRequest(request)
    10371175
    10381176    def sendACARS(self, callback, acars):
    10391177        """Send the given ACARS"""
    1040         self._addRequest(SendACARS(callback, acars))
     1178        request = \
     1179          SendACARSRPC(self._rpcClient, callback, acars) if self._config.useRPC \
     1180          else SendACARS(callback, acars)
     1181        self._addRequest(request)
    10411182
    10421183    def sendBugReport(self, callback, summary, description, email):
Note: See TracChangeset for help on using the changeset viewer.