CARMA C++
Orb.h
Go to the documentation of this file.
1 // $Id: Orb.h,v 1.3 2013/01/08 05:07:32 abeard Exp $
2 
3 #ifndef CARMA_UTIL_ORB_H
4 #define CARMA_UTIL_ORB_H
5 
15 #include "carma/corba/corba.h"
16 #include <orbsvcs/CosNamingC.h>
17 #include <tao/PortableServer/PortableServer.h>
18 
19 #include <tao/TimeBaseC.h>
20 #include <orbsvcs/CosNotifyFilterC.h>
21 #include <orbsvcs/NotifyExtC.h>
22 
24 #include "carma/util/Program.h"
25 #include "carma/util/Trace.h"
26 
27 #include <string>
28 #include <vector>
29 
30 namespace carma {
31  namespace util {
32 
33  class Orb {
34  public:
35 
36  typedef enum {
37  FAIL_ON_NIL_INPUT,
38  NIL_INPUT_OKAY
39  } NilInputDisposition;
40 
41  typedef enum {
42  FAIL_ON_INCOMPATIBLE_TYPES,
43  INCOMPATIBLE_TYPES_OKAY
44  } IncompatibleTypesDisposition;
45 
49  Orb();
50 
54  virtual ~Orb();
55 
56  // Set the ID of this ORB
57 
58  bool set(int argc, char** argv, const std::string& imrhost);
59  bool isNull();
60  bool shutdown(bool waitForCompletion);
61 
62  friend bool ProgramBase::orbInit(Orb* orb);
63 
64  CORBA::ORB_var duplicateOrb();
65  CORBA::ORB_var getORB();
66 
67  void setName(std::string name);
68  std::string getName();
69 
70  void setImrName(std::string imrName);
71  std::string getImrName();
72 
73  void allowRegistrationWithImr(bool allow);
74 
75  public:
76 
77  // A unique name for this orb
78 
79  std::string name_;
80  bool hasUniqueName_;
81 
82  // The IMR to connect to
83 
84  std::string imrName_;
85  bool hasUniqueImrName_;
86 
87  bool allowRegistrationWithImr_;
88 
89  CORBA::ORB_var orb_;
90  std::vector<std::string> lArgvVec_;
91 
92  ::pthread_mutex_t notifyChannelGuard_;
93  ::pthread_mutex_t proxyPushSupplierMapGuard_;
94  ::pthread_mutex_t proxyConsumerMapGuard_;
95 
96  typedef std::map<std::string, CosNotifyChannelAdmin::StructuredProxyPushSupplier_var> ProxyPushSupplierMap;
97  ProxyPushSupplierMap proxyPushSuppliers_;
98 
99  typedef std::map<std::string, CosNotifyChannelAdmin::StructuredProxyPushConsumer_var > ProxyConsumerMap;
100  ProxyConsumerMap proxyConsumers_;
101 
102  void throwIfNull();
103  bool destroy();
104  std::string getPersistentPOAName(const std::string& poaName);
105  PortableServer::POA_var getRootPOA();
106  PortableServer::POA_var getPOA(const std::string& poaNameString = "RootPOA");
107  PortableServer::POAManager_var getPOAManager(const std::string& poaString = "RootPOA");
108 
109  void addObject(const std::string& hierarchicalName, CORBA::Object_ptr objectPtr);
110  void addObject(const std::string& hierarchicalName, const std::string& kind, CORBA::Object_ptr objectPtr);
111  void removeObject(const std::string& hierarchicalName);
112 
113  void run(const std::string& poaString = "RootPOA");
114  void work();
115  bool workPending();
116  void performWork();
117 
118  void activatePOA(const std::string& poaString);
119  void deactivatePOA(bool etherealize, bool wait, const std::string& poaString);
120  void holdPOA(bool wait, const std::string& poaString);
121  void discardPOA(bool wait, const std::string& poaString);
122 
123  CORBA::Object_var resolve(const std::string& hierarchicalName, const std::string& kind);
124  CORBA::Object_var resolveInit(const char * idName );
125  CORBA::Object_var resolveName(const std::string & hierarchicalName, const std::string& kind);
126  CORBA::Object_var waitToResolveName(const std::string& hierarchicalName, const std::string& kind);
127  CORBA::Object_var resolveFromString(const std::string& objectString);
128  CORBA::Object_var resolveFromFile(const std::string& fileName, const std::string& directory);
129 
130  std::vector<std::string> getHierarchyIds(const std::string& hierarchicalName);
131 
132  void unbind(const std::string& hierarchicalName, const std::string& kind = "");
133  void bind(const std::string& hierarchicalName, CORBA::Object_ptr objectPtr);
134  void bind(const std::string& hierarchicalName, const std::string& kind, CORBA::Object_ptr objectPtr);
135  void rebind(const std::string& hierarchicalName, const std::string& kind, CORBA::Object_ptr objectPtr);
136  void rebind(const std::string& hierarchicalName, CORBA::Object_ptr objectPtr);
137 
138  CosNaming::NamingContext_var getRootNamingContext();
139  CosNaming::NamingContext_var getNamingContext(const std::vector<std::string>& ids);
140  CosNaming::NamingContext_var getNamingContext(const std::vector<std::string>& ids, const std::string& kind);
141 
142  //------------------------------------------------------------
143  // Notification methods
144  //------------------------------------------------------------
145 
146  int maxRetries_;
147 
148  CosNotifyChannelAdmin::ConsumerAdmin_var
149  getNotifyConsumerAdmin(const std::string & channelName);
150 
151  CosNotifyChannelAdmin::SupplierAdmin_var
152  getNotifySupplierAdmin(const std::string& channelName);
153 
154  CosNotifyChannelAdmin::EventChannel_var
155  getNotifyChannel(const std::string & channelName);
156 
157  CosNotifyChannelAdmin::StructuredProxyPushSupplier_var
158  getNotifyProxyPushSupplier(const std::string & channelName);
159 
160  CosNotifyChannelAdmin::StructuredProxyPushSupplier_var
161  getNotifyProxyPushSupplier( const std::string & channelName,
162  const std::string & proxyName,
163  CosNotifyFilter::ConstraintExpSeq* constraints = 0);
164 
165  CosNotifyChannelAdmin::StructuredProxyPushConsumer_var
166  getNotifyProxyPushConsumer(const std::string& channelName,
167  const std::string& proxyName);
168 
169  CosNotification::StructuredEvent_var
170  createEventForm( const std::string& typeName,
171  const std::string& eventName,
172  const std::string& domainName = "CARMA");
173 
174  bool sendNotification(const std::string& channelName,
175  const std::string& proxyName,
176  CosNotification::StructuredEvent_var event);
177 
178  //-----------------------------------------------------------------------
179  // Templates
180  //-----------------------------------------------------------------------
181 
182  template<typename T, typename S>
183  typename T::_var_type getNarrowedVar(const S& s,
184  const NilInputDisposition nilInputDisposition= FAIL_ON_NIL_INPUT,
185  const IncompatibleTypesDisposition incompatibleTypesDisposition= FAIL_ON_INCOMPATIBLE_TYPES)
186  {
187  typename T::_var_type tVar;
188 
189  if(CORBA::is_nil(s)) {
190  if(nilInputDisposition == FAIL_ON_NIL_INPUT) {
191  ThrowCarmaError("Input is NIL.");
192  }
193  } else {
194  try {
195  tVar = T::_narrow(s);
196 
197  if((incompatibleTypesDisposition == FAIL_ON_INCOMPATIBLE_TYPES) &&
198  CORBA::is_nil(tVar)) {
199  ThrowCarmaError("Actual type incompatible with target type.");
200  }
201 
202  } catch (const CORBA::SystemException& e) {
203  CARMA_CPTRACE(carma::util::Trace::TRACE3,
204  "Orb::getNarrowedVar< T >( ) failed: "
205  << e);
206  throw; // Rethrow this exception to a higher context
207  } catch (const CORBA::Exception& e) {
208  CARMA_CPTRACE(carma::util::Trace::TRACE3,
209  "Orb::getNarrowedVar< T >( ) failed: "
210  << e);
211  throw; // Rethrow this exception to a higher context
212  } catch (...) {
213  CARMA_CPTRACE(carma::util::Trace::TRACE3,
214  "Orb::getNarrowedVar< T >( ) failed: "
215  << "???");
216  throw; // Rethrow this exception to a higher context
217  }
218  }
219 
220  return tVar._retn(); // Relinquish ownership of tVar
221  }
222 
223  template<typename T>
224  typename T::_var_type resolveInit(const char* idName)
225  {
226  CORBA::Object_var objectVar = resolveInit(idName);
227  typename T::_var_type typedVar = getNarrowedVar<T>(objectVar);
228  return typedVar._retn( ); // Relinquish ownership of typedVar
229  }
230 
231  template<typename T>
232  typename T::_var_type resolveName(const std::string& hierarchicalName, const std::string& kind = "")
233  {
234  CORBA::Object_var objectVar = resolveName(hierarchicalName, kind);
235  typename T::_var_type typedVar = getNarrowedVar<T>(objectVar);
236  return typedVar._retn(); // Relinquish ownership of typedVar
237  }
238 
239  template<typename T>
240  typename T::_var_type waitToResolveName(const std::string& hierarchicalName, const std::string& kind = "")
241  {
242  CORBA::Object_var objectVar = waitToResolveName(hierarchicalName, kind);
243  typename T::_var_type typedVar = getNarrowedVar<T>(objectVar);
244  return typedVar._retn( ); // Relinquish ownership of typedVar
245  }
246 
247  template<typename T>
248  typename T::_var_type resolveFromString(const std::string& objectString)
249  {
250  CORBA::Object_var objectVar = resolveFromString(objectString);
251  typename T::_var_type typedVar = getNarrowedVar<T>(objectVar);
252  return typedVar._retn( ); // Relinquish ownership of typedVar
253  }
254 
255  template<typename T>
256  typename T::_var_type resolveFromFile(const std::string& fileName, const std::string& directory)
257  {
258  CORBA::Object_var objectVar = resolveFromFile(fileName, directory);
259  typename T::_var_type typedVar = getNarrowedVar<T>(objectVar);
260  return typedVar._retn( ); // Relinquish ownership of typedVar
261  }
262 
263 
264  }; // End class Orb
265 
266  } // End namespace util
267 } // End namespace carma
268 
269 
270 
271 #endif // End #ifndef CARMA_UTIL_ORB_H
#define CARMA_CPTRACE(priorityLevel, message)
Macro for people who just want to use the Trace object created for them in carma::util::Program.
Definition: Trace.h:59
bool orbInit(Orb *orb)
Set ORB parameters.
Exception class for errors.
Interface file for the carma::util::Trace class and related macros.