[Convergence] Fix for ConvergenceChannel. 29/100129/6
authorTomasz Marciniak <t.marciniak@samsung.com>
Fri, 25 Nov 2016 06:50:30 +0000 (15:50 +0900)
committertaekeun.kang <taekeun.kang@samsung.com>
Sun, 27 Nov 2016 23:59:54 +0000 (08:59 +0900)
[Feature] Create channel only in start() method.
Other methods related to Channel cause errors in case
channel is not present in channels vector.

[Verification] Code compiles.

Change-Id: Ibfaf6cde17f323fffcefc5b67b7e96a28498617c
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/convergence/convergence_api.js
src/convergence/convergence_app_communication_service.cc
src/convergence/convergence_app_communication_service.h
src/convergence/convergence_instance.cc
src/convergence/convergence_service.cc
src/convergence/convergence_service.h

index 7221f36..a61c8c3 100644 (file)
@@ -620,11 +620,6 @@ function AppCommunicationService() {
       value: null,
       writable: true,
       enumerable: false
-    },
-    _isStarted : {
-      value: false,
-      writable: true,
-      enumerable: false
     }
   });
 
@@ -669,7 +664,6 @@ native_.addListener('APP_COMMUNICATION_SERVICE_LISTENER', function(result) {
       native_.callIfPossible(s._connectCallback, s);
       break;
     case 'onStart':
-      s._isStarted = true;
       native_.callIfPossible(s._startCallback,
         new ChannelInfo(result.channel.uri, result.channel.id), null);
       break;
@@ -678,7 +672,6 @@ native_.addListener('APP_COMMUNICATION_SERVICE_LISTENER', function(result) {
         new ChannelInfo(result.channel.uri, result.channel.id), null);
       break;
     case 'onStop':
-      s._isStarted = false;
       native_.callIfPossible(s._stopCallback,
         new ChannelInfo(result.channel.uri, result.channel.id), null);
       break;
@@ -747,10 +740,6 @@ AppCommunicationService.prototype.start = function(channel, successCallback, err
     }
   ]);
 
-  if (this._isStarted) {
-    throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service already started.');
-  }
-
   var lid = this._serviceId;
   this._startCallback = successCallback;
   convergenceServices[lid] = this;
@@ -795,10 +784,6 @@ AppCommunicationService.prototype.stop = function(channel, successCallback, erro
     }
   ]);
 
-  if (!this._isStarted) {
-    throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service is not started.');
-  }
-
   var lid = -1;
   if (successCallback) {
     lid = this._serviceId;
@@ -852,10 +837,6 @@ AppCommunicationService.prototype.send = function(channel, payload, successCallb
     }
   ]);
 
-  if (!this._isStarted) {
-    throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service is not started.');
-  }
-
   var lid = this._serviceId;
   this._sendCallback = successCallback;
   convergenceServices[lid] = this;
