aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus_test/src/tests/trace/cpp-server.cpp
blob: f151286edf049d16d4223020afcc144ccd0252d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/testlib/simpleprotocol.h>
#include <vespa/messagebus/rpcmessagebus.h>
#include <vespa/messagebus/network/rpcnetworkparams.h>
#include <vespa/messagebus/emptyreply.h>
#include <vespa/vespalib/util/time.h>
#include <thread>
#include <vespa/vespalib/util/signalhandler.h>

using namespace mbus;

class Server : public IMessageHandler,
               public IReplyHandler
{
private:
    IntermediateSession::UP _session;
    std::string             _name;
public:
    Server(MessageBus &bus, const std::string &name);
    ~Server();
    void handleMessage(Message::UP msg) override;
    void handleReply(Reply::UP reply) override;
};

Server::Server(MessageBus &bus, const std::string &name)
    : _session(bus.createIntermediateSession("session", true, *this, *this)),
      _name(name)
{
    fprintf(stderr, "cpp server started: %s\n", _name.c_str());
}

Server::~Server()
{
    _session.reset();
}

void
Server::handleMessage(Message::UP msg) {
    msg->getTrace().trace(1, _name + " (message)", false);
    if (!msg->getRoute().hasHops()) {
        fprintf(stderr, "**** Server '%s' replying.\n", _name.c_str());
        auto reply = std::make_unique<EmptyReply>();
        msg->swapState(*reply);
        handleReply(std::move(reply));
    } else {
        fprintf(stderr, "**** Server '%s' forwarding message.\n", _name.c_str());
        _session->forward(std::move(msg));
    }
}

void
Server::handleReply(Reply::UP reply) {
    reply->getTrace().trace(1, _name + " (reply)", false);
    _session->forward(std::move(reply));
}

class App
{
public:
    int main(int argc, char **argv);
};

int
App::main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr, "usage: %s <service-prefix>\n", argv[0]);
        return 1;
    }
    RPCMessageBus mb(ProtocolSet().add(std::make_shared<SimpleProtocol>()),
                     RPCNetworkParams(config::ConfigUri("file:slobrok.cfg"))
                     .setIdentity(Identity(argv[1])),
                     config::ConfigUri("file:routing.cfg"));
    Server server(mb.getMessageBus(), argv[1]);
    while (true) {
        std::this_thread::sleep_for(1s);
    }
    return 0;
}

int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    App app;
    return app.main(argc, argv);
}