18 #include <sys/socket.h> 25 #define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) 26 #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) 30 static void _DLOG(
const char* func,
const int line,
const char* log, ...);
31 static void _ELOG(
const char* func,
const int line,
const char* log, ...);
32 static bool has_verb(
const string& verb);
33 static const char API[] =
"soundmanager";
35 static void _on_hangup_static(
void *closure,
struct afb_wsj1 *wsj)
40 static void _on_call_static(
void *closure,
const char *api,
const char *verb,
struct afb_wsj1_msg *msg)
45 static void _on_event_static(
void* closure,
const char*
event,
struct afb_wsj1_msg *msg)
50 static void _on_reply_static(
void *closure,
struct afb_wsj1_msg *msg)
70 LibSoundmanager::LibSoundmanager(
const int port,
const string& token)
73 if(port > 0 && token.size() > 0)
80 ELOG(
"port and token should be > 0, Initial port and token uses.");
83 ret = initialize_websocket();
86 ELOG(
"Failed to initialize websocket");
97 sd_event_unref(mploop);
99 if(sp_websock != NULL)
120 void (*event_cb)(
const std::string& event,
struct json_object* event_contents),
121 void (*reply_cb)(
struct json_object* reply_contents))
127 int LibSoundmanager::initialize_websocket()
132 int ret = sd_event_default(&mploop);
135 ELOG(
"Failed to create event loop");
140 minterface.on_hangup = _on_hangup_static;
141 minterface.on_call = _on_call_static;
142 minterface.on_event = _on_event_static;
143 muri +=
"ws://localhost:" + to_string(mport) +
"/api?token=" + mtoken;
144 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface,
this);
145 if(sp_websock == NULL)
147 ELOG(
"Failed to create websocket connection");
158 sd_event_unref(mploop);
163 static void *event_loop_run(
void *args)
165 struct sd_event* loop = (
struct sd_event*)(args);
166 DLOG(
"start eventloop");
168 sd_event_run(loop, 30000000);
184 if(mploop && sp_websock)
187 int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
190 ELOG(
"Cannot run eventloop due to error:%d", errno);
198 ELOG(
"Connecting is not established yet");
226 ELOG(
"verb doesn't exit");
229 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static,
this);
231 ELOG(
"Failed to call verb:%s",verb.c_str());
261 struct json_object* j_obj = json_object_new_object();
262 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
264 int ret = afb_wsj1_call_j(sp_websock, API,
"subscribe", j_obj, _on_reply_static,
this);
266 ELOG(
"Failed to call verb:%s",__FUNCTION__);
290 struct json_object* j_obj = json_object_new_object();
291 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
293 int ret = afb_wsj1_call_j(sp_websock, API,
"unsubscribe", j_obj, _on_reply_static,
this);
295 ELOG(
"Failed to call verb:%s",__FUNCTION__);
300 am_Error_e LibSoundmanager::connect(
const am_sourceID_t sourceID,
const am_sinkID_t sinkID, am_mainConnectionID_t& mainConnectionID)
317 am_Error_e LibSoundmanager::disconnect(
const am_mainConnectionID_t mainConnectionID)
344 DLOG(
"%s called", __FUNCTION__);
360 cout <<
"ON-EVENT:" <<
event <<
"(" << afb_wsj1_msg_object_s(msg) <<
")" << endl;
361 if(onEvent !=
nullptr)
363 const string ev(event);
364 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
365 onEvent(ev, ev_contents);
371 cout <<
"ON-REPLY:" <<
"(" << afb_wsj1_msg_object_s(msg) <<
")" << endl;
372 if(onReply !=
nullptr)
374 struct json_object* reply = afb_wsj1_msg_object_j(msg);
381 static void _ELOG(
const char* func,
const int line,
const char* log, ...)
386 if (log == NULL || vasprintf(&message, log, args) < 0)
388 cout <<
"[ERROR]" << func <<
"(" << line <<
"):" << message << endl;
393 static void _DLOG(
const char* func,
const int line,
const char* log, ...)
398 if (log == NULL || vasprintf(&message, log, args) < 0)
400 cout <<
"[DEBUG]" << func <<
"(" << line <<
"):" << message << endl;
405 static bool has_verb(
const string& verb)
407 DLOG(
"verb is %s", verb.c_str());
const std::vector< std::string > api_list
int call_sync(const std::string &verb, struct json_object *arg, struct json_object *ret)
int unsubscribe(const std::string &event_name)
int call(const std::string &verb, struct json_object *arg)
void register_callback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents))
void on_hangup(void *closure, struct afb_wsj1 *wsj)
int subscribe(const std::string &event_name)
void on_reply(void *closure, struct afb_wsj1_msg *msg)
void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)