2 * @file send_callback.cpp
4 * @brief Core send functionality for responses and events.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 2013 Intel Corporation All Rights Reserved.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation;
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
27 #include <settingsd/send_callback.hpp>
28 #include <settingsd/glib_traits.hpp>
29 #include <settingsd/json_glib_traits.hpp>
30 #include <settingsd/unique_ptr.hpp>
37 ivi::settings::send_callback::send_callback(libwebsocket * wsi)
42 ivi::settings::send_callback::send_callback(
43 send_callback const & other)
48 ivi::settings::send_callback &
49 ivi::settings::send_callback::operator=(send_callback const & other)
57 ivi::settings::send_callback::send_payload(
58 char const * send_type,
59 unique_ptr<JsonBuilder> const & builder) const
61 unique_ptr<JsonGenerator> const generator(json_generator_new());
62 unique_ptr<JsonNode> const root(json_builder_get_root(builder.get()));
63 json_generator_set_root(generator.get(), root.get());
66 json_generator_to_data(generator.get(), nullptr);
68 unique_ptr<gchar> safe_data(data);
70 if (data == nullptr) {
71 g_critical("Unable to generate JSON formatted %s payload:\n",
77 g_debug("Sending %s: %s\n", send_type, data);
79 size_t const payload_len = strlen(data);
81 // libwebsockets wants a sequence of octets (unsigned char *) rather
83 typedef std::vector<unsigned char> vector_type;
85 vector_type::size_type const buf_len =
86 LWS_SEND_BUFFER_PRE_PADDING
88 + LWS_SEND_BUFFER_POST_PADDING;
90 vector_type buf(buf_len);
91 unsigned char * const payload =
92 buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
94 // Copy the string into the buffer after libwebsockets pre-padding.
99 int const count = libwebsocket_write(wsi_,
105 g_critical("Unable to send the following data to client: %s\n",
115 // indent-tabs-mode: nil