summaryrefslogtreecommitdiff
path: root/src/components/hmi_message_handler/include/hmi_message_handler/websocket_session.h
blob: d8a79e7ef75cda8cf5cb5c616938ac95c8ba519e (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
#ifndef WEBSOCKET_SESSION_H 
#define WEBSOCKET_SESSION_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 <mutex>
#include <queue>
#include "json/json.h"

#include "utils/lock.h"
#include "utils/atomic_object.h"
#include "utils/threads/thread.h"
#include "utils/threads/message_loop_thread.h"
#include "utils/message_queue.h"

//#include "hmi_message_handler/mb_controller.h"

using namespace boost::beast::websocket;
using ::utils::MessageQueue;

#ifdef DEBUG_ON 
/**
* \def DBG_MSG
* \brief Debug message output with file name and line number.
* \param x formatted debug message.
* \return printf construction.
*/
#define DBG_MSG(x) printf("%s:%d ", __FILE__, __LINE__);\
                   printf x
#else
#define DBG_MSG(x)
#endif

#define DBG_MSG_ERROR(x) printf("ERROR!!! %s:%d ", __FILE__, __LINE__);\
                         printf x

typedef std::queue<std::shared_ptr<std::string>> AsyncQueue;                    
typedef std::shared_ptr<std::string> Message;


namespace NsMessageBroker {

  class CMessageBrokerController;

  class WebsocketSession : 
  public std::enable_shared_from_this<WebsocketSession> {

  boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws_;
  boost::asio::strand<boost::asio::io_context::executor_type> strand_;
  boost::beast::multi_buffer buffer_;
  boost::beast::multi_buffer send_buffer_;
  CMessageBrokerController* controller_;  

  public:
    WebsocketSession(boost::asio::ip::tcp::socket socket, CMessageBrokerController* controller);

    ~WebsocketSession();

    void Accept();

    void Close();

    void Shutdown();

    bool IsShuttingDown();

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

    void Send(std::string& message, Json::Value& json_message);

    void SendFromQueue();

    void sendJsonMessage(Json::Value& message);

    void OnWrite(boost::system::error_code ec, std::size_t bytes_transferred, std::shared_ptr<std::string> message);

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

    int getNextMessageId();

    void prepareMessage(Json::Value& root);

    void prepareErrorMessage(int errCode, std::string errMessage, Json::Value& error);

    std::string getDestinationComponentName(Json::Value& root);

    bool isNotification(Json::Value& root);

    bool isResponse(Json::Value& root);

    std::string findMethodById(std::string id);

    void registerController(int id = 0);

    void unregisterController();

    void subscribeTo(std::string property);

    void unsubscribeFrom(std::string property);

    bool checkMessage(Json::Value& root, Json::Value& error);

    std::string getControllersName();

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

  protected:

    sync_primitives::atomic_bool stop;

  private:
    void onMessageReceived(Json::Value message);

    std::map<std::string, std::string> mWaitResponseQueue;

    std::string m_receivingBuffer;

    int mControllersIdStart;

    int mControllersIdCurrent;

    Json::Reader m_reader;
    Json::FastWriter m_writer;
    Json::FastWriter m_receiverWriter;
    

    sync_primitives::Lock       queue_lock_;   
    sync_primitives::Lock       message_queue_lock_;    
    std::atomic_bool            shutdown_;


    MessageQueue<Message, AsyncQueue> message_queue_;

    class LoopThreadDelegate : public threads::ThreadDelegate {
     public:
      LoopThreadDelegate(MessageQueue<Message, AsyncQueue>* message_queue, 
        WebsocketSession* handler);

      virtual void threadMain() OVERRIDE;
      virtual void exitThreadMain() OVERRIDE;

      void OnWrite();

      void SetShutdown();

     private:
      void DrainQueue();  
      MessageQueue<Message, AsyncQueue>& message_queue_;
      WebsocketSession& handler_;
      sync_primitives::Lock queue_lock_;
      sync_primitives::ConditionalVariable queue_new_items_;
      std::atomic_bool write_pending_;
      std::atomic_bool shutdown_;

      sync_primitives::Lock write_lock_;

    };

    LoopThreadDelegate* thread_delegate_;
    threads::Thread* thread_;

  };

} //NsMessageBroker




#endif /* WEBSOCKET_SESSION_H */