- commit
- 9346311cc189b86ccdd71b2aabda98b9bd1b4fd9
- parent
- c01293f74fe09ff27bd2e3a4959cc43b4e5002f6
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-05 21:31
clean up server protocol api move some things from protocol to factory so everything can be implemented there.
Diffstat
| M | laneya/protocol.py | 19 | +++++++------------ |
| M | laneya/server.py | 20 | +++++++++----------- |
2 files changed, 16 insertions, 23 deletions
diff --git a/laneya/protocol.py b/laneya/protocol.py
@@ -133,13 +133,9 @@ class ServerProtocol(BaseProtocol): 133 133 def connectionLost(self, reason): 134 134 self.factory.connections.remove(self) 135 135136 -1 def requestReceived(self, user, action, **kwargs):137 -1 """Overwrite this on the server implementation."""138 -1 raise NotImplementedError139 -1140 136 def _requestReceived(self, key, user, action, **data): 141 137 try:142 -1 response = self.requestReceived(user, action, **data)-1 138 response = self.factory.requestReceived(user, action, **data) 143 139 except InvalidError as err: 144 140 return self._sendResponse(key, 'invalid', message=str(err)) 145 141 except IllegalError as err: @@ -180,20 +176,19 @@ class ServerProtocol(BaseProtocol): 180 176 } 181 177 self.sendJSON(data) 182 178183 -1 def broadcastUpdate(self, action, **kwargs):184 -1 """See :py:meth:`ServerProtocolFactory.broadcastUpdate`."""185 -1 self.factory.broadcastUpdate(action, **kwargs)186 -1187 179 188 180 class ServerProtocolFactory(Factory): 189 181 """Factory for :py:class:`ServerProtocol`.""" 190 182191 -1 def __init__(self, protocol):192 -1 self.protocol = protocol-1 183 def __init__(self): 193 184 self.connections = [] 194 185 195 186 def buildProtocol(self, addr):196 -1 return self.protocol(self)-1 187 return ServerProtocol(self) -1 188 -1 189 def requestReceived(self, user, action, **kwargs): -1 190 """Overwrite this on the server implementation.""" -1 191 raise NotImplementedError 197 192 198 193 def broadcastUpdate(self, action, **kwargs): 199 194 """Broadcast an update to all connected clients."""
diff --git a/laneya/server.py b/laneya/server.py
@@ -15,19 +15,23 @@ class User(object): 15 15 self.direction = direction 16 16 17 1718 -1 class ServerProtocol(protocol.ServerProtocol):-1 18 class Server(protocol.ServerProtocolFactory): -1 19 def __init__(self): -1 20 protocol.ServerProtocolFactory.__init__(self) -1 21 self.users = {} -1 22 19 23 def requestReceived(self, user, action, **kwargs): # TODO20 -1 if user not in self.factory.users:21 -1 self.factory.users[user] = User()-1 24 if user not in self.users: -1 25 self.users[user] = User() 22 26 print("login %s" % user) 23 27 24 28 if action == 'echo': 25 29 return kwargs 26 30 elif action == 'move':27 -1 self.factory.users[user].direction = kwargs['direction']-1 31 self.users[user].direction = kwargs['direction'] 28 32 return {} 29 33 elif action == 'logout':30 -1 del self.factory.users[user]-1 34 del self.users[user] 31 35 print("logout %s" % user) 32 36 return {} 33 37 else: @@ -35,12 +39,6 @@ class ServerProtocol(protocol.ServerProtocol): 35 39 reactor.callLater(5, self.broadcastUpdate, action, **kwargs) 36 40 return {} 37 4138 -139 -1 class Server(protocol.ServerProtocolFactory):40 -1 def __init__(self):41 -1 protocol.ServerProtocolFactory.__init__(self, ServerProtocol)42 -1 self.users = {}43 -144 42 def mainloop(self): 45 43 for key, user in self.users.iteritems(): 46 44 if user.direction == 'north':