di9it / msghub

Lightweight publisher/subscriber C++ library based on boost asio
21 stars 8 forks source link

Issue in data publishing #1

Open rakshita1006 opened 3 years ago

rakshita1006 commented 3 years ago

Hi,

I am trying to create a pub/sub model using BoostAsio library with the help of your code.

I was able to establish a connection between the publisher and subscriber but the data is neither [published nor subscribe.

Could you please help me with the issue.

Thanks & Regards Rakshita Parihar

rakshita1006 commented 3 years ago

Please help me with the solution!!!

sehe commented 3 years ago

Please help me with the solution!!!

Heads up: shouting isn't help. Nobody is obliged to solve your issues in any time frame. This is just a free project that you opt to use. I'm not a maintainer here, but I thought I'd let you know.

sehe commented 3 years ago

ON TOPIC: This question was raised on StackOverflow and I analyzed its cause.

The problem is that msghub_impl does not own the io service, YET insists on starting service threads (even when threads==0). This causes the service to complete all work before even posting the first async task, and a subsequent run() (e.g. from main()) simply has no effect (as per the documentation, the same applies to io_service in older versions.).

rakshita1006 commented 3 years ago

Hi, Thanks for the reply I tried to do all the changes in my code as suggested in above files but still data is not published.

sehe commented 3 years ago

@rakshita1006 can you run the tests? What are you doing different/y? Note also, I have made a fork that has some simplifications in the API. It is still on-the-wire compatible though (until this: https://github.com/sehe/msghub/issues/1)

rakshita1006 commented 3 years ago

Hi,

I am not sure what wrong I am doing .I am using Ubuntu 18.0 with boost 1.65.0. I downloaded the code(zipfolder) and created by own clientmain.cpp & servermain.cpp. I included all the relevant header & Library files and compiled the code.The compilation was successful but i am not getting published message.

I also made the necessary changes as suggested by you in fix issue #1,#3 ,#4 and try to run the code.

client code:

include <boost/asio.hpp>

include

using namespace std::chrono_literals;

int main() { boost::asio::io_service io_service; boost::asio::io_service::work work(io_service); { msghub msghub(io_service);

    msghub.connect("localhost", 1334);
    assert(!io_service.stopped()); // no longer fails due to `work`
    msghub.publish("Publish", "new message");
    io_service.run_one();
}
io_service.run();

}

Server Code: void on_message(const std::string& topic, std::vector& message) { // handle message }

int main()
{
    boost::asio::io_service io_service;
    // Create hub to listen on 0xbee port
    msghub msghub(io_service);
    msghub.create(1334);
    // Subscribe on "any topic"
    msghub.subscribe("Publish", on_message);
    // Current or any another client
    //msghub.publish("Publish", "new message");
    io_service.run();
}

Please find the attched file of the output Output server: output1 client: output2

U ran the code with the same code as here.

Is it the correct way to go or I am missing some step?Does we need to execute code from test folder.if yes then how as there is no main function in that?

Thanks Rakshita

sehe commented 3 years ago

@rakshita1006

What happens when you check out the code from the PR branch and run the tests?

I've added another commit that you can use instead of manually doing the changes: https://github.com/di9it/msghub/pull/7

You can use it from my repo, e.g. in a docker:

docker run --rm -ti ubuntu:20.04
apt update
apt -qqyy install build-essential git cmake libboost-all-dev
git clone https://github.com/sehe/msghub -b cmake
cd msghub
cmake . && make
./examples/server & ./examples/client ; kill %1; wait

The tester has a main function (it's in main.cpp) and depends on Boost Unit Test framework.. is also built by the CMake

sehe commented 3 years ago

Making it even easier, I reduced CMake and Boost requirements to support 18.04 again, and tested with a Dockerfile:

FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN \
        apt update; \
        apt -qqyy install build-essential git cmake libboost-all-dev

RUN git clone https://github.com/sehe/msghub --depth 1 -b cmake
WORKDIR /msghub
RUN cmake . && make

CMD ["/bin/bash", "-c", "(./examples/server & ./examples/client ; kill %1; wait)"]

Put this Dockerfile somewhere and build it:

docker build . -f containers/issue1 -t msghub:cmake

To do the simple client/server test:

docker run --rm -it msghub:cmake

There are shutdown races impactng the unit tests. I think they're all fixed on my other branch sehe/msghub: https://github.com/sehe/msghub/blob/std-over-boost/containers/std-over-boost

Live demo of the cmake container : small-cmake

Live demo of the std-over-boost branch which fixes a lot more issues and modernizes the code + interfaces:

small-std-over-boost

rakshita1006 commented 3 years ago

@sehe It finally worked for me. Thank you for your help.