Skip to content

Commit

Permalink
lib: ws use fallback port
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed May 26, 2021
1 parent 633e922 commit db404c7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ set (GINGA_BINARY_DIR ${CMAKE_BINARY_DIR}/bin)
add_definitions (-DGINGADATADIR="${CMAKE_INSTALL_FULL_DATADIR}/${PACKAGE}")
add_definitions (-DGINGABINDIR="${CMAKE_INSTALL_FULL_BINDIR}")


# set all target runtime to bin
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${GINGA_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${GINGA_BINARY_DIR})
Expand Down Expand Up @@ -376,8 +375,8 @@ set (CPACK_DEBIAN_COMPRESSION_TYPE "xz")
set (CPACK_DEBIAN_REPLACES "xz")

install (DIRECTORY ${CMAKE_BINARY_OUTPUT_DIR} DESTINATION bin)
include(GNUInstallDirs)
install(TARGETS libginga DESTINATION bin)
include (GNUInstallDirs)
install (TARGETS libginga DESTINATION bin)
install (TARGETS ginga DESTINATION bin)
install (TARGETS gingagui DESTINATION bin)
install (DIRECTORY src-gui/icons/ DESTINATION share/ginga/icons)
Expand Down
3 changes: 0 additions & 3 deletions lib/Formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ Formatter::start (const string &file, string *errmsg)
if (_state != GINGA_STATE_STOPPED)
return false;

if (_opts.webservices)
this->_webservices->start ();

// Parse document.
g_assert_null (_doc);
w = _opts.width;
Expand Down
47 changes: 30 additions & 17 deletions lib/WebServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ cb_locaction (SoupServer *server, SoupMessage *msg, const char *path,
0);

// add GingaCC-Server-* headers
value = g_strdup_printf ("http://%s:%d", ws->host_addr, WS_PORT);
value = g_strdup_printf ("http://%s:%d", ws->host_addr, ws->host_port);
soup_message_headers_append (msg->response_headers,
"GingaCC-Server-BaseURL", value);
g_free (value);
value = g_strdup_printf ("https://%s:%d", ws->host_addr, WS_PORT);
value = g_strdup_printf ("https://%s:%d", ws->host_addr, ws->host_port);
soup_message_headers_append (msg->response_headers,
"GingaCC-Server-SecureBaseURL", value);
g_free (value);
Expand Down Expand Up @@ -184,7 +184,7 @@ cb_apps (SoupServer *server, SoupMessage *msg, const char *path,
evt = node->getLookAtEvent (interface);
else
evt = node->getLookAtEvent ("@lambda");

if (evt != nullptr)
doc->evalAction (evt, Event::START);
else
Expand All @@ -196,7 +196,7 @@ cb_apps (SoupServer *server, SoupMessage *msg, const char *path,
evt = node->getLookAtEvent (interface);
else
evt = node->getLookAtEvent ("@lambda");

if (evt != nullptr)
doc->evalAction (evt, Event::STOP);
else
Expand Down Expand Up @@ -232,25 +232,38 @@ WebServices::start ()
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_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, 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,
// set server to handle both /location and other routes in WS_PORT_DEFAULT

ret = soup_server_listen_all (_server, WS_PORT_DEFAULT,
SoupServerListenOptions (0), &error);
if (!ret)
if (ret)
{
g_printerr ("Fails start WebServices: %s\n", error->message);
g_assert_not_reached ();
this->host_port = WS_PORT_DEFAULT;
}
else
{
TRACE ("WebServices fail serve at default port %d: %s\n",
WS_PORT_DEFAULT, error->message);
g_error_free (error);
// if fail use WS_PORT_DEFAULT + 1
ret = soup_server_listen_all (_server, WS_PORT_DEFAULT + 1,
SoupServerListenOptions (0), &error);
g_assert (ret);
this->host_port = WS_PORT_DEFAULT + 1;
}

// ssdp avaliable
location = g_strdup_printf ("http://%s:%u/location", this->host_addr,
this->host_port);
TRACE ("WebServices location: %s\n", location);
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));

