CARMA C++
RemoteObjHandleT.h
Go to the documentation of this file.
1 
9 #ifndef CARMA_REMOTE_OBJ_HANDLE_T_H
10 #define CARMA_REMOTE_OBJ_HANDLE_T_H
11 
12 #include <string>
13 
14 #include "carma/corba/corba.h"
15 #include "carma/corba/Client.h"
16 #include "carma/util/Program.h"
17 #include "carma/util/Trace.h"
18 
19 
20 namespace carma {
21 
22 namespace monitor {
23 
24 class MonitorPointBool;
25 class MonitorSubsystem;
26 class MonitorSystem;
27 
28 } // namespace carma::monitor
29 
30 
31 namespace control {
32 
33 
34 template < typename T >
35 class RemoteObjMethodFunctorBase;
36 
37 
40  public:
64  const ::std::string & doName,
65  monitor::MonitorPointBool * mpReachable,
66  const monitor::MonitorSubsystem * subsystem,
67  monitor::MonitorSystem * system,
68  bool defaultLogIfNotReachable,
69  bool defaultLogSentCommands );
70 
72  virtual ~RemoteObjHandleBase( );
73 
75  ::std::string doName( ) const;
76 
78  void forceFullReconnect( );
79 
89 
94  bool isObjReachable( );
95  bool isObjReachable( bool logIfNotReachable );
96 
97  protected:
98  bool getDefaultLogIfNotReachable( ) const;
99 
100  bool getDefaultLogSentCommands( ) const;
101 
102  void markObjRefValid( );
103  void invalidateObjRef( );
104 
105  void throwIfObjRefIsNotValid( ) const;
106 
107  virtual bool resolveObjRef( ) = 0;
108 
109  void throwBadRemoteObjAccess( ) const;
110 
111  void logException( const ::std::string & callString,
112  const ::std::string & exString ) const;
113 
114  void logSentCommand( const ::std::string & callString,
115  const double mjd ) const;
116  void logSentCommand( const ::std::string & callString,
117  const double mjd,
118  const ::std::string & subDoName ) const;
119 
120  void logSentCommandIfNeeded( const ::std::string & callString,
121  const double mjd ) const;
122  void logSentCommandIfNeeded( const ::std::string & callString,
123  const double mjd,
124  const ::std::string & subDoName ) const;
125 
126  void processException( const ::std::string & callString,
127  const ::CORBA::Exception & corbaException ) ;
128 
129  void invalidateObjRefIfNeededForCaught( );
130 
131  private:
132  void setReachableMpValue( bool value );
133 
134  const ::std::string doName_;
135 
136  const bool defaultLogIfNotReachable_;
137  const bool defaultLogSentCommands_;
138 
139  monitor::MonitorPointBool * const mpReachable_;
140 
141  const monitor::MonitorSubsystem * const monitorSubsystem_;
142  monitor::MonitorSystem * const monitorSystem_;
143 
144  typedef enum {
145  OBJ_REF_VALIDITY_NEVER_SET,
146  OBJ_REF_VALIDITY_BAD,
147  OBJ_REF_VALIDITY_GOOD
148  } ObjRefValidity;
149 
150  ObjRefValidity objRefValidity_;
151 };
152 
153 
155 template < typename T >
157  friend class RemoteObjMethodFunctorBase< T >;
158 
159  public:
173  explicit RemoteObjHandleT(
174  const ::std::string & doName,
175  monitor::MonitorPointBool * mpReachable,
176  const monitor::MonitorSubsystem * subsystem,
177  monitor::MonitorSystem * system,
178  bool defaultLogIfNotReachable,
179  bool defaultLogSentCommands );
180 
184  virtual ~RemoteObjHandleT( );
185 
186 
192  typename T::_var_type remoteObj( ) const;
193 
199  template < typename S >
200  typename S::_var_type
202  {
203  return S::_narrow( objRef_ );
204  }
205 
206  protected:
207  virtual bool resolveObjRef( );
208 
209  private:
210  typename T::_var_type objRef_;
211 };
212 
213 
214 } // namespace carma::control
215 } // namespace carma
216 
217 
218 inline ::std::string
220 {
221  return doName_;
222 }
223 
224 
225 inline bool
226 carma::control::RemoteObjHandleBase::getDefaultLogIfNotReachable( ) const
227 {
228  return defaultLogIfNotReachable_;
229 }
230 
231 
232 inline bool
233 carma::control::RemoteObjHandleBase::getDefaultLogSentCommands( ) const
234 {
235  return defaultLogSentCommands_;
236 }
237 
238 /*
239 inline carma::monitor::MonitorSystem const *
240 carma::control::RemoteObjHandleBase::getMonitorSystem( ) const
241 {
242  return monitorSystem_;
243 }
244 */
245 
246 
247 template < typename T >
249  const ::std::string & doName,
250  monitor::MonitorPointBool * const mpReachable,
251  const monitor::MonitorSubsystem * const subsystem,
252  monitor::MonitorSystem * const system,
253  const bool defaultLogIfNotReachable,
254  const bool defaultLogSentCommands ) :
255 RemoteObjHandleBase( doName,
256  mpReachable,
257  subsystem,
258  system,
259  defaultLogIfNotReachable,
260  defaultLogSentCommands ),
261 objRef_()
262 {
263 }
264 
265 
266 template < typename T >
268 try {
269 } catch ( ... ) {
270  // just stifle any exception
271 
272  return;
273 }
274 
275 
276 template < typename T >
277 typename T::_var_type
279 {
280  throwIfObjRefIsNotValid();
281 
282  if ( CORBA::is_nil( objRef_ ) )
283  throwBadRemoteObjAccess();
284 
285  return objRef_;
286 }
287 
288 
289 // Can retry many times before giving up; only logging the final failure.
290 // But now setup to try only once.
291 // There is no sleep in the loop.
292 // Crude, but gets around some of the startup sync issues for now.
293 template < typename T >
294 bool
296 try {
297  bool done = false;
298  bool validatedObjRef = false;
299 
300  const int maxAttempts = 1;
301  int attemptNo = 0;
302 
303  CPTRACE( util::Trace::TRACE1, " resolveObjRef(), doName()=" << doName() );
304 
305  while ( (done == false) && (attemptNo < maxAttempts) ) {
306  ++attemptNo;
307 
308  try {
309  const typename T::_var_type newObjRef =
310  carma::util::Program::getProgram().getCorbaClient().resolveName< T >( doName() );
311 
312  if ( CORBA::is_nil( newObjRef ) == false ) {
313  done = true;
314 
315  objRef_ = newObjRef;
316 
317  if ( CORBA::is_nil( objRef_ ) == false ) {
318  validatedObjRef = true;
319  markObjRefValid();
320  }
321  }
322  } catch ( ... ) {
323  CARMA_CPTRACE( util::Trace::TRACE1,
324  "Resolve attempt #" << attemptNo << " failed" );
325 
326  // Just stifle any exception
327  }
328  } // End while loop
329 
330  if ( validatedObjRef == false )
331  invalidateObjRef();
332 
333  return validatedObjRef;
334 } catch ( ... ) {
335  invalidateObjRef();
336 
337  throw;
338 }
339 
340 
341 #endif
#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
virtual ~RemoteObjHandleT()
Destructor.
void forceFullReconnect()
Force a full re-lookup of the DO by name.
virtual ~RemoteObjHandleBase()
Destructor.
bool isObjReachable()
If state is &#39;not reachable&#39; and monitor system is current, tries to reconnect.
static Program & getProgram()
Class static method to get the process-wide singleton instance of Program.
Common base class for managing connections to DOs.
T::_var_type resolveName(const ::std::string &name, bool initialRef=false)
Resolve specified name to a CORBA object of the templatized output type.
RemoteObjHandleT(const ::std::string &doName, monitor::MonitorPointBool *mpReachable, const monitor::MonitorSubsystem *subsystem, monitor::MonitorSystem *system, bool defaultLogIfNotReachable, bool defaultLogSentCommands)
Constructor.
#define CPTRACE(priorityLevel, message)
Old deprecated macro name for CARMA_CPTRACE.
Definition: Trace.h:78
RemoteObjHandleBase(const ::std::string &doName, monitor::MonitorPointBool *mpReachable, const monitor::MonitorSubsystem *subsystem, monitor::MonitorSystem *system, bool defaultLogIfNotReachable, bool defaultLogSentCommands)
Constructor.
Interface file for the carma::util::Trace class and related macros.
bool attemptToReconnectIfNeeded()
Try reconnect to the DO if needed.
S::_var_type narrowedRemoteObj() const
Returns remote object handle as var.
T::_var_type remoteObj() const
Returns remote object handle as var.
Typed class for managing connections to DOs.
Monitor system base class.
Definition: MonitorSystem.h:81
::std::string doName() const
get the DO name
Abstract MonitorSubsystem base class.