######################################################################
resourcebroker_env.AppendUnique(CPPPATH = ['include'])
resourcebroker_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
+resourcebroker_env.AppendUnique(CPPPATH = ['../common/primitiveTimer/include'])
resourcebroker_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
-resourcebroker_env.PrependUnique(LIBS = ['service_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
+resourcebroker_env.PrependUnique(LIBS = ['service_common', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
if target_os not in ['windows', 'winrt']:
BROKER_SRC_DIR + 'DeviceAssociation.cpp',
BROKER_SRC_DIR + 'DevicePresence.cpp',
BROKER_SRC_DIR + 'ResourcePresence.cpp',
- BROKER_SRC_DIR + 'ResourceBroker.cpp'
+ BROKER_SRC_DIR + 'ResourceBroker.cpp',
+ BROKER_SRC_DIR + '../../common/primitiveTimer/src/PrimitiveTimer.cpp',
+ BROKER_SRC_DIR + '../../common/primitiveTimer/src/PrimitiveTimer_Impl.cpp'
]
resourcebroker_env.InstallTarget(resourcebrokersdk, 'libResouceBroker')
# Go to build sample apps
-#SConscript('SampleApp/SConscript')
+SConscript('SampleApp/SConscript')
#include "PrimitiveResource.h"
#define BROKER_TAG PCF("BROKER")
-
+#define SAFE_TIME (5l)
#define BROKER_TRANSPORT OCConnectivityType::CT_ADAPTER_IP
using namespace OIC::Service;
struct BrokerCBResourcePair
{
- BrokerCBResourcePair(ResourcePresencePtr _pResource, BrokerCB _cb)
- : pResource(_pResource), brokerCB(_cb){}
+ BrokerCBResourcePair(ResourcePresencePtr pResource, BrokerCB cb)
+ : pResource(pResource), brokerCB(cb){}
ResourcePresencePtr pResource;
BrokerCB brokerCB;
};
const std::string&)> SubscribeCB;
typedef std::function<void(const HeaderOptions&, const ResponseStatement&, int)> RequestGetCB;
-typedef std::function<void(int)> TimeoutCB;
+typedef std::function<void * (unsigned int)> TimeoutCB;
#endif // BROKERTYPES_H_
DevicePresence(PrimitiveResourcePtr pResource);
~DevicePresence();
- const std::string getAddress() const;
void addPresenceResource(ResourcePresence * rPresence);
void removePresenceResource(ResourcePresence * rPresence);
bool isEmptyResourcePresence() const;
+ const std::string getAddress() const;
private:
+ void requestAllResourcePresence();
+ void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress);
+ void * timeOutCB(unsigned int msg);
+
+ std::list<ResourcePresence * > resourcePresenceList;
+
std::string address;
DEVICE_STATE state;
bool isWithinTime;
- std::list<ResourcePresence * > resourcePresenceList;
-
SubscribeCB pSubscribeRequestCB;
TimeoutCB pTimeoutCB;
PresenceSubscriber presenceSubscriber;
- void requestAllResourcePresence();
- void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress);
- void timeOutCB(int msg);
};
static ResourceBroker * getInstance();
BrokerID hostResource(PrimitiveResourcePtr pResource, BrokerCB cb);
-
BrokerID cancelHostResource(BrokerID brokerId);
BROKER_STATE getResourceState(BrokerID brokerId);
ResourceBroker();
~ResourceBroker();
+ BrokerID generateBrokerID();
+ ResourcePresencePtr findResourcePresence(PrimitiveResourcePtr pResource);
+
static ResourceBroker * s_instance;
static std::mutex s_mutexForCreation;
static std::unique_ptr<PresenceList> s_presenceList;
-
static std::unique_ptr<BrokerIDMap> s_brokerIDMap;
- BrokerID generateBrokerID();
-
- ResourcePresencePtr findResourcePresence(PrimitiveResourcePtr pResource);
-
};
#endif /* RESOURCEBROKER_H_ */
#include <functional>
#include <list>
#include <string>
+#include <atomic>
#include "BrokerTypes.h"
+#include "../../common/primitiveTimer/include/PrimitiveTimer.h"
class ResourcePresence
{
void addBrokerRequester(BrokerID _id, BrokerCB _cb);
void removeBrokerRequester(BrokerID _id);
void removeAllBrokerRequester();
-
void requestResourceState();
-
- PrimitiveResourcePtr getPrimitiveResource();
- BROKER_STATE getResourceState();
void setResourcestate(BROKER_STATE _state);
- void executeAllBrokerCB();
-
+ void executeAllBrokerCB(BROKER_STATE changedState);
void changePresenceMode(BROKER_MODE newMode);
bool isEmptyRequester() const;
+ PrimitiveResourcePtr getPrimitiveResource();
+ BROKER_STATE getResourceState();
+
private:
- PrimitiveResourcePtr primitiveResource;
+ void getCB(const HeaderOptions &hos, const ResponseStatement& rep, int eCode);
+ void verifiedGetResponse(int eCode);
+ void registerDevicePresence();
+ void * timeOutCB(unsigned int msg);
+ void * pollingCB(unsigned int msg);
std::unique_ptr<std::list<BrokerRequesterInfoPtr>> requesterList;
+ PrimitiveResourcePtr primitiveResource;
+ PrimitiveTimer primitiveTimer;
BROKER_STATE state;
BROKER_MODE mode;
- RequestGetCB pGetCB;
- void GetCB(const HeaderOptions &hos, const ResponseStatement& rep, int seq);
-
- TimeoutCB pTimeoutCB;
-
bool isWithinTime;
- void TimeOutCB(int msg);
+ std::atomic_bool isTimeoutCB;
+ std::atomic_long receivedTime;
+ unsigned int timeoutHandle;
- void registerDevicePresence();
+ RequestGetCB pGetCB;
+ TimeoutCB pTimeoutCB;
+ TimeoutCB pPollingCB;
};
DeviceAssociation::DeviceAssociation()
{
// TODO Auto-generated constructor stub
-
}
DeviceAssociation::~DeviceAssociation()
state = DEVICE_STATE::REQUESTED;
pTimeoutCB = std::bind(&DevicePresence::timeOutCB, this, std::placeholders::_1);
-
isWithinTime = true;
//TODO generate Timer(if(!isTimer))
- //register pTimeroutCB
}
DevicePresence::~DevicePresence()
void DevicePresence::subscribeCB(OCStackResult ret,
const unsigned int seq, const std::string& hostAddress)
{
- std::cout << "Call Back Deivce's Presence\n";
const char* log = hostAddress.c_str();
OC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",log);
OC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret);
- //TODO : cancel timer if(isTimer)
+
if(isWithinTime)
{
switch(ret)
{
OC_LOG_V(DEBUG, BROKER_TAG, "This response is Timeout but device steel alive");
this->state = DEVICE_STATE::ALIVE;
- //notify cb message to user.
isWithinTime = true;
}
}
-void DevicePresence::timeOutCB(int msg)
+void * DevicePresence::timeOutCB(unsigned int msg)
{
this->isWithinTime = false;
OC_LOG_V(DEBUG, BROKER_TAG, "Timeout execution. will be discard after receiving cb message");
state = DEVICE_STATE::LOST_SIGNAL;
+
+ return NULL;
}
ResourceBroker::ResourceBroker()
{
- // TODO Auto-generated constructor stub
if(s_presenceList == nullptr)
{
s_presenceList = std::unique_ptr<PresenceList>(new PresenceList);
ResourceBroker::~ResourceBroker()
{
- // TODO Auto-generated destructor stub
if(s_presenceList != nullptr)
{
s_presenceList->erase(s_presenceList->begin(), s_presenceList->end());
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include "ResourcePresence.h"
+#include "../include/ResourcePresence.h"
+
+#include <bits/atomic_base.h>
+#include <bits/shared_ptr_base.h>
+#include <time.h>
+#include <unistd.h>
+#include <cstdbool>
+#include <exception>
+#include <iostream>
+#include <memory>
+#include <strstream>
+
+#include "PrimitiveResource.h"
#include "DeviceAssociation.h"
#include "DevicePresence.h"
ResourcePresence::ResourcePresence(PrimitiveResourcePtr pResource)
{
primitiveResource = pResource;
-
- pGetCB = std::bind(&ResourcePresence::GetCB, this,
+ isTimeoutCB = false;
+ receivedTime = 0L;
+ pGetCB = std::bind(&ResourcePresence::getCB, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
- pTimeoutCB = std::bind(&ResourcePresence::TimeOutCB, this,
- std::placeholders::_1);
+ pTimeoutCB = std::bind(&ResourcePresence::timeOutCB, this,std::placeholders::_1);
+ pPollingCB = std::bind(&ResourcePresence::pollingCB, this,std::placeholders::_1);
requesterList
= std::unique_ptr<std::list<BrokerRequesterInfoPtr>>(new std::list<BrokerRequesterInfoPtr>);
- //TODO generate Timer(if(!isTimer))
- //register pTimeroutCB
+ timeoutHandle = primitiveTimer.requestTimer(SAFE_TIME, pTimeoutCB);
state = BROKER_STATE::REQUESTED;
isWithinTime = true;
void ResourcePresence::registerDevicePresence()
{
- // find device......
std::string deviceAddress = primitiveResource->getHost();
DevicePresencePtr foundDevice
void ResourcePresence::requestResourceState()
{
+ OC_LOG_V(DEBUG, BROKER_TAG, "Request Get\n");
primitiveResource->requestGet(pGetCB);
-
}
-void ResourcePresence::executeAllBrokerCB()
+
+void ResourcePresence::executeAllBrokerCB(BROKER_STATE changedState)
{
OC_LOG_V(DEBUG, BROKER_TAG, "executeAllBrokerCB()");
- for(BrokerRequesterInfoPtr & item : * requesterList)
+ if(state != changedState)
{
- item->brockerCB(this->state);
+ state = changedState;
+ for(BrokerRequesterInfoPtr & item : * requesterList)
+ {
+ item->brockerCB(state);
+ }
}
}
+
void ResourcePresence::setResourcestate(BROKER_STATE _state)
{
this->state = _state;
}
-void ResourcePresence::TimeOutCB(int msg)
+
+void * ResourcePresence::timeOutCB(unsigned int msg)
{
+ isTimeoutCB = true;
+
+ time_t currentTime;
+ time(¤tTime);
+ currentTime+=0L;
+
+ if((receivedTime == 0L) || ((receivedTime+SAFE_TIME) > currentTime))
+ {
+ return NULL;
+ }
this->isWithinTime = false;
OC_LOG_V(DEBUG, BROKER_TAG, "Timeout execution. will be discard after receiving cb message");
- this->state = BROKER_STATE::LOST_SIGNAL;
+ executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL);
+ pollingCB(0);
+
+ isTimeoutCB = false;
+
+ return NULL;
+}
+
+void * ResourcePresence::pollingCB(unsigned int msg)
+{
+ this->requestResourceState();
+ timeoutHandle = primitiveTimer.requestTimer(SAFE_TIME,pTimeoutCB);
+
+ return NULL;
}
-void ResourcePresence::GetCB(const HeaderOptions &hos, const ResponseStatement& rep, int seq)
+
+void ResourcePresence::getCB(const HeaderOptions &hos, const ResponseStatement& rep, int eCode)
{
+ OC_LOG_V(DEBUG, BROKER_TAG, "response getCB\n");
+ while(isTimeoutCB)
+ {
+ OC_LOG_V(DEBUG, BROKER_TAG, "wait\n");
+ sleep(2);
+ }
+ time_t currentTime;
+ time(¤tTime);
+ receivedTime = currentTime;
try
{
- //TODO : cancel timer if(isTimer)
+ state = BROKER_STATE::ALIVE;
+ verifiedGetResponse(eCode);
+
if(isWithinTime)
{
+ primitiveTimer.cancelTimer(timeoutHandle);
+ isWithinTime = true;
+ }
+
+ }
+ catch(std::exception& e)
+ {
+
+ }
+
+ if(mode == BROKER_MODE::NON_PRESENCE_MODE)
+ {
+ // TODO set timer & request get
+ primitiveTimer.requestTimer(SAFE_TIME,pPollingCB);
+ }
+
+}
- OC_LOG_V(DEBUG, BROKER_TAG, "broker state :: %d",(int)state);
+void ResourcePresence::verifiedGetResponse(int eCode)
+{
+ switch(eCode)
+ {
+ case OC_STACK_OK:
+ case OC_STACK_CONTINUE:
state = BROKER_STATE::ALIVE;
- OC_LOG_V(DEBUG, BROKER_TAG, "broker state changed :: %d",(int)state);
- OC_LOG_V(DEBUG, BROKER_TAG, "GET request was successful");
+ OC_LOG_V(DEBUG, BROKER_TAG, "resource state : %d",(int)state);
+ break;
+ case OC_STACK_INVALID_REQUEST_HANDLE:
+ case OC_STACK_RESOURCE_DELETED:
+ case OC_STACK_TIMEOUT:
+ case OC_STACK_COMM_ERROR:
+ case OC_STACK_PRESENCE_STOPPED:
+ case OC_STACK_PRESENCE_TIMEOUT:
if(!requesterList->empty())
{
- executeAllBrokerCB();
+ executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL);
}
else
{
- OC_LOG_V(DEBUG, BROKER_TAG, "None exist resource for request");
- state = BROKER_STATE::DESTROYED;
+ setResourcestate(BROKER_STATE::LOST_SIGNAL);
}
- }
- else
- {
- OC_LOG_V(DEBUG, BROKER_TAG, "its message is not available because of Timeout msg");
- OC_LOG_V(DEBUG, BROKER_TAG, "broker state :: %d",(int)state);
- state = BROKER_STATE::ALIVE;
- OC_LOG_V(DEBUG, BROKER_TAG, "broker state changed :: %d",(int)state);
- OC_LOG_V(DEBUG, BROKER_TAG, "GET request was successful");
- //notify cb message to user.
+ break;
+
+ default:
if(!requesterList->empty())
{
- executeAllBrokerCB();
+ executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL);
}
else
{
- OC_LOG_V(DEBUG, BROKER_TAG, "None exist resource for request");
- state = BROKER_STATE::DESTROYED;
+ setResourcestate(BROKER_STATE::LOST_SIGNAL);
}
- isWithinTime = true;
- }
- }
- catch(std::exception& e)
- {
-
- }
-
- if(mode == BROKER_MODE::NON_PRESENCE_MODE)
- {
- // TODO set timer & request get
+ break;
}
}