Esempio n. 1
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') )
Esempio n. 2
0
class GAConnection(object):
    """ A class encapsulating a connection to the GreenAddress service """

    MAINNET_URI = u'wss://prodwss.greenaddress.it/v2/ws/'
    TESTNET_URI = u'wss://testwss.greenaddress.it/v2/ws/'
    REGTEST_URI = u'wss://regtestwss.greenaddress.it/v2/ws/'
    USER_AGENT = u'[v2,sw]gaexamples'
    TIMEOUT_SECONDS = 30

    def __init__(self, uri=None):
        """ Create a connection to the GreenAddress service """
        self.wamp = AutobahnSync()
        exc = None
        for attempt in range(self.TIMEOUT_SECONDS):
            try:
                self.wamp.run(url=uri or self.REGTEST_URI)
                return
            except Exception as e:
                exc = e
                sleep(1)
        raise exc

    def call(self, name, *args):
        """ Make an API call to the GreenAddress service """
        return self.wamp.session.call(u'com.greenaddress.' + unicode(name), *args)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
0
 def __init__(self, uri=None):
     """ Create a connection to the GreenAddress service """
     self.wamp = AutobahnSync()
     exc = None
     for attempt in range(self.TIMEOUT_SECONDS):
         try:
             self.wamp.run(url=uri or self.REGTEST_URI)
             return
         except Exception as e:
             exc = e
             sleep(1)
     raise exc
Esempio n. 6
0
 def test_leave(self, crossbar):
     wamp = AutobahnSync()
     wamp.run()
     wamp.session.publish('com.disconnect.ready')
     wamp.session.leave()
     with pytest.raises(TransportLost):
         wamp.session.publish('com.disconnect.no_realm')
Esempio n. 7
0
 def test_no_subscribe(self, crossbar):
     wamp = AutobahnSync()
     wamp.run(realm=u'realm_limited')
     with pytest.raises(ApplicationError) as exc:
         wamp.session.subscribe(lambda: None, 'pubsub.no_subscribe.event')
     assert str(
         exc.value.args[0]
     ) == "session is not authorized to subscribe to topic 'pubsub.no_subscribe.event'"
Esempio n. 8
0
 def test_no_register(self, crossbar):
     wamp = AutobahnSync()
     wamp.run(realm=u'realm_limited')
     with pytest.raises(ApplicationError) as exc:
         wamp.session.register(lambda: None, u'rpc.no_register.func')
     assert str(
         exc.value.args[0]
     ) == u"session is not authorized to register procedure 'rpc.no_register.func'"
Esempio n. 9
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()
    def test_bad_authid(self, crossbar):
        wamp = AutobahnSync()

        @wamp.on_challenge
        def on_challenge(challenge):
            pass

        with pytest.raises(AbortError) as exc:
            wamp.run(url=u'ws://localhost:8080/ws_auth',
                     authid='dummy',
                     authmethods=[u'ticket'])
        assert str(exc.value.args[0].reason) == 'wamp.error.not_authorized'
Esempio n. 11
0
 def test_no_publish(self, crossbar):
     wamp = AutobahnSync()
     wamp.run(realm=u'realm_limited')
     wamp.session.subscribe(lambda: None, 'pubsub.no_publish.event')
     publish_opt = PublishOptions(acknowledge=True)
     with pytest.raises(ApplicationError) as exc:
         res = wamp.session.publish(u'pubsub.no_publish.event',
                                    None,
                                    options=publish_opt)
     assert str(
         exc.value.args[0]
     ) == u"session not authorized to publish to topic 'pubsub.no_publish.event'"
 def test_router_not_started(self):
     wamp = AutobahnSync()
     with pytest.raises(ConnectionRefusedError):
         wamp.run(url=u'ws://localhost:9999/missing')
     # Make sure we can reuse the wamp object
     with pytest.raises(ConnectionRefusedError):
         wamp.run(url=u'ws://localhost:9999/missing')
Esempio n. 13
0
 def test_stop(self, crossbar):
     wamp = AutobahnSync()
     wamp.run()
     wamp.session.publish('com.disconnect.ready')
     wamp.session.leave()
     wamp.stop()
     sleep(0.1)  # Dirty way to wait for stop...
     with pytest.raises(TransportLost):
         wamp.session.publish('com.disconnect.no_realm')
     with pytest.raises(NotRunningError) as exc:
         wamp.stop()
     assert str(
         exc.value.args[0]) == "This AutobahnSync instance is not started"
    def test_bad_method(self, crossbar):
        wamp = AutobahnSync()

        @wamp.on_challenge
        def on_challenge(challenge):
            raise Exception("Invalid authmethod %s" % challenge.method)

        with pytest.raises(AbortError) as exc:
            wamp.run(realm=u'realm1',
                     url=u'ws://localhost:8080/ws_auth',
                     authid='ticket_user_1',
                     authmethods=[u'ticket'])
        assert str(exc.value.args[0]) == 'Invalid authmethod ticket'
    def test_good_auth(self, crossbar):
        wamp = AutobahnSync()

        @wamp.on_challenge
        def on_challenge(challenge):
            assert challenge.method == 'ticket'
            return 'ticket_secret'

        wamp.run(realm=u'realm1',
                 url=u'ws://localhost:8080/ws_auth',
                 authid='ticket_user_1',
                 authmethods=[u'ticket'])
        # Make sure we are connected
        wamp.session.subscribe(lambda: None, u'test.challenge.event')
