return instance;
}
-TizenResult IotconClientManager::RestoreHandles() {
- ScopeLogger();
-
- for (const auto& it : presence_map_) {
- LoggerD("Restoring handle for presence event with id: %lld", it.first);
-
- PresenceEventPtr presence = it.second;
- char* host = nullptr;
- char* resource_type = nullptr;
- iotcon_connectivity_type_e con_type = IOTCON_CONNECTIVITY_IPV4;
-
- auto res = IotconUtils::ExtractFromPresenceEvent(presence, &host,
- &con_type, &resource_type);
- if (!res){
- return res;
- }
-
- const iotcon_presence_h old_handle = presence->handle;
- int ret = iotcon_add_presence_cb(host, con_type, resource_type,
- PresenceHandler, this,&(presence->handle));
- if (IOTCON_ERROR_NONE != ret || nullptr == presence->handle) {
- LogAndReturnTizenError(IotconUtils::ConvertIotconError(ret),
- ("iotcon_add_presence_cb() failed: %d (%s)",
- ret, get_error_message(ret)));
- }
- if (old_handle) {
- LoggerD("destroy handle which is currently invalid: %p", old_handle);
- iotcon_remove_presence_cb(old_handle);
- }
- LoggerD("new handle: %p", (presence->handle));
- }
-
- return TizenSuccess();
-}
-
void IotconClientManager::PresenceHandler(iotcon_presence_h presence,
iotcon_error_e err,
iotcon_presence_response_h response,
public:
static IotconClientManager& GetInstance();
- common::TizenResult RestoreHandles();
common::TizenResult AddPresenceEventListener(const char* host,
const iotcon_connectivity_type_e con_type_e,
const char* resource_type,
#include "iotcon/iotcon_instance.h"
#include <thread>
-#include <iotcon-internal.h>
#include "common/logger.h"
#include "common/scope_exit.h"
#undef REGISTER_ASYNC
// initialize connection to iotcon service
- int ret = iotcon_connect();
+ int ret = iotcon_initialize();
if (IOTCON_ERROR_NONE != ret) {
LoggerE("Could not connnect to iotcon service: %s", get_error_message(ret));
} else {
LoggerD("Iotcon service connected");
- ret = iotcon_add_connection_changed_cb(ConnectionChangedCallback, this);
- if (IOTCON_ERROR_NONE != ret) {
- LoggerE("Could not add connection changed callback for iotcon service: %s",
- get_error_message(ret));
- } else {
- LoggerD("Iotcon connection changed callback is registered");
- }
ret = iotcon_start_presence(0);
if (IOTCON_ERROR_NONE != ret) {
}
}
-void IotconInstance::ConnectionChangedCallback(bool is_connected, void* user_data) {
- ScopeLogger();
-
- if (!is_connected) {
- LoggerD("Connection lost, need to wait for connection recovery");
- } else {
- IotconInstance* instance = static_cast<IotconInstance*>(user_data);
- if (!instance) {
- LoggerE("instance is NULL");
- return;
- }
-
- LoggerD("Connection recovered, restoring handles");
- auto ret = IotconServerManager::GetInstance().RestoreHandles();
- if (!ret) {
- LoggerD("Connection recovered, but restoring handles failed");
- }
-
- ret = IotconClientManager::GetInstance().RestoreHandles();
- if (!ret) {
- LoggerD("Connection recovered, but restoring presence failed");
- }
- }
-}
-
IotconInstance::~IotconInstance() {
ScopeLogger();
iotcon_stop_presence();
- iotcon_remove_connection_changed_cb(ConnectionChangedCallback, this);
- iotcon_disconnect();
+ iotcon_deinitialize();
}
common::TizenResult IotconInstance::ResourceGetObserverIds(const picojson::object& args) {
return instance;
}
-TizenResult IotconServerManager::RestoreHandles() {
- ScopeLogger();
-
- for (const auto& it : resource_map_) {
- LoggerD("Restoring handle for resource with id: %lld", it.first);
-
- ResourceInfoPtr resource = it.second;
- char* uri_path = nullptr;
- iotcon_resource_types_h res_types = nullptr;
- iotcon_resource_interfaces_h ifaces = 0;
- int properties = 0;
-
- auto res = IotconUtils::ExtractFromResource(resource, &uri_path,
- &res_types, &ifaces, &properties);
- if (!res){
- return res;
- }
-
- const iotcon_resource_h old_handle = resource->handle;
- LoggerD("Create resource from backup data, uri: %s, res_types: %p, ifaces: %d, properties: %d",
- uri_path, res_types, ifaces, properties);
-
- int ret = iotcon_resource_create(uri_path, res_types, ifaces, properties,
- RequestHandler, // request_callback
- this, // user_data
- &(resource->handle));
- if (IOTCON_ERROR_NONE != ret || nullptr == resource->handle) {
- LogAndReturnTizenError(IotconUtils::ConvertIotconError(ret),
- ("iotcon_resource_create() failed: %d (%s)",
- ret, get_error_message(ret)));
- }
- LoggerD("new handle: %p", (resource->handle));
- if (old_handle) {
- LoggerD("destroy handle which is currently invalid: %p", old_handle);
- iotcon_resource_destroy(old_handle);
- }
- }
-
- // rebind children
- for (const auto& it : resource_map_) {
- for (const auto& child : it.second->children) {
- auto result = IotconUtils::ConvertIotconError(iotcon_resource_bind_child_resource(it.second->handle, child->handle));
- if (!result) {
- LogAndReturnTizenError(result, ("iotcon_resource_bind_child_resource() failed"));
- }
- }
- }
-
- return TizenSuccess();
-}
-
void IotconServerManager::RequestHandler(iotcon_resource_h resource,
iotcon_request_h request, void *user_data) {
ScopeLogger();
public:
static IotconServerManager& GetInstance();
- common::TizenResult RestoreHandles();
common::TizenResult CreateResource(const std::string& uri_path,
const picojson::array& interfaces_array,
const picojson::array& types_array,
case IOTCON_ERROR_OUT_OF_MEMORY:
case IOTCON_ERROR_IOTIVITY:
case IOTCON_ERROR_REPRESENTATION:
- case IOTCON_ERROR_DBUS:
case IOTCON_ERROR_SYSTEM:
default:
return common::AbortError(error);