Tntpub

Tntpub is a message bus, which is based on the serialization framework of cxxtools. It is very simple. There is no configuration needed other that a TCP port, where the server should listen and the client should connect to.

Messages are C++ objects, which are serialized and deseralized with the cxxtools serialization operators.

Tntpub implements a server and client. A client may send messages to a topic and listen on a topic. All clients, which listen on the topic will receive the message. A topic is just a name. There is no configuration, where the topics must be registered. Messages are just sent to all clients, which are subscribed to the topic of the message.

The server is either standalone or runs in a cxxtools event loop. In a event loop it runs non blocking.

Example

Pub client reads messages from one or more topics:

#include "mymessage.h"

#include <tntpub/client.h>

#include <cxxtools/json.h>
#include <cxxtools/arg.h>

#include <iostream>

int main(int argc, char* argv[])
{
try
{
    // initialize logging (read `log.properties` or `log.xml`)
    log_init();

    // accept arguments -i for liste ip and -p for port
    cxxtools::Arg<std::string> ip(argc, argv, 'i');
    cxxtools::Arg<unsigned short> port(argc, argv, 'p', 9001);

    // create pub client application
    tntpub::Client client(ip, port);

    // subscribe to topics passed as arguments
    for (int a = 1; a < argc; ++a)
    client.subscribe(argv[a]);

    // since output is buffered we need to force sending
    // the subscribe message
    client.flush();

    while (true)
    {
    // wait for a message and deserialize it
    MyMessage msg;
    client.readMessage().get(msg);

    // output in json format
    std::cout << cxxtools::Json(msg).beautify(true);
    }
}
catch (const std::exception& e)
{
    std::cerr << e.what() << std::endl;
    return 1;
}
}

Pub client sends messages to a topic

#include <tntpub/client.h>

#include <cxxtools/json.h>
#include <cxxtools/serializationinfo.h>
#include <cxxtools/arg.h>
#include <cxxtools/log.h>

#include <iostream>

struct MyMessage
{
std::string text;
unsigned number;
};

inline void operator<<= (cxxtools::SerializationInfo& si, const MyMessage& msg)
{
si.setTypeName("MyMessage");
si.addMember("text") <<= msg.text;
si.addMember("number") <<= msg.number;
}

inline void operator>>= (const cxxtools::SerializationInfo& si, MyMessage& msg)
{
si.getMember("text") >>= msg.text;
si.getMember("number") >>= msg.number;
}

int main(int argc, char* argv[])
{
try
{
    log_init();

    cxxtools::Arg<std::string> ip(argc, argv, 'i');
    cxxtools::Arg<unsigned short> port(argc, argv, 'p', 9001);

    if (argc != 2)
    {
    std::cerr << "usage: " << argv[0] << " [-i ip] [-p port] topic\n";
    return 1;
    }

    std::string topic = argv[1];

    // create pub client application
    tntpub::Client client(ip, port);

    MyMessage msg;
    msg.text = "Hello";
    msg.number = 42;

    client.sendMessage(topic, msg);
    client.flush();
}
catch (const std::exception& e)
{
    std::cerr << e.what() << std::endl;
}
}