forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopendhtadvertise.r2py
288 lines (212 loc) · 8.48 KB
/
opendhtadvertise.r2py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Author: Justin Cappos
Start Date: July 8, 2008
Description:
Advertises availability to openDHT...
This code is partially adapted from the example openDHT code.
"""
dy_import_module_symbols('random.r2py')
dy_import_module_symbols('sha.r2py')
dy_import_module_symbols('xmlrpc_client.r2py')
dy_import_module_symbols('parallelize.r2py')
opendhtadvertise_context = {}
opendhtadvertise_context["proxylist"] = []
opendhtadvertise_context["currentproxy"] = None
opendhtadvertise_context["serverlist"] = []
opendhtadvertise_context["serverlistlock"] = createlock()
def opendhtadvertise_announce(key, value, ttlval, concurrentevents=5, proxiestocheck=5, timeout=None):
"""
<Purpose>
Announce a (key, value) pair to openDHT.
<Arguments>
key:
The new key the value should be stored under.
value:
The value to associate with the given key.
ttlval:
The length of time (in seconds) to persist this key <-> value
association in DHT.
concurrentevents:
The number of concurrent events to use when checking for
functional openDHT proxies. Defaults to 5.
proxiestocheck:
The number of openDHT proxies to check. Defaults to 5.
<Exceptions>
Exception if the xmlrpc server behaves erratically.
<Side Effects>
The key <-> value association gets stored in openDHT for a while.
<Returns>
None.
"""
# JAC: Copy value because it seems that Python may otherwise garbage collect
# it in some circumstances. This seems to fix the problem
value = str(value)[:]
# convert ttl to an int
ttl = int(ttlval)
# If no timeout was specified, choose 10 seconds (completely arbitrary).
if timeout is None:
timeout = 10.0
# print "Announce key:",key,"value:",value, "ttl:",ttl
while True:
# if we have an empty proxy list and no proxy, get more
if opendhtadvertise_context["currentproxy"] == None and opendhtadvertise_context["proxylist"] == []:
opendhtadvertise_context["proxylist"] = opendhtadvertise_get_proxy_list( \
concurrentevents=concurrentevents, maxnumberofattempts=proxiestocheck)
# we couldn't get any proxies
if opendhtadvertise_context["proxylist"] == []:
return False
# if there isn't a proxy we should use, get one from our list
if opendhtadvertise_context["currentproxy"] == None and opendhtadvertise_context["proxylist"] != []:
opendhtadvertise_context["currentproxy"] = opendhtadvertise_context["proxylist"][0]
del opendhtadvertise_context["proxylist"][0]
# This code block is adopted from put.py from OpenDHT
pxy = xmlrpc_client_Client(opendhtadvertise_context["currentproxy"])
keytosend = xmlrpc_common_Binary(sha_new(str(key)).digest())
valtosend = xmlrpc_common_Binary(value)
try:
pxy.send_request("put", (keytosend, valtosend, ttl, "put.py"), timeout=timeout)
# if there isn't an exception, we succeeded
break
except (xmlrpc_common_ConnectionError, xmlrpc_common_Timeout):
# Let's avoid this proxy. It seems broken
opendhtadvertise_context["currentproxy"] = None
return True
def opendhtadvertise_lookup(key, maxvals=100, concurrentevents=5, proxiestocheck=5, timeout=None):
"""
<Purpose>
Retrieve a stored value from openDHT.
<Arguments>
key:
The key the value is stored under.
maxvals:
The maximum number of values stored under this key to
return to the caller.
concurrentevents:
The number of concurrent events to use when checking for
functional openDHT proxies. Defaults to 5.
proxiestocheck:
The number of openDHT proxies to check. Defaults to 5.
<Exceptions>
Exception if the xmlrpc server behaves erratically.
<Side Effects>
None.
<Returns>
The value stored in openDHT at key.
"""
# if no timeout is specified, pick 10 seconds (arbitrary value).
if timeout is None:
timeout = 10.0
while True:
# if we have an empty proxy list and no proxy, get more
if opendhtadvertise_context["currentproxy"] == None and opendhtadvertise_context["proxylist"] == []:
opendhtadvertise_context["proxylist"] = opendhtadvertise_get_proxy_list( \
concurrentevents=concurrentevents, maxnumberofattempts=proxiestocheck)
# we couldn't get any proxies
if opendhtadvertise_context["proxylist"] == []:
raise Exception, "Lookup failed"
# if there isn't a proxy we should use, get one from our list
if opendhtadvertise_context["currentproxy"] == None and opendhtadvertise_context["proxylist"] != []:
opendhtadvertise_context["currentproxy"] = opendhtadvertise_context["proxylist"][0]
del opendhtadvertise_context["proxylist"][0]
# This code block is adopted from get.py from OpenDHT
pxy = xmlrpc_client_Client(opendhtadvertise_context["currentproxy"])
maxvalhash = int(maxvals)
# I don't know what pm is for but I assume it's some sort of generator /
# running counter
pm = xmlrpc_common_Binary("")
keyhash = xmlrpc_common_Binary(sha_new(str(key)).digest())
listofitems = []
# If the proxy fails, then we will go to the next one...
while opendhtadvertise_context["currentproxy"]:
try:
vals, pm = pxy.send_request("get", (keyhash, maxvalhash, pm, "get.py"), timeout=timeout)
# if there isn't an exception, we succeeded
# append the .data part of the items, the other bits are:
# the ttl and hash / hash algorithm.
for item in vals:
listofitems.append(item.data)
# reached the last item. We're done!
if pm.data == "":
return listofitems
except (xmlrpc_common_ConnectionError, xmlrpc_common_Timeout):
# Let's avoid this proxy. It seems broken
opendhtadvertise_context["currentproxy"] = None
# check to see if a server is up and ready for OpenDHT...
def opendhtadvertise_checkserver(servername):
# try three times. Why three? Arbitrary value
for junkcount in range(3):
s = openconn(servername, 5851, timeout=2.0)
s.close()
# this list is the "return value". Add ourselves if no problems...
opendhtadvertise_context["serverlistlock"].acquire(True)
try:
opendhtadvertise_context["serverlist"].append(servername)
finally:
opendhtadvertise_context["serverlistlock"].release()
# Loosely based on find-gateway.py from the OpenDHT project...
def opendhtadvertise_get_proxy_list(maxnumberofattempts=5, concurrentevents=5):
"""
<Purpose>
Gets a list of active openDHT proxies.
<Arguments>
maxnumberofattemps:
Maximum number of servers to attempt to connect to.
concurrentevents:
Maximum number of events to use.
<Exceptions>
Exception if there are no servers in the server list.
<Side Effects>
Tries to connect to several proxies to see if they are online.
<Returns>
A list of openDHT approxies that appear to be up.
"""
# populate server list
socket = openconn('www.cs.washington.edu', 80)
try:
socket.send("GET /homes/arvind/servers.txt HTTP/1.0\r\nHost: www.cs.washington.edu\r\n\r\n")
body = ""
while True:
try:
newdata = socket.recv(4096)
except:
# Server decided it is done.
break
if len(newdata) == 0:
break # Server finished sending us the response.
body += newdata
finally:
socket.close()
try:
socket.close()
except:
pass
headers, payload = body.split("\r\n\r\n", 1)
lines = payload.split("\n")
# throw away the header line
lines = lines[1:]
# get the server list
servers = []
for line in lines:
if line.strip() == "":
continue
# The lines look like:
# 4: 134.121.64.7:5850 planetlab2.eecs.wsu.edu
# The third field is the server name
servers.append(line.split()[2])
if len(servers) == 0:
raise Exception, "No servers in server list"
numberofattempts = min(len(servers), maxnumberofattempts)
serverstocheck = random_sample(servers, numberofattempts)
# empty the server list
opendhtadvertise_context["serverlist"] = []
# start checking...
parhandle = parallelize_initfunction(serverstocheck, opendhtadvertise_checkserver, concurrentevents=concurrentevents)
# wait until all are finished
while not parallelize_isfunctionfinished(parhandle):
sleep(0.2)
parallelize_closefunction(parhandle)
retlist = []
for serverip in opendhtadvertise_context["serverlist"]:
# make it look like the right sort of url...
retlist.append("http://"+serverip+":5851/")
return retlist