RemoteStateWrapper.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::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 #include "RemoteStateWrapper.h"
25 
26 #include "RemoteStateOfferer.h"
27 
28 namespace armarx
29 {
30  ////////////////////////////////////////////////////////////////////////////////
31  //////////// RemoteStateWrapper
32  ////////////////////////////////////////////////////////////////////////////////
33 
35  RemoteStateIceBasePrx const& callerStatePrx) :
36  callerStatePrx(callerStatePrx), realState(realState)
37  {
38  stateName = "remoteStateWrapper_of_" + realState->stateName;
39  activeSubstate = realState;
40 
41  if (callerStatePrx)
42  {
43  ARMARX_IMPORTANT << "Calling callerStatePrx->getParentStateLayout() in " << stateName;
44  StateIceBasePtr remoteParentStateLayout;
45  try
46  {
47  remoteParentStateLayout = callerStatePrx->getParentStateLayout();
48  }
49  catch (Ice::ConnectFailedException const& e)
50  {
51  ARMARX_WARNING << "Ice connect failed, trying again in a little bit";
52  ::usleep(1000000);
53  remoteParentStateLayout = callerStatePrx->getParentStateLayout();
54  }
55 
56  if (remoteParentStateLayout)
57  {
58  this->subStateList = remoteParentStateLayout->subStateList;
59  this->transitions = remoteParentStateLayout->transitions;
60  this->initState = remoteParentStateLayout->initState;
61  }
62  }
63  }
64 
66  RemoteStateIceBasePrx const& callerStatePrx,
67  StateIceBasePtr const& remoteParentStateLayout) :
68  callerStatePrx(callerStatePrx), realState(realState)
69  {
70  stateName = "remoteStateWrapper_of_" + realState->stateName;
71  activeSubstate = realState;
72 
73  if (remoteParentStateLayout)
74  {
75  this->subStateList = remoteParentStateLayout->subStateList;
76  this->transitions = remoteParentStateLayout->transitions;
77  this->initState = remoteParentStateLayout->initState;
78  }
79  }
80 
82  Shared(),
83  Ice::Object(source),
84  StateIceBase(source),
85  Logging(source),
89  State(source)
90 
91  {
92  realState = StatePtr::dynamicCast(source.realState->clone());
93 
94  if (source.realState && !realState)
95  {
97  "Could not cast from StateBasePtr to StatePtr");
98  }
99 
100  callerStatePrx = source.callerStatePrx;
101  }
102 
104  {
105  subStateList
106  .clear(); // clear this list here, so that RemoteStates are not removed in ~StateBase(). This sublist is just for information purposes
107  }
108 
109  void
111  {
112  ARMARX_DEBUG << "calling remoteprocessbufferedevets"
113  << "" << flush;
114 
115  //call remoteProcessBufferedEvents over ice
116  if (callerStatePrx._ptr)
117  {
118  callerStatePrx->begin_remoteProcessBufferedEvents();
119  }
120  }
121 
122  void
124  {
125  // call substatesFinished over Ice
126  if (callerStatePrx)
127  {
128  callerStatePrx->remoteProcessEvent(ev, buffered);
129  }
130  else
131  {
132  ARMARX_INFO << "No remote state proxy was set - will not report back to remote state. "
133  "In case this is a toplevel state this is not an issue (Event: "
134  << ev->eventName << ")";
135  realState->_baseOnExit();
136  }
137  }
138 
139  void
141  {
142  if (callerStatePrx._ptr)
143  {
144  callerStatePrx->begin_remoteEnqueueEvent(event);
145  }
146  }
147 
148  unsigned int
150  {
151 
152  if (callerStatePrx._ptr)
153  {
154  return callerStatePrx->getRemoteUnbreakableBufferSize();
155  }
156  else
157  {
158  return 0;
159  }
160  }
161 
162  bool
164  {
165  if (callerStatePrx._ptr)
166  {
167  return callerStatePrx->getRemoteUnbreakableBufferStati();
168  }
169  else
170  {
171  return true;
172  }
173  }
174 
175  void
177  {
178  //realState->baseOnExit();
179  ARMARX_INFO << "sending remote finalize " << realState._ptr << "";
180 
181  if (callerStatePrx._ptr)
182  {
183  callerStatePrx->begin_remoteFinalize(
184  StateUtilFunctions::getSetValues(realState->getOutputParameters()), event);
185  }
186  }
187 
190  {
191  StateBasePtr result = new RemoteStateWrapper(*this);
192  return result;
193  }
194 
197  {
198  assert(0);
199  return new RemoteStateWrapper(nullptr, callerStatePrx);
200  }
201 } // namespace armarx
armarx::RemoteStateWrapper::clone
StateBasePtr clone() const override
Pure virtual function to clone of the derived class type.
Definition: RemoteStateWrapper.cpp:189
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::RemoteStateWrapper
Definition: RemoteStateWrapper.h:50
armarx::StateController
Definition: StateController.h:55
RemoteStateWrapper.h
armarx::RemoteStateWrapper::__getUnbreakableBufferSize
unsigned int __getUnbreakableBufferSize() const override
Definition: RemoteStateWrapper.cpp:149
armarx::exceptions::local::eNullPointerException
Definition: Exception.h:49
armarx::RemoteStateWrapper::__processBufferedEvents
void __processBufferedEvents() override
Processes buffered events, that could not be processed immediately due to unbreakable substates.
Definition: RemoteStateWrapper.cpp:110
IceInternal::Handle< State >
armarx::RemoteStateWrapper::callerStatePrx
RemoteStateIceBasePrx callerStatePrx
Definition: RemoteStateWrapper.h:52
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::RemoteStateWrapper::~RemoteStateWrapper
~RemoteStateWrapper() override
Definition: RemoteStateWrapper.cpp:103
armarx::RemoteStateWrapper::__getUnbreakableBufferStati
bool __getUnbreakableBufferStati() const override
Before:Function to get the unbreakable-buffer status of all parent state - recursively.
Definition: RemoteStateWrapper.cpp:163
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::State
Definition: State.h:53
armarx::RemoteStateWrapper::__processEvent
void __processEvent(const EventPtr event, bool buffered=false) override
Main function to control the statemachine/state.
Definition: RemoteStateWrapper.cpp:123
armarx::StateBase
Definition: StateBase.h:59
armarx::RemoteStateWrapper::realState
StatePtr realState
Definition: RemoteStateWrapper.h:53
armarx::StateUtility
Definition: StateUtil.h:46
Ice
Definition: DBTypes.cpp:63
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::RemoteStateWrapper::__finalize
void __finalize(const EventPtr event) override
Function that gets called, when a state enters a FinalSubstate. Virtual function, so that RemoteState...
Definition: RemoteStateWrapper.cpp:176
armarx::StateUtilFunctions::getSetValues
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:56
armarx::RemoteStateWrapper::createEmptyCopy
StateBasePtr createEmptyCopy() const override
Definition: RemoteStateWrapper.cpp:196
RemoteStateOfferer.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::RemoteStateWrapper::__enqueueEvent
void __enqueueEvent(const EventPtr event) override
Definition: RemoteStateWrapper.cpp:140
armarx::RemoteStateWrapper::RemoteStateWrapper
RemoteStateWrapper(StatePtr const &realState, RemoteStateIceBasePrx const &callerStatePrx)
Definition: RemoteStateWrapper.cpp:34