Ignore:
Timestamp:
04/07/12 08:48:34 (13 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented better connection and connection failure handling.

Location:
src/mlx/gui
Files:
3 edited

Legend:

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

    r58 r59  
    494494#-----------------------------------------------------------------------------
    495495
     496class PayloadPage(Page):
     497    """Page to allow setting up the payload."""
     498    def __init__(self, wizard):
     499        """Construct the page."""
     500        help = "The briefing contains the weights below.\n" \
     501               "Setup the cargo weight and check if the simulator\n" \
     502               "reports the expected Zero Fuel Weight."
     503        super(PayloadPage, self).__init__(wizard, "Payload", help)
     504
     505        button = gtk.Button("_Query ZFW")
     506        button.connect("clicked", self._zfwRequested)
     507        self.setMainWidget(button)
     508
     509    def _zfwRequested(self, button):
     510        """Called when the ZFW is requested from the simulator."""
     511        self._wizard.gui.simulator.requestZFW(self._handleZFW)
     512
     513    def _handleZFW(self, zfw):
     514        """Called when the ZFW value is retrieved."""
     515        print "ZFW", zfw
     516
     517#-----------------------------------------------------------------------------
     518
    496519class Wizard(gtk.VBox):
    497520    """The flight wizard."""
     
    509532        self._pages.append(GateSelectionPage(self))
    510533        self._pages.append(ConnectPage(self))
     534        self._pages.append(PayloadPage(self))
    511535
    512536        maxWidth = 0
     
    523547        self.set_size_request(maxWidth, maxHeight)
    524548
    525         self._fleet = None
    526         self._fleetCallback = None
    527         self._updatePlaneCallback = None
    528        
    529         self._loginResult = None
    530         self._bookedFlight = None
    531         self._departureGate = "-"
    532 
    533         self._logger = Logger(output = gui)
    534         self._flight = None
    535         self._simulator = None
    536        
    537         self.setCurrentPage(0)
     549        self._initialize()
    538550       
    539551    @property
     
    567579        self._pages[self._currentPage].grabDefault()
    568580
     581    def connected(self, fsType, descriptor):
     582        """Called when the connection could be made to the simulator."""
     583        self.nextPage()
     584
     585    def connectionFailed(self):
     586        """Called when the connection could not be made to the simulator."""
     587        self._initialize()
     588
     589    def disconnected(self):
     590        """Called when we have disconnected from the simulator."""
     591        self._initialize()
     592
     593    def _initialize(self):
     594        """Initialize the wizard."""
     595        self._fleet = None
     596        self._fleetCallback = None
     597        self._updatePlaneCallback = None
     598       
     599        self._loginResult = None
     600        self._bookedFlight = None
     601        self._departureGate = "-"
     602
     603        self.setCurrentPage(0)
     604       
    569605    def _getFleet(self, callback, force = False):
    570606        """Get the fleet, if needed.
     
    634670    def _connectSimulator(self):
    635671        """Connect to the simulator."""
    636         self._logger.reset()
    637         self._flight = Flight(self.gui._logger, self.gui)
    638 
    639         self._flight.aircraftType = self._bookedFlight.aircraftType
    640         aircraft = self._flight.aircraft = Aircraft.create(self._flight)
    641         self._flight.aircraft._checkers.append(self.gui)
    642        
    643         self._flight.cruiseAltitude = -1       
    644         self._flight.zfw = -1
    645 
    646         if self._simulator is None:
    647             self._simulator = fs.createSimulator(const.SIM_MSFS9, self.gui)
    648 
    649         self._flight.simulator = self._simulator
    650         self._simulator.connect(aircraft)
    651         #self._simulator.startMonitoring()
     672        self.gui.connectSimulator(self._bookedFlight.aircraftType)
    652673   
    653674#-----------------------------------------------------------------------------
  • src/mlx/gui/gui.py

    r51 r59  
    4343        self.config = config
    4444        self._connecting = False
     45        self._reconnecting = False
    4546        self._connected = False
    4647        self._logger = logger.Logger(output = self)
    4748        self._flight = None
    4849        self._simulator = None
     50        self._monitoring = False
    4951
    5052        self._stdioLock = threading.Lock()
     
    127129                                      else gdk.WATCH)
    128130
     131    @property
     132    def simulator(self):
     133        """Get the simulator used by us."""
     134        return self._simulator
     135       
    129136    def run(self):
    130137        """Run the GUI."""
     
    140147        if self._flight is not None:
    141148            simulator = self._flight.simulator
    142             simulator.stopMonitoring()
    143             simulator.disconnect()           
     149            if self._monitoring:
     150                simulator.stopMonitoring()
     151                self._monitoring = False
     152            simulator.disconnect()                       
    144153
    145154    def connected(self, fsType, descriptor):
     
    147156        self._connected = True
    148157        self._logger.untimedMessage("Connected to the simulator %s" % (descriptor,))
    149         gobject.idle_add(self._statusbar.updateConnection,
    150                          self._connecting, self._connected)
     158        gobject.idle_add(self._handleConnected, fsType, descriptor)
     159
     160    def _handleConnected(self, fsType, descriptor):
     161        """Called when the connection to the simulator has succeeded."""
     162        self._statusbar.updateConnection(self._connecting, self._connected)
     163        self.endBusy()
     164        if not self._reconnecting:
     165            self._wizard.connected(fsType, descriptor)
     166        self._reconnecting = False
     167
     168    def connectionFailed(self):
     169        """Called when the connection failed."""
     170        self._logger.untimedMessage("Connection to the simulator failed")
     171        gobject.idle_add(self._connectionFailed)
     172
     173    def _connectionFailed(self):
     174        """Called when the connection failed."""
     175        self.endBusy()
     176        self._statusbar.updateConnection(self._connecting, self._connected)
     177
     178        dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
     179                                   message_format =
     180                                   "Cannot connect to the simulator.",
     181                                   parent = self._mainWindow)
     182        dialog.format_secondary_markup("Rectify the situation, and press <b>Try again</b> "
     183                                       "to try the connection again, "
     184                                       "or <b>Cancel</b> to cancel the flight.")
     185       
     186        dialog.add_button("_Cancel", 0)
     187        dialog.add_button("_Try again", 1)
     188        dialog.set_default_response(1)
     189       
     190        result = dialog.run()
     191        dialog.hide()
     192        if result == 1:
     193            self.beginBusy("Connecting to the simulator.")
     194            self._simulator.reconnect()
     195        else:
     196            self._connecting = False
     197            self._reconnecting = False
     198            self._statusbar.updateConnection(self._connecting, self._connected)
     199            self._wizard.connectionFailed()
    151200       
    152201    def disconnected(self):
     
    154203        self._connected = False
    155204        self._logger.untimedMessage("Disconnected from the simulator")
    156         gobject.idle_add(self._statusbar.updateConnection,
    157                          self._connecting, self._connected)
     205
     206        gobject.idle_add(self._disconnected)
     207
     208    def _disconnected(self):
     209        """Called when we have disconnected from the simulator unexpectedly."""       
     210        self._statusbar.updateConnection(self._connecting, self._connected)
     211
     212        dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
     213                                   message_format =
     214                                   "The connection to the simulator failed unexpectedly.",
     215                                   parent = self._mainWindow)
     216        dialog.format_secondary_markup("If the simulator has crashed, restart it "
     217                                       "and restore your flight as much as possible "
     218                                       "to the state it was in before the crash.\n"
     219                                       "Then press <b>Reconnect</b> to reconnect.\n\n"
     220                                       "If you want to cancel the flight, press <b>Cancel</b>.")
     221
     222        dialog.add_button("_Cancel", 0)
     223        dialog.add_button("_Reconnect", 1)
     224        dialog.set_default_response(1)
     225
     226        result = dialog.run()
     227        dialog.hide()
     228        if result == 1:
     229            self.beginBusy("Connecting to the simulator.")
     230            self._reconnecting = True
     231            self._simulator.reconnect()
     232        else:
     233            self._connecting = False
     234            self._reconnecting = False
     235            self._statusbar.updateConnection(self._connecting, self._connected)
     236            self._wizard.disconnected()
    158237
    159238    def write(self, msg):
     
    285364            self._stdioAfterNewLine = False
    286365
     366    def connectSimulator(self, aircraftType):
     367        """Connect to the simulator for the first time."""
     368        self._logger.reset()
     369
     370        self._flight = flight.Flight(self._logger, self)
     371        self._flight.aircraftType = aircraftType
     372        self._flight.aircraft = acft.Aircraft.create(self._flight)
     373        self._flight.aircraft._checkers.append(self)
     374       
     375        if self._simulator is None:
     376            self._simulator = fs.createSimulator(const.SIM_MSFS9, self)
     377
     378        self._flight.simulator = self._simulator
     379
     380        self.beginBusy("Connecting to the simulator...")
     381        self._statusbar.updateConnection(self._connecting, self._connected)
     382
     383        self._connecting = True
     384        self._simulator.connect(self._flight.aircraft)       
     385
    287386    def _connectToggled(self, button):
    288387        """Callback for the connection button."""
    289388        if self._connectButton.get_active():
    290             self._logger.reset()
    291             self._flight = flight.Flight(self._logger, self)
    292 
    293389            acftListModel = self._acftList.get_model()
    294             self._flight.aircraftType = \
    295                 acftListModel[self._acftList.get_active()][1]
    296             self._flight.aircraft = acft.Aircraft.create(self._flight)
    297             self._flight.aircraft._checkers.append(self)
    298 
     390            self.connectSimulator(acftListModel[self._acftList.get_active()][1])
     391           
    299392            self._flight.cruiseAltitude = self._flSpinButton.get_value_as_int() * 100
    300393
    301394            self._flight.zfw = self._zfwSpinButton.get_value_as_int()
    302 
    303             if self._simulator is None:
    304                 self._simulator = fs.createSimulator(const.SIM_MSFS9, self)
    305 
    306             self._flight.simulator = self._simulator
    307 
    308             self._connecting = True
    309             self._simulator.connect(self._flight.aircraft)
     395           
    310396            self._simulator.startMonitoring()
     397            self._monitoring = True
    311398        else:
    312399            self.resetFlightStatus()
    313400            self._connecting = False
     401            self._connected = False
     402
    314403            self._simulator.stopMonitoring()
     404            self._monitoring = False
     405
    315406            self._simulator.disconnect()
    316407            self._flight = None
  • src/mlx/gui/statusbar.py

    r49 r59  
    8787   
    8888    def _drawConnState(self, connStateArea, eventOrContext):
    89         """Draw the connection state."""
     89        """Draw the connection state."""       
    9090        context = eventOrContext if pygobject else connStateArea.window.cairo_create()
    9191
Note: See TracChangeset for help on using the changeset viewer.