forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentralizedadvertise_v2.r2py
178 lines (123 loc) · 5.65 KB
/
centralizedadvertise_v2.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
"""
Author: Justin Cappos
Start Date: July 8, 2008
Description:
Advertisements to a central server (similar to openDHT)
"""
session= dy_import_module('session.r2py')
# I'll use socket timeout to prevent hanging when it takes a long time...
sockettimeout = dy_import_module('sockettimeout.r2py')
serialize = dy_import_module('serialize.r2py')
# Hmm, perhaps I should make an initialization call instead of hardcoding this?
# I suppose it doesn't matter since one can always override these values.
v2servername = "advertiseserver_v2.poly.edu"
# This port is updated to use the new port (legacy port is 10101)
v2serverport = 10102
class CentralAdvertiseError(Exception):
"""Error when advertising a value to the central advertise service."""
def v2centralizedadvertise_announce(key, value, ttlval):
"""
<Purpose>
Announce a key / value pair into the CHT.
<Arguments>
key: the key to put the value under. This will be converted to a string.
value: the value to store at the key. This is also converted to a string.
ttlval: the amount of time until the value expires. Must be an integer
<Exceptions>
TypeError if ttlval is of the wrong type.
ValueError if ttlval is not positive
CentralAdvertiseError is raised the server response is corrupted
Various network and timeout exceptions are raised by timeout_openconn
and session_sendmessage / session_recvmessage
<Side Effects>
The CHT will store the key / value pair.
<Returns>
None
"""
# do basic argument checking / munging
key = str(key)
value = str(value)
if not type(ttlval) is int and not type(ttlval) is long:
raise TypeError("Invalid type '"+str(type(ttlval))+"' for ttlval.")
if ttlval < 1:
raise ValueError("The argument ttlval must be positive, not '"+str(ttlval)+"'")
# build the tuple to send, then convert to a string because only strings
# (bytes) can be transmitted over the network...
datatosend = ('PUT',key,value,ttlval)
datastringtosend = serialize.serialize_serializedata(datatosend)
# send the data over a timeout socket using the session library, then
# get a response from the server.
sockobj = sockettimeout.timeout_openconnection(gethostbyname(v2servername), v2serverport,
timeout=10)
try:
session.session_sendmessage(sockobj, datastringtosend)
rawresponse = session.session_recvmessage(sockobj)
finally:
# BUG: This raises an error right now if the call times out ( #260 )
# This isn't a big problem, but it is the "wrong" exception
sockobj.close()
# We should check that the response is 'OK'
try:
response = serialize.serialize_deserializedata(rawresponse)
if response != 'OK':
raise CentralAdvertiseError("Centralized announce failed with '"+response+"'")
except ValueError, e:
raise CentralAdvertiseError("Received unknown response from server '"+rawresponse+"'")
def v2centralizedadvertise_lookup(key, maxvals=100):
"""
<Purpose>
Returns a list of valid values stored under a key
<Arguments>
key: the key to put the value under. This will be converted to a string.
maxvals: the maximum number of values to return. Must be an integer
<Exceptions>
TypeError if maxvals is of the wrong type.
ValueError if maxvals is not a positive number
CentralAdvertiseError is raised the server response is corrupted
Various network and timeout exceptions are raised by timeout_openconn
and session_sendmessage / session_recvmessage
<Side Effects>
None
<Returns>
The list of values
"""
# do basic argument checking / munging
key = str(key)
if not type(maxvals) is int and not type(maxvals) is long:
raise TypeError("Invalid type '"+str(type(maxvals))+"' for ttlval.")
if maxvals < 1:
raise ValueError("The argument ttlval must be positive, not '"+str(ttlval)+"'")
# build the tuple to send, then convert to a string because only strings
# (bytes) can be transmitted over the network...
messagetosend = ('GET',key,maxvals)
messagestringtosend = serialize.serialize_serializedata(messagetosend)
# send the data over a timeout socket using the session library, then
# get a response from the server.
sockobj = sockettimeout.timeout_openconnection(gethostbyname(v2servername), v2serverport,
timeout=10)
try:
session.session_sendmessage(sockobj, messagestringtosend)
rawreceiveddata = session.session_recvmessage(sockobj)
finally:
# BUG: This raises an error right now if the call times out ( #260 )
# This isn't a big problem, but it is the "wrong" exception
sockobj.close()
try:
responsetuple = serialize.serialize_deserializedata(rawreceiveddata)
except ValueError, e:
raise CentralAdvertiseError("Received unknown response from server '"+rawresponse+"'")
# For a set of values, 'a','b','c', I should see the response:
# ('OK', ['a','b','c']) Anything else is WRONG!!!
if not type(responsetuple) is tuple:
raise CentralAdvertiseError("Received data is not a tuple '"+rawresponse+"'")
if len(responsetuple) != 2:
raise CentralAdvertiseError("Response tuple did not have exactly two elements '"+rawresponse+"'")
if responsetuple[0] != 'OK':
raise CentralAdvertiseError("Central server returns error '"+str(responsetuple)+"'")
if not type(responsetuple[1]) is list:
raise CentralAdvertiseError("Received item is not a list '"+rawresponse+"'")
for responseitem in responsetuple[1]:
if not type(responseitem) is str:
raise CentralAdvertiseError("Received item '"+str(responseitem)+"' is not a string in '"+rawresponse+"'")
# okay, we *finally* seem to have what we expect...
return responsetuple[1]