Ticket #6217: pbechoclient.py

File pbechoclient.py, 2.0 KB (added by BrianMatthews, 20 months ago)
Line 
1
2# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3# See LICENSE for details.
4
5from twisted.internet import iocpreactor
6iocpreactor.install()
7
8from twisted.internet import reactor
9from twisted.spread import pb
10from twisted.cred.credentials import UsernamePassword
11from time import time
12from array import array
13
14
15time_then = time()
16
17data = None
18perspective = None
19transfer_count = 0
20
21def connection_success(message):
22    global time_then
23    time_then = time()
24    print "Connected:",message
25    send_data(perspective)
26    send_data(perspective)
27    send_data(perspective)
28    send_data(perspective)
29    print 'All data sent'
30 
31def failure(error):
32    print "error received:", error
33    reactor.stop()
34
35def transfer_success(message):
36    global time_then, transfer_count
37    diff = time() - time_then
38    time_then = time()
39    print "Transfer Done:",message
40    print "Time Diff:",diff
41    transfer_count += 1
42    if transfer_count >= 4:
43        shutdown()
44
45def transfer_failure(error):
46    print "Transfer error received:", error
47    reactor.stop()
48
49def shutdown():
50    print 'Shutting down in 10 seconds...'
51    reactor.callLater(10, reactor.stop)
52
53   
54def connected(p):
55    global perspective
56    perspective = p
57    perspective.callRemote('echo', "Connection started").addCallbacks(connection_success, failure)
58    print "Connection Initiated."
59
60def setup_data():
61    global data
62    data = {
63        'values': [],
64        'description': 'Some data'
65    }
66    for i in range(64000) :
67        data['values'].append({
68            'timestamp': i,
69            'value': i + 0.001,
70            'sample_id': i
71        })
72    print "Data setup"
73    return data
74
75def send_data(perspective):
76    perspective.callRemote('transfer', data, "Dict of values").addCallbacks(transfer_success, transfer_failure)
77    print "Data transmitted"
78
79   
80setup_data()
81factory = pb.PBClientFactory()
82reactor.connectTCP("localhost", pb.portno, factory)
83factory.login(
84    UsernamePassword("guest", "guest")).addCallbacks(connected, failure)
85
86reactor.run()