This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
235 lines (187 loc) · 8.3 KB
/
Client.py
File metadata and controls
235 lines (187 loc) · 8.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
#!/usr/bin/env python
from twisted.protocols import basic
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint, TCP4ServerEndpoint
from twisted.internet import error
from datetime import datetime
import sys
import os
import threading
import time
class NoLiveReplicasAvailableException(Exception):
pass
class ClientProtocol(basic.LineReceiver):
'''
Receives lines from clients, then forwards them on to the message processor for
processing (we don't do the processing in this class, because the processor
maintains the system state needed to handle messages).
'''
def lineReceived(self, line):
self._messageProcessor.processMessage(self, line)
def sendMessage(self, msg):
self.sendLine(msg)
def connectionMade(self):
self._messageProcessor = self.factory.messageProcessor
if self.factory.onConnect != None:
self.factory.onConnect(self)
def connectionLost(self, reason):
if self.factory.onDisconnect != None:
self.factory.onDisconnect(self, reason)
class ClientMiddleware(threading.Thread):
def __init__(self, faultManagerAddress, faultManagerPort, clientImplementation):
threading.Thread.__init__(self)
self._faultManagerAddress = faultManagerAddress
self._faultManagerPort = faultManagerPort
self._clientImplementation = clientImplementation
self._replicaList = []
self._faultManagerConnector = None
self._faultManagerCondition = threading.Condition()
self._operationArgument = None
self._operationCompleteCondition = threading.Condition()
self._operationResult = None
self._replicaIndexToAttempt = 0
def run(self):
self._connectToFaultManager()
reactor.run(False) #Argument False tells the reactor that it's not on the
#main thread, so it doesn't attempt to register signal
#handlers (which doesn't work on other threads)
def _connectToFaultManager(self):
factory = Factory()
factory.protocol = ClientProtocol
factory.messageProcessor = self
factory.onConnect = self.connectedToFaultManager
factory.onDisconnect = self.disconnectedFromFaultManager
point = TCP4ClientEndpoint(reactor, self._faultManagerAddress, self._faultManagerPort)
d = point.connect(factory)
d.addErrback(self._onFaultManagerConnectError)
def _onFaultManagerConnectError(self, e):
e.printTraceback()
reactor.stop()
return e
def connectedToFaultManager(self, connector):
print "Connection to fault manager established."
self._faultManagerCondition.acquire()
self._faultManagerConnector = connector
self._faultManagerCondition.notifyAll()
self._faultManagerCondition.release()
self._faultManagerConnector.sendLine("c") #inform the fault manager that we're a client
def disconnectedFromFaultManager(self, connector, reason):
if reactor.running:
print "Fault manager disconnected (did it crash?)"
if reason.check(error.ConnectionDone):
print "Fault manager connection closed normally."
else:
print "Fault manager connection error..."
reason.printTraceback()
reactor.stop()
def connectedToServerReplica(self, connector):
operationMessage = "a," + str(self._operationArgument)
print "Got a connection to a server replica. Sending command:", operationMessage
connector.sendLine(operationMessage)
def disconnectedFromServerReplica(self, connector, reason):
if self._operationResult == None: #must not have gotten a result before disconnection; try the next replica
self._connectToReplica()
else:
self._operationResult = None #now that we've disconnected, reset the result back to none
def _onServerReplicaConnectError(self, e):
self._connectToReplica()
def processMessage(self, connector, msg):
print "Client got message", msg
operation = msg[0:1] #message type is the first character of the message
if operation == "u":
self._updateReplicaList(msg[2:]) #The replicaUpdate message accepts the rest of the payload as a string
elif operation == "b":
self._operationComplete(connector, msg[2:])
else:
print "Unknown operation"
def _updateReplicaList(self, newListMessage):
if(newListMessage != ""):
#make a list out of the string we received
unprocessedReplicaList = newListMessage.split(",")
#now break these up into (ip, port) pairs (where IP is a string, and port is an integer)
self._replicaList = [ (address.split(":")[0], int(address.split(":")[1])) for address in unprocessedReplicaList]
else:
self._replicaList = []
print "Got replicas:", self._replicaList
self._replicaIndexToAttempt = 0
def _connectToReplica(self):
if self._replicaIndexToAttempt < len(self._replicaList):
factory = Factory()
factory.protocol = ClientProtocol
factory.messageProcessor = self
factory.onConnect = self.connectedToServerReplica
factory.onDisconnect = self.disconnectedFromServerReplica
(replicaAddress, replicaPort) = self._replicaList[self._replicaIndexToAttempt]
print "Connecting to primary replica", replicaAddress + ":" + str(replicaPort)
point = TCP4ClientEndpoint(reactor, replicaAddress, replicaPort)
d = point.connect(factory)
d.addErrback(self._onServerReplicaConnectError)
self._replicaIndexToAttempt += 1
else:
#if we run out of replicas, send OperationComplete with result set to None
#so that performOperation will raise an exception
self._operationCompleteCondition.acquire()
self._operationResult = None
self._operationCompleteCondition.notifyAll()
self._operationCompleteCondition.release()
def _operationComplete(self, connector, resultMessage):
self._operationCompleteCondition.acquire()
connector.transport.loseConnection() #don't maintain active connections to servers
self._operationResult = int(resultMessage)
self._operationCompleteCondition.notifyAll()
self._operationCompleteCondition.release()
def waitForFaultManager(self):
self._faultManagerCondition.acquire()
if self._faultManagerConnector == None:
self._faultManagerCondition.wait()
self._faultManagerCondition.release()
def performOperation(self, argument):
self._operationArgument = argument
reactor.callFromThread(self._connectToReplica)
self._operationCompleteCondition.acquire()
if self._operationResult == None:
self._operationCompleteCondition.wait()
#consume operation result and reset it back to None (so the next call won't
#get the result from this iteration on failure)
result = self._operationResult
self._operationCompleteCondition.release()
if result == None: #must have run out of replicas
raise NoLiveReplicasAvailableException()
return result
class Client:
def __init__(self, faultManagerAddress, faultManagerPort):
self._middleware = ClientMiddleware(faultManagerAddress, faultManagerPort, self)
def run(self):
self._middleware.start()
self._middleware.waitForFaultManager()
value = 0
while True:
time.sleep(5)
print "Sending request to server..."
try:
startTime = time.clock()
result = self._middleware.performOperation(value)
stopTime = time.clock()
computationTime = stopTime - startTime
value = value + 1 #only incremented if we have a success; therefore, when coupled
#with properly performing replicas, result should always
#increment by one
print "Got result:", result
print " Computation took", computationTime, "seconds"
except NoLiveReplicasAvailableException:
print "Couldn't connect to any active replicas..."
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage:", sys.argv[0], "faultmgr-address faultmgr-port"
exit(-1)
try:
client = Client(sys.argv[1], int(sys.argv[2]))
client.run()
except KeyboardInterrupt:
print "Got ^C... Closing"
reactor.callFromThread(reactor.stop)
except:
print "Unexpected error... dying."
reactor.callFromThread(reactor.stop)
raise