Samchon Framework for CPP  1.0.0
Client.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/protocol/IProtocol.hpp>
5 #include <samchon/templates/service/Service.hpp>
6 
7 #include <samchon/protocol/WebClientDriver.hpp>
8 
9 #define KEEP_CLIENT_ALIVE auto &__keeper = __keep_alive();
10 
11 namespace samchon
12 {
13 namespace templates
14 {
15 namespace service
16 {
17  class Server;
18  class User;
19 
38  class Client
39  : public virtual protocol::IProtocol
40  {
41  friend class Server;
42 
43  private:
44  // RELATED OBJECTS
45  User *user;
46 
47  std::shared_ptr<protocol::WebClientDriver> driver;
48  std::unique_ptr<Service> service;
49 
50  // KEY
51  size_t no;
52 
53  public:
54  /* ---------------------------------------------------------
55  CONSTRUCTORS
56  --------------------------------------------------------- */
63  Client(User *user, std::shared_ptr<protocol::WebClientDriver> driver)
64  {
65  this->user = user;
66  this->driver = driver;
67  };
68 
75  virtual ~Client() = default;
76 
77  protected:
84  virtual auto createService(const std::string &) -> Service* = 0;
85 
86  auto __keep_alive() const -> std::pair<std::shared_ptr<User>, std::shared_ptr<Client>>
87  {
88  return std::make_pair(user_weak_ptr.lock(), my_weak_ptr.lock());
89  };
90 
91  public:
92  /* ---------------------------------------------------------
93  ACCESSORS
94  --------------------------------------------------------- */
102  auto getUser() const -> User*
103  {
104  return user;
105  };
106 
112  auto getService() const -> Service*
113  {
114  return service.get();
115  };
116 
125  auto getNo() const -> size_t
126  {
127  return no;
128  };
129 
135  void changeService(const std::string &path)
136  {
137  service.reset(createService(path));
138  };
139 
146  {
147  service.reset(obj);
148 
149  service->user_weak_ptr = user_weak_ptr;
150  service->client_weak_ptr = my_weak_ptr;
151  };
152 
153  public:
154  /* ---------------------------------------------------------
155  MESSAGE CHAIN
156  --------------------------------------------------------- */
164  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
165  {
166  driver->sendData(invoke);
167  };
168 
209  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke)
210  {
211  ((protocol::IProtocol*)user)->replyData(invoke);
212  service->replyData(invoke);
213  };
214 
215  private:
216  std::weak_ptr<User> user_weak_ptr;
217  std::weak_ptr<Client> my_weak_ptr;
218  };
219 };
220 };
221 };
void changeService(const std::string &path)
Definition: Client.hpp:135
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override
Definition: Client.hpp:164
virtual void replyData(std::shared_ptr< protocol::Invoke > invoke)
Definition: Client.hpp:209
auto getService() const -> Service *
Definition: Client.hpp:112
auto getUser() const -> User *
Definition: Client.hpp:102
virtual auto createService(const std::string &) -> Service *=0
void changeService(Service *obj)
Definition: Client.hpp:145
auto getNo() const -> size_t
Definition: Client.hpp:125
Client(User *user, std::shared_ptr< protocol::WebClientDriver > driver)
Definition: Client.hpp:63