ManagedIceObjectDependency.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::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 #pragma once
26 
27 // ArmarXCore
30 
31 // interface
32 #include <Ice/Communicator.h>
33 
34 #include <ArmarXCore/interface/core/ManagedIceObjectDependencyBase.h>
35 
36 // stl
37 #include <string>
38 
39 namespace armarx
40 {
41  /**
42  * ManagedIceObjectDependency shared pointer for convenience
43  */
44  class ManagedIceObjectDependency;
46 
47  /**
48  * Map of dependency names and dependecies.
49  */
50  using DependencyList = std::map<std::string, ManagedIceObjectDependencyPtr>;
51 
52  /**
53  * @class ManagedIceObjectDependency
54  * @brief The ManagedIceObjectDependency class is part of the ManagedIceObjectConnectivity.
55  * @ingroup DistributedProcessingSub
56  *
57  * For each dependency on the distributed application, a ManagedIceObjectDependency is added.
58  * The dependencies are resolved by the ArmarXObjectScheduler.
59  *
60  * The checkDependency method needs to be implemented in each dependency.
61  */
63  public ManagedIceObjectDependencyBase
64  {
65  public:
66  /**
67  * Required by factory functions
68  */
70  {
71  }
72 
73  /**
74  * Constructs a ManagedIceObjectDependency
75  *
76  * @param iceManager pointer to the ice manager
77  * @param name name of dependant object
78  * @param type type of dependency
79  */
80  ManagedIceObjectDependency(IceManagerPtr iceManager, std::string name, std::string type)
81  {
82  this->name = name;
83  this->type = type;
84  this->iceManager = iceManager;
85 
86  resolved = false;
87  stateChanged = true;
88  }
89 
90  /**
91  * Retrieve name of dependency. Part of the Ice interface.
92  *
93  * @param c Ice context
94  * @return name of dependant object
95  */
96  std::string
97  getName(const Ice::Current& c = Ice::emptyCurrent) override
98  {
99  return name;
100  }
101 
102  /**
103  * Retrieve type of dependency. Part of the Ice interface.
104  *
105  * @param c Ice context
106  * @return type of the dependency
107  */
108  std::string
109  getType(const Ice::Current& c = Ice::emptyCurrent) override
110  {
111  return type;
112  }
113 
114  /**
115  * Retrieve whether dependency is resolved. Part of the Ice interface.
116  *
117  * @param c Ice context
118  * @return dependency resolved
119  */
120  bool
121  getResolved(const Ice::Current& c = Ice::emptyCurrent) override
122  {
123  return resolved;
124  }
125 
126  /**
127  * Retrieve whether state has changed since the last call to the method.
128  *
129  * @return state change condition
130  */
131  bool
133  {
134  bool result = stateChanged;
135  stateChanged = false;
136  return result;
137  }
138 
139  /**
140  * This method is called to check the dependency and update the resolved and statechanged members.
141  * Internally calls checkDependency of subclass.
142  */
143  void
145  {
146  bool oldResolved = resolved;
147  resolved = checkDependency();
148 
149  if (resolved != oldResolved)
150  {
151  stateChanged = true;
152  }
153  };
154 
155  protected:
156  // implement in subclass
157  virtual bool checkDependency() = 0;
158 
159  // pointer to iceManager
161 
162  private:
163  // state changed
164  bool stateChanged;
165  };
166 
167  /**
168  * @class ProxyDependency
169  * @brief The ProxyDependency class is part of the ManagedIceObjectConnectivity.
170  * @ingroup DistributedProcessingSub
171  *
172  * For each proxy used by a ManagedIceObject by calling ManagedIceObject::usingProxy, a
173  * ProxyDependency is inserted in the ManagedIceObjectConnectivity.
174  *
175  * The dependency check is realized by pinging the well-known object.
176  */
178  {
179  public:
180  /**
181  * Required by the ProxyDependencyFactory
182  */
184  {
185  }
186 
187  /**
188  * Constructs a ProxyDependency
189  *
190  * @param iceManager pointer to the ice manager
191  * @param proxyName name of dependant proxy
192  */
193  ProxyDependency(IceManagerPtr iceManager, std::string proxyName) :
194  ManagedIceObjectDependency(iceManager, proxyName, "Proxy")
195  {
196  // create proxy
197  proxy = iceManager->getCommunicator()->stringToProxy(getName());
198  }
199 
200  /**
201  * Implementation of the dependency check
202  *
203  * @return whether dependency is resolved
204  */
205  bool
206  checkDependency() override
207  {
208  // try to ping
209  try
210  {
211  // object needs to be pingable
212  proxy->ice_timeout(1000)->ice_ping();
213  return true;
214  }
215  catch (...)
216  {
217  return false;
218  }
219  }
220 
221  private:
222  // proxy of the dependency
223  Ice::ObjectPrx proxy;
224  };
225 } // namespace armarx
armarx::DependencyList
std::map< std::string, ManagedIceObjectDependencyPtr > DependencyList
Map of dependency names and dependecies.
Definition: ManagedIceObjectDependency.h:50
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::ManagedIceObjectDependency::getType
std::string getType(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve type of dependency.
Definition: ManagedIceObjectDependency.h:109
armarx::ProxyDependency::ProxyDependency
ProxyDependency()
Required by the ProxyDependencyFactory.
Definition: ManagedIceObjectDependency.h:183
armarx::ManagedIceObjectDependency::getStateChanged
bool getStateChanged()
Retrieve whether state has changed since the last call to the method.
Definition: ManagedIceObjectDependency.h:132
armarx::ManagedIceObjectDependency::getName
std::string getName(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve name of dependency.
Definition: ManagedIceObjectDependency.h:97
armarx::ProxyDependency::checkDependency
bool checkDependency() override
Implementation of the dependency check.
Definition: ManagedIceObjectDependency.h:206
armarx::ProxyDependency
The ProxyDependency class is part of the ManagedIceObjectConnectivity.
Definition: ManagedIceObjectDependency.h:177
IceManager.h
armarx::ManagedIceObjectDependency::check
void check()
This method is called to check the dependency and update the resolved and statechanged members.
Definition: ManagedIceObjectDependency.h:144
armarx::ManagedIceObjectDependency::iceManager
IceManagerPtr iceManager
Definition: ManagedIceObjectDependency.h:160
armarx::ManagedIceObjectDependency::getResolved
bool getResolved(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve whether dependency is resolved.
Definition: ManagedIceObjectDependency.h:121
armarx::ManagedIceObjectDependency
The ManagedIceObjectDependency class is part of the ManagedIceObjectConnectivity.
Definition: ManagedIceObjectDependency.h:62
armarx::ManagedIceObjectDependency::ManagedIceObjectDependency
ManagedIceObjectDependency(IceManagerPtr iceManager, std::string name, std::string type)
Constructs a ManagedIceObjectDependency.
Definition: ManagedIceObjectDependency.h:80
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::ManagedIceObjectDependency::ManagedIceObjectDependency
ManagedIceObjectDependency()
Required by factory functions.
Definition: ManagedIceObjectDependency.h:69
ImportExport.h
armarx::ProxyDependency::ProxyDependency
ProxyDependency(IceManagerPtr iceManager, std::string proxyName)
Constructs a ProxyDependency.
Definition: ManagedIceObjectDependency.h:193
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27