index 8a80eb7..41b8fca 100644 (file)
@@ -85,7 +85,7 @@ common::TizenResult ConvergenceAppCommunicationService::Start(
 }
 
 common::TizenResult ConvergenceAppCommunicationService::Stop(
-  ConvergenceChannel& channel,
+  ConvergenceChannel* channel,
   const int cur_listener_id) {
   ScopeLogger();
 
@@ -96,28 +96,18 @@ common::TizenResult ConvergenceAppCommunicationService::Stop(
 
   UpdateListener(cur_listener_id);
 
-  const int error = conv_service_stop(service_handle, channel.GetHandle(), nullptr);
+  const int error = conv_service_stop(service_handle, channel->GetHandle(), nullptr);
   if (CONV_ERROR_NONE != error) {
-    return LogAndCreateTizenError(AbortError, "Failed to stop service");
+    return LogAndCreateTizenError(AbortError, "Failed to stop service channel");
   }
 
-  std::string removed_uri = channel.GetUri();
-  std::string removed_id = channel.GetId();
-
-  for (auto it = opened_channels.begin(); it != opened_channels.end(); ++it){
-    if((*it)->GetUri() == removed_uri && (*it)->GetId() == removed_id) {
-      LoggerD("Removing channel uri: [%s] id: [%s]", removed_uri.c_str(), removed_id.c_str());
-      delete *it;
-      (*it) = nullptr;
-      opened_channels.erase(it);
-      break;
-    }
-  }
+  delete channel;
+  channel = nullptr;
 
   return TizenSuccess();
 }
 
-common::TizenResult ConvergenceAppCommunicationService::GetClientList(ConvergenceChannel& channel,
+common::TizenResult ConvergenceAppCommunicationService::GetClientList(ConvergenceChannel* channel,
                                                                       const int cur_listener_id) {
   ScopeLogger();
 
@@ -129,16 +119,16 @@ common::TizenResult ConvergenceAppCommunicationService::GetClientList(Convergenc
 
   UpdateListener(cur_listener_id);
 
-  const int ret = conv_service_read(service_handle, channel.GetHandle(), nullptr);
+  const int ret = conv_service_read(service_handle, channel->GetHandle(), nullptr);
   if (CONV_ERROR_NONE != ret) {
-    return LogAndCreateTizenError(AbortError, "conv_service_stop error");
+    return LogAndCreateTizenError(AbortError, "conv_service_read error");
   }
 
   return TizenSuccess();
 }
 
 common::TizenResult ConvergenceAppCommunicationService::Send(
-  ConvergenceChannel& channel,
+  ConvergenceChannel* channel,
   ConvergencePayloadArray& payload,
   const int cur_listener_id) {
   ScopeLogger();
@@ -150,7 +140,7 @@ common::TizenResult ConvergenceAppCommunicationService::Send(
 
   UpdateListener(cur_listener_id);
 
-  const int error = conv_service_publish(service_handle, channel.GetHandle(), payload.GetHandle());
+  const int error = conv_service_publish(service_handle, channel->GetHandle(), payload.GetHandle());
   if (CONV_ERROR_NONE != error) {
     return LogAndCreateTizenError(AbortError, "Failed to publish message");
   } else {
@@ -386,7 +376,7 @@ common::TizenResult ConvergenceAppCommunicationServerService::Start(
 }
 
 common::TizenResult ConvergenceAppCommunicationServerService::Stop(
-  ConvergenceChannel& channel,
+  ConvergenceChannel* channel,
   const int cur_listener_id) {
   ScopeLogger();
 
@@ -397,7 +387,7 @@ common::TizenResult ConvergenceAppCommunicationServerService::Stop(
   picojson::object param;
   param[kServiceListenerStatus] = picojson::value(kServiceListenerStatusOk);
 
-  param[kChannel] = ConvergenceChannel::ToJson(channel.GetHandle());
+  param[kChannel] = ConvergenceChannel::ToJson(channel->GetHandle());
   param[kServiceResultType] = picojson::value(kServiceResultTypeOnStop);
 
   common::TizenResult result = ConvergenceAppCommunicationService::Stop(channel, cur_listener_id);
index ed759d3..3821886 100644 (file)
@@ -45,11 +45,11 @@ class ConvergenceAppCommunicationService : public ConvergenceService {
  public:
   virtual common::TizenResult Start(ConvergenceChannel* channel,
    const int cur_listener_id);
-  virtual common::TizenResult Stop(ConvergenceChannel& channel,
+  virtual common::TizenResult Stop(ConvergenceChannel* channel,
    const int cur_listener_id);
-  virtual common::TizenResult GetClientList(ConvergenceChannel& channel,
+  virtual common::TizenResult GetClientList(ConvergenceChannel* channel,
    const int cur_listener_id);
-  virtual common::TizenResult Send(ConvergenceChannel& channel,
+  virtual common::TizenResult Send(ConvergenceChannel* channel,
                                    ConvergencePayloadArray& payload,
                                    const int cur_listener_id);
   common::TizenResult SetListener(const int cur_listener_id);
@@ -77,7 +77,7 @@ class ConvergenceAppCommunicationServerService : public ConvergenceAppCommunicat
  public:
   virtual common::TizenResult Start(ConvergenceChannel* channel,
    const int cur_listener_id);
-  virtual common::TizenResult Stop(ConvergenceChannel& channel,
+  virtual common::TizenResult Stop(ConvergenceChannel* channel,
    const int cur_listener_id);
 };
 
index 91f6c4d..c905ee4 100644 (file)
@@ -430,11 +430,17 @@ common::TizenResult ConvergenceInstance::AppCommunicationServiceStart(const pico
       result = LogAndCreateTizenError(NotFoundError, "Can not find the service type = 1");
     } else {
       // Running the service start procedure
-      ConvergenceChannel* channel = new ConvergenceChannel
-          (ConvergenceUtils::GetArg(args, kJSArgumentChannel));  // memory would be managed in Start method
-      const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
+      auto channel_arg = ConvergenceUtils::GetArg(args, kJSArgumentChannel);
+      std::vector<ConvergenceChannel*>::iterator channel_it = service->GetChannel(channel_arg);
 
-      result = service->Start(channel, id);
+      if (service->IsChannelStarted(channel_it)) {
+        result = LogAndCreateTizenError(InvalidStateError, "Service is already started for the channel.");
+      } else {
+        ConvergenceChannel* channel = new ConvergenceChannel(channel_arg); // memory would be managed in Start method
+        const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
+
+        result = service->Start(channel, id);
+      }
     }
 
     //in case of failure call error callback, success callback will be triggered by listener
@@ -474,11 +480,17 @@ common::TizenResult ConvergenceInstance::AppCommunicationServiceSend(const picoj
       result = LogAndCreateTizenError(NotFoundError, "Can not find the service type = 1");
     } else {
       // Running the service send procedure
-      ConvergenceChannel channel(ConvergenceUtils::GetArg(args, kJSArgumentChannel));
-      ConvergencePayloadArray payload(ConvergenceUtils::GetArg(args, kJSArgumentPayload));
-      const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
+      auto channel_arg = ConvergenceUtils::GetArg(args, kJSArgumentChannel);
+      std::vector<ConvergenceChannel*>::iterator channel_it = service->GetChannel(channel_arg);
+
+      if (!service->IsChannelStarted(channel_it)) {
+        result = LogAndCreateTizenError(InvalidStateError, "Service is not started for the channel.");
+      } else {
+        ConvergencePayloadArray payload(ConvergenceUtils::GetArg(args, kJSArgumentPayload));
+        const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
 
-      result = service->Send(channel, payload, id);
+        result = service->Send(*channel_it, payload, id);
+      }
     }
 
     //in case of failure call error callback, success callback will be triggered by listener
@@ -517,10 +529,19 @@ common::TizenResult ConvergenceInstance::AppCommunicationServiceStop(const picoj
       result = LogAndCreateTizenError(NotFoundError, "Can not find the service type = 1");
     } else {
       // Running the service stop procedure
-      ConvergenceChannel channel(ConvergenceUtils::GetArg(args, kJSArgumentChannel));
-      const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
-
-      result = service->Stop(channel, id);
+      auto channel_arg = ConvergenceUtils::GetArg(args, kJSArgumentChannel);
+      std::vector<ConvergenceChannel*>::iterator channel_it = service->GetChannel(channel_arg);
+
+      if (!service->IsChannelStarted(channel_it)) {
+        result = LogAndCreateTizenError(InvalidStateError, "Service is not started for the channel.");
+      } else {
+        const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
+
+        result = service->Stop(*channel_it, id);
+        if (result) {
+          service->RemoveChannel(channel_it);
+        }
+      }
     }
 
     //in case of failure call error callback, success callback will be triggered by listener
@@ -559,11 +580,22 @@ common::TizenResult ConvergenceInstance::AppCommunicationServiceGetClientList(co
       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<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>()));
+      auto channel_arg = ConvergenceUtils::GetArg(args, kJSArgumentChannel);
+      std::vector<ConvergenceChannel*>::iterator channel_it = service->GetChannel(channel_arg);
+
+      if (!service->IsChannelStarted(channel_it)) {
+        result = LogAndCreateTizenError(InvalidStateError, "Service is not started for the channel.");
+      } else {
+        const int id = static_cast<int>(ConvergenceUtils::GetArg(args, kJSCurrentListenerId).get<double>());
+
+        result = service->GetClientList(*channel_it, id);
+      }
     }
 
-    this->Post(token, result);
+    //in case of failure call error callback, success callback will be triggered by listener
+    if (!result) {
+      this->Post(token, result);
+    }
   };
 
   std::thread(get_client_list, token).detach();
index 7c5fc62..61d9d3b 100644 (file)
@@ -32,6 +32,7 @@ namespace {
 static const std::string kServiceType = "serviceType";
 static const std::string kServiceConnectionState = "connectionState";
 static const std::string kId = "id";
+static const std::string kUri = "uri";
 static const std::string kVersion = "version";
 } // namespace
 
@@ -59,7 +60,7 @@ ConvergenceService::~ConvergenceService() {
   if (CONV_ERROR_NONE != error) {
     LoggerW("unset_listener error %d [%s]", error, get_error_message(error));
   } else {
-    LoggerD("listener usnet");
+    LoggerD("listener unset");
   }
 
   std::for_each(opened_channels.begin(), opened_channels.end(),
@@ -70,6 +71,7 @@ ConvergenceService::~ConvergenceService() {
     } else {
       LoggerD("2 service stopped");
     }
+    delete c;
   });
 
   // later destroy handle
@@ -186,5 +188,32 @@ picojson::value ConvergenceService::ToJson() const {
   return picojson::value(service_json);
 }
 
+std::vector<ConvergenceChannel*>::iterator ConvergenceService::GetChannel(const picojson::value &channel_json) {
+  ScopeLogger();
+
+  std::vector<ConvergenceChannel*>::iterator it;
+  const auto id = channel_json.get(kId).get<std::string>();
+  const auto uri = channel_json.get(kUri).get<std::string>();
+
+  for (it = opened_channels.begin(); it != opened_channels.end(); ++it) {
+    if((*it)->GetUri() == uri && (*it)->GetId() == id) {
+      LoggerD("Found channel uri: [%s] id: [%s]", uri.c_str(), id.c_str());
+      break;
+    }
+  }
+
+  return it;
+}
+
+bool ConvergenceService::IsChannelStarted(std::vector<ConvergenceChannel*>::iterator it) {
+  ScopeLogger();
+  return it != opened_channels.end();
+}
+
+void ConvergenceService::RemoveChannel(std::vector<ConvergenceChannel*>::iterator it) {
+  ScopeLogger();
+  opened_channels.erase(it);
+}
+
 } // namespace convergence
 }  // namespace extension
index 737debd..42ff0a6 100644 (file)
@@ -45,6 +45,9 @@ class ConvergenceService {
 
  public:
   void Refresh();
+  std::vector<ConvergenceChannel*>::iterator GetChannel(const picojson::value &channel_json);
+  bool IsChannelStarted(std::vector<ConvergenceChannel*>::iterator);
+  void RemoveChannel(std::vector<ConvergenceChannel*>::iterator);
 
  public:
   //conv_service_e get_type() const { return type_; }