[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>
value: null,
writable: true,
enumerable: false
- },
- _isStarted : {
- value: false,
- writable: true,
- enumerable: false
}
});
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;
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;
}
]);
- if (this._isStarted) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service already started.');
- }
-
var lid = this._serviceId;
this._startCallback = successCallback;
convergenceServices[lid] = this;
}
]);
- if (!this._isStarted) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service is not started.');
- }
-
var lid = -1;
if (successCallback) {
lid = this._serviceId;
}
]);
- if (!this._isStarted) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Service is not started.');
- }
-
var lid = this._serviceId;
this._sendCallback = successCallback;
convergenceServices[lid] = this;
}
common::TizenResult ConvergenceAppCommunicationService::Stop(
- ConvergenceChannel& channel,
+ ConvergenceChannel* channel,
const int cur_listener_id) {
ScopeLogger();
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();
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();
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 {
}
common::TizenResult ConvergenceAppCommunicationServerService::Stop(
- ConvergenceChannel& channel,
+ ConvergenceChannel* channel,
const int cur_listener_id) {
ScopeLogger();
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);
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);
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);
};
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
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
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
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();
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
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(),
} else {
LoggerD("2 service stopped");
}
+ delete c;
});
// later destroy handle
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
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_; }