this.native = native;
this.listenerName = listenerName;
this.handle = handle || function(msg, listener, watchId) {};
+ this.requestIdToListenerId = {};
}
ListenerManager.prototype.addListener = function(callback) {
var nativeData = {
clientName: msg.clientName,
- replyId: msg.replyId,
requestId: msg.requestId,
data: data
};
});
var ReplyCommandListener = new ListenerManager(native_, '_ReplyCommandListener', function(msg, listener, watchId) {
- if (msg.replyId === watchId) {
- listener(msg.data);
+ if (this.requestIdToListenerId[watchId] === msg.requestId) {
+ listener(msg);
this.removeListener(watchId);
+ delete this.requestIdToListenerId[watchId];
return true;
}
-
return false;
});
{name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
]);
- var nativeData = {
- command: args.command,
- data: args.data,
- name: this.name
- };
-
- var replyId = ReplyCommandListener.addListener(successCallback);
-
- nativeData.replyId = replyId;
- nativeData.listenerId = ReplyCommandListener.listenerName;
var callback = function(result) {
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
return;
}
- args.successCallback(native_.getResultObject(result));
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result).data);
};
- native_.call('MediaControllerServerInfo_sendCommand', nativeData, callback);
+ var nativeData = {
+ command: args.command,
+ data: args.data,
+ name: this.name,
+ listenerId: ReplyCommandListener.listenerName
+ };
+
+ var replyListenerId = ReplyCommandListener.addListener(callback);
+ var result = native_.callSync('MediaControllerServerInfo_sendCommand', nativeData);
+
+ ReplyCommandListener.requestIdToListenerId[replyListenerId] = result.requestId;
};
MediaControllerServerInfo.prototype.addServerStatusChangeListener = function(listener) {
using common::PlatformResult;
using common::ErrorCode;
+using common::tools::ReportError;
+using common::tools::ReportSuccess;
MediaControllerClient::MediaControllerClient() : handle_(nullptr) {
ScopeLogger();
PlatformResult MediaControllerClient::SendCommand(const std::string& server_name,
const std::string& command,
const picojson::value& data,
- const std::string& reply_id,
- const JsonCallback& reply_cb) {
+ const JsonCallback& reply_cb,
+ char** request_id) {
ScopeLogger();
bundle* bundle = bundle_create();
- char* request_id = nullptr;
SCOPE_EXIT {
bundle_free(bundle);
- free(request_id);
};
int ret;
- ret = bundle_add(bundle, "replyId", reply_id.c_str());
- if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
- return LogAndCreateResult(
- ErrorCode::UNKNOWN_ERR, "Unable to add replyId to bundle",
- ("bundle_add(replyId) error: %d, message: %s", ret, get_error_message(ret)));
- }
-
ret = bundle_add(bundle, "data", data.serialize().c_str());
if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
return LogAndCreateResult(
("bundle_add(data) error: %d, message: %s", ret, get_error_message(ret)));
}
- ret = mc_client_send_custom_cmd(handle_, server_name.c_str(), command.c_str(), bundle, &request_id);
+ ret = mc_client_send_custom_cmd(handle_, server_name.c_str(), command.c_str(), bundle, request_id);
if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
return LogAndCreateResult(
ErrorCode::UNKNOWN_ERR, "Error sending custom command",
ScopeLogger();
MediaControllerClient* client = static_cast<MediaControllerClient*>(user_data);
+ picojson::value out = picojson::value(picojson::object());
+ picojson::object& out_o = out.get<picojson::object>();
picojson::value reply = picojson::value(picojson::object());
picojson::object& reply_o = reply.get<picojson::object>();
int ret;
- char* reply_id_str = nullptr;
char* data_str = nullptr;
SCOPE_EXIT {
- free(reply_id_str);
free(data_str);
};
- ret = bundle_get_str(bundle, "replyId", &reply_id_str);
- if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
- LoggerE("bundle_get_str(replyId) failed, error: %d", ret);
- return;
- }
-
- reply_o["replyId"] = picojson::value(std::string(reply_id_str));
-
ret = bundle_get_str(bundle, "data", &data_str);
if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
LoggerE("bundle_get_str(data) failed, error: %d", ret);
+ ReportError(out_o);
+ client->command_reply_callback_(&out);
return;
}
picojson::parse(data, data_str, data_str + strlen(data_str), &err);
if (!err.empty()) {
LoggerE("Failed to parse bundle data: %s", err.c_str());
+ ReportError(out_o);
+ client->command_reply_callback_(&out);
return;
}
reply_o["data"] = data;
reply_o["name"] = picojson::value(server_name);
- client->command_reply_callback_(&reply);
+ if (nullptr == request_id) {
+ LoggerE("Request id is null.");
+ ReportError(out_o);
+ client->command_reply_callback_(&out);
+ return;
+ }
+ out_o["requestId"] = picojson::value(std::string(request_id));
+
+ ReportSuccess(reply, out_o);
+ client->command_reply_callback_(&out);
}
PlatformResult MediaControllerClient::SendPlaybackState(const std::string& server_name,
common::PlatformResult SendRepeatState(const std::string& server_name, const std::string& state);
common::PlatformResult SendCommand(const std::string& server_name, const std::string& command,
- const picojson::value& data, const std::string& reply_id,
- const JsonCallback& reply_cb);
+ const picojson::value& data, const JsonCallback& reply_cb,
+ char** request_id);
common::PlatformResult SetServerStatusChangeListener(const JsonCallback& callback);
common::PlatformResult UnsetServerStatusChangeListener();
#include "common/platform_result.h"
#include "common/task-queue.h"
#include "common/tools.h"
+#include "common/scope_exit.h"
#include "mediacontroller/mediacontroller_types.h"
}
CHECK_EXIST(args, "clientName", out)
- CHECK_EXIST(args, "replyId", out)
CHECK_EXIST(args, "requestId", out)
CHECK_EXIST(args, "data", out)
- server_->CommandReply(args.get("clientName").get<std::string>(), args.get("requestId").to_str(),
- args.get("replyId").to_str(), args.get("data"));
+ server_->CommandReply(
+ args.get("clientName").get<std::string>(),
+ args.get("requestId").get<std::string>(),
+ args.get("data"));
ReportSuccess(out);
}
}
CHECK_EXIST(args, "listenerId", out)
- CHECK_EXIST(args, "replyId", out)
CHECK_EXIST(args, "name", out)
CHECK_EXIST(args, "command", out)
CHECK_EXIST(args, "data", out)
JsonCallback reply_cb = [this, args](picojson::value* reply) -> void {
picojson::object& reply_obj = reply->get<picojson::object>();
-
reply_obj["listenerId"] = args.get("listenerId");
-
Instance::PostMessage(this, reply->serialize().c_str());
};
+ char* request_id = nullptr;
+ SCOPE_EXIT {
+ free(request_id);
+ };
+
PlatformResult result = client_->SendCommand(
- args.get("name").get<std::string>(), args.get("command").get<std::string>(), args.get("data"),
- args.get("replyId").to_str(), reply_cb);
+ args.get("name").get<std::string>(),
+ args.get("command").get<std::string>(),
+ args.get("data"),
+ reply_cb,
+ &request_id);
if (result) {
ReportSuccess(out);
+ out["requestId"] = picojson::value(std::string(request_id));
} else {
LogAndReportError(result, &out, ("Failed to send command."));
}
int ret;
char* data_str = nullptr;
- char* reply_id_str = nullptr;
SCOPE_EXIT {
free(data_str);
- free(reply_id_str);
};
ret = bundle_get_str(bundle, "data", &data_str);
return;
}
- ret = bundle_get_str(bundle, "replyId", &reply_id_str);
- if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
- LoggerE("bundle_get_str(replyId) failed, error: %d", ret);
- return;
- }
-
picojson::value data;
std::string err;
picojson::parse(data, data_str, data_str + strlen(data_str), &err);
request_o["clientName"] = picojson::value(std::string(client_name));
request_o["command"] = picojson::value(std::string(command));
- request_o["replyId"] = picojson::value(std::string(reply_id_str));
request_o["requestId"] = picojson::value(std::string(request_id));
request_o["data"] = data;
PlatformResult MediaControllerServer::CommandReply(const std::string& client_name,
const std::string& request_id,
- const std::string& reply_id,
const picojson::value& data) {
ScopeLogger();
bundle_free(bundle);
};
- ret = bundle_add(bundle, "replyId", reply_id.c_str());
- if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
- return LogAndCreateResult(
- ErrorCode::UNKNOWN_ERR, "Unable to add replyId to bundle",
- ("bundle_add(replyId) error: %d, message: %s", ret, get_error_message(ret)));
- }
-
ret = bundle_add(bundle, "data", data.serialize().c_str());
if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
return LogAndCreateResult(
common::PlatformResult UnsetChangeRequestPlaybackInfoListener();
common::PlatformResult CommandReply(const std::string& client_name, const std::string& request_id,
- const std::string& reply_id, const picojson::value& data);
+ const picojson::value& data);
common::PlatformResult SetCommandListener(const JsonCallback& callback);
common::PlatformResult UnsetCommandListener();