aboutsummaryrefslogtreecommitdiffstats
path: root/emu-radio/common-library/websocket-server.cc
blob: 26a2cf7686cf2e38940e05adafff3007b34bbdd2 (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "websocket-server.h"

WebSocketServer::WebSocketServer (unsigned short port)
    : m_isRunning (false)
{
  try
    {
      // Initialize Asio
      server.init_asio ();

      // Set reuse address option
      server.set_reuse_addr (true);

      // Register our message handler
      server.set_message_handler (bind (&WebSocketServer::onMessage, this, &server, ::_1, ::_2));

      // Listen on port
      server.listen (port);

    }
  catch (websocketpp::exception const &e)
    {
      std::cout << e.what () << std::endl;
    }
  catch (...)
    {
      std::cout << "Exception occurred.." << std::endl;
    }
}

void WebSocketServer::setHandler (const HandlerFunction &handler)
{
  this->handler = handler;
}

void WebSocketServer::start ()
{
  m_isRunning = true;
  int retries = 5;

  while (m_isRunning && retries > 0)
    {

      try
        {
          // Start the server accept loop
          server.start_accept ();

          //Set interrupt callbacks

          boost::asio::io_service io_service;
          boost::asio::signal_set signals (server.get_io_service (), SIGINT, SIGQUIT);

          signals.async_wait ([this] (const boost::system::error_code &errorCode, int)
                              {
                                std::cout << "Gracefully terminating websocket server" << std::endl;
                                this->m_isRunning = false;
                                this->server.stop ();
                              });

          // Start the ASIO io_service run loop
          server.run ();

        }
      catch (websocketpp::exception const &e)
        {
          std::cout << e.what () << std::endl;
        }
      catch (...)
        {
          std::cout << "Exception occurred.." << std::endl;
        }

      retries--;
    }
}

void WebSocketServer::onMessage (Server *s, websocketpp::connection_hdl hdl, message_ptr msg)
{
  try
    {
      handler (s, hdl, msg, (uint8_t *) msg->get_payload ().c_str (), msg->get_payload ().size ());
    }
  catch (const websocketpp::lib::error_code &e)
    {

      std::cout << "Reply sent failed because: " << e << "(" << e.message () << ")" << std::endl;

    }
}