common.py
51 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
Bryan O'Sullivan
|
r6239 | # server.py - inotify common protocol code | ||
# | ||||
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | ||||
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | ||||
# | ||||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
# GNU General Public License version 2, incorporated herein by reference. | ||||
Bryan O'Sullivan
|
r6239 | |||
import cStringIO, socket, struct | ||||
Nicolas Dumazet
|
r8553 | """ | ||
Protocol between inotify clients and server: | ||||
Client sending query: | ||||
1) send protocol version number | ||||
2) send query type (string, 4 letters long) | ||||
3) send query parameters: | ||||
- For STAT, N+1 \0-separated strings: | ||||
1) N different names that need checking | ||||
2) 1 string containing all the status types to match | ||||
Nicolas Dumazet
|
r8555 | - No parameter needed for DBUG | ||
Nicolas Dumazet
|
r8553 | |||
Server sending query answer: | ||||
1) send protocol version number | ||||
2) send query type | ||||
3) send struct.pack'ed headers describing the length of the content: | ||||
e.g. for STAT, receive 8 integers describing the length of the | ||||
8 \0-separated string lists ( one list for each lmar!?ic status type ) | ||||
""" | ||||
version = 2 | ||||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r8386 | resphdrfmts = { | ||
Nicolas Dumazet
|
r8555 | 'STAT': '>llllllll', # status requests | ||
'DBUG': '>l' # debugging queries | ||||
Nicolas Dumazet
|
r8386 | } | ||
resphdrsizes = dict((k, struct.calcsize(v)) | ||||
for k, v in resphdrfmts.iteritems()) | ||||
Bryan O'Sullivan
|
r6239 | |||
def recvcs(sock): | ||||
cs = cStringIO.StringIO() | ||||
s = True | ||||
try: | ||||
while s: | ||||
s = sock.recv(65536) | ||||
cs.write(s) | ||||
finally: | ||||
sock.shutdown(socket.SHUT_RD) | ||||
cs.seek(0) | ||||
return cs | ||||