forked from xedakini/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-replicator
executable file
·62 lines (49 loc) · 1.74 KB
/
http-replicator
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
#! /usr/bin/env python
import Params, Request, Response, fiber, weakref
DOWNLOADS = weakref.WeakValueDictionary()
def Replicator( client, address ):
print 'Accepted request from [%s]:%i' % address[0:2]
request = Request.HttpRequest( client )
for state in request:
yield state
try:
while request in DOWNLOADS:
protocol = DOWNLOADS[ request ]
if protocol.Response:
if issubclass( protocol.Response, Response.DataResponse ):
print 'Joined running download'
break
del DOWNLOADS[ request ]
else:
yield fiber.WAIT()
else:
if Params.VERBOSE:
print 'Switching to', request.Protocol.__name__
protocol = DOWNLOADS[ request ] = request.Protocol( request )
server = protocol.socket()
while not protocol.Response:
if protocol.hasdata():
yield fiber.SEND( server, Params.TIMEOUT )
protocol.send( server )
else:
yield fiber.RECV( server, Params.TIMEOUT )
protocol.recv( server )
if Params.VERBOSE:
print 'Switching to', protocol.Response.__name__
response = protocol.Response( protocol, request )
server = protocol.socket()
except Exception:
if Params.VERBOSE:
print 'Switching to ExceptionResponse'
response = Response.ExceptionResponse( request )
while not response.Done:
if response.hasdata():
yield fiber.SEND( client, Params.TIMEOUT )
response.send( client )
elif response.needwait():
yield fiber.WAIT( response.needwait() )
else:
yield fiber.RECV( server, Params.TIMEOUT )
response.recv( server )
print 'Transaction successfully completed'
fiber.spawn( Replicator, Params.PORT, Params.DEBUG, Params.LOG, Params.LISTENFAMILY )