Esempio n. 16
0
    def test_decorate_before_run(self, crossbar):
        wamp = AutobahnSync()
        rets = []
        counter_func = CounterHelper()

        @wamp.register('rpc.decorate_before_run.func')
        def my_func(*args, **kwargs):
            return counter_func()

        wamp.run()
        rets.append(wamp.session.call('rpc.decorate_before_run.func'))
        rets.append(wamp.session.call('rpc.decorate_before_run.func'))
        rets.append(wamp.session.call('rpc.decorate_before_run.func'))
        assert rets == [1, 2, 3]
Esempio n. 17
0
    def test_decorate_before_run(self, crossbar):
        events = []
        wamp = AutobahnSync()

        @wamp.subscribe(u'pubsub.decorate_before_run.event')
        def on_event(*args, **kwargs):
            events.append((args, kwargs))

        wamp.run()
        publish_opt = PublishOptions(exclude_me=False)
        wamp.session.publish('pubsub.decorate_before_run.event',
                             '1',
                             options=publish_opt)
        wamp.session.publish('pubsub.decorate_before_run.event',
                             '2',
                             options=publish_opt)
        wamp.session.publish('pubsub.decorate_before_run.event',
                             opt=True,
                             options=publish_opt)
        sleep(0.1)  # Dirty way to wait for on_event to be called...
        assert events == [(('1', ), {}), (('2', ), {}), ((), {'opt': True})]
Esempio n. 18
0
 def test_router_not_started(self):
     wamp = AutobahnSync()
     with pytest.raises(ConnectionRefusedError):
         wamp.run(url=u'ws://localhost:9999/missing')
Esempio n. 19
0
def wamp(crossbar):
    wamp = AutobahnSync()
    wamp.run()
    return wamp
Esempio n. 20
0
    def __init__(
        self,
        ssl=True,
        debug=True,
        authentication=True,
        user=u"ffbo",
        secret=u"",
        url=u"wss://neuronlp.fruitflybrain.org:7777/ws",
        realm=u"realm1",
        ca_cert_file="isrgrootx1.pem",
        intermediate_cert_file="letsencryptauthorityx3.pem",
        FFBOLabcomm=None,
        FBLcomm=None,
        server_name='nk',
    ):
        if FBLcomm is None and FFBOLabcomm is not None:
            FBLcomm = FFBOLabcomm
        if os.path.exists(os.path.join(home, ".ffbo", "lib")):
            print(
                printHeader("FBL Client") +
                "Downloading the latest certificates.")
            # CertificateDownloader = urllib.URLopener()
            if not os.path.exists(os.path.join(home, ".ffbo", "lib")):
                urlRetriever(
                    "https://data.flybrainlab.fruitflybrain.org/config/FBLClient.ini",
                    os.path.join(home, ".ffbo", "config", "FBLClient.ini"),
                )
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/isrgrootx1.pem",
                os.path.join(home, ".ffbo", "lib", "caCertFile.pem"),
            )
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/letsencryptauthorityx3.pem",
                os.path.join(home, ".ffbo", "lib", "intermediateCertFile.pem"),
            )
            config_file = os.path.join(home, ".ffbo", "config",
                                       "FBLClient.ini")
            ca_cert_file = os.path.join(home, ".ffbo", "lib", "caCertFile.pem")
            intermediate_cert_file = os.path.join(home, ".ffbo", "lib",
                                                  "intermediateCertFile.pem")
        config = ConfigParser()
        config.read(config_file)
        # user = config["ComponentInfo"]["user"]
        # secret = config["ComponentInfo"]["secret"]
        # url = config["ComponentInfo"]["url"]
        self.FBLcomm = FBLcomm
        self.NKSimState = 0
        self.executionSettings = []
        extra = {"auth": authentication}
        self.lmsg = 0
        st_cert = open(ca_cert_file, "rt").read()
        c = OpenSSL.crypto
        ca_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        st_cert = open(intermediate_cert_file, "rt").read()
        intermediate_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        certs = OpenSSLCertificateAuthorities([ca_cert, intermediate_cert])
        ssl_con = CertificateOptions(trustRoot=certs)

        FBLClient = AutobahnSync()
        self.client = FBLClient

        @FBLClient.on_challenge
        def on_challenge(challenge):
            if challenge.method == u"wampcra":
                print("WAMP-CRA challenge received: {}".format(challenge))
                if u"salt" in challenge.extra:
                    # salted secret
                    salted_key = auth.derive_key(secret,
                                                 challenge.extra['salt'],
                                                 challenge.extra['iterations'],
                                                 challenge.extra['keylen'])
                    salted_key = (salted_key).decode('utf-8')
                #if user==u'ffbo':
                # plain, unsalted secret
                #    salted_key = u"kMU73GH4GS1WGUpEaSdDYwN57bdLdB58PK1Brb25UCE="
                # print(salted_key)
                # compute signature for challenge, using the key
                signature = auth.compute_wcs(salted_key,
                                             challenge.extra["challenge"])

                # return the signature to the router for verification
                return signature

            else:
                raise Exception("Invalid authmethod {}".format(
                    challenge.method))

        if ssl:
            FBLClient.run(url=url,
                          authmethods=[u'wampcra'],
                          authid=user,
                          ssl=ssl_con)
        else:
            FBLClient.run(url=url, authmethods=[u'wampcra'], authid=user)

        setProtocolOptions(FBLClient._async_session._transport,
                           maxFramePayloadSize=0,
                           maxMessagePayloadSize=0,
                           autoFragmentSize=65536)

        self.client_data = []
        self.data = []
        self.launch_queue = []

        @FBLClient.register("ffbo.nk.launch." +
                            str(FBLClient._async_session._session_id))
        def nk_launch_progressive(task, details=None):
            # print(task['user'])
            user_id = str(task["user"])
            self.launch_queue.append((user_id, task))

            def mock_result():
                result = {u"ydomain": 1, u"xdomain": 1, u"dt": 10, u"data": {}}
                """
                res = {u'ydomain': 1,
                          u'xdomain': 1,
                          u'dt': 10,
                          u'data': {}}
                print(task['forward'])
                res_to_processor = yield self.call(six.u(task['forward']), res)
                """
                return result, result

            res = mock_result()
            return res
            # res = server.launch(user_id, task)
            # returnValue(res)

        print("Procedure nk_launch_progressive Registered...")

        res = FBLClient.session.call(
            u"ffbo.server.register",
            FBLClient._async_session._session_id,
            "nk",
            {
                "name": server_name,
                "version": 1.05
            },
        )
        print("Registered self...")
 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\')'
