1 # server.py - inotify common protocol code |
|
2 # |
|
3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> |
|
4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> |
|
5 # |
|
6 # This software may be used and distributed according to the terms of the |
|
7 # GNU General Public License version 2 or any later version. |
|
8 |
|
9 import cStringIO, socket, struct |
|
10 |
|
11 """ |
|
12 Protocol between inotify clients and server: |
|
13 |
|
14 Client sending query: |
|
15 1) send protocol version number |
|
16 2) send query type (string, 4 letters long) |
|
17 3) send query parameters: |
|
18 - For STAT, N+1 \0-separated strings: |
|
19 1) N different names that need checking |
|
20 2) 1 string containing all the status types to match |
|
21 - No parameter needed for DBUG |
|
22 |
|
23 Server sending query answer: |
|
24 1) send protocol version number |
|
25 2) send query type |
|
26 3) send struct.pack'ed headers describing the length of the content: |
|
27 e.g. for STAT, receive 9 integers describing the length of the |
|
28 9 \0-separated string lists to be read: |
|
29 * one file list for each lmar!?ic status type |
|
30 * one list containing the directories visited during lookup |
|
31 |
|
32 """ |
|
33 |
|
34 version = 3 |
|
35 |
|
36 resphdrfmts = { |
|
37 'STAT': '>lllllllll', # status requests |
|
38 'DBUG': '>l' # debugging queries |
|
39 } |
|
40 resphdrsizes = dict((k, struct.calcsize(v)) |
|
41 for k, v in resphdrfmts.iteritems()) |
|
42 |
|
43 def recvcs(sock): |
|
44 cs = cStringIO.StringIO() |
|
45 s = True |
|
46 try: |
|
47 while s: |
|
48 s = sock.recv(65536) |
|
49 cs.write(s) |
|
50 finally: |
|
51 sock.shutdown(socket.SHUT_RD) |
|
52 cs.seek(0) |
|
53 return cs |
|