| 1 | |
|---|
| 2 | # Copyright (c) 2001-2004 Twisted Matrix Laboratories. |
|---|
| 3 | # See LICENSE for details. |
|---|
| 4 | |
|---|
| 5 | from twisted.internet import iocpreactor |
|---|
| 6 | iocpreactor.install() |
|---|
| 7 | |
|---|
| 8 | from twisted.internet import reactor |
|---|
| 9 | from twisted.spread import pb |
|---|
| 10 | from twisted.cred.credentials import UsernamePassword |
|---|
| 11 | from time import time |
|---|
| 12 | from array import array |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | time_then = time() |
|---|
| 16 | |
|---|
| 17 | data = None |
|---|
| 18 | perspective = None |
|---|
| 19 | transfer_count = 0 |
|---|
| 20 | |
|---|
| 21 | def 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 | |
|---|
| 31 | def failure(error): |
|---|
| 32 | print "error received:", error |
|---|
| 33 | reactor.stop() |
|---|
| 34 | |
|---|
| 35 | def 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 | |
|---|
| 45 | def transfer_failure(error): |
|---|
| 46 | print "Transfer error received:", error |
|---|
| 47 | reactor.stop() |
|---|
| 48 | |
|---|
| 49 | def shutdown(): |
|---|
| 50 | print 'Shutting down in 10 seconds...' |
|---|
| 51 | reactor.callLater(10, reactor.stop) |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | def connected(p): |
|---|
| 55 | global perspective |
|---|
| 56 | perspective = p |
|---|
| 57 | perspective.callRemote('echo', "Connection started").addCallbacks(connection_success, failure) |
|---|
| 58 | print "Connection Initiated." |
|---|
| 59 | |
|---|
| 60 | def 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 | |
|---|
| 75 | def send_data(perspective): |
|---|
| 76 | perspective.callRemote('transfer', data, "Dict of values").addCallbacks(transfer_success, transfer_failure) |
|---|
| 77 | print "Data transmitted" |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | setup_data() |
|---|
| 81 | factory = pb.PBClientFactory() |
|---|
| 82 | reactor.connectTCP("localhost", pb.portno, factory) |
|---|
| 83 | factory.login( |
|---|
| 84 | UsernamePassword("guest", "guest")).addCallbacks(connected, failure) |
|---|
| 85 | |
|---|
| 86 | reactor.run() |
|---|