Esempio n. 22
0
def wamp(crossbar):
    wamp = AutobahnSync()
    wamp.run()
    return wamp
Esempio n. 23
0
    def __init__(self,
                 ssl=True,
                 debug=True,
                 authentication=True,
                 user='******',
                 secret='guestpass',
                 url=u'wss://neuronlp.fruitflybrain.org:7777/ws',
                 realm=u'realm1',
                 ca_cert_file='isrgrootx1.pem',
                 intermediate_cert_file='letsencryptauthorityx3.pem',
                 FFBOLabcomm=None):
        """Initialization function for the ffbolabClient class.

        Args:
            ssl (bool): Whether the FFBO server uses SSL.
            debug (bool) : Whether debugging should be enabled.
            authentication (bool): Whether authentication is enabled.
            user (str): Username for establishing communication with FFBO components.
            secret (str): Password for establishing communication with FFBO components.
            url (str): URL of the WAMP server with the FFBO Processor component.
            realm (str): Realm to be connected to.
            ca_cert_file (str): Path to the certificate for establishing connection.
            intermediate_cert_file (str): Path to the intermediate certificate for establishing connection.
            FFBOLabcomm (obj) Communications object for the frontend.
        """
        self.FFBOLabcomm = FFBOLabcomm  # Current Communications Object
        self.C = None  # The Neuroballd Circuit object describing the loaded neural circuit
        self.dataPath = _FFBOLabDataPath
        extra = {'auth': authentication}
        self.lmsg = 0
        self.experimentInputs = []  # List of current experiment inputs
        self.compiled = False  # Whether the current circuit has been compiled into a NetworkX Graph
        self.sendDataToGFX = True  # Shall we send the received simulation data to GFX Component?
        self.executionSuccessful = False  # Used to wait for data loading
        self.experimentQueue = []  # A queue for experiments
        self.simData = {
        }  # Locally loaded simulation data obtained from server
        self.clientData = []  # Servers list
        self.data = [
        ]  # A buffer for data from backend; used in multiple functions so needed
        st_cert = open(ca_cert_file, 'rt').read()
        c = OpenSSL.crypto
        ca_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        st_cert = open(intermediate_cert_file, 'rt').read()
        intermediate_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        certs = OpenSSLCertificateAuthorities([ca_cert, intermediate_cert])
        ssl_con = CertificateOptions(trustRoot=certs)

        FFBOLABClient = AutobahnSync()

        @FFBOLABClient.on_challenge
        def on_challenge(challenge):
            """The On Challenge function that computes the user signature for verification.
            
            Args:
                challenge (obj): The challenge object received.

            Returns:
                str: The signature sent to the router for verification.
            """
            print(printHeader('FFBOLab Client') + 'Initiating authentication.')
            if challenge.method == u"wampcra":
                print(
                    printHeader('FFBOLab Client') +
                    "WAMP-CRA challenge received: {}".format(challenge))
                print(challenge.extra['salt'])
                if u'salt' in challenge.extra:
                    # Salted secret
                    print(printHeader('FFBOLab Client') + 'Deriving key...')
                    salted_key = auth.derive_key(secret,
                                                 challenge.extra['salt'],
                                                 challenge.extra['iterations'],
                                                 challenge.extra['keylen'])
                    print(salted_key.decode('utf-8'))

                if user == 'guest':
                    # A plain, unsalted secret for the guest account
                    salted_key = u"C5/c598Gme4oALjmdhVC2H25OQPK0M2/tu8yrHpyghA="

                # compute signature for challenge, using the key
                signature = auth.compute_wcs(salted_key,
                                             challenge.extra['challenge'])

                # return the signature to the router for verification
                return signature

            else:
                raise Exception("Invalid authmethod {}".format(
                    challenge.method))

        FFBOLABClient.run(
            url=url, authmethods=[u'wampcra'], authid='guest',
            ssl=ssl_con)  # Initialize the communication right now!

        @FFBOLABClient.subscribe('ffbo.server.update.' +
                                 str(FFBOLABClient._async_session._session_id))
        def updateServers(data):
            """Updates available servers.
            
            Args:
                data (obj): Obtained servers list.

            """
            self.clientData.append(data)
            print("Updated the Servers")

        print("Subscribed to topic 'ffbo.server.update'")

        @FFBOLABClient.register('ffbo.ui.receive_cmd.' +
                                str(FFBOLABClient._async_session._session_id))
        def receiveCommand(data):
            """The Receive Command function that receives commands and sends them to the frontend.
            
            Args:
                data (dict): Data to be sent to the frontend

            Returns:
                bool: Whether the data has been received.
            """
            self.clientData.append('Received Command')
            a = {}
            a['data'] = data
            a['messageType'] = 'Command'
            a['widget'] = 'NLP'
            self.data.append(a)
            print(printHeader('FFBOLab Client NLP') + "Received a command.")
            self.tryComms(a)
            return True

        print(
            printHeader('FFBOLab Client') +
            "Procedure ffbo.ui.receive_cmd Registered...")

        @FFBOLABClient.register('ffbo.ui.receive_gfx.' +
                                str(FFBOLABClient._async_session._session_id))
        def receiveGFX(data):
            """The Receive GFX function that receives commands and sends them to the GFX frontend.
            
            Args:
                data (dict): Data to be sent to the frontend.

            Returns:
                bool: Whether the data has been received.
            """
            self.clientData.append('Received GFX Data')
            self.data.append(data)
            print(
                printHeader('FFBOLab Client GFX') +
                "Received a message for GFX.")
            if self.sendDataToGFX == True:
                self.tryComms(data)
            else:
                if 'messageType' in data.keys():
                    if data['messageType'] == 'showServerMessage':
                        print(
                            printHeader('FFBOLab Client GFX') +
                            "Execution successful for GFX.")
                        if len(self.experimentQueue) > 0:
                            print(
                                printHeader('FFBOLab Client GFX') +
                                "Next execution now underway. Remaining simulations: "
                                + str(len(self.experimentQueue)))
                            a = self.experimentQueue.pop(0)
                            res = self.client.session.call(
                                'ffbo.gfx.sendExperiment', a)
                            res = self.client.session.call(
                                'ffbo.gfx.startExecution', {'name': a['name']})
                        else:
                            self.executionSuccessful = True
                            self.parseSimResults()
                            print(
                                printHeader('FFBOLab Client GFX') +
                                "GFX results successfully parsed.")
            return True

        print(
            printHeader('FFBOLab Client') +
            "Procedure ffbo.ui.receive_gfx Registered...")

        @FFBOLABClient.register('ffbo.ui.get_circuit.' +
                                str(FFBOLABClient._async_session._session_id))
        def get_circuit(X):
            """Obtain a circuit and save it to the local FFBOLab folder.
            
            Args:
                X (str): Name of the circuit.

            Returns:
                bool: Whether the process has been successful.
            """
            name = X['name']
            G = binascii.unhexlify(X['graph'].encode())
            with open(os.path.join(_FFBOLabDataPath, name + '.gexf.gz'),
                      "wb") as file:
                file.write(G)
            return True

        print("Procedure ffbo.ui.get_circuit Registered...")

        @FFBOLABClient.register('ffbo.ui.get_experiment' +
                                str(FFBOLABClient._async_session._session_id))
        def get_experiment(X):
            """Obtain an experiment and save it to the local FFBOLab folder.
            
            Args:
                X (str): Name of the experiment.

            Returns:
                bool: Whether the process has been successful.
            """
            print(printHeader('FFBOLab Client GFX') + "get_experiment called.")
            name = X['name']
            data = json.dumps(X['experiment'])
            with open(os.path.join(_FFBOLabDataPath, name + '.json'),
                      "w") as file:
                file.write(data)
            output = {}
            output['success'] = True
            print(
                printHeader('FFBOLab Client GFX') +
                "Experiment save successful.")
            return True

        print("Procedure ffbo.ui.get_experiment Registered...")

        @FFBOLABClient.register('ffbo.ui.receive_data.' +
                                str(FFBOLABClient._async_session._session_id))
        def receiveData(data):
            """The Receive Data function that receives commands and sends them to the NLP frontend.
            
            Args:
                data (dict): Data from the backend.

            Returns:
                bool: Whether the process has been successful.
            """
            self.clientData.append('Received Data')
            a = {}
            a['data'] = data
            a['messageType'] = 'Data'
            a['widget'] = 'NLP'
            self.data.append(a)
            print(printHeader('FFBOLab Client NLP') + "Received data.")
            self.tryComms(a)
            return True

        print(
            printHeader('FFBOLab Client') +
            "Procedure ffbo.ui.receive_data Registered...")

        @FFBOLABClient.register('ffbo.ui.receive_msg.' +
                                str(FFBOLABClient._async_session._session_id))
        def receiveMessage(data):
            """The Receive Message function that receives commands and sends them to the NLP frontend.
            
            Args:
                data (dict): Data from the backend.

            Returns:
                bool: Whether the process has been successful.
            """
            self.clientData.append('Received Message')
            a = {}
            a['data'] = data
            a['messageType'] = 'Message'
            a['widget'] = 'NLP'
            self.data.append(a)
            print(printHeader('FFBOLab Client NLP') + "Received a message.")
            self.tryComms(a)
            return True

        print(
            printHeader('FFBOLab Client') +
            "Procedure ffbo.ui.receive_msg Registered...")

        self.client = FFBOLABClient  # Set current client to the FFBOLAB Client

        self.findServerIDs()  # Get current server IDs
