1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <asiolink/asio_wrapper.h>
#include <asiolink/io_address.h>
#include <asiolink/io_error.h>
#include <asiolink/io_service.h>
#include <mt_tcp_listener_mgr.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <tcp_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <util/multi_threading_mgr.h>

#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::tcp;
using namespace isc::util;

namespace isc {
namespace tcp {

MtTcpListenerMgr::MtTcpListenerMgr(TcpListenerFactory listener_factory,
                                   const IOAddress& address,
                                   const uint16_t port,
                                   const uint16_t thread_pool_size /* = 1 */,
                                   TlsContextPtr context /* = () */,
                                   TcpConnectionFilterCallback connection_filter /* = 0 */)
    : listener_factory_(listener_factory), address_(address), port_(port),
      thread_io_service_(), tcp_listener_(), thread_pool_size_(thread_pool_size),
      thread_pool_(), tls_context_(context), connection_filter_(connection_filter),
      idle_timeout_(TCP_IDLE_CONNECTION_TIMEOUT) {
}

MtTcpListenerMgr::~MtTcpListenerMgr() {
    stop();
}

void
MtTcpListenerMgr::start() {
    if (MultiThreadingMgr::instance().isTestMode()) {
        return;
    }
    // We must be in multi-threading mode.
    if (!MultiThreadingMgr::instance().getMode()) {
        isc_throw(InvalidOperation, "MtTcpListenerMgr cannot be started"
                  " when multi-threading is disabled");
    }

    // Punt if we're already started.
    if (!isStopped()) {
        isc_throw(InvalidOperation, "MtTcpListenerMgr already started!");
    }

    try {
        // Create a new IOService.
        thread_io_service_.reset(new IOService());

        // Create a new TCPListener derivation using the factory.
        tcp_listener_ = listener_factory_(thread_io_service_,
                                          address_,
                                          port_,
                                          tls_context_,
                                          idle_timeout_,
                                          connection_filter_);

        // Instruct the HTTP listener to actually open socket, install
        // callback and start listening.
        tcp_listener_->start();

        // Create the thread pool with immediate start.
        thread_pool_.reset(new IoServiceThreadPool(thread_io_service_, thread_pool_size_));

        // OK, seems like we're good to go.
        LOG_DEBUG(tcp_logger, isc::log::DBGLVL_TRACE_BASIC, MT_TCP_LISTENER_MGR_STARTED)
            .arg(thread_pool_size_)
            .arg(address_)
            .arg(port_)
            .arg(tls_context_ ? "true" : "false");
    } catch (const std::exception& ex) {
        if (thread_pool_) {
            // Stop the thread pool.
            thread_pool_->stop();
        }

        if (tcp_listener_) {
            // Stop the listener.
            tcp_listener_->stop();
        }

        if (thread_io_service_) {
            thread_io_service_->stopAndPoll();
            thread_io_service_->stop();
        }

        // Get rid of the thread pool.
        thread_pool_.reset();

        // Get rid of the listener.
        tcp_listener_.reset();

        // Ditch the IOService.
        thread_io_service_.reset();

        isc_throw(Unexpected, "MtTcpListenerMgr::start failed:" << ex.what());
    }
}

void
MtTcpListenerMgr::checkPermissions() {
    if (thread_pool_) {
        thread_pool_->checkPausePermissions();
    }
}

void
MtTcpListenerMgr::pause() {
    if (thread_pool_) {
        thread_pool_->pause();
    }
}

void
MtTcpListenerMgr::resume() {
    if (thread_pool_) {
        thread_pool_->run();
    }
}

void
MtTcpListenerMgr::stop() {
    // Nothing to do.
    if (!thread_io_service_) {
        return;
    }

    LOG_DEBUG(tcp_logger, isc::log::DBGLVL_TRACE_BASIC, MT_TCP_LISTENER_MGR_STOPPING)
        .arg(address_)
        .arg(port_);

    // Stop the thread pool.
    thread_pool_->stop();

    // Stop the listener.
    tcp_listener_->stop();

    thread_io_service_->stopAndPoll();
    thread_io_service_->stop();

    // Get rid of the thread pool.
    thread_pool_.reset();

    // Get rid of the listener.
    tcp_listener_.reset();

    // Ditch the IOService.
    thread_io_service_.reset();

    LOG_DEBUG(tcp_logger, isc::log::DBGLVL_TRACE_BASIC, MT_TCP_LISTENER_MGR_STOPPED)
        .arg(address_)
        .arg(port_);
}

bool
MtTcpListenerMgr::isRunning() {
    if (thread_pool_) {
        return (thread_pool_->isRunning());
    }

    return (false);
}

bool
MtTcpListenerMgr::isStopped() {
    if (thread_pool_) {
        return (thread_pool_->isStopped());
    }

    return (true);
}

bool
MtTcpListenerMgr::isPaused() {
    if (thread_pool_) {
        return (thread_pool_->isPaused());
    }

    return (false);
}

} // namespace isc::config
} // namespace isc