- commit
- a7771dbef586a628ac172d5bc76445f61572b89c
- parent
- 047eac9414a40cee8b57c1252c30d0766baf312a
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-07 20:36
move functioanlity to client factory similar to what we did before on server
Diffstat
| M | laneya/client.py | 26 | ++++++++++++-------------- |
| M | laneya/protocol.py | 44 | +++++++++++++++++++++++++++++++++++--------- |
2 files changed, 47 insertions, 23 deletions
diff --git a/laneya/client.py b/laneya/client.py
@@ -1,7 +1,6 @@ 1 1 import sys 2 2 3 3 from twisted.python import log4 -1 from twisted.internet.protocol import Factory5 4 from twisted.internet.endpoints import TCP4ClientEndpoint 6 5 from twisted.internet import reactor 7 6 @@ -9,31 +8,30 @@ import protocol 9 8 import deferred as q 10 9 11 1012 -1 class ClientProtocol(protocol.ClientProtocol):-1 11 class Client(protocol.ClientProtocolFactory): 13 12 def updateReceived(self, action, **kwargs): # TODO 14 13 print(action, kwargs) 15 14 16 15 def move(self, direction): 17 16 return self.sendRequest('move', direction=direction) 18 17 -1 18 def connected(self, protocol): # TODO -1 19 self.move('south') -1 20 reactor.callLater(2, lambda: self.move('west')) -1 21 reactor.callLater(4, lambda: self.move('north')) -1 22 reactor.callLater(6, lambda: self.move('east')) -1 23 reactor.callLater(8, lambda: self.move('stop')) 19 2420 -1 def connected(protocol): # TODO21 -1 protocol.setup('testuser')22 -123 -1 protocol.move('south')24 -1 reactor.callLater(2, lambda: protocol.move('west'))25 -1 reactor.callLater(4, lambda: protocol.move('north'))26 -1 reactor.callLater(6, lambda: protocol.move('east'))27 -1 reactor.callLater(8, lambda: protocol.move('stop'))28 -129 -1 reactor.callLater(10, lambda: protocol.sendRequest('logout'))-1 25 reactor.callLater(10, lambda: self.sendRequest('logout')) 30 26 31 27 32 28 def main(): 33 29 log.startLogging(sys.stdout) -1 30 client = Client() -1 31 client.setup('testuser') 34 32 endpoint = TCP4ClientEndpoint(reactor, 'localhost', 5001)35 -1 d = endpoint.connect(Factory.forProtocol(ClientProtocol))36 -1 q.fromTwisted(d).then(connected, log.err)-1 33 d = endpoint.connect(client) -1 34 q.fromTwisted(d).then(client.connected, log.err) 37 35 reactor.run() 38 36 39 37
diff --git a/laneya/protocol.py b/laneya/protocol.py
@@ -203,12 +203,15 @@ class ServerProtocolFactory(Factory): 203 203 class ClientProtocol(BaseProtocol): 204 204 """Default implementation of the client protocol.""" 205 205206 -1 def __init__(self):-1 206 def __init__(self, factory): -1 207 self.factory = factory 207 208 self._responseDeferreds = {} 208 209209 -1 def setup(self, user):210 -1 """Setup the user for this connection."""211 -1 self.user = user-1 210 def connectionMade(self): -1 211 self.factory.connections.append(self) -1 212 -1 213 def connectionLost(self, reason): -1 214 self.factory.connections.remove(self) 212 215 213 216 def jsonReceived(self, message): 214 217 if message['type'] == 'response': @@ -234,8 +237,7 @@ class ClientProtocol(BaseProtocol): 234 237 log.err('Message type not known: %s' % message['type']) 235 238 236 239 def updateReceived(self, action, **kwargs):237 -1 """Overwrite this on the client implementation."""238 -1 raise NotImplementedError-1 240 self.factory.updateReceived(action, **kwargs) 239 241 240 242 def _timeout(self, d, key): 241 243 try: @@ -245,12 +247,10 @@ class ClientProtocol(BaseProtocol): 245 247 pass 246 248 247 249 def sendRequest(self, action, **kwargs):248 -1 """Send a request and get a promise yielding the response."""249 -1250 250 data = { 251 251 'type': 'request', 252 252 'key': generate_key(),253 -1 'user': self.user,-1 253 'user': self.factory.user, 254 254 'action': action, 255 255 'data': kwargs, 256 256 } @@ -262,10 +262,36 @@ class ClientProtocol(BaseProtocol): 262 262 return d.promise 263 263 264 264 -1 265 class ClientProtocolFactory(Factory): -1 266 """Factory for :py:class:`ClientProtocol`. -1 267 -1 268 We assume that this factory has only one active connection. -1 269 """ -1 270 -1 271 def __init__(self): -1 272 self.connections = [] -1 273 -1 274 def buildProtocol(self, addr): -1 275 return ClientProtocol(self) -1 276 -1 277 def setup(self, user): -1 278 """Setup the user for all connections.""" -1 279 self.user = user -1 280 -1 281 def sendRequest(self, action, **kwargs): -1 282 """Send a request and get a promise yielding the response.""" -1 283 self.connections[-1].sendRequest(action, **kwargs) -1 284 -1 285 def updateReceived(self, action, **kwargs): -1 286 """Overwrite this on the client implementation.""" -1 287 raise NotImplementedError -1 288 -1 289 265 290 __all__ = [ 266 291 'InvalidError', 267 292 'IllegalError', 268 293 'ServerProtocol', 269 294 'ServerProtocolFactory', 270 295 'ClientProtocol', -1 296 'ClientProtocolFactory', 271 297 ]