From 15657622b75ba090667c2d3d19dfe549126d4275 Mon Sep 17 00:00:00 2001 From: Tomasz Marciniak Date: Tue, 29 Nov 2016 09:02:59 +0900 Subject: [PATCH] [Convergence] Set readonly attributes for Service. [Verification] Code compiles. Change-Id: Ic0f96d60e8825d3d74098a87488f29d8a6e22342 Signed-off-by: Tomasz Marciniak --- src/convergence/convergence_api.js | 64 +++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/convergence/convergence_api.js b/src/convergence/convergence_api.js index faa26b42..59e2d2d1 100644 --- a/src/convergence/convergence_api.js +++ b/src/convergence/convergence_api.js @@ -68,6 +68,18 @@ function SetReadOnlyProperty(obj, n, v) { }); } +function InternalData(d) { + for (var prop in d) { + if (d.hasOwnProperty(prop)) { + this[prop] = d[prop]; + } + } +} + +function updateWithInternalData(src, dst) { + var d = new InternalData(src); + dst.connectionState = d; +} function getServiceConnectionStateName(connectionStateNumber) { switch(connectionStateNumber) { @@ -172,8 +184,9 @@ ConvergenceManager.prototype.startDiscovery = function(successCallback, continue; } - s.connectionState = getServiceConnectionStateName( - result.device.services[i].connectionState); + var state = getServiceConnectionStateName(result.device.services[i].connectionState); + updateWithInternalData({ connectionState: state }, s); + s._deviceId = id; services.push(s); } @@ -234,11 +247,29 @@ ConvergenceManager.prototype.stopDiscovery = function() { throw native_.getErrorObject(result); }; -function Service() { +function Service(connectionState_, type_) { console.log('Entered Service.constructor()'); - //validator_.isConstructorCall(this, Service); - this.connectionState = ConnectionState.NOT_CONNECTED; + var connectionState = connectionState_; + + Object.defineProperties(this, { + connectionState: { + enumerable: true, + get: function() { + return connectionState; + }, + set: function(v) { + if (v instanceof InternalData && v.hasOwnProperty('connectionState')) { + connectionState = v['connectionState']; + } + } + }, + type: { + value: type_, + writable: false, + enumerable: true + } + }) } native_.addListener('REMOTE_APP_CONTROL_SERVICE_LISTENER', function(result) { @@ -265,7 +296,7 @@ native_.addListener('REMOTE_APP_CONTROL_SERVICE_LISTENER', function(result) { switch (result_type) { case 'Connected': if (s) { // Service MUST NOT be null here - s.connectionState = ConnectionState.CONNECTED; + updateWithInternalData({ connectionState: ConnectionState.CONNECTED }, s); convergenceServices[lid] = s; } native_.callIfPossible(s._connectCallback, s); @@ -321,13 +352,14 @@ function RemoteAppControlService() { } }); - this.type = ServiceType.REMOTE_APP_CONTROL; + Service.call(this, ConnectionState.NOT_CONNECTED, ServiceType.REMOTE_APP_CONTROL); // Registering the service in the table of issued services convergenceServices[this._serviceId] = this; } RemoteAppControlService.prototype = new Service(); +RemoteAppControlService.prototype.constructor = RemoteAppControlService; RemoteAppControlService.prototype.connect = function(successCallback, errorCallback) { console.log('Entered RemoteAppControlService.connect()'); @@ -404,7 +436,7 @@ RemoteAppControlService.prototype.disconnect = function(successCallback, errorCa if (native_.isFailure(result)) { throw native_.getErrorObject(result); } else { - this.connectionState = ConnectionState.NOT_CONNECTED; + updateWithInternalData({ connectionState: ConnectionState.NOT_CONNECTED }, this); } native_.callIfPossible(successCallback, this); @@ -581,9 +613,10 @@ RemoteAppControlService.prototype.launchAppControl = function() { } }; -function AppCommunicationService() { +function AppCommunicationService(connectionState_, type_) { console.log('Entered AppCommunicationService.constructor()'); + Service.call(this, connectionState_, type_); // The device id is needed for getting the valid service handle on the // native layer // I have to implement this property here instead of base constructor in order @@ -640,6 +673,7 @@ function AppCommunicationService() { } AppCommunicationService.prototype = new Service(); +AppCommunicationService.prototype.constructor = AppCommunicationService; native_.addListener('APP_COMMUNICATION_SERVICE_LISTENER', function(result) { @@ -668,7 +702,7 @@ native_.addListener('APP_COMMUNICATION_SERVICE_LISTENER', function(result) { switch (result_type) { case 'Connected': if (s) { // Service MUST NOT be null here - s.connectionState = ConnectionState.CONNECTED; + updateWithInternalData({ connectionState: ConnectionState.CONNECTED }, s); convergenceServices[lid] = s; } native_.callIfPossible(s._connectCallback, s); @@ -948,8 +982,7 @@ function AppCommunicationServerService() { validator_.isConstructorCall(this, AppCommunicationServerService); - this.connectionState = ConnectionState.NOT_CONNECTED; - this.type = ServiceType.APP_COMM_SERVER; + AppCommunicationService.call(this, ConnectionState.NOT_CONNECTED, ServiceType.APP_COMM_SERVER); native_.callSync('AppCommunicationServerService_constructLocal', { deviceId: this._deviceId @@ -957,17 +990,18 @@ function AppCommunicationServerService() { } AppCommunicationServerService.prototype = new AppCommunicationService(); +AppCommunicationServerService.prototype.constructor = AppCommunicationServerService; function AppCommunicationClientService() { console.log('Entered AppCommunicationClientService.constructor()'); validator_.isConstructorCall(this, AppCommunicationClientService); - this.connectionState = ConnectionState.NOT_CONNECTED; - this.type = ServiceType.APP_COMM_CLIENT; + AppCommunicationService.call(this, ConnectionState.NOT_CONNECTED, ServiceType.APP_COMM_CLIENT); } AppCommunicationClientService.prototype = new AppCommunicationService(); +AppCommunicationClientService.prototype.constructor = AppCommunicationClientService; AppCommunicationClientService.prototype.connect = function(successCallback, errorCallback) { console.log('Entered AppCommunicationClientService.connect()'); @@ -1042,7 +1076,7 @@ AppCommunicationClientService.prototype.disconnect = function(successCallback, e if (native_.isFailure(result)) { throw native_.getErrorObject(result); } else { - this.connectionState = ConnectionState.NOT_CONNECTED; + updateWithInternalData({ connectionState: ConnectionState.NOT_CONNECTED }, this); } native_.callIfPossible(successCallback, this); -- 2.34.1