-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtconsd.py
64 lines (54 loc) · 1.83 KB
/
tconsd.py
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
#!/bin/env python
#=======================================================================
# Demo server
#=======================================================================
from twisted.internet import protocol, reactor
from twisted.protocols import basic
import urllib
from tupleprotocol import TupleProtocol
buffer = '\n'.join(map(str, range(12345, 12500)))
conns = {}
class ConsProtocol(TupleProtocol):
'''Handler for a connection from a client'''
def connectionMade(self):
conns[id(self)] = self
print 'connection from %s' % self.transport.getPeer()
self.sendTuple(('v', '', ''))
self.sendTuple(('c', 'localhost', len(buffer)))
def tupleReceived(self, tokens):
print 'tuple: %r' % (tokens,)
cmd = tokens[0]
args = tokens[1:]
if cmd == 'i':
data, = args
print 'input %r' % data
elif cmd == 'r':
beg, end = map(int, args)
if beg < 0 or end > len(buffer):
self.error('Invalid range')
elif end > beg:
data = buffer[beg:end]
self.reply('o', beg, data)
else:
self.error('Unknown command %r' % cmd)
def connectionLost(self, reason):
print 'Client %s disconnected' % self.transport.getPeer()
del conns[id(self)]
def error(self, err):
self.reply('e', err)
def reply(self, *args):
self.sendTuple(args)
class ConsFactory(protocol.ServerFactory):
protocol = ConsProtocol
def newData():
global buffer, conns
# Add some new data
data = str(len(buffer)) + '\n'
buffer += data
# Broadcast new size to client(s)
for prot in conns.values():
prot.reply('p', len(buffer))
reactor.callLater(10, newData)
reactor.listenTCP(8183, ConsFactory())
reactor.callLater(10, newData)
reactor.run()