WS_ADD_ROUTE (_server, WS_ROUTE_LOC, cb_locaction);
WS_ADD_ROUTE (_server, WS_ROUTE_PLAYER, cb_remoteplayer);
Expand Down
3 changes: 2 additions & 1 deletion lib/WebServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PlayerRemoteData
#define WS_ROUTE_LOC "/location"
#define WS_ROUTE_PLAYER "/remote-mediaplayer"
#define WS_ROUTE_APPS "/current-service/apps/"
#define WS_PORT 44642
#define WS_PORT_DEFAULT 44642
#define WS_JSON_REMOTE_PLAYER \
"{\
\"location\" : \"%s\", \
Expand Down Expand Up @@ -110,6 +110,7 @@ class WebServices
bool machMediaThenSetPlayerRemote (PlayerRemoteData &);
Formatter *getFormatter ();
const char *host_addr;
guint host_port;

private:
Formatter *_formatter;
Expand Down
9 changes: 6 additions & 3 deletions tests/test-WebServices-RemotePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */

Formatter *fmt;
char *test_remote_player_location;
int test_remote_player_port = WS_PORT + 10; // different port then WS_PORT;
int test_remote_player_port
= WS_PORT_DEFAULT + 10; // different port then ws port;
const char *expectedAction = nullptr;
SoupServer *server = nullptr;

Expand All @@ -41,10 +42,12 @@ remoteplayer_registry (GMainLoop *loop)
SoupMessage *msg;
SoupSession *session;
gchar *body, *url;
guint host_port = fmt->getWebServices ()->host_port;

session = soup_session_new ();
g_assert_nonnull (session);
url = g_strdup_printf ("http://localhost:%d%s", WS_PORT, WS_ROUTE_PLAYER);
url = g_strdup_printf ("http://localhost:%d%s", host_port,
WS_ROUTE_PLAYER);
g_assert_nonnull (url);
msg = soup_message_new (SOUP_METHOD_POST, url);
g_assert_nonnull (msg);
Expand Down Expand Up @@ -98,7 +101,7 @@ setup_remoteplayer_route (const char *id, GMainLoop *loop)
server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "remoteplayer",
nullptr);
g_assert_nonnull (server);
// set server to handle both /location and other routes in WS_PORT
// set server to handle both /location and other routes
ret = soup_server_listen_all (server, test_remote_player_port,
SoupServerListenOptions (0), &error);
if (!ret)
Expand Down
11 changes: 7 additions & 4 deletions tests/test-WebServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ cb_test_location (SoupSession *session, SoupMessage *msg, gpointer loop)
char *url1;
char *url2;
const char *host_addr = fmt->getWebServices ()->host_addr;
guint host_port = fmt->getWebServices ()->host_port;
SoupMessageHeaders *hdrs;

g_assert (msg->status_code == SOUP_STATUS_OK);

url1 = g_strdup_printf ("http://%s:%d", host_addr, WS_PORT);
url2 = g_strdup_printf ("https://%s:%d", host_addr, WS_PORT);
url1 = g_strdup_printf ("http://%s:%d", host_addr, host_port);
url2 = g_strdup_printf ("https://%s:%d", host_addr, host_port);

hdrs = msg->response_headers;
g_assert_cmpstr (
Expand All @@ -67,10 +68,11 @@ ws_get_location (GMainLoop *loop)
SoupMessage *msg;
SoupSession *session;
gchar *url;
guint host_port = fmt->getWebServices ()->host_port;

session = soup_session_new ();
g_assert_nonnull (session);
url = g_strdup_printf ("http://localhost:%d%s", WS_PORT, WS_ROUTE_LOC);
url = g_strdup_printf ("http://localhost:%d%s", host_port, WS_ROUTE_LOC);
g_assert_nonnull (url);
msg = soup_message_new (SOUP_METHOD_GET, url);
g_assert_nonnull (msg);
Expand All @@ -87,10 +89,11 @@ ws_post_action (const char *node, const char *action, const char *interface,
SoupMessage *msg;
SoupSession *session;
gchar *body, *url;
guint host_port = fmt->getWebServices ()->host_port;

session = soup_session_new ();
g_assert_nonnull (session);
url = g_strdup_printf ("http://localhost:%d%s1/docId/%s", WS_PORT,
url = g_strdup_printf ("http://localhost:%d%s1/docId/%s", host_port,
WS_ROUTE_APPS, node);
g_assert_nonnull (url);
msg = soup_message_new (SOUP_METHOD_POST, url);
Expand Down

0 comments on commit db404c7

Please sign in to comment.