aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus_test/src/tests/speed/cpp-server.cpp
blob: 3d29f5641a2d8b726f5da866338d5d19f50933d0 (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
// Copyright 2016 Yahoo Inc. 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/simplemessage.h>
#include <vespa/messagebus/testlib/simplereply.h>
#include <vespa/messagebus/testlib/simpleprotocol.h>
#include <vespa/messagebus/rpcmessagebus.h>
#include <vespa/fastos/app.h>

using namespace mbus;

class Server : public IMessageHandler
{
private:
    DestinationSession::UP _session;
public:
    Server(MessageBus &bus);
    ~Server();
    void handleMessage(Message::UP msg) override;
};

Server::Server(MessageBus &bus)
    : _session(bus.createDestinationSession("session", true, *this))
{
    fprintf(stderr, "cpp server started\n");
}

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

void
Server::handleMessage(Message::UP msg) {
    if ((msg->getProtocol() == SimpleProtocol::NAME)
        && (msg->getType() == SimpleProtocol::MESSAGE)
        && (static_cast<SimpleMessage&>(*msg).getValue() == "message"))
    {
        Reply::UP reply(new SimpleReply("OK"));
        msg->swapState(*reply);
        _session->reply(std::move(reply));
    } else {
        Reply::UP reply(new SimpleReply("FAIL"));
        msg->swapState(*reply);
        _session->reply(std::move(reply));
    }
}

class App : public FastOS_Application
{
public:
    int Main() override;
};

int
App::Main()
{
    RPCMessageBus mb(ProtocolSet().add(IProtocol::SP(new SimpleProtocol())),
                     RPCNetworkParams()
                     .setIdentity(Identity("server/cpp"))
                     .setSlobrokConfig("file:slobrok.cfg"),
                     "file:routing.cfg");
    Server server(mb.getMessageBus());
    while (true) {
        FastOS_Thread::Sleep(1000);
    }
    return 0;
}

int main(int argc, char **argv) {
    App app;
    return app.Entry(argc, argv);
}