Component.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5*
6* ArmarX is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* ArmarX is distributed in the hope that it will be useful, but
11* WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*
18* @package ArmarXCore::core
19* @author Kai Welke (welke at kit dot edu)
20* @date 2012
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
25#include "Component.h"
26
27#include <set>
28#include <string>
29#include <vector>
30
31#include <boost/current_function.hpp>
32
40#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
42
43namespace armarx
44{
45
47 bool hasObjectNameParameter) :
49 {
51 if (hasObjectNameParameter)
52 {
54 "ObjectName", "", "Name of IceGrid well-known object");
55 }
56 defineOptionalProperty<MessageTypeT>("MinimumLoggingLevel",
58 "Local logging level only for this component",
60 defineOptionalProperty<bool>("EnableProfiling",
61 false,
62 "enable profiler which is used for logging performance events",
64 }
65
67 {
70 configName = "";
71 configDomain = "ArmarX";
72 createdByComponentCreate = false;
73 }
74
75 std::string
77 {
79 return configDomain;
80 }
81
82 std::string
84 {
86 return configName;
87 }
88
89 std::string
91 {
93 return configDomain + "." + configName;
94 }
95
96 void
97 Component::initializeProperties(const std::string& configName,
98 const Ice::PropertiesPtr& properties,
99 const std::string& configDomain)
100 {
102 ARMARX_DEBUG << "Initializing properties.";
103
104 this->configDomain = configDomain;
105 this->configName = configName;
106
107 // set default object name to configName
108 setName(this->configName);
109
110 // set properties
111 setIceProperties(properties);
113 }
114
115 void
117 {
119 ARMARX_DEBUG << "Preparing for onInitComponent().";
120
121 MessageTypeT const currentLoggingLevel = getEffectiveLoggingLevel();
122 if (currentLoggingLevel == MessageTypeT::DEBUG or
123 currentLoggingLevel == MessageTypeT::VERBOSE)
124 {
126 << "Logging level of this component is very low (Debug or Verbose). For debugging "
127 "and development purposes, this warning can be ignored and only serves as a "
128 "reminder. A low logging level can have a performance impact, so reset the "
129 "logging level to at least Info after it is not used anymore, and especially "
130 "for stable or production workspaces.";
131 }
132
133 // Update all referenced properties.
135
136 // Subscribe to all configured topics.
137 for (const std::string& offered_topic_name : getSubscribedTopicNames())
138 {
139 ARMARX_DEBUG << "Subscribing to topic `" << offered_topic_name << "`.";
140 usingTopic(offered_topic_name);
141 }
142
143 // Signal dependencies on configured components.
144 for (const std::string& proxy_name : getComponentProxyNames())
145 {
146 ARMARX_DEBUG << "Using component proxy `" << proxy_name << "`.";
147 usingProxy(proxy_name);
148 }
149
150 // Offer all configured topics.
151 for (const std::string& proxy_name : getTopicProxyNames())
152 {
153 ARMARX_DEBUG << "Offering topic `" << proxy_name << "`.";
154 offeringTopic(proxy_name);
155 }
156 }
157
158 void
160 {
162 ARMARX_DEBUG << "Preparing for onConnectComponent().";
163 // Update all referenced proxy properties.
165 }
166
167 void
169 {
171 additionalPropertyUsers.push_back(subPropertyUser);
172 }
173
174 std::vector<PropertyUserPtr>
176 {
178 return additionalPropertyUsers;
179 }
180
181 void
182 Component::offeringTopicFromProperty(const std::string& propertyName)
183 {
185 }
186
187 void
188 Component::usingTopicFromProperty(const std::string& propertyName, bool orderedPublishing)
189 {
191 usingTopic(getProperty<std::string>(propertyName), orderedPublishing);
192 }
193
194 bool
195 Component::usingProxyFromProperty(const std::string& propertyName, const std::string& endpoints)
196 {
198 return usingProxy(getProperty<std::string>(propertyName), endpoints);
199 }
200
201 void
203 {
205 this->createdByComponentCreate = true;
206 }
207
208 void
210 {
211 ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...";
212 foreach_plugin(
213 [&](const auto& typeidx, const auto& name, const auto& plugin)
214 {
215 if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
216 {
217 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
218 << ") postCreatePropertyDefinitions...";
219 ptr->postCreatePropertyDefinitions(props);
220 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
221 << ") postCreatePropertyDefinitions...done!";
222 }
223 else
224 {
226 << "plugin '" << name << "' (" << GetTypeString(typeidx)
227 << ") is no ComponentPlugin (not calling postCreatePropertyDefinitions)";
228 }
229 },
230 __LINE__,
231 __FILE__,
232 BOOST_CURRENT_FUNCTION);
233 ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...done!";
234 }
235
242
243 void
244 Component::icePropertiesUpdated(const std::set<std::string>& changedProperties)
245 {
247 // check if properties have been initialized
248 if (getConfigDomain().empty() || getConfigName().empty())
249 {
250 return;
251 }
252
253 // set the logging behavior for this component
254 if (changedProperties.count("MinimumLoggingLevel") &&
255 getProperty<MessageTypeT>("MinimumLoggingLevel").isSet())
256 {
258 getProperty<MessageTypeT>("MinimumLoggingLevel").getValue());
259 }
260
261 // get well-known name of this component instance
262 // (default is the ManagedIceObject name itself)
263 if (changedProperties.count("ObjectName") && getProperty<std::string>("ObjectName").isSet())
264 {
266 }
267
268 // set logging tag, may be overwritten by implementer
269 setTag(getName());
270
271 // enable Profiling
272 // enable via global ArmarX.Profiling property and disable with specific one?
273 if (changedProperties.count("EnableProfiling"))
274 {
275 enableProfiler(getProperty<bool>("EnableProfiling").getValue());
276 }
277
278 //only update user properties after the object was initialized
279 if (getState() > eManagedIceObjectInitialized)
280 {
281 componentPropertiesUpdated(changedProperties);
282 ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...";
283 foreach_plugin(
284 [&](const auto& typeidx, const auto& name, const auto& plugin)
285 {
286 if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
287 {
288 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
289 << ") componentPropertiesUpdated...";
290 ptr->componentPropertiesUpdated(changedProperties);
291 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
292 << ") componentPropertiesUpdated...done!";
293 }
294 else
295 {
297 << "plugin '" << name << "' (" << GetTypeString(typeidx.name())
298 << ") is no ComponentPlugin (not calling componentPropertiesUpdated)";
299 }
300 },
301 __LINE__,
302 __FILE__,
303 BOOST_CURRENT_FUNCTION);
304 ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...done!";
305 }
306 }
307
308 void
309 Component::componentPropertiesUpdated(const std::set<std::string>& changedProperties)
310 {
312 (void)changedProperties;
313 }
314
315} // namespace armarx
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition Component.cpp:66
std::string getConfigName()
Retrieve config name for this component as set in constructor.
Definition Component.cpp:83
void forceComponentCreatedByComponentCreateFunc()
forces the flag to be set to true that the object instance was created by the Component::create funct...
virtual void componentPropertiesUpdated(const std::set< std::string > &changedProperties)
Implement this function if you would like to react to changes in the properties.
std::vector< PropertyUserPtr > getAdditionalPropertyUsers() const
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition Component.cpp:76
bool usingProxyFromProperty(const std::string &propertyName, const std::string &endpoints="")
Use a proxy whose name is specified by the given property.
void offeringTopicFromProperty(const std::string &propertyName)
Offer a topic whose name is specified by the given property.
virtual void preOnInitComponent() override
virtual void preOnConnectComponent() override
void initializeProperties(const std::string &configName, Ice::PropertiesPtr const &properties, const std::string &configDomain)
initializes the properties of this component.
Definition Component.cpp:97
void usingTopicFromProperty(const std::string &propertyName, bool orderedPublishing=false)
Use a topic whose name is specified by the given property.
virtual void icePropertiesInitialized()
Definition Component.h:308
void addPropertyUser(const PropertyUserPtr &subPropertyUser)
Add additional property users here that should show up in the application help text.
PropertyDefinitionsPtr createPropertyDefinitions() override
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void injectPropertyDefinitions(PropertyDefinitionsPtr &props) override
void setTag(const LogTag &tag)
Definition Logging.cpp:54
void setLocalMinimumLoggingLevel(MessageTypeT level)
With setLocalMinimumLoggingLevel the minimum verbosity-level of log-messages can be set.
Definition Logging.cpp:66
MessageTypeT getEffectiveLoggingLevel() const
Definition Logging.cpp:130
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
std::string getName() const
Retrieve name of object.
IceManagerPtr getIceManager() const
Returns the IceManager.
int getState() const
Retrieve current state of the ManagedIceObject.
void setName(std::string name)
Override name of well-known object.
void enableProfiler(bool enable)
setProfiler allows setting ManagedIceObject::profiler to a new instance (if the new instance is actua...
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
std::vector< std::string > getComponentProxyNames()
void updateProxies(IceManagerPtr)
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
std::vector< std::string > getSubscribedTopicNames()
std::vector< std::string > getTopicProxyNames()
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition utils.h:80
client::plugins::ComponentPlugin ComponentPlugin
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
MessageTypeT
Definition LogSender.h:46
#define ARMARX_TRACE
Definition trace.h:77