Esempio n. 24
0
    def __init__(self,
                 ssl=True,
                 debug=True,
                 authentication=True,
                 user=u"ffbo",
                 secret=u"",
                 url=u'wss://neuronlp.fruitflybrain.org:7777/ws',
                 realm=u'realm1',
                 ca_cert_file='isrgrootx1.pem',
                 intermediate_cert_file='letsencryptauthorityx3.pem',
                 FFBOLabcomm=None):
        if os.path.exists(os.path.join(home, '.ffbolab', 'lib')):
            print(
                printHeader('FFBOLab Client') +
                "Downloading the latest certificates.")
            # CertificateDownloader = urllib.URLopener()
            if not os.path.exists(os.path.join(home, '.ffbolab', 'lib')):
                urlRetriever(
                    "https://data.flybrainlab.fruitflybrain.org/config/FBLClient.ini",
                    os.path.join(home, '.ffbolab', 'config', 'FBLClient.ini'))
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/isrgrootx1.pem",
                os.path.join(home, '.ffbolab', 'lib', 'caCertFile.pem'))
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/letsencryptauthorityx3.pem",
                os.path.join(home, '.ffbolab', 'lib',
                             'intermediateCertFile.pem'))
            config_file = os.path.join(home, '.ffbolab', 'config',
                                       'FBLClient.ini')
            ca_cert_file = os.path.join(home, '.ffbolab', 'lib',
                                        'caCertFile.pem')
            intermediate_cert_file = os.path.join(home, '.ffbolab', 'lib',
                                                  'intermediateCertFile.pem')
        config = ConfigParser()
        config.read(config_file)
        user = config["ComponentInfo"]["user"]
        secret = config["ComponentInfo"]["secret"]
        url = config["ComponentInfo"]["url"]
        self.FFBOLabcomm = FFBOLabcomm
        self.NKSimState = 0
        self.executionSettings = []
        extra = {'auth': authentication}
        self.lmsg = 0
        st_cert = open(ca_cert_file, 'rt').read()
        c = OpenSSL.crypto
        ca_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        st_cert = open(intermediate_cert_file, 'rt').read()
        intermediate_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        certs = OpenSSLCertificateAuthorities([ca_cert, intermediate_cert])
        ssl_con = CertificateOptions(trustRoot=certs)

        FFBOLABClient = AutobahnSync()
        self.client = FFBOLABClient

        @FFBOLABClient.on_challenge
        def on_challenge(challenge):
            if challenge.method == u"wampcra":
                print("WAMP-CRA challenge received: {}".format(challenge))
                if u'salt' in challenge.extra:
                    # salted secret
                    salted_key = auth.derive_key(secret,
                                                 challenge.extra['salt'],
                                                 challenge.extra['iterations'],
                                                 challenge.extra['keylen'])
                    salted_key = (salted_key).decode('utf-8')
                    print(salted_key)
                #if user==u'ffbo':
                # plain, unsalted secret
                #    salted_key = u"kMU73GH4GS1WGUpEaSdDYwN57bdLdB58PK1Brb25UCE="
                #print(salted_key)
                # compute signature for challenge, using the key
                signature = auth.compute_wcs(salted_key,
                                             challenge.extra['challenge'])

                # return the signature to the router for verification
                return signature

            else:
                raise Exception("Invalid authmethod {}".format(
                    challenge.method))

        FFBOLABClient.run(url=url,
                          authmethods=[u'wampcra'],
                          authid=user,
                          ssl=ssl_con)
        self.client_data = []
        self.data = []

        @FFBOLABClient.register('ffbo.gfx.addMessage')
        def add_message(message_name, message):
            output = json.loads(message)
            output = ast.literal_eval(json.dumps(output))
            # self.log.info("add_message() returns {x}", x=output)
            print('add_message returns', output)
            if message_name == 'neurogist':
                pass
            return output

        print("Procedure ffbo.gfx.addMessage Registered...")

        @FFBOLABClient.register('ffbo.gfx.updateFileList')
        def updateFileList():
            self.files = [
                f for f in listdir(_FFBOLabDataPath)
                if isfile(join(_FFBOLabDataPath, f))
            ]
            output = ast.literal_eval(json.dumps(self.files))
            print('updateFileList returns', output)
            #self.log.info("return_files() returns {x}", x=output)
            self.data = {}
            self.data['data'] = output
            self.data['messageType'] = 'updateFileList'
            return self.data

        print("Procedure ffbo.gfx.updateFileList Registered...")

        registerOptions = RegisterOptions(details_arg='details')

        @FFBOLABClient.register('ffbo.gfx.startExecution',
                                options=registerOptions)
        def start_execution(settings, details=None):
            self.NKSimState = 1
            settings['userID'] = details.caller
            self.executionSettings.append(settings)
            return True

        print("Procedure ffbo.gfx.startExecution Registered...")

        registerOptions = RegisterOptions(details_arg='details')

        @FFBOLABClient.register('ffbo.gfx.loadResults',
                                options=registerOptions)
        def loadResults(details=None):
            userID = details.caller
            filename = 'neuroballad_temp_model_output.h5'
            G = nx.read_gexf('neuroballad_temp_model.gexf.gz')
            name_dict = {}
            for n, d in G.nodes(data=True):
                name_dict[n] = d['name']
            print(name_dict)
            f = h5py.File(filename, 'r')
            json_data = {}
            for variable_name in f.keys():
                if variable_name != 'metadata':
                    print(variable_name)
                    data = list(f[variable_name].values())[0][:]
                    labels = list(f[variable_name].values())[1][:]
                    print(labels)
                    labels = list(filter(lambda k: b'auto' not in k, labels))
                    labels = list(filter(lambda k: b'input' not in k, labels))
                    print(labels)
                    for i in range(min(5000, len(labels))):
                        str_key = variable_name + '/' + name_dict[
                            labels[i].decode('utf8')]
                        str_key = ''.join(i for i in str_key if ord(i) < 128)
                        data_to_send = data[:, i]
                        data_to_send[data_to_send >= 1E3] = 0
                        data_to_send[data_to_send <= -1E3] = 0
                        data_to_send = np.nan_to_num(data_to_send)
                        data_to_send = np.clip(data_to_send, -1000., 1000.)
                        json_data[str_key] = data_to_send.astype(
                            np.float32).tolist()
            no_keys = int(math.ceil(float(len(json_data.keys())) / 5.0))
            json_out = json.dumps(json_data, ensure_ascii=True)
            text_file = open("Output.txt", "w")
            text_file.write(json_out)
            text_file.close()

            # Switch with calls
            # yield self.publish(u'com.example.clear_results', [])
            a = {}
            a['widget'] = 'GFX'
            a['messageType'] = 'clearResults'
            a['data'] = {}
            res = self.client.session.call(
                u'ffbo.ui.receive_gfx.' + str(userID), a)
            for i in range(no_keys):
                dicttosend = dict(
                    (k, json_data[k])
                    for k in list(json_data.keys())[i * 5:(i + 1) * 5]
                    if k in list(json_data.keys()))
                #try:
                #   yield self.publish(u'com.example.load_results', [dicttosend])
                print('Sending data...')
                a = {}
                a['widget'] = 'GFX'
                a['messageType'] = 'loadResults'
                a['data'] = dicttosend
                res = self.client.session.call(
                    u'ffbo.ui.receive_gfx.' + str(userID), a)
                #except:
                #    print('Data sending failed...')
                #    pass

            # Switch with calls
            a = {}
            a['widget'] = 'GFX'
            a['messageType'] = 'showServerMessage'
            a['data'] = [
                "[GFX] Simulation results successfully obtained.", 1, 2
            ]
            res = self.client.session.call(
                u'ffbo.ui.receive_gfx.' + str(userID), a)
            # yield self.publish(u'com.example.server_message', ["Results acquired...",1,2])
            print("Results sent...")
            # print(str(data[:,i].shape))
            # print(data)
            # print(str(no_keys))
            # print(str(len(json_data.keys())))
            # print(details.caller)
            NK_sim_state = 0
            return True

        print("Procedure ffbo.gfx.loadSimResults Registered...")

        @FFBOLABClient.register('ffbo.gfx.createDiagram')
        def create_diagram(diagram):
            self.log.info("create_diagram() called with {x}", x=diagram)
            output = diagram_generator(json.loads(diagram), self.log)
            return json.dumps(output)

        print("Procedure ffbo.gfx.createDiagram Registered...")

        @FFBOLABClient.register('ffbo.gfx.sendSVG')
        def send_svg(X):
            self.log.info("send_svg() called with {x}", x=X)
            X = json.loads(X)
            name = X['name']
            G = X['svg']
            with open(_FFBOLabDataPath + name + '_visual.svg', "w") as file:
                file.write(G)
            output = {}
            output['success'] = True
            self.log.info("send_svg() responded with {x}", x=output)
            return json.dumps(output)

        print("Procedure ffbo.gfx.sendSVG Registered...")

        @FFBOLABClient.register('ffbo.gfx.sendCircuit')
        def send_circuit(X):
            name = X['name']
            G = binascii.unhexlify(X['graph'].encode())
            with open(os.path.join(_FFBOLabDataPath, name + '.gexf.gz'),
                      "wb") as file:
                file.write(G)
            G = nx.read_gexf(os.path.join(_FFBOLabDataPath, name + '.gexf.gz'))
            nx.write_gexf(G, os.path.join(_FFBOLabDataPath, name + '.gexf'))
            return True

        print("Procedure ffbo.gfx.sendCircuit Registered...")

        @FFBOLABClient.register('ffbo.gfx.getCircuit')
        def get_circuit(X):
            name = X
            with open(os.path.join(_FFBOLabDataPath, name + '.gexf.gz'),
                      'rb') as file:
                data = file.read()
            a = {}
            a['data'] = binascii.hexlify(data).decode()
            a['name'] = name
            return a

        print("Procedure ffbo.gfx.getCircuit Registered...")

        @FFBOLABClient.register('ffbo.gfx.getExperiment')
        def get_experiment(X):
            name = X
            with open(os.path.join(_FFBOLabDataPath, name + '.json'),
                      "r") as file:
                experimentInputsJson = file.read()
            experimentInputsJson = json.loads(experimentInputsJson)
            a = {}
            a['data'] = experimentInputsJson
            a['name'] = name
            return a

        print("Procedure ffbo.gfx.getExperiment Registered...")

        @FFBOLABClient.register('ffbo.gfx.getSVG')
        def get_svg(X):
            name = X
            with open(os.path.join(_FFBOLabDataPath, name + '.svg'),
                      "rb") as file:
                data = file.read()
            #experimentInputsJson = json.loads(experimentInputsJson)
            a = {}
            a['data'] = binascii.hexlify(data).decode()
            a['name'] = name
            return a

        print("Procedure ffbo.gfx.getSVG Registered...")

        @FFBOLABClient.register('ffbo.gfx.sendExperiment')
        def send_experiment(X):
            print(printHeader('FFBOLab Client GFX') + "sendExperiment called.")
            #X = json.loads(X)
            name = X['name']
            data = json.dumps(X['experiment'])
            with open(os.path.join(_FFBOLabDataPath, name + '.json'),
                      "w") as file:
                file.write(data)
            output = {}
            output['success'] = True
            print(
                printHeader('FFBOLab Client GFX') +
                "Experiment save successful.")
            return True

        print("Procedure ffbo.gfx.ffbolab.sendExperiment Registered...")

        @FFBOLABClient.register('ffbo.gfx.queryDB')
        def query_db(message):
            output = json.loads(message)
            self.log.info("query_db() called with {x}", x=output)
            return output

        print("Procedure ffbo.gfx.queryDB Registered...")

        @FFBOLABClient.register('ffbo.gfx.NKSim')
        def NK_sim(message):
            output = json.loads(message)
            self.log.info("NK_sim() called with {x}", x=output)
            self.NK_sim_state = 1
            return output

        print("Procedure ffbo.gfx.NKSim Registered...")

        @FFBOLABClient.register('ffbo.gfx.loadNeurogist')
        def load_neurogist(message):
            pass
            output = client.command(message)
            self.log.info(str(len(output)))
            #self.log.info(str((output[0].value)))
            self.log.info(str(json.dumps((output[0].oRecordData))))
            all_out = ""
            queries = []
            for i in output:
                if "tags" in i.oRecordData:
                    b = json.dumps(i.oRecordData)
                    queries.append(i.oRecordData)
                    self.log.info("load_neurogist() was called: returns {x}",
                                  x=b)
                    all_out = all_out + "\n" + str(b)
            return json.dumps(queries, indent=2)

        print("Procedure ffbo.gfx.loadNeurogist Registered...")

        @FFBOLABClient.register('ffbo.gfx.loadNeurobeholder')
        def load_neurobeholder(message):
            output = client.command(message)
            self.log.info(str(len(output)))
            #self.log.info(str((output[0].value)))
            self.log.info(str(json.dumps((output[0].oRecordData))))
            all_out = ""
            queries = []
            for i in output:
                if "tags" not in i.oRecordData:
                    b = json.dumps(i.oRecordData)
                    queries.append(i.oRecordData)
                    self.log.info(
                        "load_neurobeholder() was called: returns {x}", x=b)
                    all_out = all_out + "\n" + str(b)
            return json.dumps(queries, indent=2)

        print("Procedure ffbo.gfx.loadNeurobeholder Registered...")

        @FFBOLABClient.register('ffbo.gfx.loadNeuroscepter')
        def load_neurospecter(message):
            output = json.loads(message)
            self.log.info("load_neurospecter() called with {x}", x=output)
            return output

        print("Procedure ffbo.gfx.loadNeuroscepter Registered...")

        res = FFBOLABClient.session.call(u'ffbo.processor.server_information')

        for i in res['na']:
            if 'na' in res['na'][i]['name']:
                print('Found working NA Server: ' + res['na'][i]['name'])
                self.naServerID = i
        for i in res['nlp']:
            self.nlpServerID = i
