1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
#ifndef _RpcRetry_h
#define _RpcRetry_h
#include <vector>
#include <bstring.h>
/**
* A simple helper class to control how many times an rpc related service
* is attempted and how long to sleep between failed attempts.
*/
class RpcRetry
{
public:
/**
* The base class for rpc retry functors.
*/
class Functor
{
public:
virtual bool operator()() = 0;
};
/**
* A class to encapsulate errors.
*/
class Error
{
public:
/**
* Constructor.
*/
Error():m_errno(0), m_reason(0)
{
}
/**
* Set the errno (this will be specific to the underlying rpc call).
*
* @param theErrno the errno.
*/
virtual void setErrno(int theErrno)
{
m_errno = theErrno;
}
/**
* Get the errno.
*
* @return the errno.
*/
virtual int getErrno() const
{
return (m_errno);
}
/**
* Set the reason (this will be specific to the underlying rpc call).
*
* @param theReason the reason.
*/
virtual void setReason(int theReason)
{
m_reason = theReason;
}
/**
* Get the reason.
*
* @return the reason.
*/
virtual int getReason() const
{
return (m_reason);
}
/**
* Set the error detail string.
*
* @param theDetail the error detail string.
*/
virtual void setDetail(char const *theMessage, ...);
/**
* Get the error detail string.
*
* @return the error detail string.
*/
char const *getDetail() const
{
return (m_detail.c_str());
}
private:
int m_errno;
int m_reason;
string m_detail;
};
/**
* Default constructor.
*/
RpcRetry() {
m_numAttempts = getDefaultNumAttempts();
m_sleep = getDefaultSleep();
}
/**
* Sets the number of tries.
*
* @param numAttempts the number of attempts.
*/
void setNumAttempts(int numAttempts)
{
m_numAttempts = numAttempts;
}
/**
* Gets the number of attempts.
*
* @return the default number of attempts.
*/
int getNumAttempts() const
{
return (m_numAttempts);
}
/**
* Set the between attempt sleep value.
*
* @param numSeconds the number of seconds to sleep (0 for no sleep).
*/
void setSleep(unsigned int numSeconds)
{
m_sleep = numSeconds;
}
/**
* Get the between attempt sleep value.
*
* @return the number of seconds to sleep between attempts.
*/
unsigned int getSleep() const
{
return (m_sleep);
}
/**
* Access the rpc error.
*
* @return the rpc error.
*/
Error & getRpcError()
{
return (m_error);
}
/**
* This will call the functor provided until either the operation
* completes okay (return from functor is true) or the number of
* attempts is exhausted.
*
* @param theFunctor the functor to invoke.
* @return true if functor succeeded, false otherwise.
*/
bool doRpc(Functor & theFunctor);
/**
* Sets the default number of tries.
*
* @param numAttempts the number of attempts for default.
*/
static void setDefaultNumAttempts(int numAttempts)
{
m_defaultNumAttempts = numAttempts;
}
/**
* Gets the default number of attempts.
*
* @return the default number of attempts.
*/
static int getDefaultNumAttempts()
{
return (m_defaultNumAttempts);
}
/**
* Set the default between attempt sleep value.
*
* @param numSeconds the number of seconds to sleep (0 for no sleep).
*/
static void setDefaultSleep(unsigned int numSeconds)
{
m_defaultSleep = numSeconds;
}
/**
* Get the default between attempt sleep value.
*
* @return the number of seconds to sleep between attempts.
*/
static unsigned int getDefaultSleep()
{
return (m_defaultSleep);
}
private:
static int m_defaultNumAttempts; // default number of attempts
static unsigned int m_defaultSleep; // default between attempt sleep
int m_numAttempts; // instance number of attempts
unsigned int m_sleep; // instance between attempt sleep
Error m_error; // a placeholder for reporting errors.
};
/**
* A no argument rpc retry functor.
*/
template < class O > class TRpcRetryFunctor0:public RpcRetry::Functor {
public:
TRpcRetryFunctor0(O & theO, bool(O::*theOMethod) ()):
m_o(&theO), m_f(theOMethod) {
}
virtual bool
operator() ()
{
return ((*m_o.*m_f) ());
}
private:
O * m_o; // the object
bool(O::*m_f) (); // the method
};
/**
* A single argument rpc retry functor.
*/
template < class O, class T1 > class TRpcRetryFunctor1:public RpcRetry::Functor {
public:
TRpcRetryFunctor1(O & theO, bool(O::*theOMethod) (T1), T1 theT1):
m_o(&theO), m_f(theOMethod), m_t1(theT1) {
}
virtual bool
operator() ()
{
return ((*m_o.*m_f) (m_t1));
}
private:
O * m_o; // the object
bool(O::*m_f) (T1); // the method
T1 m_t1; // the only argument
};
/**
* A two argument rpc retry functor.
*/
template < class O, class T1, class T2 > class TRpcRetryFunctor2:public RpcRetry::Functor {
public:
TRpcRetryFunctor2(O & theO, bool(O::*theOMethod) (T1, T2), T1 theT1, T2 theT2):
m_o(&theO), m_f(theOMethod), m_t1(theT1), m_t2(theT2) {
}
virtual bool
operator() ()
{
return ((*m_o.*m_f) (m_t1, m_t2));
}
private:
O * m_o; // the object
bool(O::*m_f) (T1, T2); // the method
T1 m_t1; // the first argument
T2 m_t2; // the second argument
};
/**
* A three argument rpc retry functor.
*/
template < class O, class T1, class T2, class T3 > class TRpcRetryFunctor3:public RpcRetry::
Functor {
public:
TRpcRetryFunctor3(O & theO, bool(O::*theOMethod) (T1, T2, T3), T1 theT1, T2 theT2, T3 theT3):
m_o(&theO), m_f(theOMethod), m_t1(theT1), m_t2(theT2), m_t3(theT3) {
}
virtual bool
operator() ()
{
return ((*m_o.*m_f) (m_t1, m_t2, m_t3));
}
private:
O * m_o; // the object
bool(O::*m_f) (T1, T2, T3); // the method
T1 m_t1; // the first argument
T2 m_t2; // the second argument
T3 m_t3; // the third argument
};
/**
* A four argument rpc retry functor.
*/
template < class O, class T1, class T2, class T3, class T4 > class TRpcRetryFunctor4:public RpcRetry::
Functor
{
public:
TRpcRetryFunctor4(O & theO, bool(O::*theOMethod) (T1, T2, T3, T4), T1 theT1, T2 theT2, T3 theT3, T4 theT4):
m_o(&theO), m_f(theOMethod), m_t1(theT1), m_t2(theT2), m_t3(theT3), m_t4(theT4) {
}
virtual bool
operator() ()
{
return ((*m_o.*m_f) (m_t1, m_t2, m_t3, m_t4));
}
private:
O * m_o; // the object
bool(O::*m_f) (T1, T2, T3, T4); // the method
T1 m_t1; // the first argument
T2 m_t2; // the second argument
T3 m_t3; // the third argument
T4 m_t4; // the fourth argument
};
#endif
|