Skip to content
Snippets Groups Projects
Select Git revision
  • e5af0a7ff86126c564cb4d953bc9b0605b108fea
  • master default protected
  • fix-issue-901
  • fix-issue-928
  • fix-issue-896-zmq-publish
  • fix-issue-885
  • fix-issue-921
  • fix-910
  • fix-issue-804
  • srt-bandQ-receiver
  • fix-issue-855
  • stable
  • srt-bandW-receiver
  • fix-issue-805
  • feature-med-c-band-srv
  • fix-issue-760
  • fix-issue-628
  • fix-issue-588
  • fix-issue-derotator-328
  • OffsetReview
  • DerotatorAndMinorServo
  • discos1.0.6h
  • discos1.0.6f
  • discos1.0.6e
  • discos1.0.6d
  • discos1.0.6c
  • discos1.0.6b
  • discos1.0.6a
  • discos1.0.6
  • discos1.0.5
  • discos1.0.4
  • discos1.0.3
  • discos1.0.2
  • discos1.0.1
  • discos1.0.0
  • discos1.0-rc02
  • discos1.0-rc01
  • escs-0.5
  • escs-0.4
  • nuraghe-0.6
  • noto-0.1
41 results

ZMQPublisher.hpp

Blame
  • ZMQPublisher.hpp 2.20 KiB
    #ifndef __ZMQPUBLISHER_HPP__
    #define __ZMQPUBLISHER_HPP__
    
    #include <zmq.hpp>
    #include <zmq_addon.hpp>
    #include "ZMQDictionary.hpp"
    
    #define DEFAULT_ADDRESS std::string("127.0.0.1")
    #define DEFAULT_PORT    16001
    
    
    /**
     * This class implements a publisher object over a ZeroMQ socket, single topic only.
     * It exposes a publish method which sends a dictionary of key, value format over the ZMQ socket.
     */
    class ZMQPublisher
    {
    public:
        /**
         * Constructors. Initializes the ZMQPublisher object with the given topic, address and port.
         */
        ZMQPublisher(const std::string& topic, const std::string address, const unsigned int port);
        ZMQPublisher(const std::string& topic) : ZMQPublisher(topic, DEFAULT_ADDRESS, DEFAULT_PORT) {};
        ZMQPublisher(const std::string& topic, const std::string address) : ZMQPublisher(topic, address, DEFAULT_PORT) {};
        ZMQPublisher(const std::string& topic, const unsigned int port) : ZMQPublisher(topic, DEFAULT_ADDRESS, port) {};
    
        /**
         * Destructor.
         */
        ~ZMQPublisher();
    
        /**
         * Public publisher method. This method accepts a ZMQDictionary object reference,
         * it converts it to a json-ified string and calls the protected publisher method.
         * @param dictionary, a json-like dictionary containing tuples of key, value format.
         */
        virtual void publish(const ZMQDictionary& dictionary);
    
        /**
         * Name of the topic on which the messages will be sent.
         */
        const std::string topic;
    
    protected:
        /**
         * Protected publisher method. This method sends a tuple (topic, payload) over the ZMQ socket.
         * @param payload, a json-ified string.
         */
        virtual void publish(const std::string& payload);
    
    private:
        /**
         * ZMQ constant buffer which references the topic name.
         */
        const zmq::const_buffer m_topic;
    
        /**
         * ZMQ context shared pointer. We use a pointer since the context must not be destroyed for communications to work properly.
         */
        std::shared_ptr<zmq::context_t> m_context;
    
        /**
         * ZMQ socket shared pointer. We use a pointer since the socket must not be destroyed for communications to work properly.
         */
        std::shared_ptr<zmq::socket_t> m_socket;
    };
    
    
    #endif /*__ZMQPUBLISHER_HPP__*/