-VPATH = ../../src:../../../SampleApp/linux/sampleConsumer:../../../SampleApp/linux/sampleProvider
+VPATH = ../../src:../../src/linux:../../../SampleApp/linux/sampleConsumer:../../../SampleApp/linux/sampleProvider
OCPATH = ../../../../../resource
OCINCLUDE = $(OCPATH)/include
CXX=g++
CXXFLAGS = -O2 -g -Wall -fmessage-length=0 -std=c++0x -I$(NOTIFICATIONINCLUDE) -I$(OCINCLUDE) -I$(STACKINCLUDE) -I$(SOCKETINCLUDE) -I$(OCLOGGERINCLUDE) -I$(BOOSTPATH)
-OBJS = ResourceManager.o RegistrationManager.o VirtualRepresentation.o NotificationManager.o LinuxMain.o
+#OBJS = ResourceManager.o RegistrationManager.o VirtualRepresentation.o NotificationManager.o LinuxMain.o
+OBJS = OICPlatformConfig.o HostingHandler.o HostingInterface.o ResourceManager.o RegistrationManager.o VirtualRepresentation.o NotificationManager.o main.o
LIBS = $(OCPATH)/release/obj/liboc.a $(OCPATH)/csdk/linux/release/liboctbstack.a $(OCPATH)/oc_logger/lib/oc_logger.a
--- /dev/null
+
+#ifndef HOSTINGCONFIG_H_
+#define HOSTINGCONFIG_H_
+
+#include "NotificationManager.h"
+
+
+enum class HostingMode
+{
+ None,
+ ManualMode,
+ AutomaticMode,
+};
+
+enum class AutomaticMethod
+{
+ None,
+ NetworkStatusChange,
+ Timer,
+ DeviceStatus
+};
+
+enum class NotifyMethod
+{
+ Frequence,
+ Equalization,
+ Granularity,
+ None
+};
+
+enum class NotifyFrequency
+{
+ OnTime,
+ Periodically,
+ None
+};
+
+
+struct HostingConfig
+{
+ HostingMode hostingMode;
+ AutomaticMethod automaticMethod;
+ NotifyMethod notifyMethod;
+ NotifyFrequency frequency;
+};
+
+#endif /* HOSTINGCONFIG_H_ */
--- /dev/null
+/*
+ * HostingHandler.h
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#ifndef HOSTINGHANDLER_H_
+#define HOSTINGHANDLER_H_
+
+#include "NotificationManager.h"
+
+class HostingHandler
+{
+public:
+
+ static void initialize();
+ static void initialize(HostingConfig cfg);
+ static HostingHandler *getInstance();
+
+ void setHostingConfig(HostingConfig cfg);
+
+ void changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod = AutomaticMethod::None);
+ void changeAutomaticHostingMethod(AutomaticMethod autoMethod);
+ void changeNotifiyMethod(NotifyMethod notifyMethod);
+ void changeNotifyFrequencyType(NotifyFrequency notifyFrequency);
+
+private:
+
+ HostingHandler();
+ ~HostingHandler();
+
+ static HostingHandler *s_instance;
+ static mutex s_mutexForCreation;
+
+ HostingConfig hostingCfg;
+
+ static std::function< void(bool isHosting) > s_findHostingCandidate;
+ static std::function< void(std::string) > s_addExtraStr;
+ static std::function< void(std::shared_ptr< OCResource > resource) > s_startHosting;
+ static std::function< void(OCResourceHandle resourceHandle) > s_notify;
+
+ void startFindHost();
+ void onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle);
+ void onFoundCandidate(std::shared_ptr< OCResource > resource);
+
+ void runAutomaticHosting(AutomaticMethod autoMethod);
+ void stopAutomaticHosting();
+
+ void notifyFrequence(OCResourceHandle resourceHandle);
+
+};
+
+#endif /* HOSTINGHANDLER_H_ */
--- /dev/null
+/*
+ * HostingInterface.h
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#ifndef HOSTINGINTERFACE_H_
+#define HOSTINGINTERFACE_H_
+
+#include "NotificationManager.h"
+
+using namespace OC;
+using namespace OCPlatform;
+
+class HostingInterface
+{
+
+public:
+ HostingInterface();
+ ~HostingInterface();
+
+ int setOnFoundHostingCandidate(
+ std::function< void(std::shared_ptr< OCResource > resource) > func);
+ int setOnObserve(std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) > func);
+
+ int setFindHosting(std::function< void(bool isHosting) > &func);
+ int setStartHosting(std::function< void(std::shared_ptr< OCResource > resource) > &func);
+ int setNotifyObservers(std::function< void(OCResourceHandle resourceHandle) > &func);
+ int setAddExtraStr(std::function< void(std::string) > &func);
+
+};
+
+#endif /* HOSTINGINTERFACE_H_ */
#include <string>
#include <vector>
#include <list>
+#include <condition_variable>
-#include "OCApi.h"
-#include "OCPlatform.h"
-#include "OCResource.h"
-#include "OCResourceRequest.h"
-#include "OCResourceResponse.h"
-#include "ocstack.h"
-
+#include "OICPlatformConfig.h"
+#include "HostingConfig.h"
#include "ResourceManager.h"
#include "RegistrationManager.h"
#include "VirtualRepresentation.h"
+#include "HostingHandler.h"
+#include "HostingInterface.h"
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <net/if.h>
-#define ISFORDEMO 1
+#define IN
+#define OUT
using namespace OC;
using namespace OCPlatform;
private:
- static NotificationManager *s_instance;
-// static OCPlatform *s_nmOCPlatform;
-
- static PlatformConfig s_cfg;
+ NotificationManager();
+ NotificationManager(HostingConfig cfg);
+ ~NotificationManager();
- std::function< void(AttributeMap &inputAttMap) > m_print;
- std::function< void(std::shared_ptr< OCResource > resource) > m_onfound;
- std::function< void(AttributeMap &inputAttMap) > m_onObserve;
+ static NotificationManager *s_instance;
+ static mutex s_mutexForCreation;
- std::function< void(std::shared_ptr< OCResource > resource) > m_startHosting;
- std::function< void() > m_findHosting;
- std::function< void(std::string) > m_addExtraStr;
+ int getNetInfo(IN int& sck, IN struct ifreq* item, OUT std::string& ip_addres);
+ bool scanAndGetNetworkInterface(OUT std::string& ip_addres);
public:
- NotificationManager();
- ~NotificationManager();
-
+ static void initialize();
+ static void initialize(HostingConfig cfg);
static NotificationManager *getInstance();
- void initialize();
- void findHostingCandidate();
void registerHostingEventListener();
- int setPrint(std::function< void(AttributeMap &inputAttMap) > func);
- int setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) > func);
- int setOnObserve(std::function< void(AttributeMap &inputAttMap) > func);
-
- std::function< void(AttributeMap &inputAttMap) > getPrint();
- std::function< void(std::shared_ptr< OCResource > resource) > getOnFoundHostingCandidate();
- std::function< void(AttributeMap &inputAttMap) > getOnObserve();
-
- int setStartHosting(std::function< void(std::shared_ptr< OCResource > resource) > &func);
- int setFindHosting(std::function< void() > &func);
- int setAddExtraStr(std::function< void(std::string) > &func);
-
- std::function< void(std::shared_ptr< OCResource > resource) > getStartHosting();
- std::function< void() > getFindHosting();
- std::function< void(std::string) > getAddExtraStr();
-
};
#endif /* NOTIFICATIONMANAGER_H_ */
--- /dev/null
+/*
+ * OICPlatformConfig.h
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#ifndef OICPLATFORMCONFIG_H_
+#define OICPLATFORMCONFIG_H_
+
+#include "OCApi.h"
+#include "OCPlatform.h"
+#include "OCResource.h"
+#include "OCResourceRequest.h"
+#include "OCResourceResponse.h"
+#include "ocstack.h"
+
+using namespace OC;
+using namespace OCPlatform;
+
+class OICPlatformConfig
+{
+private:
+
+ OICPlatformConfig();
+ ~OICPlatformConfig();
+
+ static OICPlatformConfig *s_instance;
+ static mutex s_mutexForCreation;
+ static PlatformConfig s_cfg;
+
+public:
+
+ void initialize();
+ static OICPlatformConfig *getInstance();
+ void setIP(std::string ipaddress);
+};
+
+#endif /* OICPLATFORMCONFIG_H_ */
using namespace OC;
using namespace OCPlatform;
+class OICPlatformConfig;
class VirtualRepresentation;
class RegistrationManager
{
private:
+ RegistrationManager();
+ ~RegistrationManager();
+
static RegistrationManager *s_instance;
+ static mutex s_mutexForCreation;
public:
- RegistrationManager();
- ~RegistrationManager();
static RegistrationManager *getInstance();
#ifndef RESOURCEMANAGER_H_
#define RESOURCEMANAGER_H_
-#include <map>
-#include <memory>
-#include <string>
-#include "OCPlatform.h"
-#include "OCResource.h"
-#include "OCResourceResponse.h"
-#include "ocstack.h"
-
#include "NotificationManager.h"
using namespace OC;
using namespace OCPlatform;
+
+class OICPlatformConfig;
class VirtualRepresentation;
class ResourceManager
{
private:
- static ResourceManager *s_instance;
+ ResourceManager();
+ ~ResourceManager();
+ static ResourceManager *s_instance;
+ static mutex s_mutexForCreation;
static std::list< VirtualRepresentation > s_resourceList;
-
static std::string s_extraStr;
void foundResourceforhosting(std::shared_ptr< OCResource > resource);
void saveResourceDB();
public:
- ResourceManager();
- virtual ~ResourceManager();
+
+ std::function< void(std::shared_ptr< OCResource > resource) > m_onFoundforHosting;
+ std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) > m_onObserve;
+ std::function< void(OCResourceHandle resourceHandle) > m_notify;
static ResourceManager *getInstance();
- VirtualRepresentation findVirtualRepresentation(std::string uri);
- OCStackResult findNMResource(const std::string& host , const std::string& resourceName ,
- bool ishosting);
+ void findNMResource(bool isHosting);
+
+ void onFoundforHostingDefault(std::shared_ptr< OCResource > resource);
+ void onObserveDefault(AttributeMap &inputAttMap, OCResourceHandle resourceHandle);
+ void notifyObserversDefault(OCResourceHandle resourceHandle);
+
void startHosting(std::shared_ptr< OCResource > resource);
+ void notifyObservers(OCResourceHandle resourceHandle);
+ VirtualRepresentation findVirtualRepresentation(std::string uri);
AttributeMap copyAttributeMap(AttributeMap &inputAttMap);
bool isEmptyAttributeMap(AttributeMap &inputAttMap);
-
void printAttributeMap(AttributeMap &inputAttMap);
- void onFoundReport(std::shared_ptr< OCResource > resource);
void addExtraStr(std::string str);
std::string getExtraStr();
#ifndef VIRTUALREPRESENTATION_H_
#define VIRTUALREPRESENTATION_H_
-#include <functional>
-#include <condition_variable>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
#include "NotificationManager.h"
#define SUCCESS_RESPONSE 0
std::string addVirtualTag(std::string uri);
OCEntityHandlerResult entityHandler(const std::shared_ptr<OCResourceRequest> request ,
- const std::shared_ptr<OCResourceResponse> response);
+ const std::shared_ptr<OCResourceResponse> response);
void onObserve(const HeaderOptions &headerOption, const OCRepresentation &rep ,
- const int eCode , const int sequenceNumber);
+ const int eCode , const int sequenceNumber);
};
--- /dev/null
+/*
+ * HostingHandler.cpp
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#include "HostingHandler.h"
+
+HostingHandler *HostingHandler::s_instance = NULL;
+mutex HostingHandler::s_mutexForCreation;
+
+std::function< void(bool isHosting) > HostingHandler::s_findHostingCandidate;
+std::function< void(std::shared_ptr< OCResource > resource) > HostingHandler::s_startHosting;
+std::function< void(OCResourceHandle resourceHandle) > HostingHandler::s_notify;
+std::function< void(std::string) > HostingHandler::s_addExtraStr;
+
+HostingHandler::HostingHandler()
+{
+
+}
+
+HostingHandler::~HostingHandler()
+{
+
+}
+
+HostingHandler *HostingHandler::getInstance()
+{
+ if(!s_instance)
+ {
+ s_mutexForCreation.lock();
+ if(!s_instance)
+ {
+ s_instance = new HostingHandler();
+ }
+ s_mutexForCreation.unlock();
+ }
+
+ return s_instance;
+}
+
+void HostingHandler::initialize()
+{
+ // Create Hosting Handler instance
+ HostingHandler *ptr = HostingHandler::getInstance();
+
+ // Registration interface about hosting
+ HostingInterface hostingInterface;
+
+ hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
+ hostingInterface.setStartHosting(ptr->s_startHosting);
+ hostingInterface.setNotifyObservers(ptr->s_notify);
+ hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
+
+ hostingInterface.setOnFoundHostingCandidate(
+ std::function< void(std::shared_ptr< OCResource > resource) >(
+ std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() , std::placeholders::_1)));
+ hostingInterface.setOnObserve(
+ std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
+ std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1, std::placeholders::_2)));
+
+ ptr->s_addExtraStr("virtual");
+ ptr->startFindHost();
+}
+
+void HostingHandler::initialize(HostingConfig cfg)
+{
+ // Create Hosting Handler instance
+ HostingHandler *ptr = HostingHandler::getInstance();
+
+ // Registration interface about hosting
+ HostingInterface hostingInterface;
+
+ hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
+ hostingInterface.setStartHosting(ptr->s_startHosting);
+ hostingInterface.setNotifyObservers(ptr->s_notify);
+ hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
+
+ hostingInterface.setOnFoundHostingCandidate(
+ std::function< void(std::shared_ptr< OCResource > resource) >(
+ std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() , std::placeholders::_1)));
+ hostingInterface.setOnObserve(
+ std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
+ std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1, std::placeholders::_2)));
+
+ // Set Hosting Config
+ ptr->setHostingConfig(cfg);
+ ptr->changeHostingMode(ptr->hostingCfg.hostingMode);
+
+ ptr->s_addExtraStr("virtual");
+ ptr->startFindHost();
+}
+
+void HostingHandler::setHostingConfig(HostingConfig cfg)
+{
+ hostingCfg.automaticMethod = cfg.automaticMethod;
+ hostingCfg.frequency = cfg.frequency;
+ hostingCfg.hostingMode = cfg.hostingMode;
+ hostingCfg.notifyMethod = cfg.notifyMethod;
+}
+
+void HostingHandler::changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod)
+{
+ if(hostingCfg.hostingMode != hostingMode)
+ {
+ hostingCfg.hostingMode = hostingMode;
+ if(hostingCfg.hostingMode == HostingMode::AutomaticMode)
+ {
+ runAutomaticHosting(autoMethod);
+ }
+ else
+ {
+ stopAutomaticHosting();
+ }
+ }
+}
+
+void HostingHandler::runAutomaticHosting(AutomaticMethod autoMethod)
+{
+ // TODO Triggering from Event Listener.
+ if(hostingCfg.automaticMethod != autoMethod)
+ {
+ hostingCfg.automaticMethod = autoMethod;
+ switch(hostingCfg.automaticMethod)
+ {
+ case AutomaticMethod::None:
+ break;
+ case AutomaticMethod::Timer:
+ case AutomaticMethod::DeviceStatus:
+ case AutomaticMethod::NetworkStatusChange:
+ default:
+ break;
+ }
+ }
+}
+
+void HostingHandler::stopAutomaticHosting()
+{
+ if(hostingCfg.automaticMethod != AutomaticMethod::None)
+ {
+// TODO
+ }
+}
+
+void HostingHandler::changeAutomaticHostingMethod(AutomaticMethod autoMethod)
+{
+// TODO
+}
+void HostingHandler::changeNotifiyMethod(NotifyMethod notifyMethod)
+{
+// TODO
+}
+void HostingHandler::changeNotifyFrequencyType(NotifyFrequency notifyFrequency)
+{
+// TODO
+}
+
+void HostingHandler::startFindHost()
+{
+ if(hostingCfg.hostingMode != HostingMode::None)
+ {
+ s_findHostingCandidate(true);
+ }
+}
+
+void HostingHandler::onFoundCandidate(std::shared_ptr< OCResource > resource)
+{
+ // TODO
+ // Condition of Hosting
+ s_startHosting(resource);
+}
+
+void HostingHandler::onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle)
+{
+
+ switch(hostingCfg.notifyMethod)
+ {
+ case NotifyMethod::None:
+ break;
+ case NotifyMethod::Equalization:
+ case NotifyMethod::Granularity:
+ case NotifyMethod::Frequence:
+ default:
+ notifyFrequence(resourceHandle);
+ break;
+ }
+}
+
+void HostingHandler::notifyFrequence(OCResourceHandle resourceHandle)
+{
+
+ switch(hostingCfg.frequency)
+ {
+ case NotifyFrequency::None:
+ break;
+ case NotifyFrequency::Periodically:
+ case NotifyFrequency::OnTime:
+ default:
+ s_notify(resourceHandle);
+ break;
+ }
+}
--- /dev/null
+/*
+ * HostingInterface.cpp
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#include "HostingInterface.h"
+
+HostingInterface::HostingInterface()
+{
+
+}
+
+HostingInterface::~HostingInterface()
+{
+ // TODO Auto-generated destructor stub
+}
+
+int HostingInterface::setOnFoundHostingCandidate(
+ std::function< void(std::shared_ptr< OCResource > resource) > func)
+{
+ if(func != NULL)
+ {
+ try
+ {
+ ResourceManager::getInstance()->m_onFoundforHosting = func;
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+ }
+ else
+ {
+ ResourceManager::getInstance()->m_onFoundforHosting = std::function<
+ void(std::shared_ptr< OCResource > resource) >(
+ std::bind(&ResourceManager::onFoundforHostingDefault , ResourceManager::getInstance() ,
+ std::placeholders::_1));
+ }
+
+ return true;
+}
+
+int HostingInterface::setOnObserve(std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) > func)
+{
+ if(func != NULL)
+ {
+ try
+ {
+ ResourceManager::getInstance()->m_onObserve = func;
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+ }
+ else
+ {
+ ResourceManager::getInstance()->m_onObserve = std::function<
+ void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
+ std::bind(&ResourceManager::onObserveDefault , ResourceManager::getInstance() ,
+ std::placeholders::_1, std::placeholders::_2));
+ }
+
+ return true;
+}
+
+int HostingInterface::setNotifyObservers(std::function< void(OCResourceHandle resourceHandle) > &func)
+{
+ try
+ {
+ func = std::function< void(OCResourceHandle resourceHandle) >(
+ std::bind(&ResourceManager::notifyObservers , ResourceManager::getInstance() ,
+ std::placeholders::_1));
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+int HostingInterface::setStartHosting(
+ std::function< void(std::shared_ptr< OCResource > resource) > &func)
+{
+ try
+ {
+ func = std::function< void(std::shared_ptr< OCResource > resource) >(
+ std::bind(&ResourceManager::startHosting , ResourceManager::getInstance() ,
+ std::placeholders::_1));
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+int HostingInterface::setFindHosting(std::function< void(bool isHosting) > &func)
+{
+ try
+ {
+ func = std::function< void(bool isHosting) >(
+ std::bind(&ResourceManager::findNMResource ,
+ ResourceManager::getInstance() ,
+ std::placeholders::_1));
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+int HostingInterface::setAddExtraStr(std::function< void(std::string) > &func)
+{
+ try
+ {
+ func = std::function< void(std::string str) >(
+ std::bind(&ResourceManager::addExtraStr , ResourceManager::getInstance() ,
+ std::placeholders::_1));
+ }
+ catch(exception &e)
+ {
+ return false;
+ }
+
+ return true;
+}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 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.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <memory>
-#include "OCApi.h"
-#include "OCPlatform.h"
-#include "NotificationManager.h"
-
-using namespace OC;
-
-class NotificationService
-{
-
-public:
-
- static std::function< void(std::shared_ptr< OCResource > resource) > s_startHosting;
- static std::function< void() > s_findHostingCandidate;
- static std::function< void(std::string) > s_addExtraStr;
-
- void onObserve(AttributeMap &inputAttMap)
- {
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "On Observe:\n";
- for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
-
- for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
- }
-
- void onFoundCandidate(std::shared_ptr< OCResource > resource)
- {
- s_startHosting(resource);
- }
-
-};
-
-std::function< void(std::shared_ptr< OCResource > resource) > NotificationService::s_startHosting;
-std::function< void() > NotificationService::s_findHostingCandidate;
-std::function< void(std::string) > NotificationService::s_addExtraStr;
-
-int main(void)
-{
-
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "Start Notification Manager : Hosting v0.5\n";
-
- NotificationService a;
-
- NotificationManager *Ptr = NotificationManager::getInstance();
- Ptr->initialize();
-
- Ptr->setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&NotificationService::onFoundCandidate , a , std::placeholders::_1)));
- Ptr->setOnObserve(
- std::function< void(AttributeMap &inputAttMap) >(
- std::bind(&NotificationService::onObserve , a , std::placeholders::_1)));
-
- Ptr->setFindHosting(a.s_findHostingCandidate);
- Ptr->setStartHosting(a.s_startHosting);
- Ptr->setAddExtraStr(a.s_addExtraStr);
-
- a.s_addExtraStr("visual");
- a.s_findHostingCandidate();
-
- while(true)
- {
- char signal;
- cin >> signal;
-
- switch(signal)
- {
- case 'q':
- case 'Q':
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "End Notification Manager : Hosting v0.5\n";
- return true;
- default:
- break;
- }
- }
-
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "End Notification Manager : Hosting v0.5\n";
-
- return true;
-}
#include "NotificationManager.h"
NotificationManager *NotificationManager::s_instance = NULL;
-//OCPlatform *NotificationManager::s_nmOCPlatform = NULL;
-PlatformConfig NotificationManager::s_cfg;
-//(ServiceType::InProc, ModeType::Both, "134.134.161.33", 5683, QualityOfService::NonConfirmable);
+mutex NotificationManager::s_mutexForCreation;
NotificationManager::NotificationManager()
{
- m_print = NULL;
- m_onfound = NULL;
- m_onObserve = NULL;
- m_startHosting = NULL;
- m_findHosting = NULL;
- m_addExtraStr = NULL;
}
-NotificationManager::~NotificationManager()
-{
-}
-
-void NotificationManager::initialize()
+NotificationManager::NotificationManager(HostingConfig cfg)
{
- Configure(s_cfg);
-
- setPrint(NULL);
- setOnFoundHostingCandidate(NULL);
- setStartHosting(NotificationManager::getInstance()->m_startHosting);
- setFindHosting(NotificationManager::getInstance()->m_findHosting);
- setAddExtraStr(NotificationManager::getInstance()->m_addExtraStr);
-
-#ifndef ISFORDEMO
- findHostingCandidate();
-#endif
-
}
-void NotificationManager::registerHostingEventListener()
-{
- // TODO : Initial HostingEventListener (v1.0)
-}
-
-void NotificationManager::findHostingCandidate()
-{
- try
- {
- ResourceManager::getInstance()->findNMResource("" , "coap://224.0.1.187/oc/core" , true);
- }
- catch(OCException e)
- {
- }
-}
-
-NotificationManager *NotificationManager::getInstance()
-{
- if(!s_instance)
- {
- s_instance = new NotificationManager();
- }
-
- return s_instance;
-}
-
-int NotificationManager::setPrint(std::function< void(AttributeMap &inputAttMap) > func)
+NotificationManager::~NotificationManager()
{
- if(func != NULL)
- {
- try
- {
- NotificationManager::getInstance()->m_print = func;
- }
- catch(exception e)
- {
- return false;
- }
- }
- else
- {
- NotificationManager::getInstance()->m_print =
- std::function< void(AttributeMap &inputAttMap) >(
- std::bind(&ResourceManager::printAttributeMap ,
- ResourceManager::getInstance() , std::placeholders::_1));
- }
- return true;
-}
-int NotificationManager::setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) > func)
-{
- if(func != NULL)
- {
- try
- {
- NotificationManager::getInstance()->m_onfound = func;
- }
- catch(exception e)
- {
- return false;
- }
- }
- else
- {
- NotificationManager::getInstance()->m_onfound = std::function<
- void(std::shared_ptr< OCResource > resource) >(
- std::bind(&ResourceManager::onFoundReport , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
-
- return true;
}
-int NotificationManager::setOnObserve(std::function< void(AttributeMap &inputAttMap) > func)
+void NotificationManager::initialize()
{
- if(func != NULL)
- {
- try
- {
- NotificationManager::getInstance()->m_onObserve = func;
- }
- catch(exception e)
- {
- return false;
- }
- }
- return true;
-}
+ // find local ip address
+ std::string ipAddress;
+ NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
-std::function< void(AttributeMap &inputAttMap) > NotificationManager::getPrint()
-{
- return m_print;
-}
+ // set ip address
+ OICPlatformConfig::getInstance()->setIP(ipAddress);
-std::function< void(std::shared_ptr< OCResource > resource) > NotificationManager::getOnFoundHostingCandidate()
-{
- return m_onfound;
+ // initialize hosting handler
+ HostingHandler::initialize();
}
-std::function< void(AttributeMap &inputAttMap) > NotificationManager::getOnObserve()
+void NotificationManager::initialize(HostingConfig cfg)
{
- return m_onObserve;
-}
+ // find local ip address
+ std::string ipAddress;
+ NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
-int NotificationManager::setStartHosting(
- std::function< void(std::shared_ptr< OCResource > resource) > &func)
-{
- try
- {
- func = std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&ResourceManager::startHosting , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch(exception e)
- {
- return false;
- }
-
- return true;
-}
+ // set ip address
+ OICPlatformConfig::getInstance()->setIP(ipAddress);
-int NotificationManager::setFindHosting(std::function< void() > &func)
-{
- try
- {
- func = std::function< void() >(
- std::bind(&NotificationManager::findHostingCandidate ,
- NotificationManager::getInstance()));
- }
- catch(exception e)
- {
- return false;
- }
-
- return true;
+ // initialize hosting handler
+ HostingHandler::initialize(cfg);
}
-int NotificationManager::setAddExtraStr(std::function< void(std::string) > &func)
+void NotificationManager::registerHostingEventListener()
{
- try
- {
- func = std::function< void(std::string str) >(
- std::bind(&ResourceManager::addExtraStr , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch(exception e)
- {
- return false;
- }
-
- return true;
+ // TODO : Initial HostingEventListener (v1.0)
}
-std::function< void(std::shared_ptr< OCResource > resource) > NotificationManager::getStartHosting()
+NotificationManager *NotificationManager::getInstance()
{
- if(m_startHosting)
- {
- return m_startHosting;
- }
- else
- {
- return NULL;
- }
-}
+ if(!s_instance)
+ {
+ s_mutexForCreation.lock();
+ if(!s_instance)
+ {
+ s_instance = new NotificationManager();
+ }
+ s_mutexForCreation.unlock();
+ }
-std::function< void() > NotificationManager::getFindHosting()
-{
- if(m_findHosting)
- {
- return m_findHosting;
- }
- else
- {
- return NULL;
- }
+ return s_instance;
}
-std::function< void(std::string) > NotificationManager::getAddExtraStr()
-{
- if(m_addExtraStr)
- {
- return m_addExtraStr;
- }
- else
- {
- return NULL;
- }
+int NotificationManager::getNetInfo(IN int& sck, IN struct ifreq* item, OUT std::string& ip_addres)
+{
+ struct ifreq temp_ifr;
+ memset(&temp_ifr, 0, sizeof(temp_ifr));
+ strcpy(temp_ifr.ifr_name, item->ifr_name);
+
+ if (ioctl(sck, SIOCGIFFLAGS, &temp_ifr))
+ {
+ return -1;
+ }
+
+ if (!((temp_ifr.ifr_flags & IFF_UP) && (temp_ifr.ifr_flags & IFF_RUNNING)))
+ {
+ return -1;
+ }
+
+ std::string ip(inet_ntoa(((struct sockaddr_in *) &item->ifr_addr)->sin_addr));
+ if (ip.empty())
+ {
+ return -1;
+ }
+
+ if (ip.find("127.0.0") == 0)
+ {
+ return -1;
+ }
+
+ ip_addres = ip;
+ return 0;
+}
+
+bool NotificationManager::scanAndGetNetworkInterface(OUT std::string& ip_addres)
+{
+ while(1)
+ {
+ char buf[1024] = { 0, };
+ struct ifconf ifc;
+ struct ifreq *ifr;
+ int sck;
+ int interfaces;
+ int i;
+
+ sck = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sck < 0)
+ {
+ usleep(10);
+ continue;
+ }
+
+ ifc.ifc_len = sizeof(buf);
+ ifc.ifc_buf = buf;
+ if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
+ {
+ printf( "SIOCGIFCONF Failed ");
+ close(sck);
+ usleep(10);
+ continue;
+ }
+
+ ifr = ifc.ifc_req;
+ interfaces = ifc.ifc_len / sizeof(struct ifreq);
+
+ for (i = 0; i < interfaces; i++)
+ {
+ if( getNetInfo(sck, &ifr[i], ip_addres) == 0 )
+ {
+ return 0;
+ }
+ continue;
+ }
+ close(sck);
+ usleep(10);
+ }
+
+ return 0;
}
--- /dev/null
+/*
+ * OICPlatformConfig.cpp
+ *
+ * Created on: 2014. 10. 15.
+ * Author: jyong2
+ */
+
+#include "OICPlatformConfig.h"
+
+
+OICPlatformConfig *OICPlatformConfig::s_instance = NULL;
+mutex OICPlatformConfig::s_mutexForCreation;
+//OCPlatform *OICPlatformConfig::s_nmOCPlatform = NULL;
+PlatformConfig OICPlatformConfig::s_cfg;
+//(ServiceType::InProc, ModeType::Both,"", 5683, QualityOfService::NonConfirmable);
+
+OICPlatformConfig::OICPlatformConfig()
+{
+ // TODO Auto-generated constructor stub
+
+}
+
+OICPlatformConfig::~OICPlatformConfig()
+{
+ // TODO Auto-generated destructor stub
+}
+
+
+OICPlatformConfig *OICPlatformConfig::getInstance()
+{
+ if(!s_instance)
+ {
+ s_mutexForCreation.lock();
+ if(!s_instance)
+ {
+ s_instance = new OICPlatformConfig();
+ Configure(s_cfg);
+ }
+ s_mutexForCreation.unlock();
+ }
+
+ return s_instance;
+}
+
+//void OICPlatformConfig::getOCPlatform()
+//{
+//// if(!s_nmOCPlatform)
+//// {
+//// if(s_cfg.ipAddress.empty())
+//// {
+//// return NULL;
+//// }
+//// s_nmOCPlatform = new OCPlatform(s_cfg);
+//// Configure(s_cfg);
+//// }
+//// return s_nmOCPlatform;
+//}
+
+void OICPlatformConfig::initialize()
+{
+
+}
+
+void OICPlatformConfig::setIP(std::string ipaddress)
+{
+ s_cfg.ipAddress = ipaddress;
+}
#include "RegistrationManager.h"
RegistrationManager *RegistrationManager::s_instance = NULL;
+mutex RegistrationManager::s_mutexForCreation;
RegistrationManager::RegistrationManager()
{
RegistrationManager *RegistrationManager::getInstance()
{
- if(!s_instance)
- {
- s_instance = new RegistrationManager();
- }
- return s_instance;
+ if(!s_instance)
+ {
+ s_mutexForCreation.lock();
+ if(s_instance)
+ {
+ s_instance = new RegistrationManager();
+ }
+ s_mutexForCreation.unlock();
+ }
+
+ return s_instance;
}
int RegistrationManager::addResource()
OCResourceHandle resourceHandle;
OCStackResult result;
-
result = registerResource(resourceHandle , uri , type ,
- interface ,
- std::function<
- OCEntityHandlerResult(const std::shared_ptr< OCResourceRequest > request ,
- const std::shared_ptr< OCResourceResponse > response) >(
- std::bind(&VirtualRepresentation::entityHandler , resourceObject ,
- std::placeholders::_1 , std::placeholders::_2)) ,
- resourceObject.getResourceProperty());
+ interface ,
+ std::function<
+ OCEntityHandlerResult(const std::shared_ptr< OCResourceRequest > request ,
+ const std::shared_ptr< OCResourceResponse > response) >(
+ std::bind(&VirtualRepresentation::entityHandler , resourceObject ,
+ std::placeholders::_1 , std::placeholders::_2)) ,
+ resourceObject.getResourceProperty());
+
resourceObject.setResourceHandle(resourceHandle);
if(OC_STACK_OK != result)
resource->observe(ObserveType::Observe , queryParmaMap ,
std::function<
void(const HeaderOptions headerOption,
- const OCRepresentation& rep , const int eCode ,
- const int sequenceNumber) >(
+ const OCRepresentation& rep , const int& eCode ,
+ const int& sequenceNumber) >(
std::bind(&VirtualRepresentation::onObserve , resourceObject ,
std::placeholders::_1 , std::placeholders::_2 ,
std::placeholders::_3 , std::placeholders::_4)));
#include "ResourceManager.h"
ResourceManager *ResourceManager::s_instance = NULL;
+mutex ResourceManager::s_mutexForCreation;
std::list< VirtualRepresentation > ResourceManager::s_resourceList;
std::string ResourceManager::s_extraStr;
ResourceManager::ResourceManager()
{
+ m_onFoundforHosting = NULL;
+ m_onObserve = NULL;
+ m_notify = NULL;
}
ResourceManager::~ResourceManager()
{
if(!s_instance)
{
- s_instance = new ResourceManager();
+ s_mutexForCreation.lock();
+ if(!s_instance)
+ {
+ s_instance = new ResourceManager();
+ }
+ s_mutexForCreation.unlock();
}
return s_instance;
}
return retObject;
}
-OCStackResult ResourceManager::findNMResource(const std::string& host ,
- const std::string& resourceName , bool ishosting)
+void ResourceManager::findNMResource(bool isHosting)
{
- return findResource(host , resourceName ,
- std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&ResourceManager::foundResourceforhosting , this ,
- std::placeholders::_1)));
+ if(isHosting)
+ {
+ findResource("" , "coap://224.0.1.187/oc/core",
+ std::function< void(std::shared_ptr< OCResource > resource) >(
+ std::bind(&ResourceManager::foundResourceforhosting , ResourceManager::getInstance() ,
+ std::placeholders::_1)));
+ }
}
void ResourceManager::foundResourceforhosting(std::shared_ptr< OCResource > resource)
{
if(resource->uri().find("/a/NM") != std::string::npos)
{
- NotificationManager::getInstance()->getOnFoundHostingCandidate()(resource);
+ ResourceManager::getInstance()->m_onFoundforHosting(resource);
}
}
else
{
+ // TODO
}
}
- catch(std::exception& e)
+ catch(std::exception &e)
{
}
}
void ResourceManager::startHosting(std::shared_ptr< OCResource > resource)
{
+
+ cout << "start hosting" << endl;
VirtualRepresentation tmp = findVirtualRepresentation( resource->uri() );
if( !tmp.getUri().empty() )
}
+void ResourceManager::notifyObservers(OCResourceHandle resourceHandle)
+{
+ OCStackResult result = OC_STACK_OK;
+
+ result = notifyAllObservers(resourceHandle);
+
+ if(OC_STACK_NO_OBSERVERS == result)
+ {
+ // No observers.
+ // TODO
+ }
+}
+
AttributeMap ResourceManager::copyAttributeMap(AttributeMap &inputAttMap)
{
AttributeMap retAttMap;
retAttMap = inputAttMap;
-
+// for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
+// {
+// AttributeValues tmpVal;
+//
+// for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
+// {
+// std::string tmpStr;
+//
+// tmpStr.append(*valueItr);
+//
+// tmpVal.push_back(tmpStr);
+// }
+// retAttMap[it->first] = tmpVal;
+//
+// }
return retAttMap;
}
return false;
}
-void ResourceManager::onFoundReport(std::shared_ptr< OCResource > resource)
+void ResourceManager::onFoundforHostingDefault(std::shared_ptr< OCResource > resource)
+{
+ ResourceManager::getInstance()->startHosting(resource);
+}
+void ResourceManager::onObserveDefault(AttributeMap &inputAttMap , OCResourceHandle resourceHandle)
{
- NotificationManager::getInstance()->getStartHosting()(resource);
+ ResourceManager::getInstance()->notifyObservers(resourceHandle);
}
void ResourceManager::printAttributeMap(AttributeMap &inputAttMap)
int requestFlag = request->getRequestHandlerFlag();
if(requestFlag == RequestHandlerFlag::InitFlag)
- {
- }
+ {
+ }
+
else if(requestFlag == RequestHandlerFlag::RequestFlag)
{
- if(requestType == "GET")
+ if( (requestType == "GET") && response )
{
- if(response)
- {
- std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
- while(!m_isReadyAttributeMap)
- {
- s_conditionAttributeMap.wait(lck);
- }
- m_isReadyAttributeMap = false;
+ std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
+ while(!m_isReadyAttributeMap)
+ {
+ s_conditionAttributeMap.wait(lck);
+ }
+ m_isReadyAttributeMap = false;
- NotificationManager::getInstance()->getPrint()(s_attributeMap);
+ OCRepresentation rep;
+ getRepresentation(rep);
- OCRepresentation rep;
- getRepresentation(rep);
-
- response->setErrorCode(200);
- response->setResourceRepresentation(rep , "");
-
- m_isReadyAttributeMap = true;
- s_conditionAttributeMap.notify_all();
- }
- else
- {
- }
+ response->setErrorCode(200);
+ response->setResourceRepresentation(rep , DEFAULT_INTERFACE);
- }
+ m_isReadyAttributeMap = true;
+ s_conditionAttributeMap.notify_all();
+ }
else if(requestType == "PUT")
{
+ // TODO
}
else if(requestType == "POST")
{
+ // TODO
}
else if(requestType == "DELETE")
{
+ // TODO
}
else
{
+ // TODO
}
}
+ else if(requestFlag == RequestHandlerFlag::InitFlag)
+ {
+ // TODO
+ }
else if(requestFlag == RequestHandlerFlag::ObserverFlag)
{
+ // TODO
+ cout << "requestFlag == RequestHandlerFlag::ObserverFlag\n";
}
else
{
+ // requestFlag is not [Request, Init, Observer]
+ // TODO
}
}
else
{
+ // Param(request) is empty.
+ // TODO
}
-
return OC_EH_OK;
}
void VirtualRepresentation::onObserve(const HeaderOptions &headerOption, const OCRepresentation &rep , const int eCode , const int sequenceNumber)
{
+ cout << "VirtualRepresentation::onObserve Enter\n";
if(eCode == SUCCESS_RESPONSE)
{
- AttributeMap tmp = rep.getAttributeMap();
+ cout << "1\n";
+ AttributeMap inputAttributeMap = rep.getAttributeMap();
- if(ResourceManager::getInstance()->isEmptyAttributeMap(tmp))
+ if(ResourceManager::getInstance()->isEmptyAttributeMap(inputAttributeMap))
{
+ cout << "2\n";
return;
}
-
- if(NotificationManager::getInstance()->getOnObserve())
- {
- NotificationManager::getInstance()->getOnObserve()(tmp);
- }
-
- NotificationManager::getInstance()->getPrint()(tmp);
-
- OCStackResult result = OC_STACK_OK;
-
+ cout << "3\n";
VirtualRepresentation tmpObj = *this;
if(!tmpObj.getUri().empty())
{
-
- AttributeMap tmpAttMap = ResourceManager::getInstance()->copyAttributeMap(tmp);
-
+ cout << "4\n";
+ AttributeMap tmpAttMap = ResourceManager::getInstance()->copyAttributeMap(inputAttributeMap);
+ cout << "5\n";
{
+ cout << "6\n";
std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
+ cout << "7\n";
while(!m_isReadyAttributeMap)
{
+ cout << "8\n";
s_conditionAttributeMap.wait(lck);
}
+ cout << "9\n";
m_isReadyAttributeMap = false;
-
+ cout << "10\n";
s_attributeMap = tmpAttMap;
-
+ cout << "11\n";
m_isReadyAttributeMap = true;
+ cout << "12\n";
s_conditionAttributeMap.notify_all();
+ cout << "13\n";
}
- result = notifyAllObservers(tmpObj.getResourceHandle());
+ if(ResourceManager::getInstance()->m_onObserve)
+ {
+ cout << "14\n";
+ ResourceManager::getInstance()->m_onObserve(inputAttributeMap, tmpObj.getResourceHandle());
+ cout << "15\n";
+ }
}
- else
- {
- }
-
- if(OC_STACK_NO_OBSERVERS == result)
- {
- }
-
}
else
{
- std::exit(-1);
+ // Check the error.
+ // TODO
}
+ cout << "VirtualRepresentation::onObserve Out\n";
}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <iostream>
+#include "NotificationManager.h"
+
+using namespace OC;
+
+int main(void)
+{
+
+ std::cout << endl;
+ std::cout << "========================================================" << endl;
+ std::cout << "Start Notification Manager : Hosting v0.5\n";
+
+
+ HostingConfig cfg;
+ cfg.hostingMode = HostingMode::ManualMode;
+ cfg.automaticMethod = AutomaticMethod::None;
+ cfg.notifyMethod = NotifyMethod::Frequence;
+ cfg.frequency = NotifyFrequency::OnTime;
+
+ NotificationManager::initialize(cfg);
+
+ while(true)
+ {
+ char signal;
+ cin >> signal;
+
+ switch(signal)
+ {
+ case 'q':
+ case 'Q':
+ std::cout << endl;
+ std::cout << "========================================================" << endl;
+ std::cout << "End Notification Manager : Hosting v0.5\n";
+ return true;
+ default:
+ break;
+ }
+
+ }
+
+ std::cout << endl;
+ std::cout << "========================================================" << endl;
+ std::cout << "End Notification Manager : Hosting v0.5\n";
+
+ return true;
+}
using namespace OC;
-const int SUCCESS_RESPONSE = 0;
+const int SUCCESS_RESPONSE = OC_STACK_OK;
static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
std::shared_ptr< OCResource > g_curResource;
void onObserve(const HeaderOptions &headerOption , const OCRepresentation& rep , const int& eCode, const int& sequenceNumber)
{
std::cout << "onObserve" << std::endl;
- if(eCode == SUCCESS_RESPONSE)
+// if(eCode == SUCCESS_RESPONSE)
+ if(eCode <= OC_STACK_RESOURCE_DELETED)
{
AttributeMap attributeMap = rep.getAttributeMap();
if(rep.getUri().empty())
{
+ cout << "uri is null\n";
return;
}
{
if(resource)
{
- if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
+// if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
+ if(resource->uri().find("/a/NM/TempHumSensor") != std::string::npos)
{
std::cout << std::endl;
std::cout << "========================================================" << std::endl;
#include "OCPlatform.h"
#include "OCApi.h"
+#include "OCResourceResponse.h"
#include <memory>
int g_Observation = 0;
+pthread_cond_t m_cond = PTHREAD_COND_INITIALIZER;
+pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;
+
OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
- std::shared_ptr< OCResourceResponse > response);
+ std::shared_ptr< OCResourceResponse > response);
class TempHumidResource
{
}
else
{
- cout << "\t\t\t\t" << "m_power not found in the representation" << endl;
+ cout << "\t\t\t\t" << "humidity not found in the representation" << endl;
}
}
catch (exception& e)
OCRepresentation get()
{
+ cout << "resource get\n";
m_Rep.setValue("temperature", m_temp);
m_Rep.setValue("humidity", m_humid);
+ cout << "resource get : done\n";
+
return m_Rep;
}
void *ChangeLightRepresentation(void *param)
{
+ cout << "ChangeLigthRepresentation Enter\n";
+ while(1){
+ cout << "pthread_cond_wait\n";
+ pthread_cond_wait(&m_cond, &m_mutex);
+ cout << "pthread_cond_start\n";
+ if(g_Observation)
+ {
- if(g_Observation)
- {
-
- cout << endl;
- cout << "========================================================" << endl;
- cout << "HUMTepm updated to : " << myResource.m_temp << endl;
- cout << "Notifying observers with resource handle: " << myResource.getHandle() << endl;
+ cout << endl;
+ cout << "========================================================" << endl;
+ cout << "HUMTepm updated to : " << myResource.m_temp << endl;
+ cout << "Notifying observers with resource handle: " << myResource.getHandle() << endl;
- cout << endl;
- cout << "========================================================" << endl;
- cout << "Send data : \n";
- cout << "Attribute Name: Temp\tvalue: " << myResource.m_temp << endl;
- cout << "Attribute Name: Humid\tvalue: " << myResource.m_humid << endl;
+ cout << endl;
+ cout << "========================================================" << endl;
+ cout << "Send data : \n";
+ cout << "Attribute Name: Temp\tvalue: " << myResource.m_temp << endl;
+ cout << "Attribute Name: Humid\tvalue: " << myResource.m_humid << endl;
- OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
+ OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle());
+ cout << "Notify Success\n";
- if(OC_STACK_NO_OBSERVERS == result)
- {
- cout << "No More observers, stopping notifications" << endl;
- g_Observation = 0;
- }
- }
+ if(OC_STACK_NO_OBSERVERS == result)
+ {
+ cout << "No More observers, stopping notifications" << endl;
+ g_Observation = 0;
+ }
+ }
+ cout << "ChangeLigthRepresentation Out\n";
+ }
return NULL;
}
OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
std::shared_ptr< OCResourceResponse > response)
{
-
+ cout << "Sample Provider entityHandler\n";
if(request)
{
+ cout << "flag : request\n";
std::string requestType = request->getRequestType();
int requestFlag = request->getRequestHandlerFlag();
if(requestFlag == RequestHandlerFlag::InitFlag)
{
+ cout << "\t\trequestFlag : Init\n";
}
+
if(requestFlag == RequestHandlerFlag::RequestFlag)
{
+ cout << "\t\trequestFlag : Request\n";
if(requestType == "GET")
{
- if(response)
- {
- response->setErrorCode(200);
- response->setResourceRepresentation(myResource.get());
- }
+ cout << "\t\trequestType : GET\n";
+ try
+ {
+ if(response)
+ {
+ OCRepresentation rep = myResource.get();
+ cout << rep.getJSONRepresentation() << endl;
+ response->setErrorCode(200);
+ response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
+ }
+ else
+ {
+ cout << "response is null\n";
+ }
+ } catch(exception& e)
+ {
+ cout << e.what() << endl;
+ }
}
else if(requestType == "PUT")
{
}
else if(requestFlag & RequestHandlerFlag::ObserverFlag)
{
- pthread_t threadId;
+ pthread_t threadId;
+
+ cout << request->getResourceUri() << endl;
+ cout << request->getResourceRepresentation().getUri() << endl;
cout << "========================================================" << endl;
cout << "Receive ObserverFlag : Start Observe\n";
cout << "========================================================" << endl;
g_Observation = 1;
+ cout << "\t\trequestFlag : Observer\n";
static int startedThread = 0;
if(!startedThread)
{
+ cout << "\t\tpthrerad_create\n";
pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
startedThread = 1;
}
{
cout << "Temp is up!" << endl;
myResource.m_temp += 10;
- ChangeLightRepresentation((void *) NULL);
-
+ pthread_cond_signal(&m_cond);
+ cout << "ChangeLightRepresentation Done!" << endl;
break;
}
case 2:
{
cout << "Temp is down!" << endl;
myResource.m_temp -= 10;
- ChangeLightRepresentation((void *) NULL);
-
+ pthread_cond_signal(&m_cond);
+ cout << "ChangeLightRepresentation Done!" << endl;
break;
}
case 3:
}
}
}
- catch(OCException e)
+ catch(exception& e)
{
+ cout << "main exception : " << e.what() << endl;
}
}