forked from SeattleTestbed/seattlelib_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvertiseObjects.repy
209 lines (146 loc) · 5.66 KB
/
AdvertiseObjects.repy
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
"""
<Author>
Eric Kimbrel kimbrl@cs.washington.edu
<Start Date>
Jan 29 2010
<Purpose>
Provied 2 objects to make more efficent use of resouces when using advertising
or looking up values.
LookupCache:
Provide cacheing of lookups to reduce the time spent doing
lookups by programs that need to lookup the same value frequently.
The cache is global so any instance of the object will have the same
values stored in the cache.
usage: Call lookup_obj.lookup(key) to perform a lookup of key using
advertise_lookup with default arguments. Values will be returned
from the cache if they are available and not too old.
AdvertisePipe:
Stores a list of (key,value) tuples and uses a single thread to advertise
each tuple in the list. This prevents a program from using multiple threads
to repeatedly advertise values.
usage: Call ad_obj.add(key,value) to add (key,value) to the list of tuples
to be advertised. This call returns an ad_handle which can be used
with a call to ad_obj.remove(ad_handle) to remove (key,value) from
the list.
"""
include advertise.repy
class LookupCache():
# caches lookups in a global data structure
cache = {} # a dict that will map lookups to results
lock = getlock()
def __init__(self,refresh_time=120):
# refresh_time is the amount of time the we will return results
# from the cache without doing a new lookup
self.refresh_time = refresh_time
def lookup(self,key, maxvals=100, lookuptype=['central','central_v2','UDP'], \
concurrentevents=2, graceperiod=10, timeout=60):
"""
<Purpose>
lookup the values stored at the given key
<Arguments>
see advertise.repy
WARNING optional arguments are passed on to advertise.repy if a new
advertisement is performed. If cache values are returned nothing is
done with the extra arguments.
<Returns>
a list of unique values advertised at the key
<Excpetions>
see advertise_lookup from advertise.repy
"""
if key not in self.cache:
# do the initial look up
results = advertise_lookup(key, maxvals, lookuptype,concurrentevents,
graceperiod, timeout)
if len(results) > 0 and results[0] != '':
# don't cache results of a failed lookup
self.cache[key] = {'results':results,'time':getruntime()}
return results
else:
# if the key is in the cache see how old it is
time_expired = getruntime() - self.cache[key]['time']
if time_expired > self.refresh_time or time_expired < 0:
# refresh the cache value if its old or the time doesnt make since
results = advertise_lookup(key, maxvals, lookuptype,concurrentevents,
graceperiod, timeout)
if len(results) > 0 and results[0] != '':
# don't cache failed results
self.cache[key]['results'] = results
self.cache[key]['time'] = getruntime()
return results
else:
# return the cache results without a lookup
return self.cache[key]['results']
class AdvertisePipe():
# shares a thread of execution across instances to
# advertise key value pairs
advertise_dict = {} # store info to be advertised
state= {'run':False} # should the add thread be running
state_lock = getlock()
ttlv = 240
redo = 120
def _advertise_thread(self):
# add a short sleep so that key,value pairs added
# close to the same time will be advertised together
#without waiting for the next cycle
sleep(2)
# advertise values stored in the advertise_dict
while self.state['run']:
# get the start time of the advertisement pass
start = getruntime()
# advertise each key,value pair that was in the dict
# at the beggining of this pass
entry_keys = self.advertise_dict.keys()
for entry_key in entry_keys:
try:
(key,value) = self.advertise_dict[entry_key]
advertise_announce(key,value,self.ttlv)
except:
pass #the key must have been deleted
# now wait until redo time has expired
# if run has gone to false we want to stop sleeping and kill the thread
while getruntime() - start < self.redo and self.state['run']:
sleep(10)
def add(self,key,value):
"""
<Purpose>
add the key,value pair to the advertise pipe
<Arguments>
the key value pair to advertise
<Returns>
a handle that can be used to remove the key,value pair
<Excpetions>
Possible exception from settimer if the advertise thread
can not be started
"""
# create a unique handle
handle = object()
self.advertise_dict[handle]=(key,value)
# if the advertise thread is not running start it
self.state_lock.acquire()
if not self.state['run']:
self.state['run'] = True
settimer(0,self._advertise_thread,[])
self.state_lock.release()
# return the handle
return handle
def remove(self,handle):
"""
<Purpose>
removes the key,value pair corresponding to the handle from
the advertise pipe
<Arguments>
a handle returned from AdvertisePipe.add
<Returns>
None
<Excpetions>
Exception on invalid handle
"""
self.state_lock.acquire()
if handle not in self.advertise_dict:
self.state_lock.release()
raise Exception('Invalid advertise handle')
else:
del self.advertise_dict[handle]
if len(self.advertise_dict) == 0:
self.state['run'] = False
self.state_lock.release()