Kea 3.3.3
ncr_io.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef NCR_IO_H
8#define NCR_IO_H
9
49
50#include <asiolink/io_address.h>
51#include <asiolink/io_service.h>
52#include <dhcp_ddns/ncr_msg.h>
54
55#include <cstdint>
56#include <deque>
57#include <mutex>
58
59#include <boost/enable_shared_from_this.hpp>
60#include <boost/scoped_ptr.hpp>
61
62namespace isc {
63namespace dhcp_ddns {
64
74
84extern NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str);
85
92extern std::string ncrProtocolToString(NameChangeProtocol protocol);
93
96public:
97 NcrListenerError(const char* file, size_t line, const char* what) :
98 isc::Exception(file, line, what) { }
99};
100
103public:
104 NcrListenerOpenError(const char* file, size_t line, const char* what) :
105 isc::Exception(file, line, what) { }
106};
107
110public:
111 NcrListenerReceiveError(const char* file, size_t line, const char* what) :
112 isc::Exception(file, line, what) { }
113};
114
169public:
170
178
184 class RequestReceiveHandler : public boost::enable_shared_from_this<RequestReceiveHandler> {
185 public:
186
199 virtual void operator()(const Result result,
200 NameChangeRequestPtr& ncr) = 0;
201
203 }
204 };
205
207 typedef boost::shared_ptr<RequestReceiveHandler> RequestReceiveHandlerPtr;
208
214
217 }
218
229 void startListening(const isc::asiolink::IOServicePtr& io_service);
230
235 void stopListening();
236
237protected:
238
248 void receiveNext();
249
275 void invokeRecvHandler(const Result result, NameChangeRequestPtr& ncr);
276
285 void scheduleNextReceive();
286
296 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
297
305 virtual void close() = 0;
306
315 virtual void doReceive() = 0;
316
317public:
318
327 bool amListening() const {
328 return (listening_);
329 }
330
342 bool isIoPending() const {
343 return (io_pending_);
344 }
345
346private:
353 void setListening(bool value) {
354 listening_ = value;
355 }
356
358 bool listening_;
359
361 bool io_pending_;
362
364 RequestReceiveHandlerPtr recv_handler_;
365};
366
368typedef boost::shared_ptr<NameChangeListener> NameChangeListenerPtr;
369
372public:
373 NcrSenderError(const char* file, size_t line, const char* what) :
374 isc::Exception(file, line, what) { }
375};
376
379public:
380 NcrSenderOpenError(const char* file, size_t line, const char* what) :
381 isc::Exception(file, line, what) { }
382};
383
386public:
387 NcrSenderQueueFull(const char* file, size_t line, const char* what) :
388 isc::Exception(file, line, what) { }
389};
390
393public:
394 NcrSenderSendError(const char* file, size_t line, const char* what) :
395 isc::Exception(file, line, what) { }
396};
397
425
474
480public:
481
483 typedef std::deque<NameChangeRequestPtr> SendQueue;
484
486 static const size_t MAX_QUEUE_DEFAULT = 1024;
487
489 enum Result : uint16_t {
493 ERROR = 3,
494 };
495
501 static std::string const& resultToText(Result const& result) {
502 static std::vector<std::string> const text_vector {
503 "SUCCESS",
504 "TIME_OUT",
505 "STOPPED",
506 "ERROR",
507 };
508 static std::string const unknown("UNKNOWN");
509 return (result < text_vector.size() ? text_vector[result] : unknown);
510 }
511
517 class RequestSendHandler : public boost::enable_shared_from_this<RequestSendHandler> {
518 public:
519
531 virtual void operator ()(const Result result,
532 NameChangeRequestPtr& ncr) = 0;
533
535 }
536 };
537
539 typedef boost::shared_ptr<RequestSendHandler> RequestSendHandlerPtr;
540
549 size_t send_queue_max = MAX_QUEUE_DEFAULT);
550
553 }
554
564 void startSending(const isc::asiolink::IOServicePtr& io_service);
565
570 void stopSending();
571
583
596 void assumeQueue(NameChangeSender& source_sender);
597
611 virtual int getSelectFd() = 0;
612
616 virtual bool ioReady() = 0;
617
618private:
619
623 void startSendingInternal(const isc::asiolink::IOServicePtr& io_service);
624
630 void sendRequestInternal(NameChangeRequestPtr& ncr);
631
638 void assumeQueueInternal(NameChangeSender& source_sender);
639
644 void invokeSendHandlerInternal(const NameChangeSender::Result result);
645
648 void skipNextInternal();
649
654 size_t getQueueSizeInternal() const;
655
663 const NameChangeRequestPtr& peekAtInternal(const size_t index) const;
664
665protected:
666
673 void sendNext();
674
702
712 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
713
721 virtual void close() = 0;
722
733 virtual void doSend(NameChangeRequestPtr& ncr) = 0;
734
735public:
736
748 void skipNext();
749
757 void clearSendQueue();
758
765 bool amSending() const {
766 return (sending_);
767 }
768
775 bool isSendInProgress() const;
776
780 size_t getQueueMaxSize() const {
781 return (send_queue_max_);
782 }
783
792 void setQueueMaxSize(const size_t new_max);
793
797 size_t getQueueSize() const;
798
810 const NameChangeRequestPtr& peekAt(const size_t index) const;
811
831 virtual void runReadyIO();
832
833protected:
834
839 return (send_queue_);
840 }
841
842private:
843
850 void setSending(bool value) {
851 sending_ = value;
852 }
853
854protected:
855
861
862private:
863
865 bool sending_;
866
868 RequestSendHandlerPtr send_handler_;
869
871 size_t send_queue_max_;
872
874 SendQueue send_queue_;
875
877 NameChangeRequestPtr ncr_to_send_;
878
880 const boost::scoped_ptr<std::mutex> mutex_;
881};
882
884typedef boost::shared_ptr<NameChangeSender> NameChangeSenderPtr;
885
886} // namespace dhcp_ddns
887} // namespace isc
888
889#endif
This is a base class for exceptions thrown from the DNS library module.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Abstract class for defining application layer receive callbacks.
Definition ncr_io.h:184
virtual void operator()(const Result result, NameChangeRequestPtr &ncr)=0
Function operator implementing a NCR receive callback.
virtual void open(const isc::asiolink::IOServicePtr &io_service)=0
Abstract method which opens the IO source for reception.
boost::shared_ptr< RequestReceiveHandler > RequestReceiveHandlerPtr
Defines a smart pointer to an instance of a request receive handler.
Definition ncr_io.h:207
void stopListening()
Closes the IO source and stops listen logic.
Definition ncr_io.cc:91
virtual void close()=0
Abstract method which closes the IO source.
bool isIoPending() const
Returns true if the listener has an IO call in progress.
Definition ncr_io.h:342
NameChangeListener(RequestReceiveHandlerPtr recv_handler)
Constructor.
Definition ncr_io.cc:53
Result
Defines the outcome of an asynchronous NCR receive.
Definition ncr_io.h:172
virtual void doReceive()=0
Initiates an IO layer asynchronous read.
void invokeRecvHandler(const Result result, NameChangeRequestPtr &ncr)
Calls the NCR receive handler registered with the listener.
Definition ncr_io.cc:108
virtual ~NameChangeListener()
Destructor.
Definition ncr_io.h:216
bool amListening() const
Returns true if the listener is listening, false otherwise.
Definition ncr_io.h:327
void receiveNext()
Initiates an asynchronous receive.
Definition ncr_io.cc:85
void startListening(const isc::asiolink::IOServicePtr &io_service)
Prepares the IO for reception and initiates the first receive.
Definition ncr_io.cc:58
void scheduleNextReceive()
Schedules the next asynchronous receive if still listening.
Definition ncr_io.cc:127
Abstract class for defining application layer send callbacks.
Definition ncr_io.h:517
virtual void operator()(const Result result, NameChangeRequestPtr &ncr)=0
Function operator implementing a NCR send callback.
Abstract interface for sending NameChangeRequests.
Definition ncr_io.h:479
asiolink::IOServicePtr io_service_
Pointer to the IOService currently being used by the sender.
Definition ncr_io.h:860
void stopSending()
Closes the IO sink and stops send logic.
Definition ncr_io.cc:211
virtual int getSelectFd()=0
Returns a file descriptor suitable for use with select.
Definition ncr_io.cc:506
void startSending(const isc::asiolink::IOServicePtr &io_service)
Prepares the IO for transmission.
Definition ncr_io.cc:174
static std::string const & resultToText(Result const &result)
Convert enum to string.
Definition ncr_io.h:501
NameChangeSender(RequestSendHandlerPtr send_handler, size_t send_queue_max=MAX_QUEUE_DEFAULT)
Constructor.
Definition ncr_io.cc:164
void assumeQueue(NameChangeSender &source_sender)
Move all queued requests from a given sender into the send queue.
Definition ncr_io.cc:471
Result
Defines the outcome of an asynchronous NCR send.
Definition ncr_io.h:489
size_t getQueueMaxSize() const
Returns the maximum number of entries allowed in the send queue.
Definition ncr_io.h:780
size_t getQueueSize() const
Returns the number of entries currently in the send queue.
Definition ncr_io.cc:424
const NameChangeRequestPtr & peekAt(const size_t index) const
Returns the entry at a given position in the queue.
Definition ncr_io.cc:439
virtual bool ioReady()=0
Returns whether or not the sender has IO ready to process.
void skipNext()
Removes the request at the front of the send queue.
Definition ncr_io.cc:382
std::deque< NameChangeRequestPtr > SendQueue
Defines the type used for the request send queue.
Definition ncr_io.h:483
boost::shared_ptr< RequestSendHandler > RequestSendHandlerPtr
Defines a smart pointer to an instance of a request send handler.
Definition ncr_io.h:539
void clearSendQueue()
Flushes all entries in the send queue.
Definition ncr_io.cc:400
bool amSending() const
Returns true if the sender is in send mode, false otherwise.
Definition ncr_io.h:765
virtual void doSend(NameChangeRequestPtr &ncr)=0
Initiates an IO layer asynchronous send.
void setQueueMaxSize(const size_t new_max)
Sets the maximum queue size to the given value.
Definition ncr_io.cc:414
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition ncr_io.h:486
virtual ~NameChangeSender()
Destructor.
Definition ncr_io.h:552
void invokeSendHandler(const NameChangeSender::Result result)
Calls the NCR send completion handler registered with the sender.
Definition ncr_io.cc:321
virtual void open(const isc::asiolink::IOServicePtr &io_service)=0
Abstract method which opens the IO sink for transmission.
virtual void close()=0
Abstract method which closes the IO sink.
void sendRequest(NameChangeRequestPtr &ncr)
Queues the given request to be sent.
Definition ncr_io.cc:254
virtual void runReadyIO()
Processes sender IO events.
Definition ncr_io.cc:511
SendQueue & getSendQueue()
Returns a reference to the send queue.
Definition ncr_io.h:838
bool isSendInProgress() const
Returns true when a send is in progress.
Definition ncr_io.cc:461
void sendNext()
Dequeues and sends the next request on the send queue in a thread safe context.
Definition ncr_io.cc:287
NcrListenerError(const char *file, size_t line, const char *what)
Definition ncr_io.h:97
NcrListenerOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:104
NcrListenerReceiveError(const char *file, size_t line, const char *what)
Definition ncr_io.h:111
NcrSenderError(const char *file, size_t line, const char *what)
Definition ncr_io.h:373
NcrSenderOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:380
NcrSenderQueueFull(const char *file, size_t line, const char *what)
Definition ncr_io.h:387
NcrSenderSendError(const char *file, size_t line, const char *what)
Definition ncr_io.h:394
NameChangeProtocol stringToNcrProtocol(const std::string &protocol_str)
Function which converts text labels to NameChangeProtocol enums.
Definition ncr_io.cc:23
NameChangeProtocol
Defines the list of socket protocols supported.
Definition ncr_io.h:70
boost::shared_ptr< NameChangeListener > NameChangeListenerPtr
Defines a smart pointer to an instance of a listener.
Definition ncr_io.h:368
std::string ncrProtocolToString(NameChangeProtocol protocol)
Function which converts NameChangeProtocol enums to text labels.
Definition ncr_io.cc:36
boost::shared_ptr< NameChangeSender > NameChangeSenderPtr
Defines a smart pointer to an instance of a sender.
Definition ncr_io.h:884
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:245
Defines the logger used by the top-level component of kea-lfc.
This file provides the classes needed to embody, compose, and decompose DNS update requests that are ...