const std::string IoTivity::DEFAULT_PROVIDER = "Samsung";
const int IoTivity::DEFAULT_TIMEOUT = 3;
-IoTivity* IoTivity::instance = nullptr;
+IoTivity IoTivity::instance;
static FILE* openPersistentStorage(const char* ps_path, const char* mode)
{
LOG_D(TAG, "Connection Changed connection[%s], type = %s, state = %s", conn.c_str(),
getConType((OCConnectivityType)con_type).c_str(), state ? "true" : "false");
- if (nullptr != instance) {
- std::unique_lock<std::mutex> lock(instance->instance_mutex);
+ IoTivity* iotivity = IoTivity::getInstance();
+ std::unique_lock<std::mutex> lock(iotivity->instance_mutex);
- if (instance->cloud_host.find(conn) != std::string::npos) {
- LOG_I(TAG, "Cloud connection changed");
- if (!state) {
- LOG_D(TAG, "Connection losts.");
-// instance->connected = false;
- }
-
- instance->connected = state;
+ if (iotivity->cloud_host.find(conn) != std::string::npos) {
+ LOG_I(TAG, "Cloud connection changed");
+ if (!state) {
+ LOG_D(TAG, "Connection losts.");
+// iotivity->connected = false;
}
+
+ iotivity->connected = state;
}
}
if (OC_STACK_OK != res) {
LOG_E(TAG, "CAManager::setNetworkMonitorHandler failed with code = %d", res);
}
+
+ OC::OCPlatform::Configure(IoTivity::platform_config);
+ OC::OCPlatform::start();
}
IoTivity::~IoTivity()
{
FN_VISIT
signOut();
+ CAManager::unsetNetworkMonitorHandler();
OC::OCPlatform::stop();
}
IoTivity* IoTivity::getInstance()
{
- if (instance == nullptr) {
- try {
- instance = new IoTivity;
- OC::OCPlatform::Configure(IoTivity::platform_config);
- OC::OCPlatform::start();
- } catch (std::exception& e) {
- cleanUp();
- throw std::runtime_error("Out of memory");
- }
- }
- return instance;
+ return &instance;
}
IMqClient* IoTivity::getMqHandler()
{
IMqClient* handler = mq_handler.get();
+
if (handler == nullptr) {
LOG_W(TAG, "getMqHandler(): MqHandler uninitialized");
throw std::runtime_error("Failed to connect to message queue");
}
+
return handler;
}
void IoTivity::cleanUp()
{
- if (instance != nullptr) {
- delete instance;
- instance = nullptr;
- }
}
void IoTivity::registerDeviceInfo(const std::string& name, const std::string& model, const std::string& type)
void IoTivity::setPersistentStoragePath(std::string newPath)
{
- if (instance != nullptr) {
- instance->device_id.clear();
- }
+ instance.device_id.clear();
persistent_storage_path = newPath;
}