-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
147 lines (114 loc) · 4.19 KB
/
Copy pathclient.py
File metadata and controls
147 lines (114 loc) · 4.19 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
from twisted.internet import reactor, protocol
from util import Log
from util import NetInfo
from util import ComputeDelay
import util
from twisted.python import log
import config
import sys
import random
""" Client class
This class use persistent TCP connections to communicate with a broker
Specific functions are:
1. Register subscriptions
2. Publish messages
3. Waiting for published messages from the broker
"""
class Client(protocol.Protocol):
def __init__(self):
self.currentIndex = 0
def connectionMade(self):
""" Made connection to a broker
A broker's client manager will handle connection establishment
The listening port of the client manager is 10001
"""
self.username = self.factory.username
self.factory.connection = self
self.dataQueue = ['NAME,' + self.username,]
self.dataQueue.extend([t.strip() for t in open(self.factory.subFile, 'r').readlines()])
sub_num = len(self.dataQueue) - 1
self.outputFile = open('delay_' + str(sub_num) + '.res', 'w')
for i in xrange(0):
text = 'MSG,age=INTEGER:' + str(random.randint(0, 65536))
self.dataQueue.append(text)
def Subscribe(self, sub):
self.Send(sub)
def Publish(self, msg):
msg = util.AppendTimeStamp(msg)
sefl.Send(msg)
def Send(self, data):
self.transport.write(data)
def SendData(self):
if self.currentIndex >= len(self.dataQueue):
return
data = self.dataQueue[self.currentIndex]
if 'MSG' in data:
data = util.AppendTimeStamp(data)
log.msg(data)
self.Send(data)
self.currentIndex += 1
if self.currentIndex < len(self.dataQueue):
reactor.callLater(1, self.SendData)
def dataReceived(self, data):
""" Received data
The data should comply with the protocol
TODO: dupplication of nreq data causes closing connection need to remove this restriction
"""
log.msg('[client receive]' + data)
reactor.callLater(1, self.SendData)
if not ',' in data:
cmd, val = data, ''
else:
cmd, val = data.split(',', 1)
if cmd == 'MSG':
delay = ComputeDelay(val)
log.msg('[delay]' + repr(delay))
self.outputFile.write(repr(delay) + '\n')
self.outputFile.flush()
return
return
if not ',' in data:
cmd, val = data, ''
else:
cmd, val = data.split(',', 1)
if cmd == 'NREQ':
self.Send('NAME,' + self.username)
return
if cmd == 'MSG':
delay = ComputeDelay(val)
log.msg('[delay]' + repr(delay))
return
def connectionLost(self, reason):
self.factory.connection = None
class ClientFactory(protocol.ClientFactory):
protocol = Client
def __init__(self, username, remote_name, sub_file):
self.username = username
self.remoteHostName = remote_name
self.subFile = sub_file
self.retryCount = 0
self.retryLimit = 3
self.connection = None
def clientConnectionFailed(self, connector, reason):
self.retryCount += 1
Log.Err('[Connection failed]', connector.remoteHostName,
'[reason]', reason.getErrorMessage(),
'[retry]', str(self.retryCount))
if self.retryCount < self.retryLimit:
connector.connect()
def Connect(remote_host = 'localhost', remote_port = config.BR_CLIENT_LISTEN_PORT):
factory = ClientFactory(NetInfo.GetLocalDomainName(), remote_host)
connector = reactor.connectTCP(factory.remoteHostName, remote_port, factory)
connector.remoteHostName = factory.remoteHostName
return factory
import sys
if __name__ == '__main__':
Log.StartLogging(sys.stdout)
if len(sys.argv) <= 1:
sub_file = 'sub_1.data'
else:
sub_file = sys.argv[1]
factory = ClientFactory('zyx', 'localhost', sub_file)
connector = reactor.connectTCP(factory.remoteHostName, config.BR_CLIENT_LISTEN_PORT, factory)
connector.remoteHostName = factory.remoteHostName
reactor.run()