boost based Publisher/Subscriber framework in C++

Find full implementation https://github.com/Anil8753/CppPubSub
Boost based Publisher/Subscriber framework in C++. One can subscribe for a channel and another can publish the notification on the same channel.
Note: It has the dependency on boost. Please download boost library and set the boost path in project settings (CppPubSub.vcxproj). I tested on Windows only, but it should be portable to other platforms as well. Full sample code is available in Source.cpp.

Usage:


// you should create a singleton object of NotificationService class, make it accessible throughout your application. 
INotificationService* pNotificationService = new NotificationService();

// Subscribe for the event.
function<NotificationHandler> fnNotificationHandler
    = bind(&SubscriberClass::NotificationHandlerFunction, this, std::placeholders::_1);

subscriptionToken = pNotificationService->Subscribe("TEST_CHANEL", fnNotificationHandler);

// Publish event
NotificationData _data;
_data["data1"] = "Hello";
_data["data2"] = "World";
pNotificationService->Publish("TEST_CHANEL", _data);

// Unsubscribe event.
pNotificationService->Unsubscribe(subscriptionToken);


Full Usage:

#include "windows.h"
#include "NotificationService.h"

#define TEST_CHANEL "TestChanel"

typedef boost::shared_ptr<INotificationService> INotificationServicePtr;

class SubscriberClass
{
private:
    INotificationServicePtr m_pNotificationService;
    SubscriptionToken       m_SubscriptionToken;

public:
    SubscriberClass(INotificationServicePtr pNotificationService)
        :m_pNotificationService(pNotificationService)
    {
        SubscribeForNotiications();
    }

    ~SubscriberClass()
    {
        m_pNotificationService->Unsubscribe(m_SubscriptionToken);
    }

    void SubscribeForNotiications()
    {
        function<NotificationHandler> fnNotificationHandler
            = bind(&SubscriberClass::NotificationHandlerFunction, this, std::placeholders::_1);

        m_SubscriptionToken =
            m_pNotificationService->Subscribe(TEST_CHANEL, fnNotificationHandler);
    }

    void NotificationHandlerFunction(const NotificationData& data)
    {
        NotificationData _data = data;

        cout << _data["data1"] << endl;
        cout << _data["data2"] << endl;
        cout << "------------" << endl;
    }
};

class PublisherClass
{
public:
    PublisherClass(INotificationServicePtr pNotificationService)
        :m_pNotificationService(pNotificationService)
    {
        PublishNotifications();
    }

    ~PublisherClass()
    {
    }

private:
    void PublishNotifications()
    {
        INotificationServicePtr pNotificationService = m_pNotificationService;

        thread publishThread = std::thread{ [pNotificationService]() {

            while (true)
            {
                NotificationData _data;
                _data["data1"] = "Hello";
                _data["data2"] = "World";

                pNotificationService->Publish(TEST_CHANEL, _data);

                std::this_thread::sleep_for(std::chrono::milliseconds(500));
            }
        } };

        publishThread.detach();
    }

    INotificationServicePtr m_pNotificationService;
};

int main()
{
    INotificationServicePtr     pNotificationService(NotificationService::GetNotificationService());
    SubscriberClass             objSubscriberClass(pNotificationService);
    PublisherClass              objPublisherClass(pNotificationService);

    getchar();

    return 0;
}


1 comments :

Casino Bonus Codes - December 2021
No deposit bonus casino 바카라 promotions. We recommend sporting100 2021 casino bonus codes and promos for poormansguidetocasinogambling.com new players. We also www.jtmhub.com list new casino bonuses https://octcasino.com/ for December 2021.


Emoticon Emoticon