ca_process.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2016-2017 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. #include <config.h>
  7. #include <agent/ca_process.h>
  8. #include <agent/ca_controller.h>
  9. #include <agent/ca_response_creator_factory.h>
  10. #include <agent/ca_log.h>
  11. #include <asiolink/io_address.h>
  12. #include <asiolink/io_error.h>
  13. #include <cc/command_interpreter.h>
  14. #include <http/listener.h>
  15. #include <boost/pointer_cast.hpp>
  16. using namespace isc::asiolink;
  17. using namespace isc::http;
  18. using namespace isc::process;
  19. // Temporarily hardcoded configuration.
  20. /// @todo: remove once 5134 is merged.
  21. namespace {
  22. const long REQUEST_TIMEOUT = 10000;
  23. }
  24. namespace isc {
  25. namespace agent {
  26. CtrlAgentProcess::CtrlAgentProcess(const char* name,
  27. const asiolink::IOServicePtr& io_service)
  28. : DProcessBase(name, io_service, DCfgMgrBasePtr(new CtrlAgentCfgMgr())) {
  29. }
  30. CtrlAgentProcess::~CtrlAgentProcess() {
  31. }
  32. void
  33. CtrlAgentProcess::init() {
  34. }
  35. void
  36. CtrlAgentProcess::run() {
  37. LOG_INFO(agent_logger, CTRL_AGENT_STARTED).arg(VERSION);
  38. try {
  39. // Register commands.
  40. CtrlAgentControllerPtr controller =
  41. boost::dynamic_pointer_cast<CtrlAgentController>(
  42. CtrlAgentController::instance());
  43. controller->registerCommands();
  44. // Create response creator factory first. It will be used to generate
  45. // response creators. Each response creator will be used to generate
  46. // answer to specific request.
  47. HttpResponseCreatorFactoryPtr rcf(new CtrlAgentResponseCreatorFactory());
  48. DCfgContextBasePtr base_ctx = getCfgMgr()->getContext();
  49. CtrlAgentCfgContextPtr ctx =
  50. boost::dynamic_pointer_cast<CtrlAgentCfgContext>(base_ctx);
  51. if (!ctx) {
  52. isc_throw(Unexpected, "Interal logic error: bad context type");
  53. }
  54. /// @todo: If the parameter is a hostname, we need to resolve it.
  55. IOAddress server_address("::");
  56. try {
  57. server_address = IOAddress(ctx->getHttpHost());
  58. } catch (const IOError& e) {
  59. isc_throw(BadValue, "Failed to convert " << ctx->getHttpHost()
  60. << " to IP address:" << e.what());
  61. }
  62. uint16_t server_port = ctx->getHttpPort();
  63. // Create http listener. It will open up a TCP socket and be prepared
  64. // to accept incoming connection.
  65. HttpListener http_listener(*getIoService(), server_address,
  66. server_port, rcf, REQUEST_TIMEOUT);
  67. // Instruct the http listener to actually open socket, install callback
  68. // and start listening.
  69. http_listener.start();
  70. // Ok, seems we're good to go.
  71. LOG_INFO(agent_logger, CTRL_AGENT_HTTP_SERVICE_STARTED)
  72. .arg(server_address.toText()).arg(server_port);
  73. // Let's process incoming data or expiring timers in a loop until
  74. // shutdown condition is detected.
  75. while (!shouldShutdown()) {
  76. getIoService()->run_one();
  77. }
  78. stopIOService();
  79. } catch (const std::exception& ex) {
  80. LOG_FATAL(agent_logger, CTRL_AGENT_FAILED).arg(ex.what());
  81. try {
  82. stopIOService();
  83. } catch (...) {
  84. // Ignore double errors
  85. }
  86. isc_throw(DProcessBaseError,
  87. "Process run method failed: " << ex.what());
  88. }
  89. try {
  90. // Deregister commands.
  91. CtrlAgentControllerPtr controller =
  92. boost::dynamic_pointer_cast<CtrlAgentController>(
  93. CtrlAgentController::instance());
  94. controller->deregisterCommands();
  95. } catch (const std::exception&) {
  96. // What to do? Simply ignore...
  97. }
  98. LOG_DEBUG(agent_logger, DBGLVL_START_SHUT, CTRL_AGENT_RUN_EXIT);
  99. }
  100. isc::data::ConstElementPtr
  101. CtrlAgentProcess::shutdown(isc::data::ConstElementPtr /*args*/) {
  102. setShutdownFlag(true);
  103. return (isc::config::createAnswer(0, "Control Agent is shutting down"));
  104. }
  105. isc::data::ConstElementPtr
  106. CtrlAgentProcess::configure(isc::data::ConstElementPtr config_set,
  107. bool check_only) {
  108. int rcode = 0;
  109. isc::data::ConstElementPtr answer = getCfgMgr()->simpleParseConfig(config_set,
  110. check_only);
  111. config::parseAnswer(rcode, answer);
  112. return (answer);
  113. }
  114. CtrlAgentCfgMgrPtr
  115. CtrlAgentProcess::getCtrlAgentCfgMgr() {
  116. return(boost::dynamic_pointer_cast<CtrlAgentCfgMgr>(getCfgMgr()));
  117. }
  118. } // namespace isc::agent
  119. } // namespace isc