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

Implemented better connection and connection failure handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.