Kea 2.5.8
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
57#include <deque>
58#include <mutex>
59
60namespace isc {
61namespace dhcp_ddns {
62
71};
72
82extern NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str);
83
90extern std::string ncrProtocolToString(NameChangeProtocol protocol);
91
94public:
95 NcrListenerError(const char* file, size_t line, const char* what) :
96 isc::Exception(file, line, what) { };
97};
98
101public:
102 NcrListenerOpenError(const char* file, size_t line, const char* what) :
103 isc::Exception(file, line, what) { };
104};
105
108public:
109 NcrListenerReceiveError(const char* file, size_t line, const char* what) :
110 isc::Exception(file, line, what) { };
111};
112
167public:
168
170 enum Result {
174 ERROR
175 };
176
183 public:
184
197 virtual void operator()(const Result result,
198 NameChangeRequestPtr& ncr) = 0;
199
201 }
202 };
203
208 NameChangeListener(RequestReceiveHandler& recv_handler);
209
212 };
213
224 void startListening(const isc::asiolink::IOServicePtr& io_service);
225
230 void stopListening();
231
232protected:
233
243 void receiveNext();
244
270 void invokeRecvHandler(const Result result, NameChangeRequestPtr& ncr);
271
281 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
282
290 virtual void close() = 0;
291
300 virtual void doReceive() = 0;
301
302public:
303
312 bool amListening() const {
313 return (listening_);
314 }
315
327 bool isIoPending() const {
328 return (io_pending_);
329 }
330
331private:
338 void setListening(bool value) {
339 listening_ = value;
340 }
341
343 bool listening_;
344
346 bool io_pending_;
347
349 RequestReceiveHandler& recv_handler_;
350};
351
353typedef boost::shared_ptr<NameChangeListener> NameChangeListenerPtr;
354
357public:
358 NcrSenderError(const char* file, size_t line, const char* what) :
359 isc::Exception(file, line, what) { };
360};
361
364public:
365 NcrSenderOpenError(const char* file, size_t line, const char* what) :
366 isc::Exception(file, line, what) { };
367};
368
371public:
372 NcrSenderQueueFull(const char* file, size_t line, const char* what) :
373 isc::Exception(file, line, what) { };
374};
375
378public:
379 NcrSenderSendError(const char* file, size_t line, const char* what) :
380 isc::Exception(file, line, what) { };
381};
382
410
459
465public:
466
468 typedef std::deque<NameChangeRequestPtr> SendQueue;
469
471 static const size_t MAX_QUEUE_DEFAULT = 1024;
472
474 enum Result {
478 ERROR
479 };
480
487 public:
488
500 virtual void operator ()(const Result result,
501 NameChangeRequestPtr& ncr) = 0;
502
504 }
505 };
506
514 NameChangeSender(RequestSendHandler& send_handler,
515 size_t send_queue_max = MAX_QUEUE_DEFAULT);
516
519 }
520
530 void startSending(const isc::asiolink::IOServicePtr& io_service);
531
536 void stopSending();
537
549
562 void assumeQueue(NameChangeSender& source_sender);
563
577 virtual int getSelectFd() = 0;
578
582 virtual bool ioReady() = 0;
583
584private:
585
589 void startSendingInternal(const isc::asiolink::IOServicePtr & io_service);
590
596 void sendRequestInternal(NameChangeRequestPtr& ncr);
597
604 void assumeQueueInternal(NameChangeSender& source_sender);
605
610 void invokeSendHandlerInternal(const NameChangeSender::Result result);
611
614 void skipNextInternal();
615
620 size_t getQueueSizeInternal() const;
621
629 const NameChangeRequestPtr& peekAtInternal(const size_t index) const;
630
631protected:
632
639 void sendNext();
640
668
678 virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0;
679
687 virtual void close() = 0;
688
699 virtual void doSend(NameChangeRequestPtr& ncr) = 0;
700
701public:
702
714 void skipNext();
715
723 void clearSendQueue();
724
731 bool amSending() const {
732 return (sending_);
733 }
734
741 bool isSendInProgress() const;
742
746 size_t getQueueMaxSize() const {
747 return (send_queue_max_);
748 }
749
758 void setQueueMaxSize(const size_t new_max);
759
763 size_t getQueueSize() const;
764
776 const NameChangeRequestPtr& peekAt(const size_t index) const;
777
797 virtual void runReadyIO();
798
799protected:
800
805 return (send_queue_);
806 }
807
808private:
809
816 void setSending(bool value) {
817 sending_ = value;
818 }
819
820protected:
821
827
828private:
829
831 bool sending_;
832
834 RequestSendHandler& send_handler_;
835
837 size_t send_queue_max_;
838
840 SendQueue send_queue_;
841
843 NameChangeRequestPtr ncr_to_send_;
844
846 const boost::scoped_ptr<std::mutex> mutex_;
847};
848
850typedef boost::shared_ptr<NameChangeSender> NameChangeSenderPtr;
851
852} // namespace dhcp_ddns
853} // namespace isc
854
855#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:182
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:166
virtual void open(const isc::asiolink::IOServicePtr &io_service)=0
Abstract method which opens the IO source for reception.
void stopListening()
Closes the IO source and stops listen logic.
Definition: ncr_io.cc:93
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:327
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:110
virtual ~NameChangeListener()
Destructor.
Definition: ncr_io.h:211
bool amListening() const
Returns true if the listener is listening, false otherwise.
Definition: ncr_io.h:312
Result
Defines the outcome of an asynchronous NCR receive.
Definition: ncr_io.h:170
void receiveNext()
Initiates an asynchronous receive.
Definition: ncr_io.cc:87
void startListening(const isc::asiolink::IOServicePtr &io_service)
Prepares the IO for reception and initiates the first receive.
Definition: ncr_io.cc:60
Abstract class for defining application layer send callbacks.
Definition: ncr_io.h:486
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:464
asiolink::IOServicePtr io_service_
Pointer to the IOService currently being used by the sender.
Definition: ncr_io.h:826
void stopSending()
Closes the IO sink and stops send logic.
Definition: ncr_io.cc:206
virtual int getSelectFd()=0
Returns a file descriptor suitable for use with select.
Definition: ncr_io.cc:479
void startSending(const isc::asiolink::IOServicePtr &io_service)
Prepares the IO for transmission.
Definition: ncr_io.cc:169
void assumeQueue(NameChangeSender &source_sender)
Move all queued requests from a given sender into the send queue.
Definition: ncr_io.cc:444
size_t getQueueMaxSize() const
Returns the maximum number of entries allowed in the send queue.
Definition: ncr_io.h:746
size_t getQueueSize() const
Returns the number of entries currently in the send queue.
Definition: ncr_io.cc:397
const NameChangeRequestPtr & peekAt(const size_t index) const
Returns the entry at a given position in the queue.
Definition: ncr_io.cc:412
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:355
std::deque< NameChangeRequestPtr > SendQueue
Defines the type used for the request send queue.
Definition: ncr_io.h:468
void clearSendQueue()
Flushes all entries in the send queue.
Definition: ncr_io.cc:373
bool amSending() const
Returns true if the sender is in send mode, false otherwise.
Definition: ncr_io.h:731
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:387
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition: ncr_io.h:471
virtual ~NameChangeSender()
Destructor.
Definition: ncr_io.h:518
void invokeSendHandler(const NameChangeSender::Result result)
Calls the NCR send completion handler registered with the sender.
Definition: ncr_io.cc:294
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:238
virtual void runReadyIO()
Processes sender IO events.
Definition: ncr_io.cc:484
SendQueue & getSendQueue()
Returns a reference to the send queue.
Definition: ncr_io.h:804
bool isSendInProgress() const
Returns true when a send is in progress.
Definition: ncr_io.cc:434
Result
Defines the outcome of an asynchronous NCR send.
Definition: ncr_io.h:474
void sendNext()
Dequeues and sends the next request on the send queue in a thread safe context.
Definition: ncr_io.cc:271
Exception thrown if an NcrListenerError encounters a general error.
Definition: ncr_io.h:93
NcrListenerError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:95
Exception thrown if an error occurs during IO source open.
Definition: ncr_io.h:100
NcrListenerOpenError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:102
Exception thrown if an error occurs initiating an IO receive.
Definition: ncr_io.h:107
NcrListenerReceiveError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:109
Thrown when a NameChangeSender encounters an error.
Definition: ncr_io.h:356
NcrSenderError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:358
Exception thrown if an error occurs during IO source open.
Definition: ncr_io.h:363
NcrSenderOpenError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:365
Exception thrown if an error occurs initiating an IO send.
Definition: ncr_io.h:370
NcrSenderQueueFull(const char *file, size_t line, const char *what)
Definition: ncr_io.h:372
Exception thrown if an error occurs initiating an IO send.
Definition: ncr_io.h:377
NcrSenderSendError(const char *file, size_t line, const char *what)
Definition: ncr_io.h:379
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:353
NameChangeProtocol
Defines the list of socket protocols supported.
Definition: ncr_io.h:68
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:850
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 ...