Ignore:
Timestamp:
04/30/12 13:15:14 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

The general message sending works and the most important messages are sent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/fs.py

    r89 r133  
    44
    55import const
     6from sound import startSound
    67
    78import fsuipc
     9import threading
     10import time
    811
    912#-------------------------------------------------------------------------------
     
    4144           "Only MS Flight Simulator 2004 and X are supported"
    4245    return fsuipc.Simulator(connectionListener, connectAttempts = 3)
     46
     47#-------------------------------------------------------------------------------
     48
     49class MessageThread(threading.Thread):
     50    """Thread to handle messages."""
     51    def __init__(self, config, simulator):
     52        """Initialize the message thread with the given configuration and
     53        simulator."""
     54        super(MessageThread, self).__init__()
     55
     56        self._config = config
     57        self._simulator = simulator
     58
     59        self._requestCondition = threading.Condition()
     60        self._messages = []
     61        self._nextMessageTime = None
     62        self._toQuit = False
     63
     64        self.daemon = True
     65
     66    def add(self, messageType, text, duration):
     67        """Add the given message to the requested messages."""
     68        with self._requestCondition:
     69            self._messages.append((messageType, text, duration))
     70            self._requestCondition.notify()
     71
     72    def quit(self):
     73        """Quit the thread."""
     74        with self._requestCondition:
     75            self._toQuit = True
     76            self._requestCondition.notifty()
     77        self.join()
     78
     79    def run(self):
     80        """Perform the thread's operation."""
     81        while True:
     82            (messageType, text, duration) = (None, None, None)
     83            with self._requestCondition:
     84                now = time.time()
     85                while not self._toQuit and \
     86                      ((self._nextMessageTime is not None and \
     87                        self._nextMessageTime>now) or \
     88                       not self._messages):
     89                    self._requestCondition.wait(1)
     90                    now = time.time()
     91
     92                if self._toQuit: return
     93                if self._nextMessageTime is None or \
     94                   self._nextMessageTime<=now:
     95                    self._nextMessageTime = None
     96
     97                    if self._messages:
     98                        (messageType, text, duration) = self._messages[0]
     99                        del self._messages[0]
     100
     101            if text is not None:       
     102                self._sendMessage(messageType, text, duration)
     103
     104    def _sendMessage(self, messageType, text, duration):
     105        """Send the message and setup the next message time."""
     106        messageLevel = self._config.getMessageTypeLevel(messageType)
     107        if messageLevel==const.MESSAGELEVEL_SOUND or \
     108           messageLevel==const.MESSAGELEVEL_BOTH:
     109            startSound(const.SOUND_DING)
     110        if (messageLevel==const.MESSAGELEVEL_FS or \
     111            messageLevel==const.MESSAGELEVEL_BOTH):
     112            self._simulator.sendMessage("[MLX] " + text, duration = duration)
     113        self._nextMessageTime = time.time() + duration
     114
     115#-------------------------------------------------------------------------------
     116
     117_messageThread = None
     118
     119#-------------------------------------------------------------------------------
     120
     121def setupMessageSending(config, simulator):
     122    """Setup message sending with the given config and simulator."""
     123    global _messageThread
     124    if _messageThread is not None:
     125        _messageThread.quit()
     126    _messageThread = MessageThread(config, simulator)
     127    _messageThread.start()
     128
     129#-------------------------------------------------------------------------------
     130
     131def sendMessage(messageType, text, duration = 3):
     132    """Send the given message of the given type into the simulator and/or play
     133    a corresponding sound."""
     134    global _messageThread
     135    if _messageThread is not None:
     136        _messageThread.add(messageType, text, duration)
    43137
    44138#-------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.