How to call a remote procedure within subscription event handler

#1

Hi all,

I’ve been experimenting AutobahnCpp for some time and, by looking at the examples, I was wondering if it is possible to call a remote procedure while handling subscription events. To help you understand what I wish to do, here is some code I put together (based on subscribe1 and call1 examples) for my subscriber-caller app. My goal is to call a remote procedure whenever an event (after a topic gets published) occurs.

Running this code always blocks my application in the com.myapp.topic1 handler and I never get the RPC result. I wonder if someone has any explication for this behavior or if it is a limitation.

#include

#include “autobahn.hpp”

#include <boost/asio.hpp>

using namespace std;

using namespace boost;

using namespace autobahn;

using boost::asio::ip::tcp;

int main ()

{

**try**

{

    // ASIO

service object

    //

    asio::io_service io;



    // the TCP

socket we connect

    //

    tcp::socket socket(io);



    // connect

to this server/port

    //

    tcp::resolver resolver(io);

    **auto** endpoint_iterator = resolver.resolve({"127.0.0.1", "8090"});



    // create a

WAMP session that talks over TCP

    //

    **bool** debug = **false**;

    autobahn::session<tcp::socket, tcp::socket> session(io, socket, socket,

debug);

    // make

sure the future returned from the session joining a realm (see below)

    // does not

run out of scope (being destructed prematurely …)

    //

    future<**void**> session_future;



    // now do

an asynchronous connect …

    //

    boost::asio::async_connect(socket,

endpoint_iterator,

    // we

either connected or an error happened during connect …

    //

    [&](boost::system::error_code ec, tcp::resolver::iterator)

    {

        **if** (!ec)

        {

            cerr << "Connected to server"

<< endl;

            // start

the WAMP session on the transport that has been connected

            //

            session.start();



            //

join a realm with the WAMP session

            //

            session_future = session.join("realm1").then([&](future<uint64_t> s)

            {

                cerr << "Session joined to realm with session ID " << s.get() << **endl**;

               

                **auto** f1 = session.subscribe("com.myapp.topic1", [](**const** anyvec& args, **const** anymap& kwargs)

                {

                    cerr << "Got event: " <<

any_cast<uint64_t>(args[0]) << endl;

                    // call a remote procedure ..

                    //

                    **auto** c1 = session.call("com.mathservice.add2",

{any_cast<uint64_t>(args[0]), 777}).then([&](future
f)

                    {

                        // call result received

                        //

                        std::cerr << "Got RPC result " <<

any_cast<uint64_t> (f.get()) << std::endl;

                    });



                    c1.then([&](**decltype**(c1))

                    {

                        // leave the session and stop I/O loop

                        //

                        session.leave().then([&](future<string> reason)

                        {

                            std::cerr

<< “Session left (” << reason.get() << “)” << endl;

                            io.**stop**();

                        }).wait();

                    });

               

                }).then([](future<subscription> sub)

                {

                    cerr << "Subscribed with subscription ID " << sub.get().id << **endl**;

                });



                f1.wait();

            });

        }

        **else**

        {

            cerr << "Could not connect to server: " << ec.message() << **endl**;

        }

    });



    cerr << "Starting ASIO I/O loop .." << **endl**;



    io.**run**();

   

    cerr << "ASIO I/O loop ended"

<< endl;

}

**catch** (std::exception& e)

{

    cerr << e.**what**() << **endl**;

    **return** 1;

}

**return** 0;

}

Regards,

François

0 Likes