From 3d0dd1fc8fbdd1ce105b831d276f168270c55d84 Mon Sep 17 00:00:00 2001 From: Tobias Oberstein Date: Sun, 22 Dec 2013 15:52:49 +0100 Subject: [PATCH] refactoring --- autobahn/autobahn/asyncio/__init__.py | 18 +++++++ .../{asyncio.py => asyncio/websocket.py} | 22 +++++---- autobahn/autobahn/twisted/__init__.py | 18 +++++++ .../{twisted.py => twisted/websocket.py} | 22 +++++---- autobahn/autobahn/websocket2/protocol.py | 9 +--- examples/websocket2/Makefile | 4 +- .../{server.py => server_asyncio.py} | 41 +++++----------- examples/websocket2/server_twisted.py | 49 +++++++++++++++++++ 8 files changed, 125 insertions(+), 58 deletions(-) create mode 100644 autobahn/autobahn/asyncio/__init__.py rename autobahn/autobahn/{asyncio.py => asyncio/websocket.py} (77%) create mode 100644 autobahn/autobahn/twisted/__init__.py rename autobahn/autobahn/{twisted.py => twisted/websocket.py} (76%) rename examples/websocket2/{server.py => server_asyncio.py} (63%) create mode 100644 examples/websocket2/server_twisted.py diff --git a/autobahn/autobahn/asyncio/__init__.py b/autobahn/autobahn/asyncio/__init__.py new file mode 100644 index 00000000..ef019ad0 --- /dev/null +++ b/autobahn/autobahn/asyncio/__init__.py @@ -0,0 +1,18 @@ +############################################################################### +## +## Copyright 2013 Tavendo GmbH +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +############################################################################### + diff --git a/autobahn/autobahn/asyncio.py b/autobahn/autobahn/asyncio/websocket.py similarity index 77% rename from autobahn/autobahn/asyncio.py rename to autobahn/autobahn/asyncio/websocket.py index 00163040..51bf2fa3 100644 --- a/autobahn/autobahn/asyncio.py +++ b/autobahn/autobahn/asyncio/websocket.py @@ -17,27 +17,31 @@ ############################################################################### import asyncio -from asyncio import Protocol + +from autobahn.websocket2 import protocol -class AdapterProtocol(Protocol): + +class WebSocketServerProtocol(asyncio.Protocol, protocol.WebSocketServerProtocol): def connection_made(self, transport): + self.transport = transport peername = transport.get_extra_info('peername') print('connection from {}'.format(peername)) - self._protocol.transport = transport def data_received(self, data): - self._protocol.data_received(data) + self._onData(data) #print('data received: {}'.format(data.decode())) #self.transport.write(data) #self.transport.close() -class AdapterFactory: - def __init__(self, factory, loop = None): - self._factory = factory +class WebSocketServerFactory(protocol.WebSocketServerFactory): + + protocol = WebSocketServerProtocol + + def __init__(self, loop = None): if loop is None: loop = asyncio.get_event_loop() @@ -45,8 +49,6 @@ class AdapterFactory: def __call__(self): - proto = AdapterProtocol() + proto = self.protocol() proto.factory = self - proto._protocol = self._factory() - print("asyncio protocol created") return proto diff --git a/autobahn/autobahn/twisted/__init__.py b/autobahn/autobahn/twisted/__init__.py new file mode 100644 index 00000000..ef019ad0 --- /dev/null +++ b/autobahn/autobahn/twisted/__init__.py @@ -0,0 +1,18 @@ +############################################################################### +## +## Copyright 2013 Tavendo GmbH +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +############################################################################### + diff --git a/autobahn/autobahn/twisted.py b/autobahn/autobahn/twisted/websocket.py similarity index 76% rename from autobahn/autobahn/twisted.py rename to autobahn/autobahn/twisted/websocket.py index 12c0413b..4601cb04 100644 --- a/autobahn/autobahn/twisted.py +++ b/autobahn/autobahn/twisted/websocket.py @@ -17,27 +17,31 @@ ############################################################################### from __future__ import absolute_import -from twisted.internet.protocol import Protocol, Factory +import twisted.internet.protocol + +from autobahn.websocket2 import protocol -class AdapterProtocol(Protocol): + +class WebSocketServerProtocol(twisted.internet.protocol.Protocol, protocol.WebSocketServerProtocol): def connectionMade(self): peername = str(self.transport.getPeer()) print('connection from {}'.format(peername)) - self._protocol.transport = self.transport def dataReceived(self, data): - self._protocol.data_received(data) + self._onData(data) #print('data received: {}'.format(data.decode())) #self.transport.write(data) #self.transport.loseConnection() -class AdapterFactory(Factory): - def __init__(self, factory, reactor = None): - self._factory = factory +class WebSocketServerFactory(twisted.internet.protocol.Factory, protocol.WebSocketServerFactory): + + protocol = WebSocketServerProtocol + + def __init__(self, reactor = None): ## lazy import to avoid reactor install upon module import if reactor is None: @@ -46,8 +50,6 @@ class AdapterFactory(Factory): def buildProtocol(self, addr): - proto = AdapterProtocol() + proto = self.protocol() proto.factory = self - proto._protocol = self._factory() - print("twisted protocol created") return proto diff --git a/autobahn/autobahn/websocket2/protocol.py b/autobahn/autobahn/websocket2/protocol.py index 2c97f80b..82b05ae3 100644 --- a/autobahn/autobahn/websocket2/protocol.py +++ b/autobahn/autobahn/websocket2/protocol.py @@ -18,7 +18,7 @@ class WebSocketServerProtocol: - def data_received(self, data): + def _onData(self, data): print('data received: {}'.format(data.decode())) self.onMessage(data, False) @@ -31,9 +31,4 @@ class WebSocketServerProtocol: class WebSocketServerFactory: - protocol = WebSocketServerProtocol - - def __call__(self): - proto = self.protocol() - proto.factory = self - return proto + pass diff --git a/examples/websocket2/Makefile b/examples/websocket2/Makefile index c10ed2e7..96b68b80 100644 --- a/examples/websocket2/Makefile +++ b/examples/websocket2/Makefile @@ -1,8 +1,8 @@ server1_asyncio: - PYTHONPATH=../../autobahn python3 server.py asyncio + PYTHONPATH=../../autobahn python3 server_asyncio.py server1_twisted: - PYTHONPATH=../../autobahn python server.py twisted + PYTHONPATH=../../autobahn python server_twisted.py client1_asyncio: PYTHONPATH=../../autobahn python3 client.py diff --git a/examples/websocket2/server.py b/examples/websocket2/server_asyncio.py similarity index 63% rename from examples/websocket2/server.py rename to examples/websocket2/server_asyncio.py index 128b5178..55b877cc 100644 --- a/examples/websocket2/server.py +++ b/examples/websocket2/server_asyncio.py @@ -1,6 +1,6 @@ ############################################################################### ## -## Copyright 2011-2013 Tavendo GmbH +## Copyright 2013 Tavendo GmbH ## ## Licensed under the Apache License, Version 2.0 (the "License"); ## you may not use this file except in compliance with the License. @@ -16,7 +16,9 @@ ## ############################################################################### -from autobahn.websocket2.protocol import WebSocketServerProtocol, WebSocketServerFactory +from autobahn.asyncio.websocket import WebSocketServerProtocol, \ + WebSocketServerFactory + class MyServerProtocol(WebSocketServerProtocol): @@ -25,6 +27,9 @@ class MyServerProtocol(WebSocketServerProtocol): print("message received") self.sendMessage(payload) + def onConnect(self, connectionRequest): + return None + class MyServerFactory(WebSocketServerFactory): @@ -32,14 +37,16 @@ class MyServerFactory(WebSocketServerFactory): -def run_test_asyncio(factory): +if __name__ == '__main__': + import asyncio - from autobahn.asyncio import AdapterFactory + factory = MyServerFactory() loop = asyncio.get_event_loop() - coro = loop.create_server(AdapterFactory(factory), '127.0.0.1', 8888) + coro = loop.create_server(factory, '127.0.0.1', 8888) server = loop.run_until_complete(coro) + print('serving on {}'.format(server.sockets[0].getsockname())) try: @@ -49,27 +56,3 @@ def run_test_asyncio(factory): finally: server.close() loop.close() - - -def run_test_twisted(factory): - from twisted.internet.endpoints import TCP4ServerEndpoint - from twisted.internet import reactor - - from autobahn.twisted import AdapterFactory - - endpoint = TCP4ServerEndpoint(reactor, 8888) - endpoint.listen(AdapterFactory(factory)) - reactor.run() - - -if __name__ == '__main__': - import sys - - factory = MyServerFactory() - - if sys.argv[1] == "asyncio": - run_test_asyncio(factory) - elif sys.argv[1] == "twisted": - run_test_twisted(factory) - else: - raise Exception("no such variant") diff --git a/examples/websocket2/server_twisted.py b/examples/websocket2/server_twisted.py new file mode 100644 index 00000000..70aa7aeb --- /dev/null +++ b/examples/websocket2/server_twisted.py @@ -0,0 +1,49 @@ +############################################################################### +## +## Copyright 2013 Tavendo GmbH +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +############################################################################### + +from autobahn.twisted.websocket import WebSocketServerProtocol, \ + WebSocketServerFactory + + + +class MyServerProtocol(WebSocketServerProtocol): + + def onMessage(self, payload, isBinary): + print("message received") + self.sendMessage(payload) + + def onConnect(self, connectionRequest): + return None + + +class MyServerFactory(WebSocketServerFactory): + + protocol = MyServerProtocol + + + +if __name__ == '__main__': + + from twisted.internet.endpoints import TCP4ServerEndpoint + from twisted.internet import reactor + + factory = MyServerFactory() + + endpoint = TCP4ServerEndpoint(reactor, 8888) + endpoint.listen(factory) + reactor.run()