Kea 3.1.4
ncr_io.h
Go to the documentation of this file.
1// Copyright (C) 2013-2025 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 <deque>
56#include <mutex>
57
58#include <boost/enable_shared_from_this.hpp>
59#include <boost/scoped_ptr.hpp>
60
61namespace isc {
62namespace dhcp_ddns {
63
73
83extern NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str);
84
91extern std::string ncrProtocolToString(NameChangeProtocol protocol);
92
95public:
96 NcrListenerError(const char* file, size_t line, const char* what) :
97 isc::Exception(file, line, what) { }
98};
99
102public:
103 NcrListenerOpenError(const char* file, size_t line, const char* what) :
104 isc::Exception(file, line, what) { }
105};
106
109public:
110 NcrListenerReceiveError(const char* file, size_t line, const char* what) :
111 isc::Exception(file, line, what) { }
112};
113
168public:
169
177
183 class RequestReceiveHandler : public boost::enable_shared_from_this<RequestReceiveHandler> {
184 public:
185
198 virtual void operator()(const Result result,
199 NameChangeRequestPtr& ncr) = 0;
200
202 }
203 };
204
206 typedef boost::shared_ptr<RequestReceiveHandler> RequestReceiveHandlerPtr;
207
213
216 }
217
228 void startListening(const isc::asiolink::IOServicePtr& io_service);
229
234 void stopListening();
235
236protected:
237
247 void receiveNext();
248
274 void invokeRecvHandler(const Result result, NameChangeRequestPtr& ncr);
275
285 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
286
294 virtual void close() = 0;
295
304 virtual void doReceive() = 0;
305
306public:
307
316 bool amListening() const {
317 return (listening_);
318 }
319
331 bool isIoPending() const {
332 return (io_pending_);
333 }
334
335private:
342 void setListening(bool value) {
343 listening_ = value;
344 }
345
347 bool listening_;
348
350 bool io_pending_;
351
353 RequestReceiveHandlerPtr recv_handler_;
354};
355
357typedef boost::shared_ptr<NameChangeListener> NameChangeListenerPtr;
358
361public:
362 NcrSenderError(const char* file, size_t line, const char* what) :
363 isc::Exception(file, line, what) { }
364};
365
368public:
369 NcrSenderOpenError(const char* file, size_t line, const char* what) :
370 isc::Exception(file, line, what) { }
371};
372
375public:
376 NcrSenderQueueFull(const char* file, size_t line, const char* what) :
377 isc::Exception(file, line, what) { }
378};
379
382public:
383 NcrSenderSendError(const char* file, size_t line, const char* what) :
384 isc::Exception(file, line, what) { }
385};
386
414
463
469public:
470
472 typedef std::deque<NameChangeRequestPtr> SendQueue;
473
475 static const size_t MAX_QUEUE_DEFAULT = 1024;
476
478 enum Result : uint16_t {
482 ERROR = 3,
483 };
484
490 static std::string const& resultToText(Result const& result) {
491 static std::vector<std::string> const text_vector {
492 "SUCCESS",
493 "TIME_OUT",
494 "STOPPED",
495 "ERROR",
496 };
497 static std::string const unknown("UNKNOWN");
498 return (result < text_vector.size() ? text_vector[result] : unknown);
499 }
500
506 class RequestSendHandler : public boost::enable_shared_from_this<RequestSendHandler> {
507 public:
508
520 virtual void operator ()(const Result result,
521 NameChangeRequestPtr& ncr) = 0;
522
524 }
525 };
526
528 typedef boost::shared_ptr<RequestSendHandler> RequestSendHandlerPtr;
529
538 size_t send_queue_max = MAX_QUEUE_DEFAULT);
539
542 }
543
553 void startSending(const isc::asiolink::IOServicePtr& io_service);
554
559 void stopSending();
560
572
585 void assumeQueue(NameChangeSender& source_sender);
586
600 virtual int getSelectFd() = 0;
601
605 virtual bool ioReady() = 0;
606
607private:
608
612 void startSendingInternal(const isc::asiolink::IOServicePtr& io_service);
613
619 void sendRequestInternal(NameChangeRequestPtr& ncr);
620
627 void assumeQueueInternal(NameChangeSender& source_sender);
628
633 void invokeSendHandlerInternal(const NameChangeSender::Result result);
634
637 void skipNextInternal();
638
643 size_t getQueueSizeInternal() const;
644
652 const NameChangeRequestPtr& peekAtInternal(const size_t index) const;
653
654protected:
655
662 void sendNext();
663
691
701 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
702
710 virtual void close() = 0;
711
722 virtual void doSend(NameChangeRequestPtr& ncr) = 0;
723
724public:
725
737 void skipNext();
738
746 void clearSendQueue();
747
754 bool amSending() const {
755 return (sending_);
756 }
757
764 bool isSendInProgress() const;
765
769 size_t getQueueMaxSize() const {
770 return (send_queue_max_);
771 }
772
781 void setQueueMaxSize(const size_t new_max);
782
786 size_t getQueueSize() const;
787
799 const NameChangeRequestPtr& peekAt(const size_t index) const;
800
820 virtual void runReadyIO();
821
822protected:
823
828 return (send_queue_);
829 }
830
831private:
832
839 void setSending(bool value) {
840 sending_ = value;
841 }
842
843protected:
844
850
851private:
852
854 bool sending_;
855
857 RequestSendHandlerPtr send_handler_;
858
860 size_t send_queue_max_;
861
863 SendQueue send_queue_;
864
866 NameChangeRequestPtr ncr_to_send_;
867
869 const boost::scoped_ptr<std::mutex> mutex_;
870};
871
873typedef boost::shared_ptr<NameChangeSender> NameChangeSenderPtr;
874
875} // namespace dhcp_ddns
876} // namespace isc
877
878#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:183
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:206
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:331
NameChangeListener(RequestReceiveHandlerPtr recv_handler)
Constructor.
Definition ncr_io.cc:53
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:215
bool amListening() const
Returns true if the listener is listening, false otherwise.
Definition ncr_io.h:316
Result
Defines the outcome of an asynchronous NCR receive.
Definition ncr_io.h:171
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
Abstract class for defining application layer send callbacks.
Definition ncr_io.h:506
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:468
asiolink::IOServicePtr io_service_
Pointer to the IOService currently being used by the sender.
Definition ncr_io.h:849
void stopSending()
Closes the IO sink and stops send logic.
Definition ncr_io.cc:204
virtual int getSelectFd()=0
Returns a file descriptor suitable for use with select.
Definition ncr_io.cc:488
void startSending(const isc::asiolink::IOServicePtr &io_service)
Prepares the IO for transmission.
Definition ncr_io.cc:167
static std::string const & resultToText(Result const &result)
Convert enum to string.
Definition ncr_io.h:490
NameChangeSender(RequestSendHandlerPtr send_handler, size_t send_queue_max=MAX_QUEUE_DEFAULT)
Constructor.
Definition ncr_io.cc:157
void assumeQueue(NameChangeSender &source_sender)
Move all queued requests from a given sender into the send queue.
Definition ncr_io.cc:453
Result
Defines the outcome of an asynchronous NCR send.
Definition ncr_io.h:478
size_t getQueueMaxSize() const
Returns the maximum number of entries allowed in the send queue.
Definition ncr_io.h:769
size_t getQueueSize() const
Returns the number of entries currently in the send queue.
Definition ncr_io.cc:406
const NameChangeRequestPtr & peekAt(const size_t index) const
Returns the entry at a given position in the queue.
Definition ncr_io.cc:421
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:364
std::deque< NameChangeRequestPtr > SendQueue
Defines the type used for the request send queue.
Definition ncr_io.h:472
boost::shared_ptr< RequestSendHandler > RequestSendHandlerPtr
Defines a smart pointer to an instance of a request send handler.
Definition ncr_io.h:528
void clearSendQueue()
Flushes all entries in the send queue.
Definition ncr_io.cc:382
bool amSending() const
Returns true if the sender is in send mode, false otherwise.
Definition ncr_io.h:754
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:396
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition ncr_io.h:475
virtual ~NameChangeSender()
Destructor.
Definition ncr_io.h:541
void invokeSendHandler(const NameChangeSender::Result result)
Calls the NCR send completion handler registered with the sender.
Definition ncr_io.cc:303
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:247
virtual void runReadyIO()
Processes sender IO events.
Definition ncr_io.cc:493
SendQueue & getSendQueue()
Returns a reference to the send queue.
Definition ncr_io.h:827
bool isSendInProgress() const
Returns true when a send is in progress.
Definition ncr_io.cc:443
void sendNext()
Dequeues and sends the next request on the send queue in a thread safe context.
Definition ncr_io.cc:280
NcrListenerError(const char *file, size_t line, const char *what)
Definition ncr_io.h:96
NcrListenerOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:103
NcrListenerReceiveError(const char *file, size_t line, const char *what)
Definition ncr_io.h:110
NcrSenderError(const char *file, size_t line, const char *what)
Definition ncr_io.h:362
NcrSenderOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:369
NcrSenderQueueFull(const char *file, size_t line, const char *what)
Definition ncr_io.h:376
NcrSenderSendError(const char *file, size_t line, const char *what)
Definition ncr_io.h:383
NameChangeProtocol stringToNcrProtocol(const std::string &protocol_str)
Function which converts text labels to NameChangeProtocol enums.
Definition ncr_io.cc:23
boost::shared_ptr< NameChangeListener > NameChangeListenerPtr
Defines a smart pointer to an instance of a listener.
Definition ncr_io.h:357
NameChangeProtocol
Defines the list of socket protocols supported.
Definition ncr_io.h:69
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:873
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:241
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 ...