-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: boostrap WebService SSDPP and /location route
- Loading branch information
Showing
7 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#include <WebServices.h> | ||
#include <stdio.h> | ||
#include <Formatter.h> | ||
#include <gio/gio.h> | ||
#include <stdlib.h> | ||
|
||
// routes | ||
#define WS_PORT 44642 | ||
#define SSDP_UUID "uuid:b16f8e7e-8050-11eb-8036-00155dfe4f40" | ||
#define SSDP_DEVICE "upnp:rootdevice" | ||
#define SSDP_NAME "TeleMidia GingaCCWebServices" | ||
#define SSDP_USN "urn:schemas-sbtvd-org:service:GingaCCWebServices:1" | ||
|
||
/** | ||
* @brief Creates a new WebServices. | ||
* @return New #WebServices. | ||
*/ | ||
WebServices::WebServices (Formatter *fmt) | ||
{ | ||
_formatter = fmt; | ||
_started = false; | ||
} | ||
|
||
WebServices::~WebServices () | ||
{ | ||
g_object_unref (_resource_group); | ||
g_object_unref (_client); | ||
} | ||
|
||
bool WebServices::isStarted () | ||
{ | ||
return _started; | ||
} | ||
|
||
static void | ||
ws_null_callback (SoupServer *server, SoupMessage *msg, const char *path, | ||
GHashTable *query, SoupClientContext *client, | ||
gpointer user_data) | ||
{ | ||
soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND); | ||
soup_message_set_response (msg, "text/plan", SOUP_MEMORY_COPY, | ||
"route not implemented", 0); | ||
} | ||
|
||
static void | ||
ws_loc_callback (SoupServer *server, SoupMessage *msg, const char *path, | ||
GHashTable *query, SoupClientContext *client, | ||
gpointer user_data) | ||
{ | ||
WebServices *ws; | ||
char *value; | ||
|
||
ws = (WebServices *) user_data; | ||
soup_message_set_status (msg, SOUP_STATUS_OK); | ||
soup_message_set_response (msg, "text/plan", SOUP_MEMORY_COPY, NULL, 0); | ||
|
||
// Add GingaCC-WS headers | ||
value = g_strdup_printf ("http://%s:%d", ws->_host_addr, WS_PORT); | ||
soup_message_headers_append (msg->response_headers, | ||
"GingaCC-Server-BaseURL", value); | ||
|
||
value = g_strdup_printf ("https://%s:%d", ws->_host_addr, WS_PORT); | ||
soup_message_headers_append (msg->response_headers, | ||
"GingaCC-Server-SecureBaseURL", value); | ||
g_free (value); | ||
|
||
soup_message_headers_append ( | ||
msg->response_headers, "GingaCC-Server-PairingMethods", "qcode,kex"); | ||
} | ||
|
||
bool | ||
WebServices::start () | ||
{ | ||
GError *error = NULL; | ||
int ret = 0; | ||
char *location; | ||
|
||
// create ssdpclient | ||
_client = gssdp_client_new (NULL, &error); | ||
if (error) | ||
{ | ||
g_printerr ("Error creating the GSSDP client: %s\n", error->message); | ||
g_error_free (error); | ||
return false; | ||
} | ||
_resource_group = gssdp_resource_group_new (_client); | ||
g_assert (_resource_group); | ||
_host_addr = gssdp_client_get_host_ip (_client); | ||
|
||
// ssdp avaliable | ||
location = g_strdup_printf ("http://%s:%d/location", _host_addr, WS_PORT); | ||
gssdp_resource_group_add_resource_simple (_resource_group, SSDP_USN, | ||
SSDP_USN, location); | ||
g_free (location); | ||
gssdp_resource_group_set_available (_resource_group, TRUE); | ||
g_assert (gssdp_resource_group_get_available (_resource_group)); | ||
|
||
// create server | ||
_ws = soup_server_new (SOUP_SERVER_SERVER_HEADER, SSDP_NAME, nullptr); | ||
g_assert_nonnull (_ws); | ||
// set server to handle both /location and other routes in WS_PORT | ||
ret = soup_server_listen_all (_ws, WS_PORT, SoupServerListenOptions (0), | ||
&error); | ||
if (!ret) | ||
{ | ||
g_printerr ("could not start webservices listen: %s\n", | ||
error->message); | ||
goto fail; | ||
} | ||
|
||
// add route /location () | ||
soup_server_add_handler (_ws, "/location", ws_loc_callback, this, | ||
nullptr); | ||
|
||
// add route NULL | ||
soup_server_add_handler (_ws, nullptr, ws_null_callback, this, nullptr); | ||
|
||
// all done | ||
_started = true; | ||
return true; | ||
fail: | ||
g_error_free (error); | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Copyright (C) 2006-2018 PUC-Rio/Laboratorio TeleMidia | ||
This file is part of Ginga (Ginga-NCL). | ||
Ginga is free software: you can redistribute it and/or modify it | ||
under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
Ginga 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 General Public | ||
License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Ginga. If not, see <https://www.gnu.org/licenses/>. */ | ||
|
||
#ifndef WEBSERVICES_H | ||
#define WEBSERVICES_H | ||
|
||
#include "aux-ginga.h" | ||
#include <libgssdp/gssdp.h> | ||
#include <gio/gio.h> | ||
#include <libsoup/soup.h> | ||
|
||
GINGA_NAMESPACE_BEGIN | ||
class Formatter; | ||
|
||
class WebServices | ||
{ | ||
public: | ||
explicit WebServices (Formatter*); | ||
~WebServices (); | ||
bool start (); | ||
bool isStarted (); | ||
const char *_host_addr; | ||
|
||
private: | ||
Formatter* _formatter; | ||
bool _started; | ||
GSSDPClient *_client; | ||
SoupServer *_ws; | ||
GSSDPResourceGroup *_resource_group; | ||
}; | ||
|
||
GINGA_NAMESPACE_END | ||
|
||
#endif // WebServices_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters