1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
import socket
import asyncore
import traceback
import sys
configuration = sys.modules['configuration']
callback = None
def partition(string, sep):
offset = string.find(sep)
if offset == -1:
return string, '', ''
return string[:offset], sep, string[offset+len(sep):]
class PathReader(asyncore.dispatcher):
def __init__(self, conn):
asyncore.dispatcher.__init__(self, conn)
self.incoming = ''
## self.handle_error = self.handle_close
## def handle_close(self):
## self.close()
def handle_read(self):
data = self.recv(4096)
if not data:
self.handle_close()
return
self.incoming += data
fnames = []
fname, sep, self.incoming = partition(self.incoming, '\n')
while sep:
if fname:
fnames.append(fname)
fname, sep, self.incoming = partition(self.incoming, '\n')
self.incoming = fname
if fnames:
callback(fnames)
class Listener(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('127.0.0.1', 9999))
self.listen(5)
def handle_accept(self):
try:
conn, addr = self.accept()
except socket.error:
return
except TypeError:
return
PathReader(conn)
def send_documents(docs):
if configuration.nosocket:
return 0
try:
startup(0)
except socket.error, why:
if why[0] != 10048:
traceback.print_exc()
return 0
print "trying to send documents!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 9999))
docs.append('')
s.sendall('\n'.join(docs))
s.close()
return 1
else:
shutdown(0)
return 0
def poll():
asyncore.poll()
def startup(p=1):
if p:
print "Starting up document listener!"
Listener()
def shutdown(p=1):
if p:
print "Shutting down document listener!"
for i in asyncore.socket_map.values():
i.close()
|