Commit fe47f46b authored by Giuseppe Carboni's avatar Giuseppe Carboni
Browse files

Issue #896, first implementation of ZMQ Publisher class

parent fc2c1f1b
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
#ifndef __PYZMQPUBLISHER_HPP__
#define __PYZMQPUBLISHER_HPP__

#include "ZMQPublisher.hpp"
#include <boost/python.hpp>

namespace py = boost::python;


/**
 * Wrapper for the above ZMQPublisher class in order to be used with boost_python.
 */
class PyZMQPublisher : public ZMQPublisher
{
public:
    using ZMQPublisher::ZMQPublisher;

    /**
     * Re-definition of the base protected publish method as public.
     * We cannot use "using" because it won't work with boost_python.
     * @param payload, a json-ified string.
     */
    void publish(const std::string& payload) override;
};


/**
 * Python module definition.
 */
BOOST_PYTHON_MODULE(libPyZMQPublisher)
{
    /**
     * Expose DEFAULT_ADDRESS and DEFAULT_PORT to the Python module.
     */
    py::object scope = py::scope();
    scope.attr("DEFAULT_ADDRESS") = DEFAULT_ADDRESS;
    scope.attr("DEFAULT_PORT") = DEFAULT_PORT;

    /**
     * Python PyZMQPublisher class definition. It is a wrapper of the PyZMQPublisher class defined above.
     * Unlike the PyZMQPublisher class, the "publish" method is exposed as a private method "__publish".
     * The actual "publish" method is defined inside the actual python module in order to convert a python dict() to a json-ified string.
     * Take a look there for more info.
     */
    py::class_<PyZMQPublisher>("PyZMQPublisher", py::init<const std::string, const std::string, const unsigned int>())
        .def("__publish", &PyZMQPublisher::publish)
        .def_readonly("topic", &ZMQPublisher::topic);
}

#endif /*__PYZMQPUBLISHER_HPP__*/
+8 −0
Original line number Diff line number Diff line
#ifndef __ZMQDICTIONARY_HPP__
#define __ZMQDICTIONARY_HPP__

#include <json.hpp>

using ZMQDictionary = nlohmann::json;

#endif /*__ZMQDICTIONARY_HPP__*/
+69 −0
Original line number Diff line number Diff line
#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__*/
+95 −0
Original line number Diff line number Diff line
#*******************************************************************************
# This Makefile follows VLT Standards (see Makefile(5) for more).
#*******************************************************************************
# REMARKS
#    None
#------------------------------------------------------------------------

#
# user definable C-compilation flags
#USER_CFLAGS = 

#
# additional include and library search paths
#USER_INC =
USER_LIB = /usr/local/lib64/libzmq.so

#
# MODULE CODE DESCRIPTION:
# ------------------------
# As a general rule:  public file are "cleaned" and "installed"  
#                     local (_L) are not "installed".

#
# C programs (public and local)
# -----------------------------
EXECUTABLES =
EXECUTABLES_L =


PY_PACKAGES = ZMQPublisher
#PY_SCRIPTS =


#
# <brief description of xxxxx program>


#
# Includes (.h) files (public only)
# ---------------------------------
INCLUDES = ZMQDictionary.hpp ZMQPublisher.hpp PyZMQPublisher.hpp

#
# Libraries (public and local)
# ----------------------------
LIBRARIES = ZMQPublisher PyZMQPublisher
LIBRARIES_L =

ZMQPublisher_OBJECTS = ZMQPublisher

PyZMQPublisher_OBJECTS = PyZMQPublisher
PyZMQPublisher_LIBS = ZMQPublisher boost_python3


#
# list of all possible C-sources (used to create automatic dependencies)
# ------------------------------
CSOURCENAMES = \
	$(foreach exe, $(EXECUTABLES) $(EXECUTABLES_L), $($(exe)_OBJECTS)) \
	$(foreach rtos, $(RTAI_MODULES) , $($(rtos)_OBJECTS)) \
	$(foreach lib, $(LIBRARIES) $(LIBRARIES_L), $($(lib)_OBJECTS))

#
#>>>>> END OF standard rules

#
# INCLUDE STANDARDS
# -----------------

MAKEDIRTMP := $(shell searchFile include/acsMakefile)
ifneq ($(MAKEDIRTMP),\#error\#)
   MAKEDIR := $(MAKEDIRTMP)/include
   include $(MAKEDIR)/acsMakefile
endif

#
# TARGETS
# -------
all:	do_all
	@echo " . . . 'all' done" 

clean : clean_all 
	@echo " . . . clean done"

clean_dist : clean_all clean_dist_all 
	@echo " . . . clean_dist done"

man   : do_man 
	@echo " . . . man page(s) done"

install : install_all
	@echo " . . . installation done"


#___oOo___
+6 −0
Original line number Diff line number Diff line
#include "PyZMQPublisher.hpp"

void PyZMQPublisher::publish(const std::string& payload)
{
    ZMQPublisher::publish(payload);
}
Loading