- commit
- 34606df1dad98c3923ba88d97fbbd520029c6304
- parent
- 2f842c9da56fbad642909d73e680972c03a536c2
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-05 08:50
split protocol into client and server part
Diffstat
| M | laneya/client.py | 11 | ++++++----- |
| M | laneya/protocol.py | 92 | ++++++++++++++++++++++++++++++++++--------------------------- |
| M | laneya/server.py | 2 | +- |
3 files changed, 58 insertions, 47 deletions
diff --git a/laneya/client.py b/laneya/client.py
@@ -13,12 +13,12 @@ def _print(s): 13 13 print(s) 14 14 15 1516 -1 class ClientProtocol(protocol.Protocol):-1 16 class ClientProtocol(protocol.ClientProtocol): 17 17 def setup(self, user): 18 18 self.user = user 19 19 20 20 def sendRequest(self, action, **kwargs):21 -1 return protocol.Protocol.sendRequest(-1 21 return protocol.ClientProtocol.sendRequest( 22 22 self, self.user, action, **kwargs) 23 23 24 24 def updateReceived(self, action, **kwargs): # TODO @@ -29,17 +29,18 @@ def connected(protocol): # TODO 29 29 protocol.setup('testuser') 30 30 31 31 protocol.sendRequest('echo', foo='bar')\32 -1 .then(_print)-1 32 .then(_print, log.err) 33 33 34 34 protocol.sendRequest('other', foo='bar')\35 -1 .then(_print)-1 35 .then(_print, log.err) 36 36 37 37 38 38 def main(): 39 39 log.startLogging(sys.stdout) 40 40 endpoint = TCP4ClientEndpoint(reactor, 'localhost', 5001) 41 41 d = endpoint.connect(Factory.forProtocol(ClientProtocol))42 -1 q.fromTwisted(d).then(connected, log.err)-1 42 d.addCallbacks(connected, log.err) -1 43 # q.fromTwisted(d).then(connected, log.err) 43 44 reactor.run() 44 45 45 46
diff --git a/laneya/protocol.py b/laneya/protocol.py
@@ -104,20 +104,29 @@ class JSONProtocol(NetstringReceiver): 104 104 return self.sendString(json.dumps(data)) 105 105 106 106107 -1 class Protocol(JSONProtocol):108 -1 """Default implementation of the laneya protocol."""-1 107 class BaseProtocol(JSONProtocol): 109 108110 -1 def __init__(self):111 -1 self._responseDeferreds = {}-1 109 def validate_message(self, message, expected_keys): -1 110 if sorted(message.keys()) != expected_keys: -1 111 log.err('Invalid message: %s' % message) -1 112 raise InvalidError -1 113 -1 114 def validate_action(self, action, data): -1 115 try: -1 116 fn = getattr(actions, action) -1 117 fn(**data) -1 118 except: -1 119 log.err('Invalid action: %s %s' % (action, data)) -1 120 raise InvalidError -1 121 -1 122 -1 123 class ServerProtocol(BaseProtocol): -1 124 """Default implementation of the server protocol.""" 112 125 113 126 def requestReceived(self, user, action, **kwargs): 114 127 """Overwrite this on the server implementation.""" 115 128 raise NotImplementedError 116 129117 -1 def updateReceived(self, action, **kwargs):118 -1 """Overwrite this on the client implementation."""119 -1 raise NotImplementedError120 -1121 130 def _requestReceived(self, key, user, action, **data): 122 131 try: 123 132 response = self.requestReceived(user, action, **data) @@ -131,19 +140,6 @@ class Protocol(JSONProtocol): 131 140 132 141 return self._sendResponse(key, 'success', **response) 133 142134 -1 def validate_message(self, message, expected_keys):135 -1 if sorted(message.keys()) != expected_keys:136 -1 log.err('Invalid message: %s' % message)137 -1 raise InvalidError138 -1139 -1 def validate_action(self, action, data):140 -1 try:141 -1 fn = getattr(actions, action)142 -1 fn(**data)143 -1 except:144 -1 log.err('Invalid action: %s %s' % (action, data))145 -1 raise InvalidError146 -1147 143 def jsonReceived(self, message): 148 144 if message['type'] == 'request': 149 145 self.validate_message( @@ -154,8 +150,36 @@ class Protocol(JSONProtocol): 154 150 message['user'], 155 151 message['action'], 156 152 **message['data']) -1 153 else: -1 154 log.err('Message type not known: %s' % message['type']) 157 155158 -1 elif message['type'] == 'response':-1 156 def _sendResponse(self, key, status, **kwargs): -1 157 data = { -1 158 'type': 'response', -1 159 'key': key, -1 160 'status': status, -1 161 'data': kwargs, -1 162 } -1 163 self.sendJSON(data) -1 164 -1 165 def sendUpdate(self, action, **kwargs): -1 166 """Send an update.""" -1 167 data = { -1 168 'type': 'update', -1 169 'action': action, -1 170 'data': kwargs, -1 171 } -1 172 self.sendJSON(data) -1 173 -1 174 -1 175 class ClientProtocol(BaseProtocol): -1 176 """Default implementation of the client protocol.""" -1 177 -1 178 def __init__(self): -1 179 self._responseDeferreds = {} -1 180 -1 181 def jsonReceived(self, message): -1 182 if message['type'] == 'response': 159 183 self.validate_message(message, ['data', 'key', 'status', 'type']) 160 184 key = message['key'] 161 185 if key in self._responseDeferreds: @@ -177,6 +201,10 @@ class Protocol(JSONProtocol): 177 201 else: 178 202 log.err('Message type not known: %s' % message['type']) 179 203 -1 204 def updateReceived(self, action, **kwargs): -1 205 """Overwrite this on the client implementation.""" -1 206 raise NotImplementedError -1 207 180 208 def _timeout(self, d, key): 181 209 try: 182 210 d.reject('timeout', silent=True) @@ -200,23 +228,5 @@ class Protocol(JSONProtocol): 200 228 reactor.callLater(10, lambda: self._timeout(d, data['key'])) 201 229 return d.promise 202 230203 -1 def _sendResponse(self, key, status, **kwargs):204 -1 data = {205 -1 'type': 'response',206 -1 'key': key,207 -1 'status': status,208 -1 'data': kwargs,209 -1 }210 -1 self.sendJSON(data)211 -1212 -1 def sendUpdate(self, action, **kwargs):213 -1 """Send an update."""214 -1 data = {215 -1 'type': 'update',216 -1 'action': action,217 -1 'data': kwargs,218 -1 }219 -1 self.sendJSON(data)220 -1221 231222 -1 __all__ = ['InvalidError', 'IllegalError', 'Protocol']-1 232 __all__ = ['InvalidError', 'IllegalError', 'ServerProtocol', 'ClientProtocol']
diff --git a/laneya/server.py b/laneya/server.py
@@ -8,7 +8,7 @@ from twisted.internet import reactor 8 8 import protocol 9 9 10 1011 -1 class ServerProtocol(protocol.Protocol):-1 11 class ServerProtocol(protocol.ServerProtocol): 12 12 def __init__(self, factory): 13 13 self.factory = factory 14 14