Kea 2.7.5
ncr_io.h
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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 <boost/scoped_ptr.hpp>
56#include <boost/enable_shared_from_this.hpp>
57
58#include <deque>
59#include <mutex>
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
484
490 class RequestSendHandler : public boost::enable_shared_from_this<RequestSendHandler> {
491 public:
492
504 virtual void operator ()(const Result result,
505 NameChangeRequestPtr& ncr) = 0;
506
508 }
509 };
510
512 typedef boost::shared_ptr<RequestSendHandler> RequestSendHandlerPtr;
513
522 size_t send_queue_max = MAX_QUEUE_DEFAULT);
523
526 }
527
537 void startSending(const isc::asiolink::IOServicePtr& io_service);
538
543 void stopSending();
544
556
569 void assumeQueue(NameChangeSender& source_sender);
570
584 virtual int getSelectFd() = 0;
585
589 virtual bool ioReady() = 0;
590
591private:
592
596 void startSendingInternal(const isc::asiolink::IOServicePtr& io_service);
597
603 void sendRequestInternal(NameChangeRequestPtr& ncr);
604
611 void assumeQueueInternal(NameChangeSender& source_sender);
612
617 void invokeSendHandlerInternal(const NameChangeSender::Result result);
618
621 void skipNextInternal();
622
627 size_t getQueueSizeInternal() const;
628
636 const NameChangeRequestPtr& peekAtInternal(const size_t index) const;
637
638protected:
639
646 void sendNext();
647
675
685 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
686
694 virtual void close() = 0;
695
706 virtual void doSend(NameChangeRequestPtr& ncr) = 0;
707
708public:
709
721 void skipNext();
722
730 void clearSendQueue();
731
738 bool amSending() const {
739 return (sending_);
740 }
741
748 bool isSendInProgress() const;
749
753 size_t getQueueMaxSize() const {
754 return (send_queue_max_);
755 }
756
765 void setQueueMaxSize(const size_t new_max);
766
770 size_t getQueueSize() const;
771
783 const NameChangeRequestPtr& peekAt(const size_t index) const;
784
804 virtual void runReadyIO();
805
806protected:
807
812 return (send_queue_);
813 }
814
815private:
816
823 void setSending(bool value) {
824 sending_ = value;
825 }
826
827protected:
828
834
835private:
836
838 bool sending_;
839
841 RequestSendHandlerPtr send_handler_;
842
844 size_t send_queue_max_;
845
847 SendQueue send_queue_;
848
850 NameChangeRequestPtr ncr_to_send_;
851
853 const boost::scoped_ptr<std::mutex> mutex_;
854};
855
857typedef boost::shared_ptr<NameChangeSender> NameChangeSenderPtr;
858
859} // namespace dhcp_ddns
860} // namespace isc
861
862#endif
This is a base class for exceptions thrown from the DNS library module.
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.
Abstract interface for receiving NameChangeRequests.
Definition ncr_io.h:167
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:490
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:833
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
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
size_t getQueueMaxSize() const
Returns the maximum number of entries allowed in the send queue.
Definition ncr_io.h:753
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:512
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:738
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:525
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:811
bool isSendInProgress() const
Returns true when a send is in progress.
Definition ncr_io.cc:443
Result
Defines the outcome of an asynchronous NCR send.
Definition ncr_io.h:478
void sendNext()
Dequeues and sends the next request on the send queue in a thread safe context.
Definition ncr_io.cc:280
Exception thrown if an NcrListenerError encounters a general error.
Definition ncr_io.h:94
NcrListenerError(const char *file, size_t line, const char *what)
Definition ncr_io.h:96
Exception thrown if an error occurs during IO source open.
Definition ncr_io.h:101
NcrListenerOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:103
Exception thrown if an error occurs initiating an IO receive.
Definition ncr_io.h:108
NcrListenerReceiveError(const char *file, size_t line, const char *what)
Definition ncr_io.h:110
Thrown when a NameChangeSender encounters an error.
Definition ncr_io.h:360
NcrSenderError(const char *file, size_t line, const char *what)
Definition ncr_io.h:362
Exception thrown if an error occurs during IO source open.
Definition ncr_io.h:367
NcrSenderOpenError(const char *file, size_t line, const char *what)
Definition ncr_io.h:369
Exception thrown if an error occurs initiating an IO send.
Definition ncr_io.h:374
NcrSenderQueueFull(const char *file, size_t line, const char *what)
Definition ncr_io.h:376
Exception thrown if an error occurs initiating an IO send.
Definition ncr_io.h:381
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:857
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 ...