diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | c2a1af049e9deca890e9923abe64fe6c59060348 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/messagebus/test/example/client/client.cpp | |
parent | 1f553f46fb4f3c5eec631352cdd900a0709016af (diff) | |
download | ydb-c2a1af049e9deca890e9923abe64fe6c59060348.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/messagebus/test/example/client/client.cpp')
-rw-r--r-- | library/cpp/messagebus/test/example/client/client.cpp | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/library/cpp/messagebus/test/example/client/client.cpp b/library/cpp/messagebus/test/example/client/client.cpp index 3bd9a6f768..89b5f2c9be 100644 --- a/library/cpp/messagebus/test/example/client/client.cpp +++ b/library/cpp/messagebus/test/example/client/client.cpp @@ -1,81 +1,81 @@ #include <library/cpp/messagebus/test/example/common/proto.h> -#include <util/random/random.h> - -using namespace NBus; -using namespace NCalculator; - -namespace NCalculator { +#include <util/random/random.h> + +using namespace NBus; +using namespace NCalculator; + +namespace NCalculator { struct TCalculatorClient: public IBusClientHandler { - TCalculatorProtocol Proto; - TBusMessageQueuePtr MessageQueue; - TBusClientSessionPtr ClientSession; - - TCalculatorClient() { - MessageQueue = CreateMessageQueue(); - TBusClientSessionConfig config; - config.TotalTimeout = 2 * 1000; - ClientSession = TBusClientSession::Create(&Proto, this, config, MessageQueue); - } - + TCalculatorProtocol Proto; + TBusMessageQueuePtr MessageQueue; + TBusClientSessionPtr ClientSession; + + TCalculatorClient() { + MessageQueue = CreateMessageQueue(); + TBusClientSessionConfig config; + config.TotalTimeout = 2 * 1000; + ClientSession = TBusClientSession::Create(&Proto, this, config, MessageQueue); + } + ~TCalculatorClient() override { - MessageQueue->Stop(); - } - + MessageQueue->Stop(); + } + void OnReply(TAutoPtr<TBusMessage> request, TAutoPtr<TBusMessage> response0) override { Y_VERIFY(response0->GetHeader()->Type == TResponse::MessageType, "wrong response"); - TResponse* response = VerifyDynamicCast<TResponse*>(response0.Get()); - if (request->GetHeader()->Type == TRequestSum::MessageType) { - TRequestSum* requestSum = VerifyDynamicCast<TRequestSum*>(request.Get()); - int a = requestSum->Record.GetA(); - int b = requestSum->Record.GetB(); - Cerr << a << " + " << b << " = " << response->Record.GetResult() << "\n"; - } else if (request->GetHeader()->Type == TRequestMul::MessageType) { - TRequestMul* requestMul = VerifyDynamicCast<TRequestMul*>(request.Get()); - int a = requestMul->Record.GetA(); - int b = requestMul->Record.GetB(); - Cerr << a << " * " << b << " = " << response->Record.GetResult() << "\n"; - } else { + TResponse* response = VerifyDynamicCast<TResponse*>(response0.Get()); + if (request->GetHeader()->Type == TRequestSum::MessageType) { + TRequestSum* requestSum = VerifyDynamicCast<TRequestSum*>(request.Get()); + int a = requestSum->Record.GetA(); + int b = requestSum->Record.GetB(); + Cerr << a << " + " << b << " = " << response->Record.GetResult() << "\n"; + } else if (request->GetHeader()->Type == TRequestMul::MessageType) { + TRequestMul* requestMul = VerifyDynamicCast<TRequestMul*>(request.Get()); + int a = requestMul->Record.GetA(); + int b = requestMul->Record.GetB(); + Cerr << a << " * " << b << " = " << response->Record.GetResult() << "\n"; + } else { Y_FAIL("unknown request"); - } - } - + } + } + void OnError(TAutoPtr<TBusMessage>, EMessageStatus status) override { - Cerr << "got error " << status << "\n"; - } - }; - -} - -int main(int, char**) { - TCalculatorClient client; - - for (;;) { - TNetAddr addr(TNetAddr("127.0.0.1", TCalculatorProtocol().GetPort())); - - int a = RandomNumber<unsigned>(10); - int b = RandomNumber<unsigned>(10); - EMessageStatus ok; - if (RandomNumber<bool>()) { - TAutoPtr<TRequestSum> request(new TRequestSum); - request->Record.SetA(a); - request->Record.SetB(b); - Cerr << "sending " << a << " + " << b << "\n"; - ok = client.ClientSession->SendMessageAutoPtr(request, &addr); - } else { - TAutoPtr<TRequestMul> request(new TRequestMul); - request->Record.SetA(a); - request->Record.SetB(b); - Cerr << "sending " << a << " * " << b << "\n"; - ok = client.ClientSession->SendMessageAutoPtr(request, &addr); - } - - if (ok != MESSAGE_OK) { - Cerr << "failed to send message " << ok << "\n"; - } - - Sleep(TDuration::Seconds(1)); - } - - return 0; -} + Cerr << "got error " << status << "\n"; + } + }; + +} + +int main(int, char**) { + TCalculatorClient client; + + for (;;) { + TNetAddr addr(TNetAddr("127.0.0.1", TCalculatorProtocol().GetPort())); + + int a = RandomNumber<unsigned>(10); + int b = RandomNumber<unsigned>(10); + EMessageStatus ok; + if (RandomNumber<bool>()) { + TAutoPtr<TRequestSum> request(new TRequestSum); + request->Record.SetA(a); + request->Record.SetB(b); + Cerr << "sending " << a << " + " << b << "\n"; + ok = client.ClientSession->SendMessageAutoPtr(request, &addr); + } else { + TAutoPtr<TRequestMul> request(new TRequestMul); + request->Record.SetA(a); + request->Record.SetB(b); + Cerr << "sending " << a << " * " << b << "\n"; + ok = client.ClientSession->SendMessageAutoPtr(request, &addr); + } + + if (ok != MESSAGE_OK) { + Cerr << "failed to send message " << ok << "\n"; + } + + Sleep(TDuration::Seconds(1)); + } + + return 0; +} |