SkillProviderComponentPlugin.h
Go to the documentation of this file.
1#pragma once
2
3#include <experimental/memory>
4#include <map>
5#include <memory>
6#include <optional>
7#include <shared_mutex>
8#include <string>
9#include <vector>
10
11#include <Ice/Current.h>
12#include <IceUtil/Optional.h>
13
18
19#include <RobotAPI/interface/aron/Aron.h>
20#include <RobotAPI/interface/skills/SkillManagerInterface.h>
21#include <RobotAPI/interface/skills/SkillProviderInterface.h>
32
33namespace armarx
34{
35 class SkillProviderComponentPluginUser; // forward declaration
36}
37
38namespace armarx::plugins
39{
41 {
42 public:
43 using ComponentPlugin::ComponentPlugin;
44
45 void preOnInitComponent() override;
46
47 void preOnConnectComponent() override;
48 void postOnConnectComponent() override;
49
51
52 void preOnDisconnectComponent() override;
53
54 void addSkillFactory(std::unique_ptr<skills::SkillBlueprint>&&);
55
58
59 template <class SkillT, typename... Args>
60
63 addSkillFactory(Args&&... args)
64 {
65 auto fac = skills::SkillBlueprint::ForSkill<SkillT>(std::forward<Args>(args)...);
66 auto* facPtr = fac.get();
67 addSkillFactory(std::move(fac));
68 return static_cast<skills::SkillBlueprint*>(facPtr);
69 }
70
71 template <typename FactoryT, typename... Args>
72
74 FactoryT*
75 addCustomSkillFactory(Args&&... args)
76 {
77 auto fac = std::make_unique<FactoryT>(std::forward<Args>(args)...);
78 auto* facPtr = fac.get();
79 addSkillFactory(std::move(fac));
80 return static_cast<FactoryT*>(facPtr);
81 }
82
83 // Ice forwards
84 std::optional<skills::SkillStatusUpdate>
86 std::map<skills::SkillExecutionID, skills::SkillStatusUpdate>
88
89 std::optional<skills::SkillDescription> getSkillDescription(const skills::SkillID&) const;
90 std::map<skills::SkillID, skills::SkillDescription> getSkillDescriptions() const;
91
93
96
98 const armarx::aron::data::DictPtr& params);
99
100 bool abortSkill(const skills::SkillExecutionID& execId);
101
102 bool abortSkillAsync(const skills::SkillExecutionID& execId);
103
104 void updateSubSkillStatus(const skills::SkillStatusUpdate& statusUpdate);
105
106 const skills::manager::dti::SkillManagerInterfacePrx& skillManager() const;
107
108 private:
109 skills::SkillBlueprint* getSkillFactory(const armarx::skills::SkillID& name);
110
111
112 skills::manager::dti::SkillManagerInterfacePrx manager;
113 skills::provider::dti::SkillProviderInterfacePrx myPrx;
114
115 mutable std::shared_mutex skillFactoriesMutex;
116 std::map<skills::SkillID, std::unique_ptr<skills::SkillBlueprint>> skillFactories;
117
118 // Skill runtimes are owned via shared_ptr so the executing thread (which
119 // captures the shared_ptr by value in its lambda) can keep the runtime
120 // alive even if it has been removed from the map. Without this, a raw
121 // pointer into the map became dangling as soon as the cleanup pass
122 // erased the entry, or the local pointer variable in executeSkillAsync
123 // was destroyed when the function returned -> use-after-free.
124 mutable std::shared_mutex skillExecutionsMutex;
125 std::map<skills::SkillExecutionID, std::shared_ptr<skills::detail::SkillRuntime>>
126 skillExecutions;
127
128 // Removes entries from skillExecutions whose execution thread has
129 // already finished. Caller MUST hold an exclusive lock on
130 // skillExecutionsMutex. Joining of finished threads happens after
131 // collecting candidates and is done by the caller after releasing the
132 // lock to avoid blocking other operations on the map.
133 std::vector<std::shared_ptr<skills::detail::SkillRuntime>>
134 collectFinishedExecutions_locked();
135
137 };
138} // namespace armarx::plugins
139
140namespace armarx
141
142{
144 virtual public ManagedIceObject,
145 virtual public skills::provider::dti::SkillProviderInterface
146 {
147 public:
149
150 // Ice Implementations
151 IceUtil::Optional<skills::provider::dto::SkillDescription>
152 getSkillDescription(const skills::provider::dto::SkillID& skill,
153 const Ice::Current& current = Ice::Current()) override;
154
155 skills::provider::dto::SkillDescriptionMap
156 getSkillDescriptions(const Ice::Current& current = Ice::Current()) override;
157
158 IceUtil::Optional<skills::provider::dto::SkillStatusUpdate>
159 getSkillExecutionStatus(const skills::provider::dto::SkillExecutionID& executionId,
160 const Ice::Current& current = Ice::Current()) override;
161
162 skills::provider::dto::SkillStatusUpdateMap
163 getSkillExecutionStatuses(const Ice::Current& current = Ice::Current()) override;
164
165 skills::provider::dto::SkillStatusUpdate
166 executeSkill(const skills::provider::dto::SkillExecutionRequest& executionInfo,
167 const Ice::Current& current = Ice::Current()) override;
168
169 skills::provider::dto::SkillExecutionID
170 executeSkillAsync(const skills::provider::dto::SkillExecutionRequest& executionInfo,
171 const Ice::Current& current = Ice::Current()) override;
172
173 skills::provider::dto::ParameterUpdateResult
174 updateSkillParameters(const skills::provider::dto::SkillExecutionID& executionId,
175 const armarx::aron::data::dto::DictPtr& parameters,
176 const Ice::Current& current = Ice::Current()) override;
177
178 skills::provider::dto::AbortSkillResult
179 abortSkill(const skills::provider::dto::SkillExecutionID& skill,
180 const Ice::Current& current = Ice::Current()) override;
181
182 skills::provider::dto::AbortSkillResult
183 abortSkillAsync(const skills::provider::dto::SkillExecutionID& skill,
184 const Ice::Current& current = Ice::Current()) override;
185
186 void reportSkillEvent(const skills::provider::dto::SkillStatusUpdate& statusUpdate,
187 const std::string& providerName,
188 const Ice::Current& current) override;
189
190 // Plugin
193
194 protected:
195 // -----------------------------------------------------------------------------------------
196 // New
197 // -----------------------------------------------------------------------------------------
201 {
202 auto ret = plugin->addSkillFactory(desc, f);
203 return ret;
204 }
205
206 template <class SkillT, typename... Args>
207
210 addSkillFactory(Args&&... args)
211 {
212 return plugin->addSkillFactory<SkillT>(std::forward<Args>(args)...);
213 }
214
215 template <typename FacT, typename... Args>
216
218 FacT*
219 addCustomSkillFactory(Args&&... args)
220 {
221 return plugin->addCustomSkillFactory<FacT>(std::forward<Args>(args)...);
222 }
223
224 private:
226 };
227} // namespace armarx
ManagedIceObject(ManagedIceObject const &other)
IceUtil::Optional< skills::provider::dto::SkillStatusUpdate > getSkillExecutionStatus(const skills::provider::dto::SkillExecutionID &executionId, const Ice::Current &current=Ice::Current()) override
skills::provider::dto::SkillStatusUpdateMap getSkillExecutionStatuses(const Ice::Current &current=Ice::Current()) override
skills::provider::dto::AbortSkillResult abortSkill(const skills::provider::dto::SkillExecutionID &skill, const Ice::Current &current=Ice::Current()) override
IceUtil::Optional< skills::provider::dto::SkillDescription > getSkillDescription(const skills::provider::dto::SkillID &skill, const Ice::Current &current=Ice::Current()) override
skills::provider::dto::SkillStatusUpdate executeSkill(const skills::provider::dto::SkillExecutionRequest &executionInfo, const Ice::Current &current=Ice::Current()) override
skills::provider::dto::SkillExecutionID executeSkillAsync(const skills::provider::dto::SkillExecutionRequest &executionInfo, const Ice::Current &current=Ice::Current()) override
skills::provider::dto::AbortSkillResult abortSkillAsync(const skills::provider::dto::SkillExecutionID &skill, const Ice::Current &current=Ice::Current()) override
const std::experimental::observer_ptr< plugins::SkillProviderComponentPlugin > & getSkillProviderPlugin() const
void reportSkillEvent(const skills::provider::dto::SkillStatusUpdate &statusUpdate, const std::string &providerName, const Ice::Current &current) override
skills::SkillBlueprint * addSkillFactory(const skills::SkillDescription &desc, const skills::LambdaSkill::FunctionType &f)
skills::provider::dto::ParameterUpdateResult updateSkillParameters(const skills::provider::dto::SkillExecutionID &executionId, const armarx::aron::data::dto::DictPtr &parameters, const Ice::Current &current=Ice::Current()) override
skills::SkillBlueprint * addSkillFactory(Args &&... args)
skills::provider::dto::SkillDescriptionMap getSkillDescriptions(const Ice::Current &current=Ice::Current()) override
skills::SkillExecutionID executeSkillAsync(const skills::SkillExecutionRequest &executionInfo)
bool abortSkill(const skills::SkillExecutionID &execId)
skills::SkillStatusUpdate executeSkill(const skills::SkillExecutionRequest &executionInfo)
std::optional< skills::SkillStatusUpdate > getSkillExecutionStatus(const skills::SkillExecutionID &) const
std::optional< skills::SkillDescription > getSkillDescription(const skills::SkillID &) const
void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
const skills::manager::dti::SkillManagerInterfacePrx & skillManager() const
std::map< skills::SkillExecutionID, skills::SkillStatusUpdate > getSkillExecutionStatuses() const
void addSkillFactory(std::unique_ptr< skills::SkillBlueprint > &&)
bool updateSkillParameters(const skills::SkillExecutionID &id, const armarx::aron::data::DictPtr &params)
std::map< skills::SkillID, skills::SkillDescription > getSkillDescriptions() const
void updateSubSkillStatus(const skills::SkillStatusUpdate &statusUpdate)
skills::SkillBlueprint * addSkillFactory(Args &&... args)
bool abortSkillAsync(const skills::SkillExecutionID &execId)
std::function< TerminatedSkillStatus()> FunctionType
Definition LambdaSkill.h:12
static std::unique_ptr< SkillBlueprint > ForSkill(Args &&... args)
::IceInternal::Handle< Dict > DictPtr
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.