RemoteStateOfferer.h
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::Statechart
19 * @author Mirko Waechter( mirko.waechter 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 #pragma once
25 
26 // Slice Includes
27 #include <ArmarXCore/interface/statechart/RemoteStateOffererIce.h>
28 
29 // ArmarX Includes
33 
34 // Statechart Includes
35 #include "State.h"
36 #include "RemoteState.h"
37 #include "RemoteStateWrapper.h"
38 #include "StatechartManager.h"
39 
40 #include <type_traits>
41 
42 namespace armarx
43 {
44  std::string joinStrings(std::vector<std::string> const& input, std::string const& seperator);
45 
47  : virtual RemoteStateOffererIceBase
48  , virtual State
49  {
50  //! \brief Implement this function to specify the RemoteStateOfferer prefix.
51  //! \note Do not override getDefaultName()!
52  virtual std::string getStateOffererName() const = 0;
53  //! \brief Pure virtual function, in which the states must be added, that should be remote-accessable.
54  virtual void onInitRemoteStateOfferer() = 0;
55  //! \brief Virtual function, in which the user can fetch some proxies.
56  virtual void onConnectRemoteStateOfferer();
57  //! \brief Virtual function, in which the user can implement some clean up.
58  virtual void onExitRemoteStateOfferer();
59 
60 
61  virtual void waitUntilComponentStarted() = 0;
62 
63  // inherited from RemoteStateOffererInterface
64  CreateRemoteStateInstanceOutput createRemoteStateInstanceNew(
65  CreateRemoteStateInstanceInput const& input,
66  Ice::Current const& context) override;
67 
69  const std::string& stateName,
70  const RemoteStateIceBasePrx& remoteStatePrx,
71  const std::string& parentStateItentifierStr,
72  const std::string& instanceName,
73  const Ice::Current& context = Ice::emptyCurrent) override;
74 
76  int stateId,
77  const std::string& parentId,
78  const Ice::Current& context = Ice::emptyCurrent) override;
79 
80  void callRemoteState(
81  int stateId,
82  const StringVariantContainerBaseMap& properties,
83  const Ice::Current& context = Ice::emptyCurrent) override;
84 
85  void exitRemoteState(
86  int stateId,
87  const ::Ice::Current& context = Ice::emptyCurrent) override;
88 
89  bool breakRemoteState(
90  int stateId,
91  const EventBasePtr& evt,
92  const ::Ice::Current& context = Ice::emptyCurrent) override;
93 
95  int stateId,
96  const ::Ice::Current& context = Ice::emptyCurrent) override;
97 
99  int stateId,
100  const EventBasePtr& evt,
101  const ::Ice::Current& context = Ice::emptyCurrent) override;
102 
103  void notifyEventBufferedDueToUnbreakableStateRemote(int stateId, bool eventBuffered, const ::Ice::Current& context = Ice::emptyCurrent) override;
104  StateIceBasePtr refetchRemoteSubstates(int stateId, const ::Ice::Current& context = Ice::emptyCurrent) override;
105  StateParameterMap getRemoteInputParameters(const std::string& stateName, const ::Ice::Current& context = Ice::emptyCurrent) override;
106  StateParameterMap getRemoteOutputParameters(const std::string& stateName, const ::Ice::Current& context = Ice::emptyCurrent) override;
107  StateParameterMap getRemoteInputParametersById(int stateId, const ::Ice::Current& context = Ice::emptyCurrent) override;
108  StateParameterMap getRemoteOutputParametersById(int stateId, const ::Ice::Current& context = Ice::emptyCurrent) override;
109 
110  bool hasSubstatesRemote(const std::string& stateName, const ::Ice::Current& context = Ice::emptyCurrent) const override;
111  bool hasActiveSubstateRemote(int stateId, const ::Ice::Current& context = Ice::emptyCurrent) override;
112  Ice::StringSeq getAvailableStates(const ::Ice::Current& context = Ice::emptyCurrent) override;
113  StateIdNameMap getAvailableStateInstances(const ::Ice::Current& context = Ice::emptyCurrent) override;
114  StateIceBasePtr getStatechart(const std::string& stateName, const ::Ice::Current& context = Ice::emptyCurrent) override;
115  StateIceBasePtr getStatechartInstance(int stateId, const Ice::Current& = Ice::emptyCurrent) override;
116  StateIceBasePtr getStatechartInstanceByGlobalIdStr(const std::string& globalStateIdStr, const Ice::Current& = Ice::emptyCurrent) override;
117  bool isHostOfStateByGlobalIdStr(const std::string& globalStateIdStr, const Ice::Current& = Ice::emptyCurrent) override;
118 
119  void removeInstance(int stateId, const Ice::Current& = Ice::emptyCurrent) override;
120  void issueEvent(int receivingStateId, const EventBasePtr& evt, const Ice::Current& = Ice::emptyCurrent) override;
121  void issueEventWithGlobalIdStr(const std::string& globalStateIdStr, const EventBasePtr& evt, const Ice::Current& = Ice::emptyCurrent) override;
122 
123  //! \brief Overridden so that the user cannot use it
124  RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName) override;
125  //! \brief Overridden so that the user cannot use it
126  RemoteStatePtr addDynamicRemoteState(std::string instanceName) override;
127 
128 
129  /** \struct RemoteStateData
130  A struct that holds meta data for a remoteaccessable state instances.
131  */
133  {
134  //! Local id of this RemoteStateOfferer, that identifies the state instance in the stateInstanceList
135  int id;
136  //! Not used yet.
137  std::string callerIceName;
138  //! Proxy to the state, that called this state
139  RemoteStateIceBasePrx callerStatePrx;
140  //! Pointer to a Pseudo parent state, that contains the real state instance.
142 
143  };
144  RemoteStateData getInstance(int stateId);
145  StateBasePtr getGlobalInstancePtr(int globalId) const;
146  virtual StateBasePtr getStatePtr(const std::string& stateName) const;
147  std::map<int, StateBasePtr> getChildStatesByName(int parentId, std::string stateName);
148 
149  StateIceBasePtr getStatechartInstanceByGlobalIdStrRecursive(const std::string& globalStateIdStr, StateBasePtr state, int& stateCounter);
150 
152  void onExitStatechartImpl();
153  void run() override;
154 
155  void initState(State& state);
156 
157 
158 
160  //! Holds the instances that where requested from remotely located states
161  std::map<int, RemoteStateData> stateInstanceList;
162  std::string componentName;
163  };
164 
165  /**
166  \class RemoteStateOfferer
167  \brief Class that holds states, which offer functionality for other states over Ice.
168  \ingroup StatechartGrp
169  To offer such functionality one must derive from this class and implement
170  the function armarx::RemoteStateOfferer::onInitRemoteStateOfferer() and add the states offering remote functionality there.<br/>
171  For every incoming call to a state a copy of the base instance is created, so that concurrent calls dont interfere with each other.<br/>
172  The implemented, remoteaccessable states must call setRemoteaccessable().<br/>
173  An Example can be found in applications/StateChartExamples/RemoteAccessableStateExample.
174 
175  \tparam ContextType The template parameter must be a class that derives from
176  StatechartContext
177 
178  \see StatechartContext, RemoteState, DynamicRemoteState, RemoteStateWrapper
179 
180  */
181  template <typename ContextType = StatechartContext>
183  : virtual RemoteStateOffererBase
184  , virtual ContextType
185  {
186  void waitUntilComponentStarted() override;
187 
188  static_assert(std::is_base_of_v<StatechartContext, ContextType>,
189  "The template parameter of RemoteStateOfferer, must be a class that derives from StatechartContext or StatechartContext itself");
190 
191  std::string getDefaultName() const override
192  {
193  return getStateOffererName() + "StateOfferer";
194  }
195 
196  void onInitStatechart() override;
197  void onConnectStatechart() override;
198  void onExitStatechart() override;
199  };
200 
201 
202  /////////////////////////////////////////////////////////////
203  ////////////// Implementation
204  /////////////////////////////////////////////////////////////
205 
206 
207  template <typename ContextType>
209  {
210  stateName = getDefaultName();
211  ContextType::setToplevelState(this);
212 
213  StatePhase oldPhase = getStatePhase();
214  setStatePhase(eSubstatesDefinitions);
215 
216 
217  try
218  {
219  onInitRemoteStateOfferer();
220  }
221  catch (...)
222  {
223  setStatePhase(oldPhase);
225  throw;
226  }
227 
228  for (AbstractStateIceBasePtr state : subStateList)
229  {
230  StateControllerPtr stateController = StateControllerPtr::dynamicCast(state);
231 
232  if (stateController)
233  {
234  int numberLogLevels = Component::getProperty<int>("ProfilingDepth").getValue();
235  stateController->addProfilerRecursive(Component::getProfiler(), numberLogLevels);
236  stateController->addProfilerRecursive(StatechartContext::stateReporter, -1);
237  }
238  }
239 
240  ContextType::setAutoEnterToplevelState(false);
241  }
242 
243  template<typename ContextType>
245  {
246  onConnectStatechartImpl();
247  }
248 
249  template<typename ContextType>
251  {
252  onExitStatechartImpl();
253  }
254 
255  template<typename ContextType>
257  {
258  if (!ContextType::getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted, 5000))
259  {
260  throw LocalException() << "Cannot create a remote state instance because the RemoteStateOfferer '"
261  << ContextType::getName()
262  << "' is still waiting for dependencies: "
263  << joinStrings(ContextType::getUnresolvedDependencies(), ", ");
264  }
265  }
266 
267 }
armarx::RemoteStateOffererBase::createRemoteStateInstance
int createRemoteStateInstance(const std::string &stateName, const RemoteStateIceBasePrx &remoteStatePrx, const std::string &parentStateItentifierStr, const std::string &instanceName, const Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:92
armarx::RemoteStateOffererBase::waitUntilComponentStarted
virtual void waitUntilComponentStarted()=0
armarx::RemoteStateOffererBase::breakRemoteState
bool breakRemoteState(int stateId, const EventBasePtr &evt, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:187
armarx::RemoteStateOffererBase::isRemoteStateFinished
bool isRemoteStateFinished(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:207
armarx::RemoteStateOffererBase::refetchRemoteSubstates
StateIceBasePtr refetchRemoteSubstates(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:224
RemoteStateWrapper.h
armarx::RemoteStateOffererBase::createRemoteStateInstanceNew
CreateRemoteStateInstanceOutput createRemoteStateInstanceNew(CreateRemoteStateInstanceInput const &input, Ice::Current const &context) override
Definition: RemoteStateOfferer.cpp:16
ArmarXManager.h
armarx::RemoteStateOffererBase::getRemoteOutputParametersById
StateParameterMap getRemoteOutputParametersById(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:251
armarx::RemoteStateOffererBase::hasActiveSubstateRemote
bool hasActiveSubstateRemote(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:269
armarx::RemoteStateOffererBase::run
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
Definition: RemoteStateOfferer.cpp:632
armarx::RemoteStateOffererBase::getStatePtr
virtual StateBasePtr getStatePtr(const std::string &stateName) const
Definition: RemoteStateOfferer.cpp:508
armarx::RemoteStateOffererBase
Definition: RemoteStateOfferer.h:46
armarx::StateBase::StatePhase
StatePhase
enum that specifies the phase in which the state is currently in used to control the usage of state-f...
Definition: StateBase.h:265
armarx::RemoteStateOffererBase::removeInstance
void removeInstance(int stateId, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:401
armarx::HiddenTimedMutex
Definition: Synchronization.h:81
armarx::RemoteStateOffererBase::RemoteStateData::id
int id
Local id of this RemoteStateOfferer, that identifies the state instance in the stateInstanceList.
Definition: RemoteStateOfferer.h:135
armarx::RemoteStateOffererBase::onConnectRemoteStateOfferer
virtual void onConnectRemoteStateOfferer()
Virtual function, in which the user can fetch some proxies.
Definition: RemoteStateOfferer.cpp:8
armarx::ManagedIceObject::getProfiler
Profiler::ProfilerPtr getProfiler() const
getProfiler returns an instance of armarx::Profiler
Definition: ManagedIceObject.cpp:359
armarx::RemoteStateOfferer::onConnectStatechart
void onConnectStatechart() override
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
Definition: RemoteStateOfferer.h:244
armarx::RemoteStateOffererBase::notifyEventBufferedDueToUnbreakableStateRemote
void notifyEventBufferedDueToUnbreakableStateRemote(int stateId, bool eventBuffered, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:217
armarx::RemoteStateOffererBase::issueEvent
void issueEvent(int receivingStateId, const EventBasePtr &evt, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:418
armarx::RemoteStateOffererBase::getAvailableStateInstances
StateIdNameMap getAvailableStateInstances(const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:286
armarx::RemoteStateOfferer::onExitStatechart
void onExitStatechart() override
onExitStatechart can be implemented by subclasses
Definition: RemoteStateOfferer.h:250
armarx::RemoteStateOffererBase::addDynamicRemoteState
RemoteStatePtr addDynamicRemoteState(std::string instanceName) override
Overridden so that the user cannot use it.
Definition: RemoteStateOfferer.cpp:463
armarx::RemoteStateOffererBase::getChildStatesByName
std::map< int, StateBasePtr > getChildStatesByName(int parentId, std::string stateName)
Definition: RemoteStateOfferer.cpp:535
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RemoteStateOffererBase::getAvailableStates
Ice::StringSeq getAvailableStates(const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:274
armarx::RemoteStateOffererBase::RemoteStateData::callerStatePrx
RemoteStateIceBasePrx callerStatePrx
Proxy to the state, that called this state.
Definition: RemoteStateOfferer.h:139
armarx::RemoteStateOfferer
Class that holds states, which offer functionality for other states over Ice.
Definition: RemoteStateOfferer.h:182
armarx::RemoteStateOffererBase::getRemoteInputParametersById
StateParameterMap getRemoteInputParametersById(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:246
armarx::RemoteStateOffererBase::initState
void initState(State &state)
Definition: RemoteStateOfferer.cpp:636
armarx::RemoteStateOffererBase::getStatechart
StateIceBasePtr getStatechart(const std::string &stateName, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:300
armarx::RemoteStateOffererBase::onConnectStatechartImpl
void onConnectStatechartImpl()
Definition: RemoteStateOfferer.cpp:597
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
State.h
armarx::RemoteStateOffererBase::breakActiveSubstateRemotely
bool breakActiveSubstateRemotely(int stateId, const EventBasePtr &evt, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:212
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
RemoteState.h
armarx::RemoteStateOffererBase::RemoteStateData::remoteWrappedState
RemoteStateWrapperPtr remoteWrappedState
Pointer to a Pseudo parent state, that contains the real state instance.
Definition: RemoteStateOfferer.h:141
armarx::ManagedIceObject::terminate
void terminate()
Initiates termination of this IceManagedObject.
Definition: ManagedIceObject.cpp:420
armarx::StatechartContext::stateReporter
Profiler::ProfilerPtr stateReporter
Definition: StatechartContext.h:191
armarx::RemoteStateOffererBase::componentName
std::string componentName
Definition: RemoteStateOfferer.h:162
ArmarXObjectScheduler.h
armarx::RemoteStateOffererBase::hasSubstatesRemote
bool hasSubstatesRemote(const std::string &stateName, const ::Ice::Current &context=Ice::emptyCurrent) const override
Definition: RemoteStateOfferer.cpp:257
armarx::RemoteStateOffererBase::getGlobalInstancePtr
StateBasePtr getGlobalInstancePtr(int globalId) const
Definition: RemoteStateOfferer.cpp:491
armarx::State
Definition: State.h:54
Component.h
armarx::RemoteStateOffererBase::updateGlobalStateIdRecursive
void updateGlobalStateIdRecursive(int stateId, const std::string &parentId, const Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:158
armarx::RemoteStateOffererBase::RemoteStateData::callerIceName
std::string callerIceName
Not used yet.
Definition: RemoteStateOfferer.h:137
armarx::RemoteStateOffererBase::getRemoteInputParameters
StateParameterMap getRemoteInputParameters(const std::string &stateName, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:234
armarx::RemoteStateOffererBase::RemoteStateData
Definition: RemoteStateOfferer.h:132
StatechartManager.h
armarx::RemoteStateOffererBase::onExitRemoteStateOfferer
virtual void onExitRemoteStateOfferer()
Virtual function, in which the user can implement some clean up.
Definition: RemoteStateOfferer.cpp:12
armarx::RemoteStateOffererBase::stateInstanceList
std::map< int, RemoteStateData > stateInstanceList
Holds the instances that where requested from remotely located states.
Definition: RemoteStateOfferer.h:161
armarx::RemoteStateOffererBase::callRemoteState
void callRemoteState(int stateId, const StringVariantContainerBaseMap &properties, const Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:170
armarx::RemoteStateOffererBase::getStatechartInstanceByGlobalIdStr
StateIceBasePtr getStatechartInstanceByGlobalIdStr(const std::string &globalStateIdStr, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:314
armarx::RemoteStateOffererBase::getRemoteOutputParameters
StateParameterMap getRemoteOutputParameters(const std::string &stateName, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:240
armarx::RemoteStateOffererBase::onExitStatechartImpl
void onExitStatechartImpl()
Definition: RemoteStateOfferer.cpp:603
armarx::RemoteStateOffererBase::getStatechartInstanceByGlobalIdStrRecursive
StateIceBasePtr getStatechartInstanceByGlobalIdStrRecursive(const std::string &globalStateIdStr, StateBasePtr state, int &stateCounter)
Definition: RemoteStateOfferer.cpp:569
armarx::RemoteStateOffererBase::issueEventWithGlobalIdStr
void issueEventWithGlobalIdStr(const std::string &globalStateIdStr, const EventBasePtr &evt, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:445
armarx::RemoteStateOffererBase::onInitRemoteStateOfferer
virtual void onInitRemoteStateOfferer()=0
Pure virtual function, in which the states must be added, that should be remote-accessable.
armarx::RemoteStateOffererBase::isHostOfStateByGlobalIdStr
bool isHostOfStateByGlobalIdStr(const std::string &globalStateIdStr, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:361
armarx::RemoteStateOfferer::onInitStatechart
void onInitStatechart() override
onInitStatechart this method is called when the statechart is started.
Definition: RemoteStateOfferer.h:208
armarx::RemoteStateOffererBase::addRemoteState
RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName) override
Overridden so that the user cannot use it.
Definition: RemoteStateOfferer.cpp:458
armarx::RemoteStateOfferer::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: RemoteStateOfferer.h:191
armarx::RemoteStateOfferer::waitUntilComponentStarted
void waitUntilComponentStarted() override
Definition: RemoteStateOfferer.h:256
armarx::RemoteStateOffererBase::getStateOffererName
virtual std::string getStateOffererName() const =0
Implement this function to specify the RemoteStateOfferer prefix.
armarx::RemoteStateOffererBase::getInstance
RemoteStateData getInstance(int stateId)
Definition: RemoteStateOfferer.cpp:468
armarx::RemoteStateOffererBase::stateInstanceListMutex
HiddenTimedMutex stateInstanceListMutex
Definition: RemoteStateOfferer.h:159
armarx::RemoteStateOffererBase::getStatechartInstance
StateIceBasePtr getStatechartInstance(int stateId, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:307
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RemoteStateOffererBase::exitRemoteState
void exitRemoteState(int stateId, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:182
armarx::joinStrings
std::string joinStrings(std::vector< std::string > const &input, std::string const &seperator)
Definition: State.cpp:44