From 442fd546d596206bc5c260a5511474ca29886e20 Mon Sep 17 00:00:00 2001
From: Piotr Kosko
Date: Thu, 21 Jan 2016 08:51:52 +0100
Subject: [PATCH] [Iotcon] Added server_manager and utils stubs
[Verification] Code compiles without errors
Change-Id: I0f606a402a0f0f6faf4b9939aab4330d133f0ef9
Signed-off-by: Piotr Kosko
---
src/iotcon/iotcon.gyp | 6 ++-
src/iotcon/iotcon_instance.cc | 77 +++++++++++++++++++++++++++--
src/iotcon/iotcon_instance.h | 3 ++
src/iotcon/iotcon_server_manager.cc | 50 +++++++++++++++++++
src/iotcon/iotcon_server_manager.h | 70 ++++++++++++++++++++++++++
src/iotcon/iotcon_utils.cc | 39 +++++++++++++++
src/iotcon/iotcon_utils.h | 38 ++++++++++++++
7 files changed, 277 insertions(+), 6 deletions(-)
create mode 100644 src/iotcon/iotcon_server_manager.cc
create mode 100644 src/iotcon/iotcon_server_manager.h
create mode 100644 src/iotcon/iotcon_utils.cc
create mode 100644 src/iotcon/iotcon_utils.h
diff --git a/src/iotcon/iotcon.gyp b/src/iotcon/iotcon.gyp
index 5d23de52..b5fd720f 100644
--- a/src/iotcon/iotcon.gyp
+++ b/src/iotcon/iotcon.gyp
@@ -14,7 +14,11 @@
'iotcon_extension.cc',
'iotcon_extension.h',
'iotcon_instance.cc',
- 'iotcon_instance.h'
+ 'iotcon_instance.h',
+ 'iotcon_server_manager.cc',
+ 'iotcon_server_manager.h',
+ 'iotcon_utils.cc',
+ 'iotcon_utils.h',
],
'includes': [
'../common/pkg-config.gypi',
diff --git a/src/iotcon/iotcon_instance.cc b/src/iotcon/iotcon_instance.cc
index 826671e7..0cd3e939 100644
--- a/src/iotcon/iotcon_instance.cc
+++ b/src/iotcon/iotcon_instance.cc
@@ -16,14 +16,40 @@
#include "iotcon/iotcon_instance.h"
-#include
-
#include "common/logger.h"
+#include "common/task-queue.h"
+#include "common/tools.h"
+
+#include "iotcon/iotcon_utils.h"
+
+using common::PlatformResult;
+using common::ErrorCode;
+using common::TypeMismatchException;
+using common::tools::ReportError;
+using common::tools::ReportSuccess;
+using common::Instance;
+using common::TaskQueue;
namespace extension {
namespace iotcon {
-IotconInstance::IotconInstance() {
+namespace {
+const std::string kCallbackId = "callbackId";
+const std::string kIsDiscoverable = "isDiscoverable";
+const std::string kIsObservable = "isObservable";
+const std::string kResourceTypes = "resourceTypes";
+const std::string kUriPath = "uriPath";
+
+#define CHECK_EXIST(args, name, out) \
+ if (!args.contains(name)) {\
+ std::string message = std::string(name) + " is required argument";\
+ common::tools::ReportError(\
+ common::PlatformResult(common::ErrorCode::TYPE_MISMATCH_ERR, message), &out);\
+ return;\
+ }
+}
+
+IotconInstance::IotconInstance() : manager_(this) {
LoggerD("Enter");
using std::placeholders::_1;
using std::placeholders::_2;
@@ -102,8 +128,10 @@ IotconInstance::IotconInstance() {
LoggerE("Could not connnect to iotcon service: %s", get_error_message(ret));
} else {
ret = iotcon_add_connection_changed_cb(ConnectionChangedCallback, nullptr);
- LoggerE("Could not add connection changed callback for iotcon service: %s",
- get_error_message(ret));
+ if (IOTCON_ERROR_NONE != ret) {
+ LoggerE("Could not add connection changed callback for iotcon service: %s",
+ get_error_message(ret));
+ }
}
}
@@ -280,7 +308,46 @@ void IotconInstance::IotconClientGetPlatformInfo(const picojson::value& args,
void IotconInstance::IotconServerCreateResource(const picojson::value& args,
picojson::object& out) {
LoggerD("Enter");
+ LoggerD("args: %s", args.serialize().c_str());
+ //args: {"callbackId":2,"isDiscoverable":true,
+ //"isObservable":true,"resourceTypes":["t1","t2"],"uriPath":"uriPath"}
+
+ CHECK_EXIST(args, kCallbackId, out)
+ CHECK_EXIST(args, kIsDiscoverable, out)
+ CHECK_EXIST(args, kIsObservable, out)
+ CHECK_EXIST(args, kResourceTypes, out)
+ CHECK_EXIST(args, kUriPath, out)
+
+ const double callback_id = args.get(kCallbackId).get();
+ const bool is_discoverable = args.get(kIsDiscoverable).get();
+ const bool is_observable = args.get(kIsObservable).get();
+ const auto& resource_type = args.get(kResourceTypes).get();
+ const std::string& uri_path = args.get(kUriPath).get();
+
+ auto create = [this, callback_id, is_discoverable, is_observable, resource_type, uri_path]
+ (const std::shared_ptr& response) -> void {
+ LoggerD("Create resource");
+ picojson::value result = picojson::value(picojson::object());
+ // TODO implement CreateResource
+ PlatformResult ret = manager_.CreateResource();
+ if (ret.IsError()) {
+ LogAndReportError(ret,&(response->get()));
+ return;
+ }
+ ReportSuccess(result, response->get());
+ };
+
+ auto create_response = [this, callback_id](const std::shared_ptr& response) -> void {
+ LoggerD("Response");
+ picojson::object& obj = response->get();
+ obj.insert(std::make_pair("callbackId", picojson::value{static_cast(callback_id)}));
+ LoggerD("message: %s", response->serialize().c_str());
+ Instance::PostMessage(this, response->serialize().c_str());
+ };
+
+ auto data = std::shared_ptr(new picojson::value(picojson::object()));
+ TaskQueue::GetInstance().Queue(create, create_response, data);
}
void IotconInstance::IotconServerRemoveResource(const picojson::value& args,
diff --git a/src/iotcon/iotcon_instance.h b/src/iotcon/iotcon_instance.h
index ea8b3cc9..97bfb528 100644
--- a/src/iotcon/iotcon_instance.h
+++ b/src/iotcon/iotcon_instance.h
@@ -19,6 +19,7 @@
#include "common/picojson.h"
#include "common/extension.h"
+#include "iotcon/iotcon_server_manager.h"
namespace extension {
namespace iotcon {
@@ -90,6 +91,8 @@ class IotconInstance : public common::ParsedInstance {
picojson::object& out);
void IotconSetTimeout(const picojson::value& args,
picojson::object& out);
+
+ IotconServerManager manager_;
};
} // namespace iotcon
diff --git a/src/iotcon/iotcon_server_manager.cc b/src/iotcon/iotcon_server_manager.cc
new file mode 100644
index 00000000..07663d4c
--- /dev/null
+++ b/src/iotcon/iotcon_server_manager.cc
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "iotcon/iotcon_server_manager.h"
+
+#include "common/logger.h"
+
+#include "iotcon/iotcon_instance.h"
+
+namespace extension {
+namespace iotcon {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+IotconServerManager::IotconServerManager(IotconInstance* instance)
+ : instance_(instance) {
+ LoggerD("Entered");
+}
+
+IotconServerManager::~IotconServerManager() {
+ LoggerD("Enter");
+}
+
+PlatformResult IotconServerManager::RestoreHandles() {
+ LoggerD("Entered");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult IotconServerManager::CreateResource() {
+ LoggerD("Entered");
+ // TODO implement
+ return PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet");
+}
+
+} // namespace iotcon
+} // namespace extension
diff --git a/src/iotcon/iotcon_server_manager.h b/src/iotcon/iotcon_server_manager.h
new file mode 100644
index 00000000..8e3e45a6
--- /dev/null
+++ b/src/iotcon/iotcon_server_manager.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WEBAPI_PLUGINS_IOTCON_SERVER_MANAGER_H__
+#define WEBAPI_PLUGINS_IOTCON_SERVER_MANAGER_H__
+
+#include
+#include