PageRenderTime 65ms CodeModel.GetById 57ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/udplatency.py

https://bitbucket.org/prologic/circuits/
Python | 36 lines | 24 code | 11 blank | 1 comment | 1 complexity | e357eed159f2bb90000ae0fb5656f3c9 MD5 | raw file
 1#!/usr/bin/env python
 2
 3from time import time
 4
 5from circuits.net.sockets import UDPClient, UDPServer, Write
 6
 7class LatencyServer(UDPServer):
 8
 9    channel = "server"
10
11    def read(self, address, data):
12        self.fire(Write(address, data))
13
14class LatencyClient(UDPClient):
15
16    channel = "client"
17
18    count = 0
19    data = []
20
21    def ready(self, cmp):
22        self.fire(Write(("127.0.0.1", 8000), str(time())))
23
24    def read(self, address, data):
25        latency = time() - float(data)
26        self.data.append(latency)
27        print "Round Trip: %0.2fs" % latency
28        self.count += 1
29        if self.count < 10:
30            self.fire(Write(("127.0.0.1", 8000), str(time())))
31        else:
32            average = sum(self.data) / len(self.data)
33            print "Average Latency: %0.2fs" % average
34            raise SystemExit, 0
35
36(LatencyServer(8000) + LatencyClient(8001)).run()