summaryrefslogtreecommitdiffstats
path: root/src/bin/agent/ca_process.cc
blob: 7847ac9bc2f33c2448eb70186f6df8ecefe86b2b (plain)
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
// Copyright (C) 2016-2017 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 <agent/ca_process.h>
#include <agent/ca_response_creator_factory.h>
#include <agent/ca_log.h>
#include <asiolink/io_address.h>
#include <asiolink/io_error.h>
#include <cc/command_interpreter.h>
#include <http/listener.h>
#include <boost/pointer_cast.hpp>

using namespace isc::asiolink;
using namespace isc::http;
using namespace isc::process;

// Temporarily hardcoded configuration.
/// @todo: remove once 5134 is merged.
namespace {

const long REQUEST_TIMEOUT = 10000;

}

namespace isc {
namespace agent {

CtrlAgentProcess::CtrlAgentProcess(const char* name,
                                   const asiolink::IOServicePtr& io_service)
    : DProcessBase(name, io_service, DCfgMgrBasePtr(new CtrlAgentCfgMgr())) {
}

CtrlAgentProcess::~CtrlAgentProcess() {
}

void
CtrlAgentProcess::init() {
}

void
CtrlAgentProcess::run() {
    LOG_INFO(agent_logger, CTRL_AGENT_STARTED).arg(VERSION);

    try {

        // Create response creator factory first. It will be used to generate
        // response creators. Each response creator will be used to generate
        // answer to specific request.
        HttpResponseCreatorFactoryPtr rcf(new CtrlAgentResponseCreatorFactory());

        DCfgContextBasePtr base_ctx = getCfgMgr()->getContext();
        CtrlAgentCfgContextPtr ctx = boost::dynamic_pointer_cast<CtrlAgentCfgContext>(base_ctx);
        if (!ctx) {
            isc_throw(Unexpected, "Interal logic error: bad context type");
        }

        /// @todo: If the parameter is a hostname, we need to resolve it.
        IOAddress server_address("::");
        try {
            server_address = IOAddress(ctx->getHttpHost());
        } catch (const IOError& e) {
            isc_throw(BadValue, "Failed to convert " << ctx->getHttpHost()
                      << " to IP address:" << e.what());
        }

        uint16_t server_port = ctx->getHttpPort();

        // Create http listener. It will open up a TCP socket and be prepared
        // to accept incoming connection.
        HttpListener http_listener(*getIoService(), server_address,
                                   server_port, rcf, REQUEST_TIMEOUT);

        // Instruct the http listener to actually open socket, install callback
        // and start listening.
        http_listener.start();

        // Ok, seems we're good to go.
        LOG_INFO(agent_logger, CTRL_AGENT_HTTP_SERVICE_STARTED)
            .arg(server_address.toText()).arg(server_port);

        // Let's process incoming data or expiring timers in a loop until
        // shutdown condition is detected.
        while (!shouldShutdown()) {
            getIoService()->run_one();
        }
        stopIOService();
    } catch (const std::exception& ex) {
        LOG_FATAL(agent_logger, CTRL_AGENT_FAILED).arg(ex.what());
        try {
            stopIOService();
        } catch (...) {
            // Ignore double errors
        }
        isc_throw(DProcessBaseError,
                  "Process run method failed: " << ex.what());
    }

    LOG_DEBUG(agent_logger, DBGLVL_START_SHUT, CTRL_AGENT_RUN_EXIT);
}

isc::data::ConstElementPtr
CtrlAgentProcess::shutdown(isc::data::ConstElementPtr /*args*/) {
    setShutdownFlag(true);
    return (isc::config::createAnswer(0, "Control Agent is shutting down"));
}

isc::data::ConstElementPtr
CtrlAgentProcess::configure(isc::data::ConstElementPtr config_set,
                            bool check_only) {
    int rcode = 0;
    isc::data::ConstElementPtr answer = getCfgMgr()->simpleParseConfig(config_set,
                                                                       check_only);
    config::parseAnswer(rcode, answer);
    return (answer);
}

isc::data::ConstElementPtr
CtrlAgentProcess::command(const std::string& command,
                          isc::data::ConstElementPtr /*args*/) {
    return (isc::config::createAnswer(COMMAND_INVALID, "Unrecognized command: "
                                      + command));
}


CtrlAgentCfgMgrPtr
CtrlAgentProcess::getCtrlAgentCfgMgr() {
    return(boost::dynamic_pointer_cast<CtrlAgentCfgMgr>(getCfgMgr()));
}

} // namespace isc::agent
} // namespace isc