Skip to content

Commit

Permalink
lib: add ws disconnect and capture abort in ginga.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed May 11, 2021
1 parent a5588ac commit 4e069d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/PlayerRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WebServices;
\"appId\": \"%s\",\
\"documentId\": \"%s\",\
\"sceneNode\": \"%s\",\
\"sceneURL\": \"%s\",\
\"type\": \"%s\",\
\"notifyEvents\": %s\
}"
Expand Down
36 changes: 22 additions & 14 deletions lib/WebServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ WebServices::WebServices (Formatter *fmt)

WebServices::~WebServices ()
{
this->stop ();
g_object_unref (_resource_group);
g_object_unref (_client);
g_object_unref (_server);
Expand All @@ -52,9 +53,13 @@ bool
WebServices::stop ()
{
_state = WS_STATE_STOPPED;
gssdp_resource_group_set_available (_resource_group, FALSE);
// stop ssdp
gssdp_resource_group_set_available (_resource_group, false);
// stop http
soup_server_disconnect (_server);
return true;
}

static void
cb_null (SoupServer *server, SoupMessage *msg, const char *path,
GHashTable *query, SoupClientContext *client, gpointer user_data)
Expand All @@ -73,7 +78,8 @@ cb_locaction (SoupServer *server, SoupMessage *msg, const char *path,
WebServices *ws = (WebServices *) user_data;

soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/plan", SOUP_MEMORY_COPY, NULL, 0);
soup_message_set_response (msg, "text/plan", SOUP_MEMORY_COPY, nullptr,
0);

// add GingaCC-Server-* headers
value = g_strdup_printf ("http://%s:%d", ws->host_addr, WS_PORT);
Expand Down Expand Up @@ -195,41 +201,41 @@ cb_apps (SoupServer *server, SoupMessage *msg, const char *path,
bool
WebServices::start ()
{
GError *error = NULL;
GError *error = nullptr;
int ret = 0;
char *location;

// create ssdpclient
_client = gssdp_client_new (NULL, &error);
_client = gssdp_client_new (nullptr, &error);
if (error)
{
g_printerr ("Error creating the GSSDP client: %s\n", error->message);
g_error_free (error);
return false;
}
gssdp_client_set_server_id (_client, WS_NAME);
_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);
gssdp_resource_group_add_resource_simple (
_resource_group, SSDP_ST, SSDP_UUID "::" SSDP_ST, location);
gssdp_resource_group_set_available (_resource_group, true);
g_assert (gssdp_resource_group_get_available (_resource_group));
g_assert (gssdp_client_get_active (_client));

// create server
_server = soup_server_new (SOUP_SERVER_SERVER_HEADER, SSDP_NAME, nullptr);
_server = soup_server_new (SOUP_SERVER_SERVER_HEADER, WS_NAME, nullptr);
g_assert_nonnull (_server);
// set server to handle both /location and other routes in WS_PORT
ret = soup_server_listen_all (_server, WS_PORT, SoupServerListenOptions (0),
&error);
ret = soup_server_listen_all (_server, WS_PORT,
SoupServerListenOptions (0), &error);
if (!ret)
{
g_printerr ("could not start webservices listen: %s\n",
error->message);
goto fail;
g_printerr ("Fails start WebServices: %s\n", error->message);
g_assert_not_reached ();
}

WS_ADD_ROUTE (_server, WS_ROUTE_LOC, cb_locaction);
Expand All @@ -238,6 +244,8 @@ WebServices::start ()
WS_ADD_ROUTE (_server, nullptr, cb_null);

_state = WS_STATE_STARTED;

g_free (location);
return true;
fail:
g_error_free (error);
Expand Down
7 changes: 3 additions & 4 deletions lib/WebServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ class PlayerRemoteData
list<string> recognizedableEvents;
};

#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"
#define SSDP_ST "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"
#define SSDP_UUID "uuid:1234abcd-12ab-12ab-12ab-1234567abc13"
#define WS_NAME "TeleMidia GingaCC-WebServices"
#define WS_ROUTE_LOC "/location"
#define WS_ROUTE_PLAYER "/remote-mediaplayer"
#define WS_ROUTE_APPS "/current-service/apps/"
Expand Down
15 changes: 5 additions & 10 deletions src/ginga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static Ginga *GINGA = nullptr;
static gboolean opt_debug = FALSE; // toggle debug
static gboolean opt_experimental = FALSE; // toggle experimental stuff
static gboolean opt_fullscreen = FALSE; // toggle fullscreen-mode
static gboolean opt_webservices = FALSE; // toggle webservices-only-mode
static gboolean opt_webservices = FALSE; // toggle webservices-only-mode
static gboolean opt_opengl = FALSE; // toggle OpenGL backend
static string opt_background = ""; // background color
static gint opt_width = 800; // initial window width
Expand Down Expand Up @@ -414,15 +414,10 @@ main (int argc, char **argv)
g_assert_nonnull (GINGA);
int fail_count = 0;

// // Run only GingaCC-WebServices
// if (opt_webservices && saved_argc < 2)
// {
// GINGA->startWebServices ();
// GMainLoop * main_loop = g_main_loop_new (NULL, FALSE);
// g_main_loop_run (main_loop);
// g_main_loop_unref (main_loop);
// goto done;
// }
// add abort/ctrl+C handler
auto gingaStopFunc = [] (int signum) { GINGA->stop (); };
signal (SIGABRT, gingaStopFunc);
signal (SIGTERM, gingaStopFunc);

// Run each NCL file, one after another.
for (int i = 1; i < saved_argc; i++)
Expand Down

0 comments on commit 4e069d6

Please sign in to comment.