Esempio n. 25
0
    def __init__(self,
                 ssl=True,
                 debug=True,
                 authentication=True,
                 user=u"ffbo",
                 secret=u"",
                 url=u'wss://neuronlp.fruitflybrain.org:7777/ws',
                 realm=u'realm1',
                 ca_cert_file='isrgrootx1.pem',
                 intermediate_cert_file='letsencryptauthorityx3.pem',
                 FFBOLabcomm=None):
        if os.path.exists(os.path.join(home, '.ffbolab', 'lib')):
            print(
                printHeader('FFBOLab Client') +
                "Downloading the latest certificates.")
            # CertificateDownloader = urllib.URLopener()
            if not os.path.exists(os.path.join(home, '.ffbolab', 'lib')):
                urlRetriever(
                    "https://data.flybrainlab.fruitflybrain.org/config/FBLClient.ini",
                    os.path.join(home, '.ffbolab', 'config', 'FBLClient.ini'))
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/isrgrootx1.pem",
                os.path.join(home, '.ffbolab', 'lib', 'caCertFile.pem'))
            urlRetriever(
                "https://data.flybrainlab.fruitflybrain.org/lib/letsencryptauthorityx3.pem",
                os.path.join(home, '.ffbolab', 'lib',
                             'intermediateCertFile.pem'))
            config_file = os.path.join(home, '.ffbolab', 'config',
                                       'FBLClient.ini')
            ca_cert_file = os.path.join(home, '.ffbolab', 'lib',
                                        'caCertFile.pem')
            intermediate_cert_file = os.path.join(home, '.ffbolab', 'lib',
                                                  'intermediateCertFile.pem')
        config = ConfigParser()
        config.read(config_file)
        # user = config["ComponentInfo"]["user"]
        # secret = config["ComponentInfo"]["secret"]
        # url = config["ComponentInfo"]["url"]
        self.FFBOLabcomm = FFBOLabcomm
        self.NKSimState = 0
        self.executionSettings = []
        extra = {'auth': authentication}
        self.lmsg = 0
        st_cert = open(ca_cert_file, 'rt').read()
        c = OpenSSL.crypto
        ca_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        st_cert = open(intermediate_cert_file, 'rt').read()
        intermediate_cert = c.load_certificate(c.FILETYPE_PEM, st_cert)
        certs = OpenSSLCertificateAuthorities([ca_cert, intermediate_cert])
        ssl_con = CertificateOptions(trustRoot=certs)

        FFBOLABClient = AutobahnSync()
        self.client = FFBOLABClient

        @FFBOLABClient.on_challenge
        def on_challenge(challenge):
            if challenge.method == u"wampcra":
                print("WAMP-CRA challenge received: {}".format(challenge))
                if u'salt' in challenge.extra:
                    # salted secret
                    salted_key = auth.derive_key(secret,
                                                 challenge.extra['salt'],
                                                 challenge.extra['iterations'],
                                                 challenge.extra['keylen'])
                    salted_key = (salted_key).decode('utf-8')
                    print(salted_key)
                #if user==u'ffbo':
                # plain, unsalted secret
                #    salted_key = u"kMU73GH4GS1WGUpEaSdDYwN57bdLdB58PK1Brb25UCE="
                #print(salted_key)
                # compute signature for challenge, using the key
                signature = auth.compute_wcs(salted_key,
                                             challenge.extra['challenge'])

                # return the signature to the router for verification
                return signature

            else:
                raise Exception("Invalid authmethod {}".format(
                    challenge.method))

        FFBOLABClient.run(url=url,
                          authmethods=[u'wampcra'],
                          authid=user,
                          ssl=ssl_con)
        self.client_data = []
        self.data = []
        self.launch_queue = []

        @FFBOLABClient.register('ffbo.nk.launch.' +
                                str(FFBOLABClient._async_session._session_id))
        def nk_launch_progressive(task, details=None):
            # print(task['user'])
            user_id = str(task['user'])
            self.launch_queue.append((user_id, task))

            def mock_result():
                result = {u'ydomain': 1, u'xdomain': 1, u'dt': 10, u'data': {}}
                """
                res = {u'ydomain': 1,
                          u'xdomain': 1,
                          u'dt': 10,
                          u'data': {}}
                print(task['forward'])
                res_to_processor = yield self.call(six.u(task['forward']), res)
                """
                return result, result

            res = mock_result()
            return res
            # res = server.launch(user_id, task)
            # returnValue(res)

        print("Procedure nk_launch_progressive Registered...")

        res = FFBOLABClient.session.call(
            u'ffbo.server.register', FFBOLABClient._async_session._session_id,
            'nk', 'nk_server')
        print("Registered self...")
 def test_no_auth_method(self, crossbar):
     wamp = AutobahnSync()
     with pytest.raises(AbortError) as exc:
         wamp.run(url=u'ws://localhost:8080/ws_auth',
                  authmethods=[u'wampcra'])
     assert str(exc.value.args[0].reason) == 'wamp.error.no_auth_method'
Esempio n. 27
0
 def test_already_running(self, crossbar):
     wamp = AutobahnSync()
     wamp.run()
     with pytest.raises(AlreadyRunningError):
         wamp.run()
Esempio n. 28
0
 def test_get_session(self, crossbar):
     wamp = AutobahnSync()
     with pytest.raises(NotRunningError) as exc:
         wamp.session
     assert str(exc.value.args[0]
                ) == 'No session available, is AutobahnSync running ?'
Esempio n. 29
0
 def test_connect(self, crossbar):
     wamp = AutobahnSync()
     wamp.run()
Esempio n. 30
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\')'