320cfec28377 — Oben Sonne 11 years ago
Server: proceed with glib wrapper
3 files changed, 35 insertions(+), 29 deletions(-)

M module/remuco/gw.py => module/remuco/ml.py
M module/remuco/net.py
M module/remuco/player.py
M module/remuco/gw.py => module/remuco/ml.py +24 -11
@@ 10,33 10,36 @@ import glib
 # - another source for bugs
 # - the limited interface to glib may not be sufficient in rare cases
 
+ml = glib.MainLoop()
 
-def repeat(fn, interval):
+def repeat(interval, fn, *args):
     """Call a function repeatedly with given `interval`.
 
     Returns the event source ID for the repeated timeout event.
 
     """
     fn = lambda: fn() or True
-    return glib.timeout_add(interval, fn, priority=glib.PRIORITY_LOW)
+    return glib.timeout_add(interval, fn, *args, priority=glib.PRIORITY_LOW)
 
-def delay(fn):
-    """Call a function with a slight delay.
+def delay(fn, *args):
+    """Call a function once with a slight delay.
+
+    Practically a *very* relaxed on-idle call.
 
     Returns the event source ID for the timeout event.
 
     """
     fn = lambda: fn() and False
-    return glib.timeout_add(100, fn, priority=glib.PRIORITY_LOW)
+    return glib.timeout_add(100, fn, *args, priority=glib.PRIORITY_LOW)
 
-def watchin(fd, fin, ferr):
+def watchin(fd, fin, ferr, *args):
     """Watch a file descriptor for input and errors.
 
     Returns the event source IDs for incoming and error events.
 
     """
-    fin = lambda: fin() or True
-    ferr = lambda: ferr() or True
+    fin = lambda _fd, _cond: fin(*args) or True
+    ferr = lambda _fd, _cond: ferr(*args) or True
     return (
         glib.io_add_watch(fd, glib.IO_IN, fin),
         glib.io_add_watch(fd, glib.IO_ERR | glib.IO_HUP, ferr),

          
@@ 50,8 53,18 @@ def watchout(fd, fout):
     """
     return glib.io_add_watch(fd, glib.IO_OUT, fout),
 
-def disable(*sids):
+def disable(sid):
     """Disable one or more event sources, i.e. stop handling them."""
 
-    for sid in sids:
-        glib.source_remove(sid)
+    sids = sid if isinstance(sid, (tuple, list)) else (sid,)
+    [glib.source_remove(s) for s in sids if s]
+
+def start():
+    """Start the main loop."""
+
+    ml.run()
+
+def stop():
+    """Stop the main loop."""
+
+    ml.stop()
  No newline at end of file

          
M module/remuco/net.py +10 -17
@@ 3,7 3,7 @@ 
 import json
 import socket
 
-import glib
+from remuco import ml
 
 _MSEP = "\n\n"
 

          
@@ 109,10 109,8 @@ class Client(object):
         self._chandler = chandler
 
         self._sock.setblocking(0)
-        self._source_in = glib.io_add_watch(self._sock, glib.IO_IN, self._on_in)
-        self._source_out = None
-        self._source_hup = glib.io_add_watch(self._sock, glib.IO_ERR |
-                                             glib.IO_HUP, self._on_hup)
+        self._eventin = ml.watchin(self._sock, self._on_in, self._on_hup)
+        self._eventout = None
 
         self._inbuff = []
         self._outbuff = []

          
@@ 123,7 121,7 @@ class Client(object):
 
         return "%s|%s" % (self._addr[0], self._addr[1])
 
-    def _on_in(self, fd, cond):
+    def _on_in(self):
 
         try:
             input = self._sock.recv(4096)

          
@@ 167,7 165,7 @@ class Client(object):
 
         return True
 
-    def _on_out(self, fd, cond):
+    def _on_out(self):
 
         if self._outbuff:
             try:

          
@@ 186,14 184,12 @@ class Client(object):
         self._source_out = None
         return False
 
-    def _on_hup(self, fd, cond):
+    def _on_err(self):
 
         self._rem.warn("lost connection to client %s" % self)
         self.disconnect()
         self._chandler.on_disconnect(self)
 
-        return False
-
     def send(self, cmd, params):
         """Send a message to client."""
 

          
@@ 208,9 204,8 @@ class Client(object):
         self._outbuff.extend(msg)
         self._outbuff.extend(_MSEP)
 
-        if not self._source_out:
-            self._source_out = glib.io_add_watch(self._sock, glib.IO_OUT,
-                                                 self._on_out)
+        if not self._eventout:
+            self._eventout = ml.watchout(self._sock, self._on_out)
 
     def disconnect(self):
         """Disconnect client. Does *not* fire a disconnect event."""

          
@@ 219,10 214,8 @@ class Client(object):
 
         self._down = True
 
-        glib.source_remove(self._source_in)
-        if self._source_out:
-            glib.source_remove(self._source_out)
-        glib.source_remove(self._source_hup)
+        ml.disable(self._eventin)
+        ml.disable(self._eventout)
 
         try:
             self._sock.shutdown(socket.SHUT_RDWR)

          
M module/remuco/player.py +1 -1
@@ 105,7 105,7 @@ class Player(object):
         if name in self._sources:
             return
 
-        self._sources[name] = glib.idle_add(self._sync, priority=PLOW)
+        self._sources[name] = glib.idle_add(self._sync, name, priority=PLOW)
 
     def on_client_message(self, client, cmd, params):