server.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <library/cpp/messagebus/test/example/common/proto.h>
  2. using namespace NBus;
  3. using namespace NCalculator;
  4. namespace NCalculator {
  5. struct TCalculatorServer: public IBusServerHandler {
  6. TCalculatorProtocol Proto;
  7. TBusMessageQueuePtr MessageQueue;
  8. TBusServerSessionPtr ServerSession;
  9. TCalculatorServer() {
  10. MessageQueue = CreateMessageQueue();
  11. TBusServerSessionConfig config;
  12. ServerSession = TBusServerSession::Create(&Proto, this, config, MessageQueue);
  13. }
  14. ~TCalculatorServer() override {
  15. MessageQueue->Stop();
  16. }
  17. void OnMessage(TOnMessageContext& request) override {
  18. if (request.GetMessage()->GetHeader()->Type == TRequestSum::MessageType) {
  19. TRequestSum* requestSum = VerifyDynamicCast<TRequestSum*>(request.GetMessage());
  20. int a = requestSum->Record.GetA();
  21. int b = requestSum->Record.GetB();
  22. int result = a + b;
  23. Cerr << "requested " << a << " + " << b << ", sending " << result << "\n";
  24. TAutoPtr<TResponse> response(new TResponse);
  25. response->Record.SetResult(result);
  26. request.SendReplyMove(response);
  27. } else if (request.GetMessage()->GetHeader()->Type == TRequestMul::MessageType) {
  28. TRequestMul* requestMul = VerifyDynamicCast<TRequestMul*>(request.GetMessage());
  29. int a = requestMul->Record.GetA();
  30. int b = requestMul->Record.GetB();
  31. int result = a * b;
  32. Cerr << "requested " << a << " * " << b << ", sending " << result << "\n";
  33. TAutoPtr<TResponse> response(new TResponse);
  34. response->Record.SetResult(result);
  35. request.SendReplyMove(response);
  36. } else {
  37. Y_ABORT("unknown request");
  38. }
  39. }
  40. };
  41. }
  42. int main(int, char**) {
  43. TCalculatorServer server;
  44. Cerr << "listening on port " << server.ServerSession->GetActualListenPort() << "\n";
  45. for (;;) {
  46. Sleep(TDuration::Seconds(1));
  47. }
  48. return 0;
  49. }