Ejemplo n.º 1
0
def crossbar(request):
    if START_CROSSBAR:
        # Start a wamp router
        subprocess.Popen(["crossbar", "start", "--cbdir", CROSSBAR_CONF_DIR])
    started = False
    for _ in range(20):
        sleep(0.5)
        # Try to engage a wamp connection with crossbar to make sure it is started
        try:
            test_app = AutobahnSync()
            test_app.run()
            # test_app.session.disconnect()  # TODO: fix me
        except ConnectionRefusedError:
            continue
        else:
            started = True
            break
    if not started:
        raise RuntimeError("Couldn't connect to crossbar router")

    def finalizer():
        p = subprocess.Popen(["crossbar", "stop", "--cbdir", CROSSBAR_CONF_DIR])
        p.wait()

    if START_CROSSBAR:
        request.addfinalizer(finalizer)
Ejemplo n.º 2
0
 def __init__(self, url):
     self.app = AutobahnSync()
     self.url = url
     # each ticker_stream topic is a currency pair
     self.ticker_stream = pubsub.MultitopicPublisher() 
     self.ticker_stream.add_start_callback( lambda : self.start_substream('ticker') )
     self.ticker_stream.add_stop_callback( lambda : self.stop_substream('ticker') )
Ejemplo n.º 3
0
class PoloniexCombinedStream(object):
    def __init__(self, url):
        self.app = AutobahnSync()
        self.url = url
        # each ticker_stream topic is a currency pair
        self.ticker_stream = pubsub.MultitopicPublisher() 
        self.ticker_stream.add_start_callback( lambda : self.start_substream('ticker') )
        self.ticker_stream.add_stop_callback( lambda : self.stop_substream('ticker') )

    def start_transport(self):
        log.info('starting transport')
        self.app.run(url=self.url)

    def stop_transport(self):
        log.info('stopping transport')
        self.app.stop()

    def start_substream(self, substream):
        assert substream in SUBSTREAMS
        self.start_transport()
        if substream == TICKER_STREAM:
            self.ticker_subscription = self.app.session.subscribe(handler=self.ticker_callback, topic=TICKER_STREAM)

    def stop_substream(self, substream):
        assert substream in SUBSTREAMS
        if substream == TICKER_STREAM:
            self.app.session.unsubscribe(self.ticker_subscription)
            self.ticker_subscription = None
        self.stop_transport()

    def ticker_callback(self, *data):
        try:
            if CURRENCY_PAIR_CODE_TO_MARKET is None:
                raise Exception("You need to set CURRENCY_PAIR_CODE_TO_MARKET")
            currency_pair_code, last, ask, bid, percent_change, base_volume, quote_volume, is_frozen, high, low = data
            market = CURRENCY_PAIR_CODE_TO_MARKET[currency_pair_code]
            time = datetime.datetime.now()
            ticker_data={"bid":bid, "ask":ask, "last":last, "high":high, "low":low}
            ticker = PoloniexTicker.from_data(ticker_data, market)
            self.ticker_stream.send(ticker, currency_pair_code)
        except Exception as e:
            import traceback
            traceback.print_exc()
Ejemplo n.º 4
0
def wamp(crossbar):
    wamp = AutobahnSync()
    wamp.run()
    return wamp
Ejemplo n.º 5
0
 def test_bad_realm(self, crossbar):
     wamp = AutobahnSync()
     with pytest.raises(AbortError) as exc:
         wamp.run(realm=u'bad_realm')
     assert str(exc.value.args[0]) == 'CloseDetails(reason=<wamp.error.no_such_realm>, message=\'no realm "bad_realm" exists on this router\')'
Ejemplo n.º 6
0
 def test_router_not_started(self):
     wamp = AutobahnSync()
     with pytest.raises(ConnectionRefusedError):
         wamp.run(url=u'ws://localhost:9999/missing')