-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathping_server.py
More file actions
327 lines (282 loc) · 11.3 KB
/
Copy pathping_server.py
File metadata and controls
327 lines (282 loc) · 11.3 KB
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
import ping, threading, time, socket, select, sys, struct, Queue
import binascii, collections, math, random, logging
import ping_reporter
log = ping_reporter.setup_log('PingServer')
class PingTimer(threading.Thread): # helper class for PingServer to manage timeouts
def __init__(self, event):
self.queue = Queue.PriorityQueue()
threading.Thread.__init__(self)
self.running = False
self.event = event
def stop(self):
log.debug('PingTimeout terminating')
self.running = False
self.event.set()
def run(self):
self.running = True
log.debug('PingTimeout starting')
while self.running:
timeout = None
self.event.clear()
timeout = self.process()
self.event.wait(timeout)
def process(self):
while self.queue.qsize() > 0:
try: expire,event,callback,cb_args = item = self.queue.get_nowait()
except Queue.Empty: break # our qsize check isn't guaranteed to prevent this
if event.is_set(): continue # event was completed; ignore it
if expire > time.time():
self.queue.put(item)
return expire - time.time()
callback(*cb_args)
event.set() # make sure no one executes it
return None
def add_callback(self, timeout, handler, args):
event = threading.Event()
item = (time.time()+timeout,event,handler,args)
self.queue.put(item)
self.event.set()
return event
class PingServer(threading.Thread):
def __init__(self, d_addr, block_size=1024, initial_timeout=2):
self.block_size = block_size # default; use setup for exact
self.server = d_addr,socket.gethostbyname(d_addr)
self.running_timeout = initial_timeout
threading.Thread.__init__(self)
self.listeners = []
self.debug = 0
# timeout events are queued and executed in a seperate thread
self.timer_event = threading.Event()
self.timer = PingTimer(self.timer_event)
self.blocks = 0
self.running = False
self.socket = ping.build_socket()
self.empty_block = self.null_block()
self.queued_events = collections.defaultdict(collections.deque)
def timeout(self): return 2.0/5.0 # self.running_timeout
def safe_timeout(self): return 3 * self.timeout()
def setup_timeout(self, ID=0):
Time = time.time()
Times = struct.pack('d',Time)
if ID == 0: ID = random.getrandbits(32) # ID size in bits
ping.data_ping(self.socket,self.server[1],ID,Times)
msg = ping.read_ping(self.socket,self.timeout())
if not msg: raise Exception('PingServer::setup_timeout: no valid response from '+self.server[0])
addr,rID,data = msg['address'],msg['ID'],msg['payload']
log.debug("Addr=%s rID=%d Data=%d bytes"%(addr[0],rID,len(data)))
if len(data) == 0: raise Exception('PingServer::setup_timeout: null response from '+self.server[0])
if rID != ID: raise Exception('PingServer::setup_timeout: invalid response id from '+self.server[0])
if data != Times: raise Exception('PingServer::setup_timeout: invalid response data from '+self.server[0])
if addr[0] != self.server[1]: raise Exception('PingServer::setup_timeout: invalid response server from '+self.server[0])
delay = time.time() - Time
log.notice('echo delay: %.02fms'%(1000*delay))
def setup_block(self, ID = 0):
if ID == 0: ID = random.getrandbits(32) # ID size in bits
Fill = chr(random.getrandbits(8)) # repeated data
Filler = self.block_size * Fill
ping.data_ping(self.socket,self.server[1],ID,Filler)
msg = ping.read_ping(self.socket,self.timeout())
if not msg: raise Exception('PingServer::setup_block: no valid response from '+self.server[0])
addr,rID,data = msg['address'],msg['ID'],msg['payload']
log.debug("Addr=%s rID=%d Data=%d bytes"%(addr[0],rID,len(data)))
if len(data) == 0: raise Exception('PingServer::setup_block: null response from '+self.server[0])
if rID != ID: raise Exception('PingServer::setup_block: invalid response id from '+self.server[0])
if data != len(data)*Fill: raise Exception('PingServer::setup_block: invalid response data from '+self.server[0])
if addr[0] != self.server[1]: raise Exception('PingServer::setup_block: invalid response server from '+self.server[0])
self.block_size = len(data)
self.empty_block = self.null_block()
log.notice('echo length: %d bytes'%self.block_size)
def setup(self):
log.trace('PingServer::setup: testing server "%s"'%self.server[0])
ID = random.getrandbits(32)
self.setup_timeout(ID)
self.setup_block(ID)
def stop(self):
self.running = False
log.info('PingServer terminating')
self.timer.stop()
def run(self):
self.running = True
log.notice('PingServer starting')
self.timer.start()
while self.running:
start_blocks = self.blocks # updated asynchronously
ready = select.select([self.socket], [], [], self.timeout())
if ready[0] == []: # timeout
if start_blocks != 0 and self.blocks != 0:
log.error('%s timed out'%self.server[0])
try:
msg = ping.recv_ping(self.socket,self.timeout())
if not msg: continue
except:
continue
addr,block_id,data = msg['address'],msg['ID'],msg['payload']
if block_id == 0:
import binascii
raise Exception('received packet w/ ID 0 packet: '+binascii.hexlify(msg['raw']))
self.process_block(addr[0],block_id,data)
def process_block(self, addr, ID, data):
if ID == 0: raise Exception('server responded with ID 0 packet')
while len(self.queued_events[ID]):
handler,event,args = self.queued_events[ID].popleft()
if event.is_set(): continue
if handler == self.write_block_timeout:
if self.debug: log.trace('%s (block %d) updated'%(self.server[0],ID))
data = args[1]
elif handler == self.read_block_timeout:
if self.debug: log.trace('%s (block %d) read'%(self.server[0],ID))
callback,cb_args = args[1],args[2]
if len(data) > 0: callback(ID,data,*cb_args)
else: callback(ID,self.null_block(),*cb_args)
elif handler == self.delete_block_timeout:
if self.debug: log.trace('%s (block %d) deleted'%(self.server[0],ID))
data = ''
event.set()
if len(data) == 0:
self.blocks = self.blocks - 1
else:
if len(self.listeners): self.process_listeners(addr, ID, data)
#log.trace('%s: sending %d bytes from block %d'%(self.server[0],len(data),ID))
ping.data_ping(self.socket, addr, ID, data)
def process_listeners(self, addr, ID, data):
if not self.listeners: raise Exception('process_listeners invoked without valid listeners on ID=%d'%ID)
self.listeners = [l for l in self.listeners if l[0] >= time.time()] # clean the listeners
for x in self.listeners:
expire,handler,cb_args = x
handler(ID, addr, data, *cb_args)
def add_listener(self, handler, timeout, args):
log.debug('add_listener: timeout=%d handler=%s'%(timeout,handler))
expire = time.time() + timeout
self.listeners.append((expire,handler,args))
def null_block(self):
return self.block_size * struct.pack('B',0)
def event_insert(self, ID, handler, args):
event = self.timer.add_callback(self.timeout(), handler, args)
self.queued_events[ID].append((handler,event,args))
return event
# read / write / delete a single block
def write_block(self, ID, data, blocking = False):
# add a block to the queue (or delete if equivalent)
log.trace('PingServer::write_block: ID=%d bytes=%d blocking=%s'%(ID,len(data),blocking))
if ID == 0: raise Exception('write_block: invalid block ID (0)')
if data == '%c'%0 * len(data): return self.delete_block(ID,blocking)
event = self.event_insert(ID,self.write_block_timeout,[ID,data[:self.block_size]])
if blocking: event.wait()
return event
def delete_block(self, ID, blocking = False):
log.trace('PingServer::delete_block: ID=%d blocking=%s'%(ID,blocking))
if ID == 0: raise Exception('delete_block: invalid block ID (0)')
t = self.event_insert(ID,self.delete_block_timeout,[ID])
if blocking: t.wait()
return t
def read_block(self, ID, callback, cb_args = [], blocking = False):
log.trace('PingServer::read_block: ID=%d blocking=%s'%(ID,blocking))
if ID == 0: raise Exception('read_block: invalid block ID (0)')
t = self.event_insert(ID,self.read_block_timeout,[ID,callback,cb_args])
if blocking: t.wait()
return t
def read_block_timeout(self, ID, callback, cb_args):
log.debug('PingServer::read_block_timeout: ID=%d callback=%s'%(ID,callback.__name__))
callback(ID,self.null_block(),*cb_args)
def delete_block_timeout(self, ID):
log.debug('PingServer::delete_block_timeout: ID=%d'%ID)
# do nothing; we're marked invalid anyhow
pass
def write_block_timeout(self, ID, data):
log.trace('PingServer::write_block_timeout: ID=%d bytes=%d'%(ID,len(data)))
self.blocks = self.blocks + 1
# force update queue (as if packet arrived)
if ID == 0: raise Exception('write_block_timeout: ID == 0')
self.process_block(self.server[1], ID, data)
def print_block(ID, data):
print '----- print block -----'
print 'block',ID,'bytes',len(data)
print data
print '----- print block -----'
def __live_blocks(ID, addr, data, datastore):
datastore[ID] = 1
def live_blocks(PServer, timeout=None):
store = {}
if not timeout: timeout = PServer.safe_timeout()
PServer.add_listener(__live_blocks,timeout,[store])
time.sleep(timeout)
return store
def used_blocks(blocks):
result,lookup = {},{}
for x in blocks:
if x-1 in lookup:
lookup[x] = lookup[x-1]
result[lookup[x]] += 1
else:
lookup[x] = x
result[x] = 1
return result
def free_blocks(blocks):
result = {}
if 1 not in blocks:
if not blocks: result[1] = 0
elif len(blocks) == 0: result[1] = 0
else: result[1] = min(blocks.keys())-1
for x in blocks:
if not x+1 in blocks: result[x+1] = 0
if not x-1 in blocks:
if not len(result): continue
block = max(result.keys())
result[block] = x-block
return result
if __name__ == "__main__":
ping_reporter.start_log(log,logging.DEBUG)
server = ping.select_server(log,1)
from ping_reporter import humanize_bytes
try:
PS = PingServer(server)
PS.debug = 1
PS.setup()
PS.start()
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
PS.read_block(2,print_block)
time.sleep(2)
PS.write_block(2,'coconut')
time.sleep(1)
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
PS.write_block(1,'apples')
PS.read_block(1,print_block)
PS.read_block(1,print_block)
time.sleep(2)
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
log.info('testing block metrics')
blocks = live_blocks(PS)
log.debug('blocks: %s'%blocks)
log.debug('--used: %s'%used_blocks(blocks))
log.debug('--free: %s'%free_blocks(blocks))
PS.delete_block(1)
time.sleep(2)
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
PS.write_block(1,'apples')
time.sleep(2)
PS.read_block(1,print_block)
time.sleep(4)
PS.read_block(1,print_block)
time.sleep(1)
PS.write_block(1,'bananas')
time.sleep(1)
PS.read_block(1,print_block)
time.sleep(1)
PS.read_block(1,print_block)
PS.read_block(1,print_block)
time.sleep(1)
PS.delete_block(1)
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
while True:
time.sleep(1)
print 'terminate'
except KeyboardInterrupt:
print "Keyboard Interrupt"
except Exception:
print 'General Exception'
from traceback import print_exc
print_exc()
finally:
PS.stop()
print 'traffic:',ping.ping_count,'pings ('+humanize_bytes(ping.ping_bandwidth)+')'
sys.exit(1)