parlay.server

parlay.server.broker

class parlay.server.broker.Broker(reactor)[source]

Bases: object

The Broker is the sole holder of global state. There should be only one. It also coordinates all communication between protocols.

class Modes[source]

These are the modes that the broker can run in. * Development mode is purposefully easy to use an insecure to allow logging and easy control of the parlay system * Production mode is locked down and more secure (Security should always be validated independently)

DEVELOPMENT = 'DEVELOPER_MODE'
PRODUCTION = 'PRODUCTION_MODE'
classmethod Broker.call_on_start(func)[source]

Call the supplied function when the broker starts OR if the broker has already started, call ASAP

classmethod Broker.call_on_stop(func)[source]

Call the supplied function when the broker stops OR if the broker has already stopped, call ASAP

Broker.cleanup(stop_reactor=True)[source]

called on exit to clean up the parlay environment

static Broker.get_instance()[source]

@rtype Broker

static Broker.get_local_ip()[source]
Broker.handle_broker_message(msg, message_callback)[source]

Any message with topic type ‘broker’ should be passed into here. ‘broker’ messages are special messages that don’t get ‘published’. They are for querying the state of the system. ‘broker’ messages have a ‘request’ field and will reply with an appropriate ‘response’ field

message_callback is the function to call to send the message back to the protocol

Broker.handle_subscribe_message(msg, message_callback)[source]
Broker.handle_unsubscribe_message(msg, message_callback)[source]
Broker.instance = <parlay.server.broker.Broker object>
Broker.open_protocol(protocol_name, open_params)[source]

Open a protocol with the given name and parameters (only run this once the Broker has started running

Broker.publish(msg, write_method=None)[source]

Publish a message to the Parlay system :param msg : The message to publish :param write_method : the protocol’s method to callback if the broker needs to send a response :type msg : dict

Broker.run(mode='DEVELOPER_MODE', ssl_only=False, use_ssl=False, open_browser=True, ui_path=None)[source]

Start up and run the broker. This method call with not return

static Broker.start(mode='DEVELOPER_MODE', ssl_only=False, open_browser=True, http_port=8080, https_port=8081, websocket_port=8085, secure_websocket_port=8086, ui_path=None, log_level=10)[source]

Run the default Broker implementation. This call will not return.

static Broker.start_for_test()[source]
static Broker.stop()[source]
static Broker.stop_for_test()[source]
Broker.subscribe(func, _owner_=None, **kwargs)[source]

Register a listener. The kwargs is a dictionary of args that all must be true to call this listener. You may register the same function multiple times with different kwargs, and it may be called multiple times for each message. @param func: The function to run @param kwargs: The key/value pairs to listen for

Broker.unsubscribe(owner, TOPICS)[source]

Unsubscribe owner from all subscriptions that match TOPICS. Only EXACT matches will be unsubscribed

Broker.unsubscribe_all(owner, root_list=None)[source]

Unsubscribe all function in our list that have a n owner that matches ‘owner’

class parlay.server.broker.BrokerSSlContextFactory[source]

Bases: twisted.internet.ssl.ContextFactory

A more secure context factory than the default one. Only supports high security encryption ciphers and exchange formats. Last Updated August 2015

getContext()[source]

Return a SSL.Context object. override in subclasses.

parlay.server.broker.main()[source]
parlay.server.broker.run_in_broker(fn)[source]

Decorator: Wrap any method in this when you want to be sure it’s called from the broker thread. If in a background thread, it will block until completion. If already in a reactor thread, then no change

parlay.server.broker.run_in_thread(fn)[source]

Decorator: Wrap any method in this when you want to be sure it’s called from a background thread . If in a background thread, no change. If in the broker thread, will move to background thread and return deferred with result.

parlay.server.reactor

We’ve added a number of enhancments to the Twisted reactor to enable easier threading handling. Instead of importing the twisted.internet reactor import this reactor like from parlay.server.reactor import reactor

class parlay.server.reactor.ReactorWrapper(wrapped_reactor)[source]

Bases: object

in_reactor_thread()[source]

Returns true if we’re in the reactor thread context. False otherwise

maybeCallFromThread(callable, *args, **kwargs)[source]

If we’re in a separate thread from the reactor, then call from the reactor thread. If we’re in the reactor thread, then schedule for call later in 0 seconds

maybeDeferToThread(callable, *args, **kwargs)[source]

Call callable from DIFFERENT THREAD. If we’re already in a different thread, then JUST CALL IT and return result If we’re in the reactor thead, then call it in a different thread and return a deferred with the result

maybeblockingCallFromThread(callable, *args, **kwargs)[source]

Call callable from the reactor thread. If we are in the reactor thread, then call it and return a Deferred. If we are not in the reactor thread, then block on that deferred instal of returning it

run(installSignalHandlers=True)[source]
parlay.server.reactor.run_in_reactor(reactor)[source]

Decorator for automatically handling deferred <-> thread handoff. Any function wrapped in this will work in both a threaded context and a ‘reactor’ async context. Use this decorator if you want the function to always be run in the reactor context

:param reactor the Reactor to use (MUST BE A REACTORWRAPPER)

parlay.server.reactor.run_in_thread(reactor)[source]

Decorator for automatically handling deferred <-> thread handoff. Any function wrapped in this will work in both a threaded context and a ‘reactor’ async context. Use this decorator if you want the function to always be run in the threaded context :param reactor the Reactor to use (MUST BE A REACTORWRAPPER)