Skip to content

Commit

Permalink
lib: refactory and add test-WebServices
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed May 6, 2021
1 parent d98edcb commit ea192af
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 109 deletions.
47 changes: 35 additions & 12 deletions lib/Formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,6 @@ Formatter::getState ()
return _state;
}

bool
Formatter::startWebServices ()
{
if (_opts.webservice && !_webservices->isStarted ())
return _webservices->start ();
else
return false;
}

bool
Formatter::start (const string &file, string *errmsg)
{
Expand All @@ -129,8 +120,8 @@ Formatter::start (const string &file, string *errmsg)
if (_state != GINGA_STATE_STOPPED)
return false;

if (_opts.webservice && !_webservices->isStarted ())
this->startWebServices ();
if (_opts.webservices)
this->_webservices->start ();

// Parse document.
g_assert_null (_doc);
Expand Down Expand Up @@ -182,6 +173,10 @@ Formatter::start (const string &file, string *errmsg)
// Sets formatter state.
_state = GINGA_STATE_PLAYING;

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

return true;
}

Expand Down Expand Up @@ -450,7 +445,7 @@ Formatter::Formatter (const GingaOptions *opts) : Ginga (opts)
_opts.width = 800;
_opts.height = 600;
_opts.debug = false;
_opts.webservice = false;
_opts.webservices = false;
_opts.opengl = false;
_opts.experimental = false;
};
Expand Down Expand Up @@ -492,6 +487,16 @@ Formatter::getDocument ()
return _doc;
}

/**
* @brief Gets WebServices.
* @return WebServices or null (no current document).
*/
WebServices *
Formatter::getWebServices ()
{
return _webservices;
}

/**
* @brief Gets EOS flag.
* @return EOS flag.
Expand Down Expand Up @@ -557,6 +562,24 @@ Formatter::setOptionDebug (Formatter *self, const string &name, bool value)
TRACE ("%s:=%s", name.c_str (), strbool (value));
}

/**
* @brief Sets WebServices option option of the given Formatter.
* @param self Formatter.
* @param name Must be the string "webservies".
* @param value webservies flag value.
*/
void
Formatter::setOptionWebServices (Formatter *self, const string &name,
bool value)
{
g_assert (name == "webservices");
if (value)
self->_webservices->start ();
else
self->_webservices->stop ();
TRACE ("%s:=%s", name.c_str (), strbool (value));
}

/**
* @brief Sets the experimental option of the given Formatter.
* @param self Formatter.
Expand Down
5 changes: 3 additions & 2 deletions lib/Formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Formatter : public Ginga

bool start (const std::string &, std::string *);
bool stop ();
bool startWebServices ();

void resize (int, int);
void redraw (cairo_t *);
Expand All @@ -64,11 +63,13 @@ class Formatter : public Ginga
~Formatter ();

Document *getDocument ();
WebServices *getWebServices ();
bool getEOS ();
void setEOS (bool);

static void setOptionBackground (Formatter *, const string &, string);
static void setOptionDebug (Formatter *, const string &, bool);
static void setOptionWebServices (Formatter *, const string &, bool);
static void setOptionExperimental (Formatter *, const string &, bool);
static void setOptionOpenGL (Formatter *, const string &, bool);
static void setOptionSize (Formatter *, const string &, int);
Expand Down Expand Up @@ -97,7 +98,7 @@ class Formatter : public Ginga

/// @brief Current WebServices.
WebServices *_webservices;

/// @brief Current document tree.
Document *_doc;

Expand Down
40 changes: 23 additions & 17 deletions lib/PlayerRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */

GINGA_NAMESPACE_BEGIN

#define REMOTE_PLAYER_ACT_JSON "{ \"action\" : \"%s\", \"delay\" : \"%d\"}"

/**
* @brief Creates PlayerRemote
* @param fmt Formatter
Expand All @@ -50,7 +48,7 @@ PlayerRemote::doSetProperty (Property code, const string &name,
if (_url != nullptr)
g_free (_url);
_url = g_strdup_printf ("http://%s/scene/nodes/%s", value.c_str (),
_media->getId ().c_str());
_media->getId ().c_str ());
break;
default:
return Player::doSetProperty (code, name, value);
Expand All @@ -61,66 +59,74 @@ PlayerRemote::doSetProperty (Property code, const string &name,
PlayerRemote::~PlayerRemote ()
{
g_free (_url);
g_object_unref (_session);
}

bool
static void
ws_action_callback (SoupSession *session, SoupMessage *msg,
gpointer user_data)
{
string url;
auto player = (PlayerRemote *) user_data;
url = player->getProperty ("remotePlayerBaseURL");
if (!msg->status_code == SOUP_STATUS_OK)
WARNING ("Failed to perform request %s", url.c_str ());
TRACE_SOUP_REQ_MSG (msg);
}

void
PlayerRemote::sendAction (const string &action)
{
guint status;
SoupMessage *msg;
char *body;

g_assert_nonnull (_url);
if (!_sessionStarted)
{
_session
= soup_session_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
SOUP_TYPE_CONTENT_SNIFFER, NULL);
_sessionStarted = true;
}
g_assert_nonnull (_url);
msg = soup_message_new ("POST", _url);
body = g_strdup_printf (REMOTE_PLAYER_ACT_JSON, action.c_str (), 0);
body = g_strdup_printf (REMOTE_PLAYER_JSON_ACT, action.c_str (), 0);
soup_message_set_request (msg, "application/json", SOUP_MEMORY_COPY, body,
strlen (body));
status = soup_session_send_message (_session, msg);
if(!status){
WARNING ("Failed perform request %s", _url);
}
soup_session_queue_message (_session, msg, ws_action_callback, this);

g_free (msg);
g_free (body);
g_free (msg);
return true;
}

void
PlayerRemote::start ()
{
g_assert (sendAction ("start"));
sendAction ("start");
}

void
PlayerRemote::startPreparation ()
{
g_assert (sendAction ("prepare"));
sendAction ("prepare");
}

void
PlayerRemote::stop ()
{
g_assert (sendAction ("stop"));
sendAction ("stop");
}

void
PlayerRemote::pause ()
{
g_assert (sendAction ("pause"));
sendAction ("pause");
}

void
PlayerRemote::resume ()
{
g_assert (sendAction ("resume"));
sendAction ("resume");
}

GINGA_NAMESPACE_END
9 changes: 8 additions & 1 deletion lib/PlayerRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ GINGA_NAMESPACE_BEGIN

class WebServices;

#define REMOTE_PLAYER_JSON_ACT \
"{\
\"action\" : \"%s\",\
\"delay\" : \"%d\"\
}"


class PlayerRemote : public Player
{
public:
Expand All @@ -38,7 +45,7 @@ class PlayerRemote : public Player

protected:
bool doSetProperty (Property, const string &, const string &);
bool sendAction (const string &);
void sendAction (const string &);
char * _url;
bool _sessionStarted;
WebServices *_ws;
Expand Down
Loading

0 comments on commit ea192af

Please sign in to comment.