Update Resource Broker Initial Version
authorhunje.yeon <hunje.yeon@samsung.com>
Mon, 1 Jun 2015 06:21:35 +0000 (15:21 +0900)
committerUze Choi <uzchoi@samsung.com>
Mon, 1 Jun 2015 06:51:43 +0000 (06:51 +0000)
This will update the intial files for resource-manipulation branch.
README describes each functions in brief.
As a sample, the skeletons of resource broker moulde are included.

Change-Id: Id37fab5dd38fb7ef4eaea62c11103794e43734e3
Signed-off-by: hunje.yeon <hunje.yeon@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1157
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/basis/README [new file with mode: 0644]
service/basis/SConscript [new file with mode: 0644]
service/basis/resourceBroker/include/PresenceChecker.h [new file with mode: 0755]
service/basis/resourceBroker/include/PresenceInfo.h [new file with mode: 0755]
service/basis/resourceBroker/include/PrimitiveResource.h [new file with mode: 0755]
service/basis/resourceBroker/include/ResourceBroker.h [new file with mode: 0755]
service/basis/resourceBroker/src/PresenceChecker.cpp [new file with mode: 0755]
service/basis/resourceBroker/src/PresenceInfo.cpp [new file with mode: 0755]
service/basis/resourceBroker/src/PrimitiveResource.cpp [new file with mode: 0755]
service/basis/resourceBroker/src/ResourceBroker.cpp [new file with mode: 0755]

