#include <map>
#include <vector>
#include <memory>
+#include <mutex>
+
#include "NotificationReceiver.h"
#include "RCSResourceAttributes.h"
*
* @return void
*/
- void registerObserver(NotificationReceiver *pNotiReceiver);
+ void registerObserver(NotificationReceiver* pNotiReceiver);
/**
* Return all attributes of the resource
*
* @return Attributes of the resource
*/
- RCSResourceAttributes &getAttributes();
+ const RCSResourceAttributes getAttributes();
/**
* Set attributes of the resource
*
* @return void
*/
- void setAttributes(RCSResourceAttributes &attrs);
+ void setAttributes(const RCSResourceAttributes &attrs);
/**
* Return the value of an attribute
void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value,
bool notify);
+ /**
+ * Sets the value of an attribute
+ *
+ * @param key Name of attribute to set
+ *
+ * @param value Value of attribute to set
+ *
+ * @param notify Flag to indicate if OIC clients should be notified about an update
+ *
+ * @return void
+ */
+ void setAttribute(const std::string &key, RCSResourceAttributes::Value &value,
+ bool notify);
+
/**
* Sets the value of an attribute
*
void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value);
/**
- * Sends a notification to all observers.
+ * Sets the value of an attribute
*
- * Calling this is not needed when setAttribute() was called
- * with notify == true.
+ * @param key Name of attribute to set
+ *
+ * @param value Value of attribute to set
*
* @return void
*/
- void sendNotification();
+ void setAttribute(const std::string &key, RCSResourceAttributes::Value &value);
/**
* This function should be implemented by the according bundle resource
*
* @return All attributes
*/
- virtual RCSResourceAttributes &handleGetAttributesRequest() = 0;
+ virtual RCSResourceAttributes handleGetAttributesRequest() = 0;
/**
* This function should be implemented by the according bundle resource
*
* @return void
*/
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs) = 0;
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs) = 0;
+ private:
+ void sendNotification(NotificationReceiver *notficiationRecevier, std::string uri);
public:
std::string m_bundleId;
std::vector< std::map< std::string, std::string > > > m_mapResourceProperty;
private:
- NotificationReceiver *m_pNotiReceiver;
+ NotificationReceiver* m_pNotiReceiver;
RCSResourceAttributes m_resourceAttributes;
+ std::mutex m_resourceAttributes_mutex;
};
}
}
*
* @return Value of all attributes
*/
- virtual RCSResourceAttributes &handleGetAttributesRequest() = 0;
+ virtual RCSResourceAttributes handleGetAttributesRequest() = 0;
/**
* This function should be implemented by the according bundle resource
*
* @return void
*/
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs) = 0;
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs) = 0;
};
}
}
*
* @return Value of all attributes
*/
- virtual RCSResourceAttributes &handleGetAttributesRequest() = 0;
+ virtual RCSResourceAttributes handleGetAttributesRequest() = 0;
/**
* This function should be implemented by the according bundle resource
*
* @return void
*/
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs) = 0;
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs) = 0;
/**
* SoftSensor logic. Has to be provided by the soft sensor developer.
BMISensorResource& operator=( const BMISensorResource& rhs )=delete;
~BMISensorResource();
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs);
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs);
- virtual RCSResourceAttributes &handleGetAttributesRequest();
+ virtual RCSResourceAttributes handleGetAttributesRequest();
virtual void executeLogic();
}
void BMISensorResource::handleSetAttributesRequest(
- RCSResourceAttributes &value)
+ const RCSResourceAttributes &value)
{
BundleResource::setAttributes(value);
}
-RCSResourceAttributes &BMISensorResource::handleGetAttributesRequest()
+RCSResourceAttributes BMISensorResource::handleGetAttributesRequest()
{
return BundleResource::getAttributes();
}
m_mapInputData.insert(std::make_pair("height", values.back().toString()));
executeLogic();
-}
\ No newline at end of file
+}
// Invoke resource's put API with rep, query map and the callback parameter
- resource->put(rep, QueryParamsMap(), &onPut);
+ resource->post(rep, QueryParamsMap(), &onPut);
}
}
std::cout << "\tcolor: " << mylight.m_color << std::endl;
std::cout << "\tdim: " << mylight.m_dim << std::endl;
- putLightRepresentation(curResource);
+ postLightRepresentation(curResource);
}
else
{
DiscomfortIndexSensorResource(const DiscomfortIndexSensorResource &other)=delete;
DiscomfortIndexSensorResource& operator=( const DiscomfortIndexSensorResource& rhs )=delete;
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs);
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs);
- virtual RCSResourceAttributes &handleGetAttributesRequest();
+ virtual RCSResourceAttributes handleGetAttributesRequest();
virtual void executeLogic();
}
void DiscomfortIndexSensorResource::handleSetAttributesRequest(
- RCSResourceAttributes &value)
+ const RCSResourceAttributes &value)
{
BundleResource::setAttributes(value);
}
-RCSResourceAttributes &DiscomfortIndexSensorResource::handleGetAttributesRequest()
+RCSResourceAttributes DiscomfortIndexSensorResource::handleGetAttributesRequest()
{
return BundleResource::getAttributes();
}
virtual void initAttributes();
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs);
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs);
- virtual RCSResourceAttributes &handleGetAttributesRequest();
+ virtual RCSResourceAttributes handleGetAttributesRequest();
private:
BundleResource::setAttribute("color", 0);
}
-RCSResourceAttributes &HueLight::handleGetAttributesRequest()
+RCSResourceAttributes HueLight::handleGetAttributesRequest()
{
cout << "HueLight::handleGetAttributesRequest" << endl;
// TODO read from HueLight and update attribute data
return BundleResource::getAttributes();
}
-void HueLight::handleSetAttributesRequest(RCSResourceAttributes &value)
+void HueLight::handleSetAttributesRequest(const RCSResourceAttributes &value)
{
cout << "HueLight::handleSetAttributesRequest" << std::endl;
// TODO construct single write
- for (RCSResourceAttributes::iterator it = value.begin(); it != value.end(); it++)
+ for (RCSResourceAttributes::const_iterator it = value.begin(); it != value.end(); it++)
{
std::string attributeName = it->key();
RCSResourceAttributes::Value attrValue = it->value();
if (attributeName == "on-off")
{
- m_connector->transmit(this->m_address + "/state", "{\"on\":" + attrValue.toString() + "}");
+ //m_connector->transmit(this->m_address + "/state", "{\"on\":" + attrValue.toString() + "}");
}
if (attributeName == "dim")
{
// needs conversion * 2.5
- m_connector->transmit(this->m_address + "/state", "{\"bri\":" + attrValue.toString() + "}");
+ //m_connector->transmit(this->m_address + "/state", "{\"bri\":" + attrValue.toString() + "}");
}
if (attributeName == "color")
{
// needs conversion *650
- m_connector->transmit(this->m_address + "/state", "{\"hue\":" + attrValue.toString() + "}");
+ //m_connector->transmit(this->m_address + "/state", "{\"hue\":" + attrValue.toString() + "}");
}
}
BundleResource::setAttributes(value);
-}
\ No newline at end of file
+}
#include <list>
#include <string.h>
+#include <iostream>
+#include <boost/thread.hpp>
+#include "NotificationReceiver.h"
#include "InternalTypes.h"
{
namespace Service
{
- BundleResource::BundleResource()
+ BundleResource::BundleResource() : m_pNotiReceiver(nullptr)
{
- m_pNotiReceiver = nullptr;
+
}
BundleResource::~BundleResource()
std::list< std::string > BundleResource::getAttributeNames()
{
std::list< std::string > ret;
- for (RCSResourceAttributes::iterator it = m_resourceAttributes.begin();
- it != m_resourceAttributes.end(); ++it)
- {
- ret.push_back(it->key());
+
+ for (auto &it : m_resourceAttributes){
+ ret.push_back(it.key());
}
+
return ret;
}
- RCSResourceAttributes &BundleResource::getAttributes()
+ const RCSResourceAttributes BundleResource::getAttributes()
{
- return m_resourceAttributes;
+ std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+ return RCSResourceAttributes(m_resourceAttributes);
}
- void BundleResource::setAttributes(RCSResourceAttributes &attrs)
+ void BundleResource::setAttributes(const RCSResourceAttributes &attrs)
{
- for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
- {
+ std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+
+ for (auto &it : m_resourceAttributes){
OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'",
- std::string(it->key() + "\', with " + it->value().toString()).c_str());
+ std::string(it.key() + "\', with " + it.value().toString()).c_str());
- m_resourceAttributes[it->key()] = it->value();
+ m_resourceAttributes[it.key()] = it.value();
}
+
+ // asynchronous notification
+ auto notifyFunc = [](NotificationReceiver *notificationReceiver,
+ std::string uri)
+ {
+ if (notificationReceiver){
+ notificationReceiver->onNotificationReceived(uri);
+ }
+ };
+ auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
+ boost::thread notifyThread(f);
+
}
void BundleResource::setAttribute(const std::string &key,
{
OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'", std::string(key + "\', with " +
value.toString()).c_str());
+ std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+ m_resourceAttributes[key] = std::move(value);
+
+ if(notify){
+ // asynchronous notification
+ auto notifyFunc = [](NotificationReceiver *notificationReceiver,
+ std::string uri)
+ {
+ if (notificationReceiver){
+ notificationReceiver->onNotificationReceived(uri);
+ }
+ };
+ auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
+ boost::thread notifyThread(f);
+ }
- m_resourceAttributes[key] = value;
+ }
- sendNotification();
+ void BundleResource::setAttribute(const std::string &key,
+ RCSResourceAttributes::Value &value, bool notify)
+ {
+ setAttribute(key, RCSResourceAttributes::Value(value), notify);
}
- void BundleResource::setAttribute(const std::string &key, RCSResourceAttributes::Value &&value)
+ void BundleResource::setAttribute(const std::string &key,
+ RCSResourceAttributes::Value &&value)
{
setAttribute(key, std::move(value), true);
}
- RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
+ void BundleResource::setAttribute(const std::string &key,
+ RCSResourceAttributes::Value &value)
{
- OIC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
-
- return m_resourceAttributes.at(key);
+ setAttribute(key, value, true);
}
- void BundleResource::sendNotification()
+ RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
{
- if (m_pNotiReceiver)
- m_pNotiReceiver->onNotificationReceived(m_uri);
+ OIC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
+ std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+ return m_resourceAttributes.at(key);
}
}
}
}
-void JavaBundleResource::handleSetAttributesRequest(RCSResourceAttributes &attrs){
+void JavaBundleResource::handleSetAttributesRequest(const RCSResourceAttributes &attrs){
for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
{
handleSetAttributeRequest(it->key(),std::move(it->value()));
}
}
-RCSResourceAttributes & JavaBundleResource::handleGetAttributesRequest()
+const RCSResourceAttributes JavaBundleResource::handleGetAttributesRequest()
{
std::list<string> attrsNames = getAttributeNames();
for(std::list<string>::iterator iterator = attrsNames.begin();
RCSResourceAttributes::Value handleGetAttributeRequest(const std::string& key);
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs);
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs);
- virtual RCSResourceAttributes& handleGetAttributesRequest();
+ virtual RCSResourceAttributes handleGetAttributesRequest();
virtual void initAttributes();
private:
}
// to get notified if bundle resource attributes are updated
- resource->registerObserver((NotificationReceiver *) this);
+ resource->registerObserver(this);
ret = 0;
}
}
}
void ResourceContainerImpl::addBundle(const std::string &bundleId,
- const std::string &bundleUri, const std::string &bundlePath,
- const std::string &activator, std::map< string, string > params)
+ const std::string &bundleUri,
+ const std::string &bundlePath,
+ const std::string &activator,
+ std::map< string, string > params)
{
(void) bundleUri;
public:
virtual void initAttributes() { }
- virtual void handleSetAttributesRequest(RCSResourceAttributes &attr)
+ virtual void handleSetAttributesRequest(const RCSResourceAttributes &attr)
{
BundleResource::setAttributes(attr);
}
- virtual RCSResourceAttributes &handleGetAttributesRequest()
+ virtual RCSResourceAttributes handleGetAttributesRequest()
{
return BundleResource::getAttributes();
}
public:
void initAttributes() { };
- RCSResourceAttributes &handleGetAttributesRequest()
+ RCSResourceAttributes handleGetAttributesRequest()
{
return BundleResource::getAttributes();
}
void handleSetAttributesRequest(
- RCSResourceAttributes &value)
+ const RCSResourceAttributes &value)
{
BundleResource::setAttributes(value);
}
};
-#endif /* TESTBUNDLE_H_ */
\ No newline at end of file
+#endif /* TESTBUNDLE_H_ */