Samchon Framework for CPP  1.0.0
CalculatorApplication.hpp
1 #pragma once
2 #include <samchon/protocol/IProtocol.hpp>
3 
4 #include <iostream>
5 #include <functional>
6 #include <memory>
7 
8 #include <samchon/library/StringUtil.hpp>
9 #include <samchon/protocol/ServerConnector.hpp>
10 
11 namespace samchon
12 {
13 namespace examples
14 {
15 namespace calculator
16 {
17  class CalculatorApplication
18  : public virtual protocol::IProtocol
19  {
20  private:
21  std::shared_ptr<protocol::ServerConnector> connector;
22 
23  public:
24  /* ------------------------------------------------------------------
25  CONSTRUCTORS
26  ------------------------------------------------------------------ */
27  CalculatorApplication()
28  {
29  connector.reset(new protocol::ServerConnector(this));
30  connector->connect("127.0.0.1", 17823);
31  };
32  virtual ~CalculatorApplication() = default;
33 
34  /* ------------------------------------------------------------------
35  INVOKE MESSAGE CHAIN
36  ------------------------------------------------------------------ */
37  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
38  {
39  connector->sendData(invoke);
40  };
41 
42  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
43  {
44  std::function<void(double, double, double)> listener;
45  double x = invoke->at(0)->getValue<double>();
46  double y = invoke->at(1)->getValue<double>();;
47  double ret = invoke->at(2)->getValue<double>();
48 
49  if (invoke->getListener() == "printPlus")
50  listener = std::bind(&CalculatorApplication::printPlus, this);
51  else if (invoke->getListener() == "printMinus")
52  listener = std::bind(&CalculatorApplication::printMinus, this);
53  else if (invoke->getListener() == "printMultiply")
54  listener = std::bind(&CalculatorApplication::printMultiply, this);
55  else if (invoke->getListener() == "printDivide")
56  listener = std::bind(&CalculatorApplication::printDivide, this);
57 
58  listener(x, y, ret);
59  };
60 
61  private:
62  /* ------------------------------------------------------------------
63  PROCEDURES
64  ------------------------------------------------------------------ */
65  void printPlus(double x, double y, double ret)
66  {
67  std::cout <<
68  library::StringUtil::substitute("{1} + {2} = {3}", x, y, ret)
69  << std::endl;
70  };
71  void printMinus(double x, double y, double ret)
72  {
73  std::cout <<
74  library::StringUtil::substitute("{1} - {2} = {3}", x, y, ret)
75  << std::endl;
76  };
77  void printMultiply(double x, double y, double ret)
78  {
79  std::cout <<
80  library::StringUtil::substitute("{1} * {2} = {3}", x, y, ret)
81  << std::endl;
82  };
83  void printDivide(double x, double y, double ret)
84  {
85  std::cout <<
86  library::StringUtil::substitute("{1} / {2} = {3}", x, y, ret)
87  << std::endl;
88  };
89 
90  public:
91  /* ------------------------------------------------------------------
92  STATIC MAIN
93  ------------------------------------------------------------------ */
94  static void main()
95  {
96  CalculatorApplication();
97  };
98  };
99 };
100 };
101 };
virtual void replyData(std::shared_ptr< Invoke >)=0
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:54
virtual void sendData(std::shared_ptr< Invoke >)=0