#include <glib.h>
#include <d2d_conv_internal.h>
#include <stdlib.h>
+#include <memory>
#include "convergence/convergence_instance.h"
#include "convergence/convergence_channel_info.h"
}
common::TizenResult ConvergenceAppCommunicationService::Start(
- ConvergenceChannel& channel,
+ ConvergenceChannel* channel,
const int cur_listener_id) {
ScopeLogger();
LoggerI("Starting service [0x%x] with handle [0x%x]", this, service_handle_);
+ std::unique_ptr<ConvergenceChannel> ch_ptr(channel); // auto release memory in case of error
conv_service_h service_handle = FindServiceHandle();
if (!service_handle) {
UpdateListener(cur_listener_id);
- const int error = conv_service_start(service_handle, channel.GetHandle(), nullptr);
+ const int error = conv_service_start(service_handle, channel->GetHandle(), nullptr);
if (CONV_ERROR_NONE != error) {
return LogAndCreateTizenError(AbortError, "Failed to start service");
}
+ opened_channels.push_back(ch_ptr.release());
+
return TizenSuccess();
}
if (CONV_ERROR_NONE != error) {
return LogAndCreateTizenError(AbortError, "Failed to stop service");
}
+
+ 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;
+ }
+ }
+
return TizenSuccess();
}
}
common::TizenResult ConvergenceAppCommunicationServerService::Start(
- ConvergenceChannel& channel,
+ ConvergenceChannel* channel,
const int cur_listener_id) {
ScopeLogger();
picojson::object param;
param[kServiceListenerStatus] = picojson::value(kServiceListenerStatusOk);
- param[kChannel] = ConvergenceChannel::ToJson(channel.GetHandle()); // Define string as constant
+ param[kChannel] = ConvergenceChannel::ToJson(channel->GetHandle()); // Define string as constant
param[kServiceResultType] = picojson::value(kServiceResultTypeOnStart);
common::TizenResult result = ConvergenceAppCommunicationService::Start(channel, cur_listener_id);
ConvergenceAppCommunicationService& operator=(const ConvergenceAppCommunicationService&) = delete;
ConvergenceAppCommunicationService& operator=(ConvergenceAppCommunicationService&&) = delete;
public:
- virtual common::TizenResult Start(ConvergenceChannel& channel,
+ virtual common::TizenResult Start(ConvergenceChannel* channel,
const int cur_listener_id);
virtual common::TizenResult Stop(ConvergenceChannel& channel,
const int cur_listener_id);
ConvergenceAppCommunicationServerService& operator=(ConvergenceAppCommunicationServerService&&) = delete;
public:
- virtual common::TizenResult Start(ConvergenceChannel& channel,
+ virtual common::TizenResult Start(ConvergenceChannel* channel,
const int cur_listener_id);
virtual common::TizenResult Stop(ConvergenceChannel& channel,
const int cur_listener_id);
return channel_handle_;
}
+std::string ConvergenceChannel::GetUri() const {
+ return uri_;
+}
+
+std::string ConvergenceChannel::GetId() const {
+ return id_;
+}
+
void ConvergenceChannel::FromJson(const picojson::value &channel_json) {
ScopeLogger();
return;
}
- const std::string id = channel_json.get(kId).to_str();
- error = conv_channel_set_string(channel_handle_, kChannelId.c_str(), id.c_str());
+ id_ = channel_json.get(kId).to_str();
+ error = conv_channel_set_string(channel_handle_, kChannelId.c_str(), id_.c_str());
if (CONV_ERROR_NONE != error) {
trace_conv_error(error, __LINE__, "setting channel string Id");
}
- const std::string uri = channel_json.get(kUri).to_str();
- error = conv_channel_set_string(channel_handle_, kUri.c_str(), uri.c_str());
+ uri_ = channel_json.get(kUri).to_str();
+ error = conv_channel_set_string(channel_handle_, kUri.c_str(), uri_.c_str());
if (CONV_ERROR_NONE != error) {
trace_conv_error(error, __LINE__, "setting channel string URI");
}
public:
conv_channel_h GetHandle() const;
+ std::string GetId() const;
+ std::string GetUri() const;
void FromJson(const picojson::value &channel_json);
public:
private:
conv_channel_h channel_handle_;
+ std::string id_;
+ std::string uri_;
};
} // namespace convergence
result = LogAndCreateTizenError(NotFoundError, "Can not find the service type = 1");
} else {
// Running the service start procedure
- ConvergenceChannel channel(ConvergenceUtils::GetArg(args, kJSArgumentChannel));
+ 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>());
result = service->Start(channel, id);
ConvergenceService::~ConvergenceService() {
ScopeLogger();
- LoggerI("DESTROYING SERVICE HANDLE [0x0%x] in ConvergenceService destr", service_handle_);
+ LoggerI("!!! DESTROYING SERVICE HANDLE [0x0%x] in ConvergenceService destr", service_handle_);
+ // first try to stop service
+ int error = conv_service_unset_listener_cb(service_handle_);
+ if (CONV_ERROR_NONE != error) {
+ LoggerW("unset_listener error %d [%s]", error, get_error_message(error));
+ } else {
+ LoggerD("listener usnet");
+ }
+
+ std::for_each(opened_channels.begin(), opened_channels.end(),
+ [this](ConvergenceChannel* c){
+ int error = conv_service_stop(service_handle_, c->GetHandle(), nullptr);
+ if (CONV_ERROR_NONE != error) {
+ LoggerW("2 service was not stopped - error %d [%s]", error, get_error_message(error));
+ } else {
+ LoggerD("2 service stopped");
+ }
+ });
+
+ // later destroy handle
conv_service_destroy(service_handle_);
}
#include <string>
#include <unordered_map>
+#include <vector>
#include "common/tizen_result.h"
+#include "convergence/convergence_channel_info.h"
namespace extension {
namespace convergence {
conv_device_h device_; // TODO rename to device_handle_
conv_service_e type_;
mutable conv_service_h service_handle_;
+ std::vector<ConvergenceChannel*> opened_channels;
friend class ConvergenceAppCommunicationServerService; // It is needed to register the local service
private:
conv_service_connection_state_e connection_state_;