summaryrefslogtreecommitdiff
path: root/src/components/hmi_message_handler/include/hmi_message_handler/mb_controller.h
blob: 60dc50ad7a1835de2fea39e933dd1ee4a322bcee (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Copyright (c) 2018 Livio, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

* Neither the name of SmartDeviceLink Consortium, Inc. nor the names of its
 contributors may be used to endorse or promote products derived from
 this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef MB_CONTROLLER_H
#define MB_CONTROLLER_H

#include <iostream>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/make_shared.hpp>
#include <boost/thread/thread.hpp>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <map>
#include "json/json.h"
#include "utils/macro.h"
#include "utils/lock.h"
#include "utils/atomic_object.h"
#include "websocket_session.h"

using namespace boost::beast::websocket;

namespace hmi_message_handler {

CREATE_LOGGERPTR_GLOBAL(mb_logger_, "HMIMessageHandler")

enum ErrorCode {
  CONTROLLER_EXISTS = -32000,
  SUBSCRIBTION_EXISTS = -32001,
  INVALID_REQUEST = -32600
};

class WebsocketSession;

class CMessageBrokerController
    : public std::enable_shared_from_this<CMessageBrokerController> {
 public:
  CMessageBrokerController(const std::string& address,
                           uint16_t port,
                           std::string name,
                           int num_ports);

  ~CMessageBrokerController();

  bool StartListener();

  bool Run();

  void WaitForConnection();

  void StartSession(boost::system::error_code ec);

  void OnAccept(
      boost::system::error_code ec,
      boost::asio::strand<boost::asio::io_context::executor_type>& strand,
      stream<boost::asio::ip::tcp::socket>& ws);

  void OnRead(boost::system::error_code ec, std::size_t bytes_transferred);

  bool isNotification(Json::Value& message);

  void sendNotification(Json::Value& message);

  bool isResponse(Json::Value& message);

  void sendResponse(Json::Value& message);

  void sendJsonMessage(Json::Value& message);

  void subscribeTo(std::string property);

  void registerController(int id = 0);

  void unregisterController();

  void* MethodForReceiverThread(void* arg);

  bool Connect();

  void exitReceivingThread();

  virtual void processResponse(std::string method, Json::Value& root) = 0;

  virtual void processRequest(Json::Value& root) = 0;

  virtual void processNotification(Json::Value& root) = 0;

  std::string getMethodName(std::string& method);

  std::string GetComponentName(std::string& method);

  void processInternalRequest(Json::Value& message,
                              WebsocketSession* ws_session);

  void pushRequest(Json::Value& message, WebsocketSession* ws_session);

  // Registry
  bool addController(WebsocketSession* ws_session, std::string name);

  void deleteController(WebsocketSession* ws_session);

  void deleteController(std::string name);

  void removeSubscribersBySession(const WebsocketSession* ws);

  bool addSubscriber(WebsocketSession* ws_session, std::string name);

  void deleteSubscriber(WebsocketSession* ws, std::string name);

  int getSubscribersFd(std::string name,
                       std::vector<WebsocketSession*>& result);

  int getNextControllerId();

 private:
  boost::asio::io_context ioc_;
  const std::string& address_;
  uint16_t port_;
  std::string name_;
  int num_threads_;
  std::vector<std::thread> thread_vector_;

  boost::asio::ip::tcp::acceptor acceptor_;
  boost::asio::ip::tcp::socket socket_;
  boost::beast::multi_buffer buffer_;
  boost::asio::ip::tcp::endpoint endpoint_;

  int mControllersIdCounter;

  // Registry
  std::vector<std::shared_ptr<hmi_message_handler::WebsocketSession> >
      mConnectionList;
  sync_primitives::Lock mConnectionListLock;

  std::map<std::string, WebsocketSession*> mControllersList;
  sync_primitives::Lock mControllersListLock;

  std::multimap<std::string, WebsocketSession*> mSubscribersList;
  sync_primitives::Lock mSubscribersListLock;

  std::map<std::string, WebsocketSession*> mRequestList;
  sync_primitives::Lock mRequestListLock;

  std::atomic_bool shutdown_;
};

}  // hmi_message_handler

#endif /* MB_CONTROLLER_H */