diff --git a/service/basis/README b/service/basis/README
new file mode 100644 (file)
index 0000000..1c65801
--- /dev/null
@@ -0,0 +1,14 @@
+== Brief Guide to Service Basis 
+
+Service Basis provides common functions such as resource broker, cache, resource container.
+
+1. resourceBroker 
+resourceBroker checks and monitors the status of resources. 
+This module notifies the change of reachability of selected resource to users. 
+
+2. resourceCache
+resourceCache provides the up-to-date "DATA" of remote resource to users.
+It tracks the most recent value of the selected resource and notifies to users according to the request notifying policy.
+
+3. resourceContainer
+Need to be updated.
diff --git a/service/basis/SConscript b/service/basis/SConscript
new file mode 100644 (file)
index 0000000..28f6e70
--- /dev/null
@@ -0,0 +1,34 @@
+#******************************************************************
+#
+# Copyright 2015 Samsung Electronics 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# basis build script
+##
+import platform
+
+Import('env')
+
+#SConscript('resourceBroker/SConscript')
+#SConscript('resourceCache/SConscript')
+#SConscript('resourceContainer/SConscript')
+#SConscript('serverBuilder/SConscript')
+#SConscript('sdk/SConscript')
+
+
diff --git a/service/basis/resourceBroker/include/PresenceChecker.h b/service/basis/resourceBroker/include/PresenceChecker.h
new file mode 100755 (executable)
index 0000000..e824444
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * PresenceChecker.h
+ *
+ *  Created on: May 20, 2015
+ *      Author: root
+ */
+
+#ifndef PRESENCECHECKER_H_
+#define PRESENCECHECKER_H_
+
+// Iotivity Base CAPI
+#include "ocstack.h"
+#include "logger.h"
+class PresenceChecker {
+public:
+    PresenceChecker();
+    virtual ~PresenceChecker();
+    OCStackResult addRequestCB(BrokerCB _cb);
+    OCStackResult deleteRequestCB(BrokerCB _cb);
+};
+
+#endif /* PRESENCECHECKER_H_ */
diff --git a/service/basis/resourceBroker/include/PresenceInfo.h b/service/basis/resourceBroker/include/PresenceInfo.h
new file mode 100755 (executable)
index 0000000..eec3d71
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * PresenceInfo.h
+ *
+ */
+
+#ifndef PRESENCEINFO_H_
+#define PRESENCEINFO_H_
+
+#include <iostream>
+#include <functional>
+#include <pthread.h>
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <vector>
+#include <list>
+#include <condition_variable>
+
+// Iotivity Base CAPI
+#include "ocstack.h"
+#include "logger.h"
+
+#include "PrimitiveResource.h"
+#include "ResourceBroker.h"
+
+enum class BROKER_STATE;
+
+typedef std::function<OCStackResult(BROKER_STATE)> BrokerCB;
+typedef std::function<OCStackApplicationResult(void *, OCDoHandle, OCClientResponse *)> RequestCB;
+
+struct BrokerRequesterInfo {
+    BrokerCB brockerCB;
+};
+
+class PresenceInfo: public PrimitiveResource {
+public:
+    PresenceInfo(PrimitiveResource & pResource, BrokerCB _cbrequesterList);
+    PresenceInfo(std::string _Uri, std::string _Address, BrokerCB _cb);
+    ~PresenceInfo();
+
+    const RequestCB pRequestCB;
+    const BrokerCB brokerCB;
+
+    std::list<BrokerRequesterInfo *> * requesterList;
+private:
+    OCStackApplicationResult requestCB(void * ctx, OCDoHandle handle,
+            OCClientResponse * clientResponse);
+};
+
+#endif /* PRESENCEINFO_H_ */
+
diff --git a/service/basis/resourceBroker/include/PrimitiveResource.h b/service/basis/resourceBroker/include/PrimitiveResource.h
new file mode 100755 (executable)
index 0000000..108aba2
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * PrimitiveResource.h
+ *
+ *  Created on: May 20, 2015
+ *      Author: root
+ */
+
+#ifndef PRIMITIVERESOURCE_H_
+#define PRIMITIVERESOURCE_H_
+
+class PrimitiveResource {
+public:
+    PrimitiveResource();
+    PrimitiveResource(std::string _Uri, std::string _Address):Uri(_Uri), Address(_Address){}
+    ~PrimitiveResource();
+
+    const std::string getUri();
+    const std::string getAddress();
+
+    const std::string Uri;
+    const std::string Address;
+};
+
+#endif /* PRIMITIVERESOURCE_H_ */
diff --git a/service/basis/resourceBroker/include/ResourceBroker.h b/service/basis/resourceBroker/include/ResourceBroker.h
new file mode 100755 (executable)
index 0000000..4ee3146
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * ResourceBroker.h
+ *
+ *  Created on: May 20, 2015
+ *      Author: root
+ */
+
+#ifndef RESOURCEBROKER_H_
+#define RESOURCEBROKER_H_
+
+#include <iostream>
+#include <functional>
+#include <pthread.h>
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <vector>
+#include <list>
+#include <mutex>
+#include <thread>
+#include <condition_variable>
+#include <algorithm>
+
+// Iotivity Base CAPI
+#include "ocstack.h"
+#include "logger.h"
+
+// internal class
+#include "PresenceInfo.h"
+
+#define CACHE_TAG PCF("BROKER")
+
+
+typedef std::function<OCStackResult(PrimitiveResource * resource)> DiscoverCB;
+enum class BROKER_STATE
+{
+    ALIVE = 0,
+    REQUESTED,
+    LOST_SIGNAL,
+    DESTROYED
+};
+class ResourceBroker {
+public:
+    ResourceBroker * getInstance();
+
+    OCStackResult discoverResource(PrimitiveResource & pResource, DiscoverCB cb);
+    OCStackResult discoverResource(const std::string uri, const std::string address, DiscoverCB cb);
+
+    OCStackResult hostResource(PrimitiveResource & pResource, BrokerCB cb);
+    OCStackResult hostResource(
+            const std::string Uri, const std::string address, BrokerCB cb);
+
+    OCStackResult cancelHostResource(PrimitiveResource & pResource);
+    OCStackResult cancelHostResource(
+            const std::string uri, const std::string address);
+
+    OCStackResult getResourceState(PrimitiveResource & pResource);
+    OCStackResult getResourceState(
+            const std::string address, const std::string uri);
+
+private:
+    ResourceBroker();
+    ~ResourceBroker();
+    bool isPrimitiveResource(PrimitiveResource& pResource, BrokerCB cb);
+    static ResourceBroker * s_instance;
+    static std::mutex s_mutexForCreation;
+    static std::list< PresenceInfo * > s_presenceInfo;
+};
+
+#endif /* RESOURCEBROKER_H_ */
diff --git a/service/basis/resourceBroker/src/PresenceChecker.cpp b/service/basis/resourceBroker/src/PresenceChecker.cpp
new file mode 100755 (executable)
index 0000000..6b07ab0
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * PresenceChecker.cpp
+ *
+ *  Created on: May 20, 2015
+ *      Author: root
+ */
+
+#include "PresenceChecker.h"
+
+PresenceChecker::PresenceChecker() {
+    // TODO Auto-generated constructor stub
+
+}
+
+PresenceChecker::~PresenceChecker() {
+    // TODO Auto-generated destructor stub
+}
+
diff --git a/service/basis/resourceBroker/src/PresenceInfo.cpp b/service/basis/resourceBroker/src/PresenceInfo.cpp
new file mode 100755 (executable)
index 0000000..40be10b
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * PresenceInfo.cpp
+ *
+ */
+
+#include "PresenceInfo.h"
+
+PresenceInfo::PresenceInfo(PrimitiveResource & pResource, BrokerCB _cb) :
+        Uri(pResource.getUri()), Address(pResource.getAddress()) {
+    pRequestCB =(RequestCB) (std::bind(&PresenceInfo::requestCB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
+    requesterList = new std::list<BrokerRequesterInfo *>();
+    BrokerRequesterInfo *requester = new BrokerRequesterInfo();
+    requester->brockerCB = _cb;
+    requesterList.push_back(requester);
+}
+
+PresenceInfo::PresenceInfo(std::string _Uri, std::string _Address, BrokerCB _cb) :
+        Uri(_Uri), Address(_Address) {
+    pRequestCB =(RequestCB) (std::bind(&PresenceInfo::requestCB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
+    requesterList = new std::list<BrokerRequesterInfo *>();
+    BrokerRequesterInfo *requester = new BrokerRequesterInfo();
+    requester->brockerCB = _cb;
+    requesterList.push_back(requester);
+}
+
+PresenceInfo::~PresenceInfo() {
+    // TODO Auto-generated destructor stub
+    // delete requester all
+//     auto it = requesterList->begin();
+
+    requesterList->clear();
+    delete requesterList;
+}
+
+OCStackApplicationResult PresenceInfo::requestCB(void * ctx, OCDoHandle handle,
+        OCClientResponse * clientResponse) {
+    OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION;
+
+    return ret;
+}
+
+OCStackResult PresenceInfo::addRequestCB(BrokerCB _cb) {
+    OCStackResult ret = OC_STACK_ERROR;
+
+    requesterList->push_back(_cb);
+
+    return ret;
+}
diff --git a/service/basis/resourceBroker/src/PrimitiveResource.cpp b/service/basis/resourceBroker/src/PrimitiveResource.cpp
new file mode 100755 (executable)
index 0000000..68b0a8a
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * PrimitiveResource.cpp
+ *
+ *  Created on: May 20, 2015
+ *      Author: root
+ */
+
+#include "PrimitiveResource.h"
+
+PrimitiveResource::PrimitiveResource() {
+    // TODO Auto-generated constructor stub
+
+}
+
+PrimitiveResource::~PrimitiveResource() {
+    // TODO Auto-generated destructor stub
+}
+
diff --git a/service/basis/resourceBroker/src/ResourceBroker.cpp b/service/basis/resourceBroker/src/ResourceBroker.cpp
new file mode 100755 (executable)
index 0000000..7d35fc1
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * ResourceBroker.cpp
+ *
+ */
+
+#include "ResourceBroker.h"
+
+#define OIC_COAP "coap://"
+#define DEFAULT_CONTEXT_VALUE 0x99
+
+typedef std::function<OCStackApplicationResult(void *, OCDoHandle, OCClientResponse *)> FindCB;
+
+ResourceBroker * ResourceBroker::s_instance = null;
+
+class DiscoverResource: public PrimitiveResource {
+public:
+    DiscoverResource();
+    DiscoverResource(PrimitiveResource & pResource, DiscoverCB _cb) :
+            Uri(pResource.getUri()), Address(pResource.getAddress()), discovercb(_cb) {
+    pFindCB = (FindCB)(std::bind(&DiscoverResource::findCB,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
+        }
+    DiscoverResource(std::string _Uri, std::string _Address, DiscoverCB _cb) :
+            Uri(_Uri), Address(_Address), discovercb(_cb) {
+    pFindCB = (FindCB)(std::bind(&DiscoverResource::findCB, this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
+        }
+    ~DiscoverResource();
+    const DiscoverCB discovercb;
+    const FindCB pFindCB;
+
+private:
+    OCStackApplicationResult findCB(void * ctx, OCDoHandle handle,OCClientResponse * clientResponse)
+    {
+        OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION;
+        // parse about resource info
+
+        // create primitiveResource
+        PrimitiveResource * retResource = new PrimitiveResource(this->Uri, this->Address);
+        // callback
+        this->discovercb(retResource);
+        //why using auto_ptr?
+        std::auto_ptr bye_bye(this);
+        /////////////////////////////////////////////////////////////////////////////
+        std::cout << "Is Possible?? : " << this->getAddress() << std::endl;
+        /////////////////////////////////////////////////////////////////////////////
+        return ret;
+     }
+};
+
+        ResourceBroker::ResourceBroker() {
+            // TODO Auto-generated constructor stub
+
+        }
+
+        ResourceBroker::~ResourceBroker() {
+            // TODO Auto-generated destructor stub
+        }
+
+        ResourceBroker * ResourceBroker::getInstance() {
+            if (!s_instance) {
+                s_mutexForCreation.lock();
+                if (!s_instance) {
+                    s_instance = new ResourceBroker();
+                }
+                s_mutexForCreation.unlock();
+            }
+            return s_instance;
+        }
+
+        OCStackResult ResourceBroker::discoverResource(const std::string _uri,
+                const std::string _address, DiscoverCB _cb) {
+            OCStackResult ret = OC_STACK_INVALID_PARAM;
+
+            if (_uri.empty() || _address.empty()) {
+                return ret;
+            }
+            if (!_cb) {
+                ret = OC_STACK_INVALID_CALLBACK;
+                return ret;
+            }
+
+            DiscoverResource * dResource = new DiscoverResource(_uri, _address, _cb);
+            OCDoHandle handle;
+            OCCallbackData cbData;
+            std::string fullAddress = OIC_COAP + _address;
+
+            cbData.cb = dResource->pFindCB;
+            cbData.context = (void *) DEFAULT_CONTEXT_VALUE;
+            cbData.cd = NULL;
+
+            return OCDoResource(&handle, OC_REST_GET, fullAddress.c_str(), _uri.c_str(), 0, OC_ALL,
+                    OC_LOW_QOS, &cbData, NULL, 0);
+        }
+
+        OCStackResult ResourceBroker::discoverResource(PrimitiveResource & pResource,
+                DiscoverCB _cb) {
+            return this->discoverResource(pResource.getUri(), pResource.getAddress(), _cb);
+        }
+
+        OCStackResult ResourceBroker::hostResource(const std::string _uri,const std::string _address, BrokerCB _cb)
+        {
+            OCStackResult ret = OC_STACK_INVALID_PARAM;
+            if (uri.empty() || address.empty()) {
+                return ret;
+            }
+            if (!cb) {
+                ret = OC_STACK_INVALID_CALLBACK;
+                return ret;
+            }
+
+            return ret;
+        }
+        OCStackResult ResourceBroker::hostResource(PrimitiveResource & pResource, BrokerCB cb)
+        {
+            OCStackResult ret = OC_STACK_INVALID_PARAM;
+            if (pResource.getUri().empty() || pResource.getAddress().empty()) {
+                return ret;
+            }
+            if (!cb) {
+                ret = OC_STACK_INVALID_CALLBACK;
+                return ret;
+            }
+
+            return ret;
+        }
+        bool ResourceBroker::isPrimitiveResource(PrimitiveResource& pResource, BrokerCB cb)
+        {
+            if(s_presenceInfo.empty())
+                return false;
+            else
+            {
+                std::list<PresenceInfo*>::iterator it;
+                PresenceInfo item = new PresenceInfo(pResource.getUri(), pResource.getAddress(), cb);
+                item.PrimitiveResource(pResource.getUri(),pResource.getAddress());
+                it = std::find(s_presenceInfo.begin(),s_presenceInfo.end(),item);
+                if(it==s_presenceInfo.end())
+                    return false;
+                else
+                    return true;
+            }
+        }