inotify: Abstract the layer format and sizes to a inotify.common dictionary
authorNicolas Dumazet <nicdumz.commits@gmail.com>
Thu, 14 May 2009 15:48:47 +0200
changeset 8386 4aad982111b6
parent 8385 1536501ade62
child 8387 50b6af595e0c
inotify: Abstract the layer format and sizes to a inotify.common dictionary Instead of having a single possible request format, introduce a dictionary of possible messages in inotify.common
hgext/inotify/client.py
hgext/inotify/common.py
hgext/inotify/server.py
--- a/hgext/inotify/client.py	Mon May 11 08:19:28 2009 +0900
+++ b/hgext/inotify/client.py	Thu May 14 15:48:47 2009 +0200
@@ -46,8 +46,12 @@
                   'version %d)\n') % version)
         return None
 
+    # only one type of request is supported for now
+    type = 'STAT'
+    hdrfmt = common.resphdrfmts[type]
+    hdrsize = common.resphdrsizes[type]
     try:
-        resphdr = struct.unpack(common.resphdrfmt, cs.read(common.resphdrsize))
+        resphdr = struct.unpack(hdrfmt, cs.read(hdrsize))
     except struct.error:
         return None
 
--- a/hgext/inotify/common.py	Mon May 11 08:19:28 2009 +0900
+++ b/hgext/inotify/common.py	Thu May 14 15:48:47 2009 +0200
@@ -10,8 +10,11 @@
 
 version = 1
 
-resphdrfmt = '>llllllll'
-resphdrsize = struct.calcsize(resphdrfmt)
+resphdrfmts = {
+    'STAT': '>llllllll' # status requests
+}
+resphdrsizes = dict((k, struct.calcsize(v))
+                    for k, v in resphdrfmts.iteritems())
 
 def recvcs(sock):
     cs = cStringIO.StringIO()
--- a/hgext/inotify/server.py	Mon May 11 08:19:28 2009 +0900
+++ b/hgext/inotify/server.py	Thu May 14 15:48:47 2009 +0200
@@ -589,8 +589,6 @@
         cs = common.recvcs(sock)
         version = ord(cs.read(1))
 
-        sock.sendall(chr(common.version))
-
         if version != common.version:
             self.ui.warn(_('received query from incompatible client '
                            'version %d\n') % version)
@@ -638,7 +636,9 @@
 
         try:
             try:
-                sock.sendall(struct.pack(common.resphdrfmt,
+                v = chr(common.version)
+
+                sock.sendall(v + struct.pack(common.resphdrfmts['STAT'],
                                          *map(len, results)))
                 sock.sendall(''.join(results))
             finally: