asiolink.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __ASIOLINK_H
  15. #define __ASIOLINK_H 1
  16. // IMPORTANT NOTE: only very few ASIO headers files can be included in
  17. // this file. In particular, asio.hpp should never be included here.
  18. // See the description of the namespace below.
  19. #include <unistd.h> // for some network system calls
  20. #include <asio/ip/address.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <boost/function.hpp>
  23. #include <functional>
  24. #include <string>
  25. #include <vector>
  26. #include <utility>
  27. #include <dns/buffer.h>
  28. #include <dns/message.h>
  29. #include <dns/question.h>
  30. #include <exceptions/exceptions.h>
  31. #include <asiolink/ioaddress.h>
  32. #include <asiolink/ioendpoint.h>
  33. #include <asiolink/iomessage.h>
  34. #include <asiolink/iosocket.h>
  35. namespace asio {
  36. // forward declaration for IOService::get_io_service() below
  37. class io_service;
  38. }
  39. /// \namespace asiolink
  40. /// \brief A wrapper interface for the ASIO library.
  41. ///
  42. /// The \c asiolink namespace is used to define a set of wrapper interfaces
  43. /// for the ASIO library.
  44. ///
  45. /// BIND 10 uses the non-Boost version of ASIO because it's header-only,
  46. /// i.e., does not require a separate library object to be linked, and thus
  47. /// lowers the bar for introduction.
  48. ///
  49. /// But the advantage comes with its own costs: since the header-only version
  50. /// includes more definitions in public header files, it tends to trigger
  51. /// more compiler warnings for our own sources, and, depending on the
  52. /// compiler options, may make the build fail.
  53. ///
  54. /// We also found it may be tricky to use ASIO and standard C++ libraries
  55. /// in a single translation unit, i.e., a .cc file: depending on the order
  56. /// of including header files, ASIO may or may not work on some platforms.
  57. ///
  58. /// This wrapper interface is intended to centralize these
  59. /// problematic issues in a single sub module. Other BIND 10 modules should
  60. /// simply include \c asiolink.h and use the wrapper API instead of
  61. /// including ASIO header files and using ASIO-specific classes directly.
  62. ///
  63. /// This wrapper may be used for other IO libraries if and when we want to
  64. /// switch, but generality for that purpose is not the primary goal of
  65. /// this module. The resulting interfaces are thus straightforward mapping
  66. /// to the ASIO counterparts.
  67. ///
  68. /// Notes to developers:
  69. /// Currently the wrapper interface is fairly specific to use by a
  70. /// DNS server, i.e., b10-auth or b10-resolver. But the plan is to
  71. /// generalize it and have other modules use it as well.
  72. ///
  73. /// One obvious drawback of this approach is performance overhead
  74. /// due to the additional layer. We should eventually evaluate the cost
  75. /// of the wrapper abstraction in benchmark tests. Another drawback is
  76. /// that the wrapper interfaces don't provide all features of ASIO
  77. /// (at least for the moment). We should also re-evaluate the
  78. /// maintenance overhead of providing necessary wrappers as we develop
  79. /// more.
  80. ///
  81. /// On the other hand, we may be able to exploit the wrapper approach to
  82. /// simplify the interfaces (by limiting the usage) and unify performance
  83. /// optimization points.
  84. ///
  85. /// As for optimization, we may want to provide a custom allocator for
  86. /// the placeholder of callback handlers:
  87. /// http://think-async.com/Asio/asio-1.3.1/doc/asio/reference/asio_handler_allocate.html
  88. namespace asiolink {
  89. class DNSServiceImpl;
  90. struct IOServiceImpl;
  91. struct IntervalTimerImpl;
  92. /// \brief An exception that is thrown if an error occurs within the IO
  93. /// module. This is mainly intended to be a wrapper exception class for
  94. /// ASIO specific exceptions.
  95. class IOError : public isc::Exception {
  96. public:
  97. IOError(const char* file, size_t line, const char* what) :
  98. isc::Exception(file, line, what) {}
  99. };
  100. /// \brief Forward declarations for classes used below
  101. class SimpleCallback;
  102. class DNSLookup;
  103. class DNSAnswer;
  104. /// \brief The \c IOService class is a wrapper for the ASIO \c io_service
  105. /// class.
  106. ///
  107. class IOService {
  108. ///
  109. /// \name Constructors and Destructor
  110. ///
  111. /// Note: The copy constructor and the assignment operator are
  112. /// intentionally defined as private, making this class non-copyable.
  113. //@{
  114. private:
  115. IOService(const IOService& source);
  116. IOService& operator=(const IOService& source);
  117. public:
  118. /// \brief The constructor
  119. IOService();
  120. /// \brief The destructor.
  121. ~IOService();
  122. //@}
  123. /// \brief Start the underlying event loop.
  124. ///
  125. /// This method does not return control to the caller until
  126. /// the \c stop() method is called via some handler.
  127. void run();
  128. /// \brief Run the underlying event loop for a single event.
  129. ///
  130. /// This method return control to the caller as soon as the
  131. /// first handler has completed. (If no handlers are ready when
  132. /// it is run, it will block until one is.)
  133. void run_one();
  134. /// \brief Stop the underlying event loop.
  135. ///
  136. /// This will return the control to the caller of the \c run() method.
  137. void stop();
  138. /// \brief Return the native \c io_service object used in this wrapper.
  139. ///
  140. /// This is a short term work around to support other BIND 10 modules
  141. /// that share the same \c io_service with the authoritative server.
  142. /// It will eventually be removed once the wrapper interface is
  143. /// generalized.
  144. asio::io_service& get_io_service();
  145. private:
  146. IOServiceImpl* io_impl_;
  147. };
  148. ///
  149. /// DNSService is the service that handles DNS queries and answers with
  150. /// a given IOService. This class is mainly intended to hold all the
  151. /// logic that is shared between the authoritative and the recursive
  152. /// server implementations. As such, it handles asio, including config
  153. /// updates (through the 'Checkinprovider'), and listening sockets.
  154. ///
  155. class DNSService {
  156. ///
  157. /// \name Constructors and Destructor
  158. ///
  159. /// Note: The copy constructor and the assignment operator are
  160. /// intentionally defined as private, making this class non-copyable.
  161. //@{
  162. private:
  163. DNSService(const DNSService& source);
  164. DNSService& operator=(const DNSService& source);
  165. public:
  166. /// \brief The constructor with a specific IP address and port on which
  167. /// the services listen on.
  168. ///
  169. /// \param io_service The IOService to work with
  170. /// \param port the port to listen on
  171. /// \param address the IP address to listen on
  172. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  173. /// \param lookup The lookup provider (see \c DNSLookup)
  174. /// \param answer The answer provider (see \c DNSAnswer)
  175. DNSService(IOService& io_service, const char& port,
  176. const char& address, SimpleCallback* checkin,
  177. DNSLookup* lookup, DNSAnswer* answer);
  178. /// \brief The constructor with a specific port on which the services
  179. /// listen on.
  180. ///
  181. /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
  182. /// IPv4/IPv6 services will be available if and only if \c use_ipv4
  183. /// or \c use_ipv6 is \c true, respectively.
  184. ///
  185. /// \param io_service The IOService to work with
  186. /// \param port the port to listen on
  187. /// \param ipv4 If true, listen on ipv4 'any'
  188. /// \param ipv6 If true, listen on ipv6 'any'
  189. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  190. /// \param lookup The lookup provider (see \c DNSLookup)
  191. /// \param answer The answer provider (see \c DNSAnswer)
  192. DNSService(IOService& io_service, const char& port,
  193. const bool use_ipv4, const bool use_ipv6,
  194. SimpleCallback* checkin, DNSLookup* lookup,
  195. DNSAnswer* answer);
  196. /// \brief The constructor without any servers.
  197. ///
  198. /// Use addServer() to add some servers.
  199. DNSService(IOService& io_service, SimpleCallback* checkin,
  200. DNSLookup* lookup, DNSAnswer* answer);
  201. /// \brief The destructor.
  202. ~DNSService();
  203. //@}
  204. /// \brief Add another server to the service
  205. void addServer(uint16_t port, const std::string &address);
  206. void addServer(const char &port, const std::string &address);
  207. /// \brief Remove all servers from the service
  208. void clearServers();
  209. /// \brief Return the native \c io_service object used in this wrapper.
  210. ///
  211. /// This is a short term work around to support other BIND 10 modules
  212. /// that share the same \c io_service with the authoritative server.
  213. /// It will eventually be removed once the wrapper interface is
  214. /// generalized.
  215. asio::io_service& get_io_service() { return io_service_.get_io_service(); }
  216. private:
  217. DNSServiceImpl* impl_;
  218. IOService& io_service_;
  219. };
  220. /// \brief The \c DNSServer class is a wrapper (and base class) for
  221. /// classes which provide DNS server functionality.
  222. ///
  223. /// The classes derived from this one, \c TCPServer and \c UDPServer,
  224. /// act as the interface layer between clients sending queries, and
  225. /// functions defined elsewhere that provide answers to those queries.
  226. /// Those functions are described in more detail below under
  227. /// \c SimpleCallback, \c DNSLookup, and \c DNSAnswer.
  228. ///
  229. /// Notes to developers:
  230. /// When constructed, this class (and its derived classes) will have its
  231. /// "self_" member set to point to "this". Objects of this class (as
  232. /// instantiated through a base class) are sometimes passed by
  233. /// reference (as this superclass); calls to methods in the base
  234. /// class are then rerouted via this pointer to methods in the derived
  235. /// class. This allows code from outside asiolink, with no specific
  236. /// knowledge of \c TCPServer or \c UDPServer, to access their methods.
  237. ///
  238. /// This class is both assignable and copy-constructable. Its subclasses
  239. /// use the "stackless coroutine" pattern, meaning that it will copy itself
  240. /// when "forking", and that instances will be posted as ASIO handler
  241. /// objects, which are always copied.
  242. ///
  243. /// Because these objects are frequently copied, it is recommended
  244. /// that derived classes be kept small to reduce copy overhead.
  245. class DNSServer {
  246. protected:
  247. ///
  248. /// \name Constructors and destructors
  249. ///
  250. /// This is intentionally defined as \c protected, as this base class
  251. /// should never be instantiated except as part of a derived class.
  252. //@{
  253. DNSServer() : self_(this) {}
  254. public:
  255. /// \brief The destructor
  256. virtual ~DNSServer() {}
  257. //@}
  258. ///
  259. /// \name Class methods
  260. ///
  261. /// These methods all make their calls indirectly via the "self_"
  262. /// pointer, ensuring that the functions ultimately invoked will be
  263. /// the ones in the derived class. This makes it possible to pass
  264. /// instances of derived classes as references to this base class
  265. /// without losing access to derived class data.
  266. ///
  267. //@{
  268. /// \brief The funtion operator
  269. virtual void operator()(asio::error_code ec = asio::error_code(),
  270. size_t length = 0)
  271. {
  272. (*self_)(ec, length);
  273. }
  274. /// \brief Resume processing of the server coroutine after an
  275. /// asynchronous call (e.g., to the DNS Lookup provider) has completed.
  276. ///
  277. /// \param done If true, this signals the system there is an answer
  278. /// to return.
  279. virtual void resume(const bool done) { self_->resume(done); }
  280. /// \brief Indicate whether the server is able to send an answer
  281. /// to a query.
  282. ///
  283. /// This is presently used only for testing purposes.
  284. virtual bool hasAnswer() { return (self_->hasAnswer()); }
  285. /// \brief Returns the current value of the 'coroutine' object
  286. ///
  287. /// This is a temporary method, intended to be used for debugging
  288. /// purposes during development and removed later. It allows
  289. /// callers from outside the coroutine object to retrieve information
  290. /// about its current state.
  291. ///
  292. /// \return The value of the 'coroutine' object
  293. virtual int value() { return (self_->value()); }
  294. /// \brief Returns a pointer to a clone of this DNSServer object.
  295. ///
  296. /// When a \c DNSServer object is copied or assigned, the result will
  297. /// normally be another \c DNSServer object containing a copy
  298. /// of the original "self_" pointer. Calling clone() guarantees
  299. /// that the underlying object is also correctly copied.
  300. ///
  301. /// \return A deep copy of this DNSServer object
  302. virtual DNSServer* clone() { return (self_->clone()); }
  303. //@}
  304. protected:
  305. /// \brief Lookup handler object.
  306. ///
  307. /// This is a protected class; it can only be instantiated
  308. /// from within a derived class of \c DNSServer.
  309. ///
  310. /// A server object that has received a query creates an instance
  311. /// of this class and scheudles it on the ASIO service queue
  312. /// using asio::io_service::post(). When the handler executes, it
  313. /// calls the asyncLookup() method in the server object to start a
  314. /// DNS lookup. When the lookup is complete, the server object is
  315. /// scheduled to resume, again using io_service::post().
  316. ///
  317. /// Note that the calling object is copied into the handler object,
  318. /// not referenced. This is because, once the calling object yields
  319. /// control to the handler, it falls out of scope and may disappear
  320. template <typename T>
  321. class AsyncLookup {
  322. public:
  323. AsyncLookup(T& caller) : caller_(caller) {}
  324. void operator()() { caller_.asyncLookup(); }
  325. private:
  326. T caller_;
  327. };
  328. /// \brief Carries out a DNS lookup.
  329. ///
  330. /// This function calls the \c DNSLookup object specified by the
  331. /// DNS server when the \c IOService was created, passing along
  332. /// the details of the query and a pointer back to the current
  333. /// server object. It is called asynchronously via the AsyncLookup
  334. /// handler class.
  335. virtual void asyncLookup() { self_->asyncLookup(); }
  336. private:
  337. DNSServer* self_;
  338. };
  339. /// \brief The \c DNSLookup class is an abstract base class for a DNS
  340. /// Lookup provider function.
  341. ///
  342. /// Specific derived class implementations are hidden within the
  343. /// implementation. Instances of the derived classes can be called
  344. /// as functions via the operator() interface. Pointers to these
  345. /// instances can then be provided to the \c IOService class
  346. /// via its constructor.
  347. ///
  348. /// A DNS Lookup provider function obtains the data needed to answer
  349. /// a DNS query (e.g., from authoritative data source, cache, or upstream
  350. /// query). After it has run, the OutputBuffer object passed to it
  351. /// should contain the answer to the query, in an internal representation.
  352. class DNSLookup {
  353. ///
  354. /// \name Constructors and Destructor
  355. ///
  356. /// Note: The copy constructor and the assignment operator are
  357. /// intentionally defined as private, making this class non-copyable.
  358. //@{
  359. private:
  360. DNSLookup(const DNSLookup& source);
  361. DNSLookup& operator=(const DNSLookup& source);
  362. protected:
  363. /// \brief The default constructor.
  364. ///
  365. /// This is intentionally defined as \c protected as this base class
  366. /// should never be instantiated (except as part of a derived class).
  367. DNSLookup() : self_(this) {}
  368. public:
  369. /// \brief The destructor
  370. virtual ~DNSLookup() {}
  371. //@}
  372. /// \brief The function operator
  373. ///
  374. /// This makes its call indirectly via the "self" pointer, ensuring
  375. /// that the function ultimately invoked will be the one in the derived
  376. /// class.
  377. ///
  378. /// \param io_message The event message to handle
  379. /// \param message The DNS MessagePtr that needs handling
  380. /// \param buffer The final answer is put here
  381. /// \param DNSServer DNSServer object to use
  382. virtual void operator()(const IOMessage& io_message,
  383. isc::dns::MessagePtr message,
  384. isc::dns::MessagePtr answer_message,
  385. isc::dns::OutputBufferPtr buffer,
  386. DNSServer* server) const
  387. {
  388. (*self_)(io_message, message, answer_message, buffer, server);
  389. }
  390. private:
  391. DNSLookup* self_;
  392. };
  393. /// \brief The \c DNSAnswer class is an abstract base class for a DNS
  394. /// Answer provider function.
  395. ///
  396. /// Specific derived class implementations are hidden within the
  397. /// implementation. Instances of the derived classes can be called
  398. /// as functions via the operator() interface. Pointers to these
  399. /// instances can then be provided to the \c IOService class
  400. /// via its constructor.
  401. ///
  402. /// A DNS Answer provider function takes answer data that has been obtained
  403. /// from a DNS Lookup provider functon and readies it to be sent to the
  404. /// client. After it has run, the OutputBuffer object passed to it should
  405. /// contain the answer to the query rendered into wire format.
  406. class DNSAnswer {
  407. ///
  408. /// \name Constructors and Destructor
  409. ///
  410. /// Note: The copy constructor and the assignment operator are
  411. /// intentionally defined as private, making this class non-copyable.
  412. //@{
  413. private:
  414. DNSAnswer(const DNSAnswer& source);
  415. DNSAnswer& operator=(const DNSAnswer& source);
  416. protected:
  417. /// \brief The default constructor.
  418. ///
  419. /// This is intentionally defined as \c protected as this base class
  420. /// should never be instantiated (except as part of a derived class).
  421. DNSAnswer() {}
  422. public:
  423. /// \brief The destructor
  424. virtual ~DNSAnswer() {}
  425. //@}
  426. /// \brief The function operator
  427. ///
  428. /// This makes its call indirectly via the "self" pointer, ensuring
  429. /// that the function ultimately invoked will be the one in the derived
  430. /// class.
  431. ///
  432. /// \param io_message The event message to handle
  433. /// \param message The DNS MessagePtr that needs handling
  434. /// \param buffer The result is put here
  435. virtual void operator()(const IOMessage& io_message,
  436. isc::dns::MessagePtr message,
  437. isc::dns::MessagePtr answer_message,
  438. isc::dns::OutputBufferPtr buffer) const = 0;
  439. };
  440. /// \brief The \c SimpleCallback class is an abstract base class for a
  441. /// simple callback function with the signature:
  442. ///
  443. /// void simpleCallback(const IOMessage& io_message) const;
  444. ///
  445. /// Specific derived class implementations are hidden within the
  446. /// implementation. Instances of the derived classes can be called
  447. /// as functions via the operator() interface. Pointers to these
  448. /// instances can then be provided to the \c IOService class
  449. /// via its constructor.
  450. ///
  451. /// The \c SimpleCallback is expected to be used for basic, generic
  452. /// tasks such as checking for configuration changes. It may also be
  453. /// used for testing purposes.
  454. class SimpleCallback {
  455. ///
  456. /// \name Constructors and Destructor
  457. ///
  458. /// Note: The copy constructor and the assignment operator are
  459. /// intentionally defined as private, making this class non-copyable.
  460. //@{
  461. private:
  462. SimpleCallback(const SimpleCallback& source);
  463. SimpleCallback& operator=(const SimpleCallback& source);
  464. protected:
  465. /// \brief The default constructor.
  466. ///
  467. /// This is intentionally defined as \c protected as this base class
  468. /// should never be instantiated (except as part of a derived class).
  469. SimpleCallback() : self_(this) {}
  470. public:
  471. /// \brief The destructor
  472. virtual ~SimpleCallback() {}
  473. /// \brief The function operator
  474. //@}
  475. ///
  476. /// This makes its call indirectly via the "self" pointer, ensuring
  477. /// that the function ultimately invoked will be the one in the derived
  478. /// class.
  479. ///
  480. /// \param io_message The event message to handle
  481. virtual void operator()(const IOMessage& io_message) const {
  482. (*self_)(io_message);
  483. }
  484. private:
  485. SimpleCallback* self_;
  486. };
  487. /// \brief The \c RecursiveQuery class provides a layer of abstraction around
  488. /// the ASIO code that carries out an upstream query.
  489. ///
  490. /// This design is very preliminary; currently it is only capable of
  491. /// handling simple forward requests to a single resolver.
  492. class RecursiveQuery {
  493. ///
  494. /// \name Constructors
  495. ///
  496. //@{
  497. public:
  498. /// \brief Constructor for use when acting as a forwarder
  499. ///
  500. /// This is currently the only way to construct \c RecursiveQuery
  501. /// object. The addresses of the forward nameservers is specified,
  502. /// and every upstream query will be sent to one random address.
  503. /// \param dns_service The DNS Service to perform the recursive
  504. /// query on.
  505. /// \param upstream Addresses and ports of the upstream servers
  506. /// to forward queries to.
  507. /// \param upstream_root Addresses and ports of the root servers
  508. /// to use when resolving.
  509. /// \param timeout How long to timeout the query, in ms
  510. /// -1 means never timeout (but do not use that).
  511. /// TODO: This should be computed somehow dynamically in future
  512. /// \param retries how many times we try again (0 means just send and
  513. /// and return if it returs).
  514. RecursiveQuery(DNSService& dns_service,
  515. const std::vector<std::pair<std::string, uint16_t> >&
  516. upstream,
  517. const std::vector<std::pair<std::string, uint16_t> >&
  518. upstream_root,
  519. int timeout = -1, unsigned retries = 0);
  520. //@}
  521. /// \brief Initiates an upstream query in the \c RecursiveQuery object.
  522. ///
  523. /// When sendQuery() is called, a message is sent asynchronously to
  524. /// the upstream name server. When a reply arrives, 'server'
  525. /// is placed on the ASIO service queue via io_service::post(), so
  526. /// that the original \c DNSServer objct can resume processing.
  527. ///
  528. /// \param question The question being answered <qname/qclass/qtype>
  529. /// \param buffer An output buffer into which the response can be copied
  530. /// \param server A pointer to the \c DNSServer object handling the client
  531. void sendQuery(const isc::dns::Question& question,
  532. isc::dns::MessagePtr answer_message,
  533. isc::dns::OutputBufferPtr buffer,
  534. DNSServer* server);
  535. private:
  536. DNSService& dns_service_;
  537. boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
  538. upstream_;
  539. boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
  540. upstream_root_;
  541. int timeout_;
  542. unsigned retries_;
  543. };
  544. /// \brief The \c IntervalTimer class is a wrapper for the ASIO
  545. /// \c asio::deadline_timer class.
  546. ///
  547. /// This class is implemented to use \c asio::deadline_timer as
  548. /// interval timer.
  549. ///
  550. /// \c setupTimer() sets a timer to expire on (now + interval) and
  551. /// a call back function.
  552. ///
  553. /// \c IntervalTimerImpl::callback() is called by the timer when
  554. /// it expires.
  555. ///
  556. /// The function calls the call back function set by \c setupTimer()
  557. /// and updates the timer to expire in (now + interval) seconds.
  558. /// The type of call back function is \c void(void).
  559. ///
  560. /// The call back function will not be called if the instance of this
  561. /// class is destructed before the timer is expired.
  562. ///
  563. /// Note: Destruction of an instance of this class while call back
  564. /// is pending causes throwing an exception from \c IOService.
  565. ///
  566. /// Sample code:
  567. /// \code
  568. /// void function_to_call_back() {
  569. /// // this function will be called periodically
  570. /// }
  571. /// int interval_in_seconds = 1;
  572. /// IOService io_service;
  573. ///
  574. /// IntervalTimer intervalTimer(io_service);
  575. /// intervalTimer.setupTimer(function_to_call_back, interval_in_seconds);
  576. /// io_service.run();
  577. /// \endcode
  578. ///
  579. class IntervalTimer {
  580. public:
  581. /// \name The type of timer callback function
  582. typedef boost::function<void()> Callback;
  583. ///
  584. /// \name Constructors and Destructor
  585. ///
  586. /// Note: The copy constructor and the assignment operator are
  587. /// intentionally defined as private, making this class non-copyable.
  588. //@{
  589. private:
  590. IntervalTimer(const IntervalTimer& source);
  591. IntervalTimer& operator=(const IntervalTimer& source);
  592. public:
  593. /// \brief The constructor with \c IOService.
  594. ///
  595. /// This constructor may throw a standard exception if
  596. /// memory allocation fails inside the method.
  597. /// This constructor may also throw \c asio::system_error.
  598. ///
  599. /// \param io_service A reference to an instance of IOService
  600. ///
  601. IntervalTimer(IOService& io_service);
  602. /// \brief The destructor.
  603. ///
  604. /// This destructor never throws an exception.
  605. ///
  606. /// On the destruction of this class the timer will be canceled
  607. /// inside \c asio::deadline_timer.
  608. ///
  609. ~IntervalTimer();
  610. //@}
  611. /// \brief Register timer callback function and interval.
  612. ///
  613. /// This function sets callback function and interval in seconds.
  614. /// Timer will actually start after calling \c IOService::run().
  615. ///
  616. /// \param cbfunc A reference to a function \c void(void) to call back
  617. /// when the timer is expired (should not be an empty functor)
  618. /// \param interval Interval in seconds (greater than 0)
  619. ///
  620. /// Note: IntervalTimer will not pass \c asio::error_code to
  621. /// call back function. In case the timer is cancelled, the function
  622. /// will not be called.
  623. ///
  624. /// \throw isc::InvalidParameter cbfunc is empty
  625. /// \throw isc::BadValue interval is 0
  626. /// \throw isc::Unexpected ASIO library error
  627. ///
  628. void setupTimer(const Callback& cbfunc, const uint32_t interval);
  629. /// Cancel the timer.
  630. ///
  631. /// If the timer has been set up, this method cancels any asynchronous
  632. /// events waiting on the timer and stops the timer itself.
  633. /// If the timer has already been canceled, this method effectively does
  634. /// nothing.
  635. ///
  636. /// This method never throws an exception.
  637. void cancel();
  638. /// Return the timer interval.
  639. ///
  640. /// This method returns the timer interval in seconds if it's running;
  641. /// if the timer has been canceled it returns 0.
  642. ///
  643. /// This method never throws an exception.
  644. ///
  645. /// Note: We may want to change the granularity of the timer to
  646. /// milliseconds or even finer. If and when this happens the semantics
  647. /// of the return value of this method will be changed accordingly.
  648. uint32_t getInterval() const;
  649. private:
  650. IntervalTimerImpl* impl_;
  651. };
  652. } // asiolink
  653. #endif // __ASIOLINK_H
  654. // Local Variables:
  655. // mode: c++
  656. // End: