forked from SeattleTestbed/monitor_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentralizedputget.py
1063 lines (759 loc) · 27.9 KB
/
centralizedputget.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
<Program Name>
centralizedputget.mix
<Started>
January 8, 2008
<Author>
Justin Cappos
<Purpose>
Attempt to put a (k,v) into our centralized hash table and then get it back.
On error send an email to some folks.
<Usage>
Modify the following global var params to have this script functional:
- notify_list, a list of strings with emails denoting who will be
emailed when something goes wrong
This script takes no arguments. A typical use of this script is to
have it run periodically using something like the following crontab line:
7 * * * * /usr/bin/python /home/seattle/centralizedputget.py > /home/seattle/cron_log.centralizedputget
"""
import time
import os
import socket
import sys
import traceback
import threading
import random
import send_gmail
import integrationtestlib
import nonportable
from repyportability import *
#begin include centralizedadvertise.repy
"""
Author: Justin Cappos
Start Date: July 8, 2008
Description:
Advertisements to a central server (similar to openDHT)
"""
#begin include centralizedadvertise_base.repy
"""
Author: Justin Cappos
Start Date: July 8, 2008
Description:
Advertisements to a central server (similar to openDHT)
"""
#begin include session.repy
# This module wraps communications in a signaling protocol. The purpose is to
# overlay a connection-based protocol with explicit message signaling.
#
# The protocol is to send the size of the message followed by \n and then the
# message itself. The size of a message must be able to be stored in
# sessionmaxdigits. A size of -1 indicates that this side of the connection
# should be considered closed.
#
# Note that the client will block while sending a message, and the receiver
# will block while recieving a message.
#
# While it should be possible to reuse the connectionbased socket for other
# tasks so long as it does not overlap with the time periods when messages are
# being sent, this is inadvisable.
class SessionEOF(Exception):
pass
sessionmaxdigits = 20
# get the next message off of the socket...
def session_recvmessage(socketobj):
messagesizestring = ''
# first, read the number of characters...
for junkcount in range(sessionmaxdigits):
currentbyte = socketobj.recv(1)
if currentbyte == '\n':
break
# not a valid digit
if currentbyte not in '0123456789' and messagesizestring != '' and currentbyte != '-':
raise ValueError, "Bad message size"
messagesizestring = messagesizestring + currentbyte
else:
# too large
raise ValueError, "Bad message size"
try:
messagesize = int(messagesizestring)
except ValueError:
raise ValueError, "Bad message size"
# nothing to read...
if messagesize == 0:
return ''
# end of messages
if messagesize == -1:
raise SessionEOF, "Connection Closed"
if messagesize < 0:
raise ValueError, "Bad message size"
data = ''
while len(data) < messagesize:
chunk = socketobj.recv(messagesize-len(data))
if chunk == '':
raise SessionEOF, "Connection Closed"
data = data + chunk
return data
# a private helper function
def session_sendhelper(socketobj,data):
sentlength = 0
# if I'm still missing some, continue to send (I could have used sendall
# instead but this isn't supported in repy currently)
while sentlength < len(data):
thissent = socketobj.send(data[sentlength:])
sentlength = sentlength + thissent
# send the message
def session_sendmessage(socketobj,data):
header = str(len(data)) + '\n'
# Sending these piecemeal does not accomplish anything, and can contribute
# to timeout issues when run by constantly overloaded machines.
# session_sendhelper(socketobj,header)
# Concatenate the header and data, rather than sending both separately.
complete_packet = header + data
# session_sendhelper(socketobj,data)
session_sendhelper(socketobj, complete_packet)
#end include session.repy
# I'll use socket timeout to prevent hanging when it takes a long time...
#begin include sockettimeout.repy
"""
<Author>
Justin Cappos, Armon Dadgar
This is a rewrite of the previous version by Richard Jordan
<Start Date>
26 Aug 2009
<Description>
A library that causes sockets to timeout if a recv / send call would
block for more than an allotted amount of time.
"""
class SocketTimeoutError(Exception):
"""The socket timed out before receiving a response"""
class _timeout_socket():
"""
<Purpose>
Provides a socket like object which supports custom timeouts
for send() and recv().
"""
# Initialize with the socket object and a default timeout
def __init__(self,socket,timeout=10, checkintv='fibonacci'):
"""
<Purpose>
Initializes a timeout socket object.
<Arguments>
socket:
A socket like object to wrap. Must support send,recv,close, and willblock.
timeout:
The default timeout for send() and recv().
checkintv:
How often socket operations (send,recv) should check if
they can run. The smaller the interval the more time is
spent busy waiting.
"""
# Store the socket, timeout and check interval
self.socket = socket
self.timeout = timeout
self.checkintv = checkintv
# Allow changing the default timeout
def settimeout(self,timeout=10):
"""
<Purpose>
Allows changing the default timeout interval.
<Arguments>
timeout:
The new default timeout interval. Defaults to 10.
Use 0 for no timeout. Given in seconds.
"""
# Update
self.timeout = timeout
# Wrap willblock
def willblock(self):
"""
See socket.willblock()
"""
return self.socket.willblock()
# Wrap close
def close(self):
"""
See socket.close()
"""
return self.socket.close()
# Provide a recv() implementation
def recv(self,bytes,timeout=None):
"""
<Purpose>
Allows receiving data from the socket object with a custom timeout.
<Arguments>
bytes:
The maximum amount of bytes to read
timeout:
(Optional) Defaults to the value given at initialization, or by settimeout.
If provided, the socket operation will timeout after this amount of time (sec).
Use 0 for no timeout.
<Exceptions>
As with socket.recv(), socket.willblock(). Additionally, SocketTimeoutError is
raised if the operation times out.
<Returns>
The data received from the socket.
"""
# It's worth noting that this fibonacci backoff begins with a 2ms poll rate, and
# provides a simple exponential backoff scheme.
fibonacci_backoff = False
backoff_cap = 100 # Never use more than 100ms poll rate.
pre_value = 1.0 # Our iterators for Fibonacci sequence.
pre_pre_value = 1.0 #
# Since we want to be able to initialize with static poll rates (backwards
# compatibility) we specify a string if we're using the fibonacci backoff.
if type(self.checkintv) is str:
if self.checkintv == 'fibonacci':
fibonacci_backoff = True
# Set the timeout if None
if timeout is None:
timeout = self.timeout
# Get the start time
starttime = getruntime()
# Block until we can read
rblock, wblock = self.socket.willblock()
while rblock:
# Check if we should break
if timeout > 0:
# Get the elapsed time
diff = getruntime() - starttime
# Raise an exception
if diff > timeout:
raise SocketTimeoutError,"recv() timed out!"
if fibonacci_backoff:
# Iterate the sequence once
sleep_length = pre_value + pre_pre_value
pre_pre_value = pre_value
pre_value = sleep_length
# Make sure we don't exceed maximum backoff.
if sleep_length > backoff_cap:
sleep_length = backoff_cap
# Unit conversion to seconds
sleep_length = sleep_length / 1000.0
# Sleep
sleep(sleep_length)
else: # Classic functionality.
# Sleep
try:
sleep(float(self.checkintv))
except:
sleep(0.1)
# If available, move to the next value of checkintv.
# Update rblock
rblock, wblock = self.socket.willblock()
# Do the recv
return self.socket.recv(bytes)
# Provide a send() implementation
def send(self,data,timeout=None):
"""
<Purpose>
Allows sending data with the socket object with a custom timeout.
<Arguments>
data:
The data to send
timeout:
(Optional) Defaults to the value given at initialization, or by settimeout.
If provided, the socket operation will timeout after this amount of time (sec).
Use 0 for no timeout.
<Exceptions>
As with socket.send(), socket.willblock(). Additionally, SocketTimeoutError is
raised if the operation times out.
<Returns>
The number of bytes sent.
"""
# Set the timeout if None
if timeout is None:
timeout = self.timeout
# Get the start time
starttime = getruntime()
# Block until we can write
rblock, wblock = self.socket.willblock()
while wblock:
# Check if we should break
if timeout > 0:
# Get the elapsed time
diff = getruntime() - starttime
# Raise an exception
if diff > timeout:
raise SocketTimeoutError,"send() timed out!"
# Sleep
# Since switching to the fibonacci backoff, the nature of
# this field has changed. Rather than implement the backoff
# for checking block status (seems wasteful) we'll just use
# a constant value. Ten ms seems appropriate.
sleep(0.010)
# Update rblock
rblock, wblock = self.socket.willblock()
# Do the recv
return self.socket.send(data)
def timeout_openconn(desthost, destport, localip=None, localport=None, timeout=5):
"""
<Purpose>
Wrapper for openconn. Very, very similar
<Args>
Same as Repy openconn
<Exception>
Raises the same exceptions as openconn.
<Side Effects>
Creates a socket object for the user
<Returns>
socket obj on success
"""
realsocketlikeobject = openconn(desthost, destport, localip, localport, timeout)
thissocketlikeobject = _timeout_socket(realsocketlikeobject, timeout)
return thissocketlikeobject
def timeout_waitforconn(localip, localport, function, timeout=5):
"""
<Purpose>
Wrapper for waitforconn. Essentially does the same thing...
<Args>
Same as Repy waitforconn with the addition of a timeout argument.
<Exceptions>
Same as Repy waitforconn
<Side Effects>
Sets up event listener which calls function on messages.
<Returns>
Handle to listener.
"""
# We use a closure for the callback we pass to waitforconn so that we don't
# have to map mainch's to callback functions or deal with potential race
# conditions if we did maintain such a mapping.
def _timeout_waitforconn_callback(localip, localport, sockobj, ch, mainch):
# 'timeout' is the free variable 'timeout' that was the argument to
# timeout_waitforconn.
thissocketlikeobject = _timeout_socket(sockobj, timeout)
# 'function' is the free variable 'function' that was the argument to
# timeout_waitforconn.
return function(localip, localport, thissocketlikeobject, ch, mainch)
return waitforconn(localip, localport, _timeout_waitforconn_callback)
# a wrapper for stopcomm
def timeout_stopcomm(commhandle):
"""
Wrapper for stopcomm. Does the same thing...
"""
return stopcomm(commhandle)
#end include sockettimeout.repy
#begin include serialize.repy
"""
Author: Justin Cappos
Start date: October 9th, 2009
Purpose: A simple library that serializes and deserializes built-in repy types.
This includes strings, integers, floats, booleans, None, complex, tuples,
lists, sets, frozensets, and dictionaries.
There are no plans for including objects.
Note: that all items are treated as separate references. This means things
like 'a = []; a.append(a)' will result in an infinite loop. If you have
'b = []; c = (b,b)' then 'c[0] is c[1]' is True. After deserialization
'c[0] is c[1]' is False.
I can add support or detection of this if desired.
"""
# The basic idea is simple. Say the type (a character) followed by the
# type specific data. This is adequate for simple types
# that do not contain other types. Types that contain other types, have
# a length indicator and then the underlying items listed sequentially.
# For a dict, this is key1value1key2value2.
def serialize_serializedata(data):
"""
<Purpose>
Convert a data item of any type into a string such that we can
deserialize it later.
<Arguments>
data: the thing to seriailize. Can be of essentially any type except
objects.
<Exceptions>
TypeError if the type of 'data' isn't allowed
<Side Effects>
None.
<Returns>
A string suitable for deserialization.
"""
# this is essentially one huge case statement...
# None
if type(data) == type(None):
return 'N'
# Boolean
elif type(data) == type(True):
if data == True:
return 'BT'
else:
return 'BF'
# Integer / Long
elif type(data) is int or type(data) is long:
datastr = str(data)
return 'I'+datastr
# Float
elif type(data) is float:
datastr = str(data)
return 'F'+datastr
# Complex
elif type(data) is complex:
datastr = str(data)
if datastr[0] == '(' and datastr[-1] == ')':
datastr = datastr[1:-1]
return 'C'+datastr
# String
elif type(data) is str:
return 'S'+data
# List or tuple or set or frozenset
elif type(data) is list or type(data) is tuple or type(data) is set or type(data) is frozenset:
# the only impact is the first letter...
if type(data) is list:
mystr = 'L'
elif type(data) is tuple:
mystr = 'T'
elif type(data) is set:
mystr = 's'
elif type(data) is frozenset:
mystr = 'f'
else:
raise Exception("InternalError: not a known type after checking")
for item in data:
thisitem = serialize_serializedata(item)
# Append the length of the item, plus ':', plus the item. 1 -> '2:I1'
mystr = mystr + str(len(thisitem))+":"+thisitem
mystr = mystr + '0:'
return mystr
# dict
elif type(data) is dict:
mystr = 'D'
keysstr = serialize_serializedata(data.keys())
# Append the length of the list, plus ':', plus the list.
mystr = mystr + str(len(keysstr))+":"+keysstr
# just plop the values on the end.
valuestr = serialize_serializedata(data.values())
mystr = mystr + valuestr
return mystr
# Unknown!!!
else:
raise TypeError("Unknown type '"+str(type(data))+"' for data :"+str(data))
def serialize_deserializedata(datastr):
"""
<Purpose>
Convert a serialized data string back into its original types.
<Arguments>
datastr: the string to deseriailize.
<Exceptions>
ValueError if the string is corrupted
TypeError if the type of 'data' isn't allowed
<Side Effects>
None.
<Returns>
Items of the original type
"""
if type(datastr) != str:
raise TypeError("Cannot deserialize non-string of type '"+str(type(datastr))+"'")
typeindicator = datastr[0]
restofstring = datastr[1:]
# this is essentially one huge case statement...
# None
if typeindicator == 'N':
if restofstring != '':
raise ValueError("Malformed None string '"+restofstring+"'")
return None
# Boolean
elif typeindicator == 'B':
if restofstring == 'T':
return True
elif restofstring == 'F':
return False
raise ValueError("Malformed Boolean string '"+restofstring+"'")
# Integer / Long
elif typeindicator == 'I':
try:
return int(restofstring)
except ValueError:
raise ValueError("Malformed Integer string '"+restofstring+"'")
# Float
elif typeindicator == 'F':
try:
return float(restofstring)
except ValueError:
raise ValueError("Malformed Float string '"+restofstring+"'")
# Float
elif typeindicator == 'C':
try:
return complex(restofstring)
except ValueError:
raise ValueError("Malformed Complex string '"+restofstring+"'")
# String
elif typeindicator == 'S':
return restofstring
# List / Tuple / set / frozenset / dict
elif typeindicator == 'L' or typeindicator == 'T' or typeindicator == 's' or typeindicator == 'f':
# We'll split this and keep adding items to the list. At the end, we'll
# convert it to the right type
thislist = []
data = restofstring
# We'll use '0:' as our 'end separator'
while data != '0:':
lengthstr, restofdata = data.split(':', 1)
length = int(lengthstr)
# get this item, convert to a string, append to the list.
thisitemdata = restofdata[:length]
thisitem = serialize_deserializedata(thisitemdata)
thislist.append(thisitem)
# Now toss away the part we parsed.
data = restofdata[length:]
if typeindicator == 'L':
return thislist
elif typeindicator == 'T':
return tuple(thislist)
elif typeindicator == 's':
return set(thislist)
elif typeindicator == 'f':
return frozenset(thislist)
else:
raise Exception("InternalError: not a known type after checking")
elif typeindicator == 'D':
lengthstr, restofdata = restofstring.split(':', 1)
length = int(lengthstr)
# get this item, convert to a string, append to the list.
keysdata = restofdata[:length]
keys = serialize_deserializedata(keysdata)
# The rest should be the values list.
values = serialize_deserializedata(restofdata[length:])
if type(keys) != list or type(values) != list or len(keys) != len(values):
raise ValueError("Malformed Dict string '"+restofstring+"'")
thisdict = {}
for position in xrange(len(keys)):
thisdict[keys[position]] = values[position]
return thisdict
# Unknown!!!
else:
raise ValueError("Unknown typeindicator '"+str(typeindicator)+"' for data :"+str(restofstring))
#end include serialize.repy
class CentralAdvertiseError(Exception):
"""Error when advertising a value to the central advertise service."""
def centralizedadvertisebase_announce(servername, serverport, key, value, ttlval):
"""
<Purpose>
Announce a key / value pair into the CHT.
<Arguments>
servername: the server ip/name to contact. Must be a string.
serverport: the server port to contact. Must be an integer.
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_serializedata(datatosend)
# send the data over a timeout socket using the session library, then
# get a response from the server.
sockobj = timeout_openconn(servername,serverport, timeout=10)
try:
session_sendmessage(sockobj, datastringtosend)
rawresponse = 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_deserializedata(rawresponse)
if response != 'OK':
raise CentralAdvertiseError("Centralized announce failed with '"+response+"'")
except ValueError, e:
raise CentralAdvertiseError("Received unknown response from server '"+rawresponse+"'")
def centralizedadvertisebase_lookup(servername, serverport, key, maxvals=100):
"""
<Purpose>
Returns a list of valid values stored under a key
<Arguments>
servername: the server ip/name to contact. Must be a string.
serverport: the server port to contact. Must be an integer.
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_serializedata(messagetosend)
sockobj = timeout_openconn(servername,serverport, timeout=10)
try:
session_sendmessage(sockobj, messagestringtosend)
rawreceiveddata = 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_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]
#end include centralizedadvertise_base.repy
# 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
servername = "advertiseserver.poly.edu"
# This port is updated to use the new port (legacy port is 10101)
serverport = 10102
def centralizedadvertise_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
"""
return centralizedadvertisebase_announce(servername, serverport, key, value, ttlval)
def centralizedadvertise_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
"""
return centralizedadvertisebase_lookup(servername, serverport, key, maxvals)
#end include centralizedadvertise.repy
# event for communicating when the lookup is done or timedout
lookup_done_event = threading.Event()
def lookup_timedout():
"""
<Purpose>
Waits for lookup_done_event and notifies the folks on the
notify_list (global var) of the lookup timeout.
<Arguments>
None.
<Exceptions>
None.
<Side Effects>
Sends an email to the notify_list folks
<Returns>
None.
"""
integrationtestlib.log("in lookup_timedout()")
notify_msg = "Centralized lookup failed -- lookup_timedout() fired after 30 sec."
# wait for the event to be set, timeout after 30 minutes
wait_time = 1800
tstamp_before_wait = nonportable.getruntime()
lookup_done_event.wait(wait_time)
tstamp_after_wait = nonportable.getruntime()
t_waited = tstamp_after_wait - tstamp_before_wait
if abs(wait_time - t_waited) < 5:
notify_msg += " And lookup stalled for over 30 minutes (max timeout value)."
else:
notify_msg += " And lookup stalled for " + str(t_waited) + " seconds"
integrationtestlib.notify(notify_msg)
return