working on new API

This commit is contained in:
Tobias Oberstein 2015-09-27 22:37:35 +02:00
parent 163b94d9b9
commit 61556e03c1
3 changed files with 28 additions and 19 deletions

View File

@ -33,6 +33,7 @@ from twisted.internet.defer import inlineCallbacks
from twisted.internet.interfaces import IStreamClientEndpoint
from twisted.internet.endpoints import UNIXClientEndpoint
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet.task import react
try:
_TLS = True
@ -282,7 +283,7 @@ class Component(component.Component):
reconnect = False
def run(reactor, components):
def _run(reactor, components):
if isinstance(components, Component):
components = [components]
@ -293,10 +294,16 @@ def run(reactor, components):
if not isinstance(c, Component):
raise RuntimeError('"components" must be a list of Component objects - encountered item of type {0}'.format(type(c)))
# all components are started in parallel
dl = []
for c in components:
# a component can be of type MAIN or SETUP
dl.append(c.start(reactor))
d = txaio.gather(dl, consume_exceptions=True)
return d
def run(components):
react(_run, [components])

View File

@ -254,14 +254,16 @@ class Component(ObservableMixin):
if self._entry_type == Component.TYPE_MAIN:
def on_join(session, details):
print("session on_join: {details}", details)
self.log.debug("session on_join: {details}", details=details)
d = txaio.as_future(self._entry, reactor, session)
def main_success(_):
print("main_success")
self.log.debug("main_success")
txaio.resolve(done, None)
def main_error(err):
print("main_error", err)
self.log.debug("main_error", err)
txaio.reject(done, err)
txaio.add_callbacks(d, main_success, main_error)
@ -270,14 +272,14 @@ class Component(ObservableMixin):
elif self._entry_type == Component.TYPE_SETUP:
def on_join(session, details):
print("session on_join: {details}", details)
self.log.debug("session on_join: {details}", details=details)
d = txaio.as_future(self._entry, reactor, session)
def setup_success(_):
print("setup_success")
self.log.debug("setup_success")
def setup_error(err):
print("setup_error", err)
self.log.debug("setup_error", err)
txaio.add_callbacks(d, setup_success, setup_error)
@ -299,9 +301,9 @@ class Component(ObservableMixin):
if was_clean:
# eg the session has left the realm, and the transport was properly
# shut down. successfully finish the connection
done.callback(None)
txaio.resolve(done, None)
else:
done.errback(RuntimeError('transport closed uncleanly'))
txaio.reject(done, RuntimeError('transport closed uncleanly'))
session.on('disconnect', on_disconnect)

View File

@ -1,10 +1,11 @@
from twisted.internet.defer import inlineCallbacks as coroutine
from autobahn.twisted.util import sleep
@coroutine
def component1_setup(reactor, session):
# the session is joined and ready for use.
def shutdown():
print('backend component shutting down ..')
print('backend component: shutting down ..')
session.leave()
yield session.subscribe(shutdown, u'com.example.shutdown')
@ -17,7 +18,7 @@ def component1_setup(reactor, session):
yield session.register(add2, u'com.example.add2')
# yield session.register(u'com.example.add2', add2)
print('backend component ready.')
print('backend component: ready.')
# as we exit, this signals we are ready! the session must be kept.
@ -25,24 +26,23 @@ def component1_setup(reactor, session):
@coroutine
def component2_main(reactor, session):
# the session is joined and ready
yield sleep(.2) # "enforce" order: backend must have started before we call it
print('frontend component: ready')
result = yield session.call(u'com.example.add2', 2, 3)
print('result={}'.format(result))
print('frontend component: result={}'.format(result))
session.publish(u'com.example.shutdown')
# as we exit, this signals we are done with the session! the session
# can be recycled
print('frontend component: shutting down ..')
if __name__ == '__main__':
from autobahn.twisted.component import Component
from twisted.internet.task import react
#component = Component(setup=component1_setup)
#react(component.start)
from autobahn.twisted.component import Component, run
transports = [
{
'type': 'rawsocket',
@ -75,4 +75,4 @@ if __name__ == '__main__':
Component(main=component2_main, transports=transports, config=config)
]
react(run, [components])
run(components)