aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/test/example/server/server.cpp
blob: 1d065c1ef665436c74507f4615a73c6f3bf30f84 (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
#include <library/cpp/messagebus/test/example/common/proto.h>

using namespace NBus;
using namespace NCalculator;

namespace NCalculator {
    struct TCalculatorServer: public IBusServerHandler { 
        TCalculatorProtocol Proto;
        TBusMessageQueuePtr MessageQueue;
        TBusServerSessionPtr ServerSession;

        TCalculatorServer() {
            MessageQueue = CreateMessageQueue();
            TBusServerSessionConfig config;
            ServerSession = TBusServerSession::Create(&Proto, this, config, MessageQueue);
        }

        ~TCalculatorServer() override {
            MessageQueue->Stop();
        }

        void OnMessage(TOnMessageContext& request) override {
            if (request.GetMessage()->GetHeader()->Type == TRequestSum::MessageType) {
                TRequestSum* requestSum = VerifyDynamicCast<TRequestSum*>(request.GetMessage());
                int a = requestSum->Record.GetA();
                int b = requestSum->Record.GetB();
                int result = a + b;
                Cerr << "requested " << a << " + " << b << ", sending " << result << "\n";
                TAutoPtr<TResponse> response(new TResponse);
                response->Record.SetResult(result);
                request.SendReplyMove(response);
            } else if (request.GetMessage()->GetHeader()->Type == TRequestMul::MessageType) {
                TRequestMul* requestMul = VerifyDynamicCast<TRequestMul*>(request.GetMessage());
                int a = requestMul->Record.GetA();
                int b = requestMul->Record.GetB();
                int result = a * b;
                Cerr << "requested " << a << " * " << b << ", sending " << result << "\n";
                TAutoPtr<TResponse> response(new TResponse);
                response->Record.SetResult(result);
                request.SendReplyMove(response);
            } else {
                Y_FAIL("unknown request");
            }
        }
    };
} 

int main(int, char**) {
    TCalculatorServer server;

    Cerr << "listening on port " << server.ServerSession->GetActualListenPort() << "\n";

    for (;;) {
        Sleep(TDuration::Seconds(1));
    }

    return 0;
}