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