summaryrefslogtreecommitdiff
path: root/src/websocket_server.cpp
blob: e4548391075e218048bf4340b2ed3071781c2fb0 (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
/**
 * @file websocket_server.cpp
 *
 * @brief Settings daemon WebSocket server source.
 *
 * @author Ossama Othman @<ossama.othman@@intel.com@>
 *
 * @copyright @par
 * Copyright 2013 Intel Corporation All Rights Reserved.
 * @par
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 * @par
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * @par
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "websocket_server.hpp"
#include "configurator.hpp"
#include "../lib/config.hpp"
#include "../lib/manager.hpp"

#include <stdexcept>
#include <thread>


namespace
{
  // --------------------------------------------------------------------
  // libwebsockets callbacks and related data.
  // --------------------------------------------------------------------

  int
  callback_ivi_settings(libwebsocket_context * context,
                        libwebsocket * wsi,
                        enum libwebsocket_callback_reasons reason,
                        void * /* user */,
                        void * in,
                        size_t /* len */)
  {
    using namespace ivi::settings;

    manager * const mgr = static_cast<manager *>(
        libwebsocket_context_user(context));

    switch(reason) {
    case LWS_CALLBACK_RECEIVE:
      // Request has come in from Settings app.  Pass it on to the
      // settings manager.
      mgr->dispatch(static_cast<char const *>(in), wsi);
      break;

    default:
      break;
    }

    return 0;
  }

  struct ws_session_data_type
  {
  };

  libwebsocket_protocols settings_protocols[] = {
    {
      "http-only",//"ivi-settings-protocol",
      callback_ivi_settings,
      sizeof(ws_session_data_type),
      0,
      nullptr,
      0
    },
    {
      nullptr,
      nullptr,
      0,
      0,
      nullptr,
      0
    }
  };

}

// ----------------------------------------------------------------------

ivi::settings::websocket_server::websocket_server(
  ivi::settings::configurator const & config,
  ivi::settings::manager & mgr)
  : context_(nullptr)
{
  lws_context_creation_info info;
  info.port  = config.websocket_port();
  info.iface = "lo";  // Only allow communication over the loopback
                      // interface.

  info.protocols  = settings_protocols;
  info.extensions = libwebsocket_get_internal_extensions();

  info.ssl_cert_filepath        = config.ssl_cert_file();
  info.ssl_private_key_filepath = config.ssl_private_key_file();
  info.ssl_ca_filepath          = config.ssl_ca_file();
  info.ssl_cipher_list          = nullptr;

  info.gid = -1;
  info.uid = -1;

  info.options = 0;

  info.user = &mgr;

  info.ka_time     = 0;
  info.ka_probes   = 0;
  info.ka_interval = 0;

  lws_set_log_level(LLL_INFO, lwsl_emit_syslog);

  context_ = libwebsocket_create_context(&info);
  if (context_ == nullptr)
    throw std::runtime_error("Unable to initialize websocket.\n");

  std::cout << "\n" PACKAGE_NAME " listening for requests on port "
            << info.port << ".\n";
}

ivi::settings::websocket_server::~websocket_server()
{
  libwebsocket_context_destroy(context_);
}

void
ivi::settings::websocket_server::run()
{
  // Run the websocket event loop in its own thread.
  std::thread(
    [](libwebsocket_context * context){
      try {
        // Run the libwebsockets event loop with an infinite timeout.
        // The negative timeout causes the underlying call poll() to
        // block indefinitely until an event occurs.
        constexpr int const timeout = -1;
        while (libwebsocket_service(context, timeout) >= 0) {
        }

        std::cerr << "WebSocket event loop ended.\n";
      } catch(std::exception & e) {
        std::cerr << e.what() << std::endl;
      }
    },
    context_).detach();
}


/**
 * @todo Add 'state' event do determine whether technology is
 *       enabled/disabled.
 */

// Local Variables:
// mode:c++
// c-basic-offset:2
// indent-tabs-mode: nil
// End: