From 7c154c0f850ea08b655d0b29fa285b017478ad51 Mon Sep 17 00:00:00 2001 From: Andrzej Popowski Date: Sun, 20 Nov 2016 19:51:35 +0900 Subject: [PATCH] [Convergence] - Adding AppCommunicationService.getClientList method Change-Id: I08da9a524750ce0a28f6655ee49acfcacad59017 Signed-off-by: Andrzej Popowski --- src/convergence/convergence_api.js | 95 +++++++++++++++---- .../convergence_app_communication_service.cc | 21 +++- .../convergence_app_communication_service.h | 8 +- src/convergence/convergence_instance.cc | 38 ++++++++ src/convergence/convergence_instance.h | 1 + 5 files changed, 142 insertions(+), 21 deletions(-) diff --git a/src/convergence/convergence_api.js b/src/convergence/convergence_api.js index c2c93ad7..94b38487 100644 --- a/src/convergence/convergence_api.js +++ b/src/convergence/convergence_api.js @@ -20,6 +20,7 @@ var types_ = validator_.Types; var utils_ = xwalk.utils; var native_ = new utils_.NativeManager(extension); var T_ = utils_.type; +var converter_ = utils_.converter; // Flag showing if the discovery procedure has started var discoveryStarted = false; @@ -360,7 +361,7 @@ RemoteAppControlService.prototype.disconnect = function(successCallback, errorCa native_.callIfPossible(successCallback, this); }; -RemoteAppControlService.prototype.start = function(successCallback, errorCallback) { +RemoteAppControlService.prototype.start = function() { var args = validator_.validateArgs(arguments, [{ name: 'successCallback', type: types_.FUNCTION, @@ -379,17 +380,17 @@ RemoteAppControlService.prototype.start = function(successCallback, errorCallbac */ var lid = this._serviceId; - this._startCallback = successCallback; + this._startCallback = args.successCallback; convergenceServices[lid] = this; var callArgs = {}; - callArgs.reply = !!successCallback; + callArgs.reply = !!args.successCallback; callArgs.deviceId = this._deviceId; callArgs.curListenerId = lid; var callback = function(result) { - if (native.isFailure(result)) { - native_.callIfPossible(errorCallback, native_.getErrorObject(result)); + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, native_.getErrorObject(result)); } }; @@ -399,7 +400,7 @@ RemoteAppControlService.prototype.start = function(successCallback, errorCallbac } }; -RemoteAppControlService.prototype.stop = function(successCallback, errorCallback) { +RemoteAppControlService.prototype.stop = function() { var args = validator_.validateArgs(arguments, [{ name: 'successCallback', type: types_.FUNCTION, @@ -413,17 +414,17 @@ RemoteAppControlService.prototype.stop = function(successCallback, errorCallback }]); var lid = this._serviceId; - this._stopCallback = successCallback; + this._stopCallback = args.successCallback; convergenceServices[lid] = this; var callArgs = {}; - callArgs.reply = !!successCallback; + callArgs.reply = !!args.successCallback; callArgs.deviceId = this._deviceId; callArgs.curListenerId = lid; var callback = function(result) { - if (native.isFailure(result)) { - native_.callIfPossible(errorCallback, native_.getErrorObject(result)); + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, native_.getErrorObject(result)); } }; @@ -512,25 +513,25 @@ RemoteAppControlService.prototype.launchAppControl = function() { }]); var lid = this._serviceId; - this._remoteAppControlCallback = successCallback; + this._remoteAppControlCallback = args.successCallback; convergenceServices[lid] = this; var callArgs = {}; callArgs.appControl = args.appControl; - callArgs.appId = args.appId ? appId : ""; - callArgs.reply = !!successCallback; + callArgs.appId = args.appId ? args.appId : ""; + callArgs.reply = !!args.successCallback; callArgs.deviceId = this._deviceId; callArgs.curListenerId = lid; var callback = function(result) { - if (native.isFailure(result)) { - native_.callIfPossible(errorCallback, native_.getErrorObject(result)); + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, native_.getErrorObject(result)); } }; - var result = native.call('RemoteAppControlService_launchAppControl', callArgs, callback); - if (native.isFailure(result)) { - throw native.getErrorObject(result); + var result = native_.call('RemoteAppControlService_launchAppControl', callArgs, callback); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); } }; @@ -645,6 +646,23 @@ native_.addListener('APP_COMMUNICATION_SERVICE_LISTENER', function(result) { native_.callIfPossible(s._stopCallback, new ChannelInfo(result.channel.uri, result.channel.id), null); break; + case 'onRead': + var clients_array = []; + + + for(var i = 0; i < result.payload.length; i++) { + if (result.payload[i].key === 'client_list') { + var value = JSON.parse(result.payload[i].value); + var client = new ClientInfo( + converter_.toBoolean(value.isHost, false), + converter_.toString(value.clientId, false), + converter_.toLong(value.connectTime, false) + ); + clients_array.push(client); + } + } + native_.callIfPossible(s._getClientListCallback, clients_array); + break; case 'onMessage': { var payload = result.payload; native_.callIfPossible(s._listenerCallback, @@ -822,6 +840,47 @@ AppCommunicationService.prototype.send = function(channel, payload, successCallb } }; +AppCommunicationService.prototype.getClientList = function() { + var args = validator_.validateArgs(arguments, [{ + name: 'channel', + type: types_.PLATFORM_OBJECT, + values: tizen.ChannelInfo, + optional: false, + nullable: false + }, { + name: 'successCallback', + type: types_.FUNCTION, + optional: false, + nullable: false + }, { + name: 'errorCallback', + type: types_.FUNCTION, + optional: true, + nullable: true + } + ]); + + var lid = this._serviceId; + this._getClientListCallback = args.successCallback; + convergenceServices[lid] = this; + + var callArgs = {}; + callArgs.deviceId = this._deviceId; + callArgs.curListenerId = lid; + callArgs.channel_data = args.channel; + + var callback = function(result) { + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, native_.getErrorObject(result)); + } + }; + + var result = native_.call('AppCommunicationService_getClientList', callArgs, callback); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } +}; + AppCommunicationService.prototype.setListener = function(listenerCallback) { console.log('Entered AppCommunicationService.setListener()'); var args = validator_.validateArgs(arguments, [ diff --git a/src/convergence/convergence_app_communication_service.cc b/src/convergence/convergence_app_communication_service.cc index c2c33e5a..e88b82f3 100644 --- a/src/convergence/convergence_app_communication_service.cc +++ b/src/convergence/convergence_app_communication_service.cc @@ -96,6 +96,25 @@ common::TizenResult ConvergenceAppCommunicationService::Stop( if (CONV_ERROR_NONE != error) { return LogAndCreateTizenError(AbortError, "Failed to stop service"); } + return TizenSuccess(); +} + +common::TizenResult ConvergenceAppCommunicationService::GetClientList(ConvergenceChannel& channel, + const int cur_listener_id) { + ScopeLogger(); + + conv_service_h service_handle = FindServiceHandle(); + if (!service_handle) { + return LogAndCreateTizenError(NotFoundError, + "Service with specified type does not exist"); + } + + UpdateListener(cur_listener_id); + + const int ret = conv_service_read(service_handle, channel.GetHandle(), nullptr); + if (CONV_ERROR_NONE != ret) { + return LogAndCreateTizenError(AbortError, "conv_service_stop error"); + } return TizenSuccess(); } @@ -181,7 +200,7 @@ void ConvergenceAppCommunicationService::ServiceListenerCb(conv_service_h servic } else if (kServiceResultTypeOnClientDisconnect == result_type) { return; // TODO } else if (kServiceResultTypeOnRead == result_type) { - // TODO + param[kPayload] = ConvergencePayloadArray::ToJson(result); } else { // Unsupported payload type, ignoring it LoggerE("ERROR: Unsupported payload type; ignored"); diff --git a/src/convergence/convergence_app_communication_service.h b/src/convergence/convergence_app_communication_service.h index ca91bb38..57dd5de0 100644 --- a/src/convergence/convergence_app_communication_service.h +++ b/src/convergence/convergence_app_communication_service.h @@ -43,8 +43,12 @@ class ConvergenceAppCommunicationService : public ConvergenceService { ConvergenceAppCommunicationService& operator=(const ConvergenceAppCommunicationService&) = delete; ConvergenceAppCommunicationService& operator=(ConvergenceAppCommunicationService&&) = delete; public: - virtual common::TizenResult Start(ConvergenceChannel& channel, const int cur_listener_id); - virtual common::TizenResult Stop(ConvergenceChannel& channel, const int cur_listener_id); + virtual common::TizenResult Start(ConvergenceChannel& channel, + const int cur_listener_id); + virtual common::TizenResult Stop(ConvergenceChannel& channel, + const int cur_listener_id); + virtual common::TizenResult GetClientList(ConvergenceChannel& channel, + const int cur_listener_id); virtual common::TizenResult Send(ConvergenceChannel& channel, ConvergencePayloadArray& payload, const int cur_listener_id); diff --git a/src/convergence/convergence_instance.cc b/src/convergence/convergence_instance.cc index a9d69022..02a5e0eb 100644 --- a/src/convergence/convergence_instance.cc +++ b/src/convergence/convergence_instance.cc @@ -81,6 +81,7 @@ ConvergenceInstance::ConvergenceInstance() { REGISTER_ASYNC("AppCommunicationClientService_disconnect", AppCommunicationClientServiceDisconnect); REGISTER_ASYNC("AppCommunicationService_start", AppCommunicationServiceStart); REGISTER_ASYNC("AppCommunicationService_stop", AppCommunicationServiceStop); + REGISTER_ASYNC("AppCommunicationService_getClientList", AppCommunicationServiceGetClientList); REGISTER_ASYNC("AppCommunicationService_send", AppCommunicationServiceSend); REGISTER_ASYNC("RemoteAppControlService_disconnect", RemoteAppControlServiceDisconnect); REGISTER_ASYNC("RemoteAppControlService_connect", RemoteAppControlServiceConnect); @@ -528,6 +529,43 @@ common::TizenResult ConvergenceInstance::AppCommunicationServiceStop(const picoj return TizenSuccess(); } +common::TizenResult ConvergenceInstance::AppCommunicationServiceGetClientList(const picojson::object& args, + const common::AsyncToken& token) { + ScopeLogger(); + CHECK_PRIVILEGE(kPrivilegeInternet); + CHECK_PRIVILEGE(kPrivilegeBluetooth); + CHECK_PRIVILEGE(kPrivilegeDataSharing); + + CHECK_EXIST(args, kJSArgumentDeviceId); + CHECK_EXIST(args, kJSArgumentChannel); + CHECK_EXIST(args, kJSCurrentListenerId); + + auto get_client_list = [this, args](const common::AsyncToken& token) -> void { + ScopeLogger("get_client_list"); + + TizenResult result = TizenSuccess(); + + // Finding the service + ConvergenceAppCommunicationService *service = + static_cast( + ConvergenceManager::GetInstance(this).GetService(ConvergenceUtils::GetArg(args, kJSArgumentDeviceId).to_str().c_str(), + CONV_SERVICE_APP_TO_APP_COMMUNICATION)); + if (!service) { + result = LogAndCreateTizenError(NotFoundError, "Can not find the service type = 1"); + } else { + // Running the service getClientList procedure + ConvergenceChannel channel(ConvergenceUtils::GetArg(args, kJSArgumentChannel)); + result = service->GetClientList(channel, static_cast(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get())); + } + + this->Post(token, result); + }; + + std::thread(get_client_list, token).detach(); + + return TizenSuccess(); +} + common::TizenResult ConvergenceInstance::AppCommunicationServiceSetListener(const picojson::object& args) { ScopeLogger(); diff --git a/src/convergence/convergence_instance.h b/src/convergence/convergence_instance.h index 5695157d..9b849224 100644 --- a/src/convergence/convergence_instance.h +++ b/src/convergence/convergence_instance.h @@ -66,6 +66,7 @@ class ConvergenceInstance : public common::TizenInstance { // App Communication Service common::TizenResult AppCommunicationServiceStart(const picojson::object& args, const common::AsyncToken& token); common::TizenResult AppCommunicationServiceStop(const picojson::object& args, const common::AsyncToken& token); + common::TizenResult AppCommunicationServiceGetClientList(const picojson::object& args, const common::AsyncToken& token); common::TizenResult AppCommunicationServiceSend(const picojson::object& args, const common::AsyncToken& token); common::TizenResult AppCommunicationServiceSetListener(const picojson::object& args); common::TizenResult AppCommunicationServiceUnsetListener(const picojson::object& args); -- 2.34.1