gnome-lirc-properties r13 - in trunk: . gnome_lirc_properties



Author: hasselmm
Date: Mon Apr 28 11:39:54 2008
New Revision: 13
URL: http://svn.gnome.org/viewvc/gnome-lirc-properties?rev=13&view=rev

Log:
Try to restart key-listener when the lircd socket is closed.
Should fix bug #529909.

* gnome_lirc_properties/lirc.py (KeyListener.__init__(),
  KeyListener.start(), KeyListener.stop(),
  KeyListener.__on_io_event()): Add internal __running flag to monitor
  listener state. This flag is changed by the start() and stop()
  methods. The I/O handler considers this flag to decide wheither to
  restart or to disconnect on socket shutdown. Also turned the nested
  on_io() function of KeyListener.__connect() into a regular class
  member (__on_io_event()).


Modified:
   trunk/ChangeLog
   trunk/gnome_lirc_properties/lirc.py

Modified: trunk/gnome_lirc_properties/lirc.py
==============================================================================
--- trunk/gnome_lirc_properties/lirc.py	(original)
+++ trunk/gnome_lirc_properties/lirc.py	Mon Apr 28 11:39:54 2008
@@ -818,6 +818,7 @@
         # pylint: disable-msg=E1002
         super(KeyListener, self).__init__()
 
+        self.__running = False
         self.__socket_name = socket_name
         self.__reconnect_source = 0
         self.__socket_source = 0
@@ -825,74 +826,88 @@
         self.__buffer = ''
 
     def start(self):
-        '''
-        Starts listening.
-        '''
+        '''Starts listening.'''
+
+        self.__running = True
 
         if not self.__connect():
             self.__reconnect()
 
-    def __connect(self):
-        '''
-        Connects to the lircd socket.
-        '''
+    def stop(self):
+        '''Stops listening.'''
 
+        self.__running = False
         self.__disconnect()
 
-        logging.info('trying to connect to lircd...')
+    def __on_io_event(self, fd, condition):
+        '''Handle I/O events on the lircd socket.'''
 
-        self.__socket = None
-        self.__buffer = ''
+        logging.info('I/O event on lirc socket %d: %d', fd, condition)
 
-        try:
-            # pylint: disable-msg=W0613
-            def on_io(fd, condition):
-                '''
-                Handle I/O events on the lircd socket.
-                '''
-
-                logging.info('I/O event on lirc socket %d: %d', fd, condition)
+        if condition & gobject.IO_IN:
+            logging.info('reading from lirc socket %d...', fd)
+            packet = self.__socket and self.__socket.recv(128)
 
-                if condition & gobject.IO_IN:
-                    logging.info('reading from lirc socket %d...', fd)
-                    packet = self.__socket and self.__socket.recv(128)
+            logging.info('...%d bytes received.', len(packet))
+            self.__buffer += packet
 
-                    logging.info('...%d bytes received.', len(packet))
-                    self.__buffer += packet
+            while True:
+                eol = self.__buffer.find('\n')
 
-                    while True:
-                        eol = self.__buffer.find('\n')
+                if eol < 0:
+                    break
 
-                        if eol < 0:
-                            break
+                packet = self.__buffer[:eol]
+                self.__buffer = self.__buffer[eol + 1:]
 
-                        packet = self.__buffer[:eol]
-                        self.__buffer = self.__buffer[eol + 1:]
+                logging.info('processing packet: %r', packet)
 
-                        logging.info('processing packet: %r', packet)
+                try:
+                    code, repeat, name, remote = packet.split()
+                    repeat = int(repeat, 16)
+                    code = long(code, 16)
 
-                        try:
-                            code, repeat, name, remote = packet.split()
-                            repeat = int(repeat, 16)
-                            code = long(code, 16)
+                    # pylint: disable-msg=E1101
+                    self.emit('key-pressed', remote, repeat, name, code)
 
-                            # pylint: disable-msg=E1101
-                            self.emit('key-pressed', remote, repeat, name, code)
+                # pylint: disable-msg=W0704
+                except ValueError:
+                    pass
 
-                        # pylint: disable-msg=W0704
-                        except ValueError:
-                            pass
+        if condition & gobject.IO_HUP:
+            if self.__running:
+                def restart():
+                    if self.__running:
+                        self.start()
 
-                if condition & gobject.IO_HUP:
-                    self.__disconnect()
                     return False
 
-                return True
+                gobject.timeout_add(1000, restart)
+
+            else:
+                self.__disconnect()
+
+            return False
+
+        return True
 
+    def __connect(self):
+        '''Connects to the lircd socket.'''
+
+        self.__disconnect()
+
+        logging.info('trying to connect to lircd...')
+
+        self.__socket = None
+        self.__buffer = ''
+
+        try:
             self.__socket = socket(AF_UNIX)
-            self.__socket_source = gobject.io_add_watch(self.__socket.fileno(),
-                                                        gobject.IO_IN | gobject.IO_HUP,
-                                                        on_io)
+
+            self.__socket_source = (
+                gobject.io_add_watch(self.__socket.fileno(),
+                                     gobject.IO_IN | gobject.IO_HUP,
+                                     self.__on_io_event))
 
             self.__socket.connect(self.__socket_name)
 
@@ -914,9 +929,7 @@
             return False
 
     def __disconnect(self):
-        '''
-        Disconnects from the lircd socket.
-        '''
+        '''Disconnects from the lircd socket.'''
 
         self.__buffer = ''
 
@@ -934,17 +947,13 @@
         self.emit('changed')
 
     def __reconnect(self):
-        '''
-        Tries to reconnects to the lircd socket.
-        '''
+        '''Tries to reconnects to the lircd socket.'''
 
         if not self.__reconnect_source:
             self.__reconnect_source = gobject.timeout_add(5000, self._on_reconnect)
 
     def _on_reconnect(self):
-        '''
-        Called regularly when the key listener tries to reconnect.
-        '''
+        '''Called regularly when the key listener tries to reconnect.'''
 
         if self.__connect():
             self.__reconnect_source = 0



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]