more cleanup

This commit is contained in:
Tobias Oberstein
2014-04-11 01:29:12 +02:00
parent d4467b9dcc
commit ea67aa00b3
6 changed files with 60 additions and 28 deletions

View File

@@ -99,3 +99,52 @@ class RouterSessionFactory(FutureMixin, protocol.RouterSessionFactory):
WAMP router session factory for asyncio-based applications.
"""
session = RouterSession
import sys
import traceback
import asyncio
from autobahn.wamp.types import ComponentConfig
from autobahn.asyncio.websocket import WampWebSocketClientFactory
class ApplicationRunner:
def __init__(self, endpoint, url, realm, extra = {}, debug = False,
debug_wamp = False, debug_app = False):
self.endpoint = endpoint
self.url = url
self.realm = realm
self.extra = extra
self.debug = debug
self.debug_wamp = debug_wamp
self.debug_app = debug_app
self.make = None
def run(self, make):
## 1) factory for use ApplicationSession
def create():
cfg = ComponentConfig(self.realm, self.extra)
try:
session = make(cfg)
except Exception as e:
## the app component could not be created .. fatal
print(traceback.format_exc())
asyncio.get_event_loop().stop()
session.debug_app = self.debug_app
return session
## 2) create a WAMP-over-WebSocket transport client factory
transport_factory = WampWebSocketClientFactory(create, url = self.url,
debug = self.debug, debug_wamp = self.debug_wamp)
## 3) start the client
loop = asyncio.get_event_loop()
coro = loop.create_connection(transport_factory, args.host, args.port)
loop.run_until_complete(coro)
## 4) now enter the asyncio event loop
loop.run_forever()
loop.close()

View File

@@ -26,14 +26,13 @@ from twisted.internet.endpoints import clientFromString
from autobahn.twisted import wamp, websocket
class MyFrontendComponent(wamp.ApplicationSession):
"""
Application code goes here. This is an example component that calls
a remote procedure on a WAMP peer, subscribes to a topic to receive
events, and then stops the world after some events.
"""
def onConnect(self):
self.join(u"realm1")
@inlineCallbacks
def onJoin(self, details):
@@ -60,13 +59,12 @@ class MyFrontendComponent(wamp.ApplicationSession):
sub = yield self.subscribe(on_event, u'com.myapp.topic1')
print("Subscribed with subscription ID {}".format(sub.id))
def onLeave(self, details):
self.disconnect()
def onDisconnect(self):
reactor.stop()
if __name__ == '__main__':
## 0) start logging to console

View File

@@ -37,8 +37,6 @@ class MyBackendComponent(wamp.ApplicationSession):
a simple procedure which can be called remotely from any WAMP peer.
It also publishes an event every second to some topic.
"""
def onConnect(self):
self.join(u"realm1")
@inlineCallbacks
def onJoin(self, details):
@@ -63,6 +61,7 @@ class MyBackendComponent(wamp.ApplicationSession):
yield sleep(1)
if __name__ == '__main__':
## 0) start logging to console

View File

@@ -87,7 +87,7 @@ class VoteGameBackend(ApplicationSession):
def run(txn):
## FIXME: make the following into 1 (atomic) SQL statement
## => does SQLite feature "UPDATE .. RETURNING"?
txn.execute("UPDATE votes SET count = count + 1 WHERE item = ?", [item])
txn.execute("UPDATE votes SET count = count + 1 WHERE item = ?", [item])
txn.execute("SELECT count FROM votes WHERE item = ?", [item])
count = int(txn.fetchone()[0])
@@ -99,9 +99,6 @@ class VoteGameBackend(ApplicationSession):
return self.db.runInteraction(run)
def onConnect(self):
self.join(self.config.realm)
@inlineCallbacks
def onJoin(self, details):
@@ -120,9 +117,6 @@ class VoteGameBackend(ApplicationSession):
print("VoteGame Backend ready!")
def onLeave(self, details):
self.disconnect()
def onDisconnect(self):
reactor.stop()
@@ -133,7 +127,7 @@ def make(config):
##
## This component factory creates instances of the
## application component to run.
##
##
## The function will get called either during development
## using the ApplicationRunner below, or as a plugin running
## hosted in a WAMPlet container such as a Crossbar.io worker.

View File

@@ -29,6 +29,7 @@ from autobahn.wamp.exception import ApplicationError
from wampirc.client import IRCClientFactory
class Bot:
"""
Tracks currently running bot instances.
@@ -39,6 +40,7 @@ class Bot:
self.client = client
class IRCComponent(ApplicationSession):
"""
IRC bot services component.
@@ -50,6 +52,7 @@ class IRCComponent(ApplicationSession):
self._bots = {}
self._bot_no = 0
@wamp.procedure('com.myapp.start_bot')
def start_bot(self, nick, channels):
self._bot_no += 1
@@ -67,6 +70,7 @@ class IRCComponent(ApplicationSession):
return d
@wamp.procedure('com.myapp.stop_bot')
def stop_bot(self, id):
if id in self._bots:
@@ -78,8 +82,6 @@ class IRCComponent(ApplicationSession):
else:
raise ApplicationError('com.myapp.error.no_such_bot')
def onConnect(self):
self.join(self.config.realm)
@inlineCallbacks
def onJoin(self, details):
@@ -91,8 +93,6 @@ class IRCComponent(ApplicationSession):
print("IRC Bot Backend ready!")
def onLeave(self, details):
self.disconnect()
def onDisconnect(self):
reactor.stop()

View File

@@ -25,17 +25,11 @@ from autobahn.twisted.wamp import ApplicationSession
from calculator import Calculator
## WAMP application component with our app code.
##
class Component1(ApplicationSession):
def __init__(self, config):
ApplicationSession.__init__(self)
self.config = config
def onConnect(self):
self.join(self.config.realm)
@inlineCallbacks
def onJoin(self, details):
@@ -61,8 +55,6 @@ class Component1(ApplicationSession):
else:
print("Failed to register procedure: {}".format(res.value))
def onLeave(self, details):
self.disconnect()
def onDisconnect(self):
reactor.stop()
@@ -73,7 +65,7 @@ def make(config):
##
## This component factory creates instances of the
## application component to run.
##
##
## The function will get called either during development
## using the ApplicationRunner below, or as a plugin running
## hosted in a WAMPlet container such as a Crossbar.io worker.