ncr_io.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (C) 2013 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. #include <dhcp_ddns/dhcp_ddns_log.h>
  15. #include <dhcp_ddns/ncr_io.h>
  16. namespace isc {
  17. namespace dhcp_ddns {
  18. //************************** NameChangeListener ***************************
  19. NameChangeListener::NameChangeListener(RequestReceiveHandler&
  20. recv_handler)
  21. : listening_(false), io_pending_(false), recv_handler_(recv_handler) {
  22. };
  23. void
  24. NameChangeListener::startListening(isc::asiolink::IOService& io_service) {
  25. if (amListening()) {
  26. // This amounts to a programmatic error.
  27. isc_throw(NcrListenerError, "NameChangeListener is already listening");
  28. }
  29. // Call implementation dependent open.
  30. try {
  31. open(io_service);
  32. } catch (const isc::Exception& ex) {
  33. stopListening();
  34. isc_throw(NcrListenerOpenError, "Open failed:" << ex.what());
  35. }
  36. // Set our status to listening.
  37. setListening(true);
  38. // Start the first asynchronous receive.
  39. try {
  40. receiveNext();
  41. } catch (const isc::Exception& ex) {
  42. stopListening();
  43. isc_throw(NcrListenerReceiveError, "doReceive failed:" << ex.what());
  44. }
  45. }
  46. void
  47. NameChangeListener::receiveNext() {
  48. io_pending_ = true;
  49. doReceive();
  50. }
  51. void
  52. NameChangeListener::stopListening() {
  53. try {
  54. // Call implementation dependent close.
  55. close();
  56. } catch (const isc::Exception &ex) {
  57. // Swallow exceptions. If we have some sort of error we'll log
  58. // it but we won't propagate the throw.
  59. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_LISTEN_CLOSE_ERROR)
  60. .arg(ex.what());
  61. }
  62. // Set it false, no matter what. This allows us to at least try to
  63. // re-open via startListening().
  64. setListening(false);
  65. }
  66. void
  67. NameChangeListener::invokeRecvHandler(const Result result,
  68. NameChangeRequestPtr& ncr) {
  69. // Call the registered application layer handler.
  70. // Surround the invocation with a try-catch. The invoked handler is
  71. // not supposed to throw, but in the event it does we will at least
  72. // report it.
  73. try {
  74. io_pending_ = false;
  75. recv_handler_(result, ncr);
  76. } catch (const std::exception& ex) {
  77. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
  78. .arg(ex.what());
  79. }
  80. // Start the next IO layer asynchronous receive.
  81. // In the event the handler above intervened and decided to stop listening
  82. // we need to check that first.
  83. if (amListening()) {
  84. try {
  85. receiveNext();
  86. } catch (const isc::Exception& ex) {
  87. // It is possible though unlikely, for doReceive to fail without
  88. // scheduling the read. While, unlikely, it does mean the callback
  89. // will not get called with a failure. A throw here would surface
  90. // at the IOService::run (or run variant) invocation. So we will
  91. // close the window by invoking the application handler with
  92. // a failed result, and let the application layer sort it out.
  93. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_RECV_NEXT_ERROR)
  94. .arg(ex.what());
  95. // Call the registered application layer handler.
  96. // Surround the invocation with a try-catch. The invoked handler is
  97. // not supposed to throw, but in the event it does we will at least
  98. // report it.
  99. NameChangeRequestPtr empty;
  100. try {
  101. io_pending_ = false;
  102. recv_handler_(ERROR, empty);
  103. } catch (const std::exception& ex) {
  104. LOG_ERROR(dhcp_ddns_logger,
  105. DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
  106. .arg(ex.what());
  107. }
  108. }
  109. }
  110. }
  111. //************************* NameChangeSender ******************************
  112. NameChangeSender::NameChangeSender(RequestSendHandler& send_handler,
  113. size_t send_queue_max)
  114. : sending_(false), send_handler_(send_handler),
  115. send_queue_max_(send_queue_max) {
  116. // Queue size must be big enough to hold at least 1 entry.
  117. if (send_queue_max == 0) {
  118. isc_throw(NcrSenderError, "NameChangeSender constructor"
  119. " queue size must be greater than zero");
  120. }
  121. }
  122. void
  123. NameChangeSender::startSending(isc::asiolink::IOService& io_service) {
  124. if (amSending()) {
  125. // This amounts to a programmatic error.
  126. isc_throw(NcrSenderError, "NameChangeSender is already sending");
  127. }
  128. // Clear send marker.
  129. ncr_to_send_.reset();
  130. // Call implementation dependent open.
  131. try {
  132. open(io_service);
  133. } catch (const isc::Exception& ex) {
  134. stopSending();
  135. isc_throw(NcrSenderOpenError, "Open failed: " << ex.what());
  136. }
  137. // Set our status to sending.
  138. setSending(true);
  139. }
  140. void
  141. NameChangeSender::stopSending() {
  142. try {
  143. // Call implementation dependent close.
  144. close();
  145. } catch (const isc::Exception &ex) {
  146. // Swallow exceptions. If we have some sort of error we'll log
  147. // it but we won't propagate the throw.
  148. LOG_ERROR(dhcp_ddns_logger,
  149. DHCP_DDNS_NCR_SEND_CLOSE_ERROR).arg(ex.what());
  150. }
  151. // Set it false, no matter what. This allows us to at least try to
  152. // re-open via startSending().
  153. setSending(false);
  154. }
  155. void
  156. NameChangeSender::sendRequest(NameChangeRequestPtr& ncr) {
  157. if (!amSending()) {
  158. isc_throw(NcrSenderError, "sender is not ready to send");
  159. }
  160. if (!ncr) {
  161. isc_throw(NcrSenderError, "request to send is empty");
  162. }
  163. if (send_queue_.size() >= send_queue_max_) {
  164. isc_throw(NcrSenderQueueFull, "send queue has reached maximum capacity:"
  165. << send_queue_max_ );
  166. }
  167. // Put it on the queue.
  168. send_queue_.push_back(ncr);
  169. // Call sendNext to schedule the next one to go.
  170. sendNext();
  171. }
  172. void
  173. NameChangeSender::sendNext() {
  174. if (ncr_to_send_) {
  175. // @todo Not sure if there is any risk of getting stuck here but
  176. // an interval timer to defend would be good.
  177. // In reality, the derivation should ensure they timeout themselves
  178. return;
  179. }
  180. // If queue isn't empty, then get one from the front. Note we leave
  181. // it on the front of the queue until we successfully send it.
  182. if (!send_queue_.empty()) {
  183. ncr_to_send_ = send_queue_.front();
  184. // @todo start defense timer
  185. // If a send were to hang and we timed it out, then timeout
  186. // handler need to cycle thru open/close ?
  187. // Call implementation dependent send.
  188. doSend(ncr_to_send_);
  189. }
  190. }
  191. void
  192. NameChangeSender::invokeSendHandler(const NameChangeSender::Result result) {
  193. // @todo reset defense timer
  194. if (result == SUCCESS) {
  195. // It shipped so pull it off the queue.
  196. send_queue_.pop_front();
  197. }
  198. // Invoke the completion handler passing in the result and a pointer
  199. // the request involved.
  200. // Surround the invocation with a try-catch. The invoked handler is
  201. // not supposed to throw, but in the event it does we will at least
  202. // report it.
  203. try {
  204. send_handler_(result, ncr_to_send_);
  205. } catch (const std::exception& ex) {
  206. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR)
  207. .arg(ex.what());
  208. }
  209. // Clear the pending ncr pointer.
  210. ncr_to_send_.reset();
  211. // Set up the next send
  212. try {
  213. sendNext();
  214. } catch (const isc::Exception& ex) {
  215. // It is possible though unlikely, for sendNext to fail without
  216. // scheduling the send. While, unlikely, it does mean the callback
  217. // will not get called with a failure. A throw here would surface
  218. // at the IOService::run (or run variant) invocation. So we will
  219. // close the window by invoking the application handler with
  220. // a failed result, and let the application layer sort it out.
  221. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_SEND_NEXT_ERROR)
  222. .arg(ex.what());
  223. // Invoke the completion handler passing in failed result.
  224. // Surround the invocation with a try-catch. The invoked handler is
  225. // not supposed to throw, but in the event it does we will at least
  226. // report it.
  227. try {
  228. send_handler_(ERROR, ncr_to_send_);
  229. } catch (const std::exception& ex) {
  230. LOG_ERROR(dhcp_ddns_logger,
  231. DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR).arg(ex.what());
  232. }
  233. }
  234. }
  235. void
  236. NameChangeSender::skipNext() {
  237. if (!send_queue_.empty()) {
  238. // Discards the request at the front of the queue.
  239. send_queue_.pop_front();
  240. }
  241. }
  242. void
  243. NameChangeSender::clearSendQueue() {
  244. if (amSending()) {
  245. isc_throw(NcrSenderError, "Cannot clear queue while sending");
  246. }
  247. send_queue_.clear();
  248. }
  249. } // namespace isc::dhcp_ddns
  250. } // namespace isc