16 #ifndef ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONPROPERTIESHANDLER_H_ 17 #define ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONPROPERTIESHANDLER_H_ 22 #include <unordered_map> 32 namespace acsdkCommunication {
39 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationProperty<T>>
property;
40 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationPropertyValidatorInterface<T>>
writeValidator;
60 ,
public notifier::Notifier<acsdkCommunicationInterfaces::CommunicationPropertyChangeSubscriber<T>> {
65 this->notifyObservers(
67 observer) { observer->onCommunicationPropertyChange(propertyName, newValue); });
73 std::shared_ptr<acsdkCommunicationInterfaces::CommunicationProperty<T>> registerProperty(
78 void deregisterProperty(
81 bool writeProperty(
const std::string& propertyName, T newValue)
override;
83 bool readProperty(
const std::string& propertyName, T& value)
override;
85 bool subscribeToPropertyChangeEvent(
90 bool unsubscribeToPropertyChangeEvent(
98 std::unordered_map<std::string, PropertyInfo<T>> m_properties;
100 std::unordered_map<std::string, std::shared_ptr<WeakSubscriptionProxy>> m_subscribers;
102 std::mutex m_propertiesMutex;
104 template <
typename T>
111 std::unique_lock<std::mutex> lock(m_propertiesMutex);
112 auto it = m_properties.find(propertyName);
113 if (it != m_properties.end()) {
114 if (!it->second.property.expired()) {
116 "InMemoryCommunicationPropertiesHandler",
"registerProperty")
117 .
m(
"Property is already Registered")
118 .d(
"property", propertyName));
122 m_properties.erase(it);
126 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationProperty<T>> weakProperty =
127 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationProperty<T>>(
property);
128 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationPropertyValidatorInterface<T>> weakValidator =
129 std::weak_ptr<acsdkCommunicationInterfaces::CommunicationPropertyValidatorInterface<T>>(
writeValidator);
133 .
m(
"Property is Registered")
134 .d(
"property", propertyName));
136 m_properties.insert({propertyName, currentInfo});
138 auto& subscriberProxy = m_subscribers[propertyName];
139 if (!subscriberProxy) {
140 subscriberProxy = std::make_shared<WeakSubscriptionProxy>();
142 property->addSubscriber(subscriberProxy);
147 template <
typename T>
151 std::unique_lock<std::mutex> lock(m_propertiesMutex);
152 auto it = m_properties.find(propertyName);
153 if (it == m_properties.end()) {
155 "InMemoryCommunicationPropertiesHandler",
"deregisterProperty")
156 .
m(
"Property is not Registered")
157 .d(
"property", propertyName));
160 if (it->second.property.lock() !=
property) {
162 "InMemoryCommunicationPropertiesHandler",
"deregisterProperty")
163 .
m(
"Property is registered but can not be matched")
164 .d(
"property", propertyName));
167 m_properties.erase(it);
170 template <
typename T>
172 std::unique_lock<std::mutex> lock(m_propertiesMutex);
173 auto it = m_properties.find(propertyName);
174 if (it == m_properties.end()) {
176 "InMemoryCommunicationPropertiesHandler",
"writeProperty")
177 .
m(
"Property is not Registered")
178 .d(
"property", propertyName));
181 auto property = it->second.property.lock();
184 "InMemoryCommunicationPropertiesHandler",
"writeProperty")
185 .
m(
"Property has expired")
186 .d(
"property", propertyName));
187 m_properties.erase(it);
192 "InMemoryCommunicationPropertiesHandler",
"writeProperty")
193 .
m(
"Property is not writeable")
194 .d(
"property", propertyName));
197 auto validator = it->second.writeValidator.lock();
200 "InMemoryCommunicationPropertiesHandler",
"writeProperty")
201 .
m(
"Can't validate property")
202 .d(
"property", propertyName));
206 if (validator->validateWriteRequest(propertyName, newValue)) {
207 property->setValue(newValue);
212 template <
typename T>
214 std::unique_lock<std::mutex> lock(m_propertiesMutex);
215 auto it = m_properties.find(propertyName);
216 if (it == m_properties.end()) {
219 .
m(
"Property is not Registered")
220 .d(
"property", propertyName));
223 auto property = it->second.property.lock();
227 .
m(
"Property has expired")
228 .d(
"property", propertyName));
229 m_properties.erase(it);
232 value =
property->getValue();
236 template <
typename T>
240 std::unique_lock<std::mutex> lock(m_propertiesMutex);
241 auto& subscriberProxy = m_subscribers[propertyName];
242 if (!subscriberProxy) {
243 subscriberProxy = std::make_shared<WeakSubscriptionProxy>();
245 subscriberProxy->addWeakPtrObserver(subscriber);
246 return !subscriber.expired();
249 template <
typename T>
253 std::unique_lock<std::mutex> lock(m_propertiesMutex);
254 auto& subscriberProxy = m_subscribers[propertyName];
255 if (!subscriberProxy) {
256 subscriberProxy = std::make_shared<WeakSubscriptionProxy>();
258 subscriberProxy->removeWeakPtrObserver(subscriber);
264 #endif // ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONPROPERTIESHANDLER_H_ bool readProperty(const std::string &propertyName, T &value) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:213
std::shared_ptr< acsdkCommunicationInterfaces::CommunicationProperty< T > > registerProperty(const std::string &propertyName, T initValue, const std::shared_ptr< acsdkCommunicationInterfaces::CommunicationPropertyValidatorInterface< T >> &writeValidator=nullptr) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:106
::std::string string
Definition: gtest-port.h:1097
bool unsubscribeToPropertyChangeEvent(const std::string &propertyName, const std::shared_ptr< acsdkCommunicationInterfaces::CommunicationPropertyChangeSubscriber< T >> &subscriber) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:250
Definition: InMemoryCommunicationPropertiesHandler.h:58
Definition: CommunicationPropertyChangeSubscriber.h:28
#define ACSDK_DEBUG(entry)
Definition: Logger.h:454
std::weak_ptr< acsdkCommunicationInterfaces::CommunicationProperty< T > > property
Definition: InMemoryCommunicationPropertiesHandler.h:39
Generic implementation of NotifierInterface.
Definition: Notifier.h:38
Definition: InMemoryCommunicationPropertiesHandler.h:38
std::mutex m
Definition: AlexaPresentationTest.cpp:91
static std::shared_ptr< CommunicationProperty< T > > create(const std::string &name, T initValue, bool writeable)
Definition: CommunicationProperty.h:74
Definition: CommunicationPropertyValidatorInterface.h:30
void onCommunicationPropertyChange(const std::string &propertyName, T newValue) override
Definition: InMemoryCommunicationPropertiesHandler.h:64
bool writeProperty(const std::string &propertyName, T newValue) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:171
std::weak_ptr< acsdkCommunicationInterfaces::CommunicationPropertyValidatorInterface< T > > writeValidator
Definition: InMemoryCommunicationPropertiesHandler.h:40
#define ACSDK_ERROR(entry)
Definition: Logger.h:481
void deregisterProperty(const std::string &propertyName, const std::shared_ptr< acsdkCommunicationInterfaces::CommunicationProperty< T >> &property) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:148
Definition: InMemoryCommunicationPropertiesHandler.h:47
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
Definition: CommunicationPropertiesHandlerInterface.h:35
Definition: CommunicationProperty.h:34
bool subscribeToPropertyChangeEvent(const std::string &propertyName, const std::weak_ptr< acsdkCommunicationInterfaces::CommunicationPropertyChangeSubscriber< T >> &subscriber) override
}@
Definition: InMemoryCommunicationPropertiesHandler.h:237
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33