ncr_io.cc 7.7 KB

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