Leosac  0.8.0
Open Source Access Control
CoreAPI.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "CoreAPI.hpp"
21 #include "Scheduler.hpp"
23 #include "kernel.hpp"
24 #include "tools/GenGuid.h"
25 #include "tools/Mail.hpp"
27 #include <chrono>
28 
29 using namespace Leosac;
30 
32  : kernel_(k)
33 {
34 }
35 
36 uint64_t CoreAPI::config_version() const
37 {
38  auto t = std::make_shared<Tasks::GetLocalConfigVersion>(kernel_);
39  kernel_.core_utils()->scheduler().enqueue(t, TargetThread::MAIN);
40  t->wait();
41  ASSERT_LOG(t->succeed(), "Tasks::GetLocalConfigVersion failed.");
42  return t->config_version_;
43 }
44 
45 boost::property_tree::ptree CoreAPI::kernel_config() const
46 {
47  boost::property_tree::ptree out;
48  auto task = Tasks::GenericTask::build([&]() {
49  out = kernel_.config_manager().kconfig();
50  return true;
51  });
52  kernel_.core_utils()->scheduler().enqueue(task, TargetThread::MAIN);
53  task->wait();
54  ASSERT_LOG(task->succeed(),
55  "Retrieving `kernel configuration` from CoreAPI failed.");
56 
57  return out;
58 }
59 
60 uint64_t CoreAPI::uptime() const
61 {
62  using namespace std::chrono;
63  uint64_t out;
64  auto task = Tasks::GenericTask::build([&]() {
65  auto now = steady_clock::now();
66  out = duration_cast<seconds>(now - kernel_.start_time()).count();
67  return true;
68  });
69  kernel_.core_utils()->scheduler().enqueue(task, TargetThread::MAIN);
70  task->wait();
71  ASSERT_LOG(task->succeed(), "Retrieving `uptime` from CoreAPI failed.");
72 
73  return out;
74 }
75 
76 std::string CoreAPI::instance_name() const
77 {
78  std::string out;
79  auto task = Tasks::GenericTask::build([&]() {
81  return true;
82  });
83  kernel_.core_utils()->scheduler().enqueue(task, TargetThread::MAIN);
84  task->wait();
85  ASSERT_LOG(task->succeed(), "Retrieving `instance_name` from CoreAPI failed.");
86 
87  return out;
88 }
89 
90 std::vector<std::string> CoreAPI::modules_names() const
91 {
92  std::vector<std::string> out;
93  auto task = Tasks::GenericTask::build([&]() {
95  return true;
96  });
97  kernel_.core_utils()->scheduler().enqueue(task, TargetThread::MAIN);
98  task->wait();
99  ASSERT_LOG(task->succeed(), "Retrieving `modules names` from CoreAPI failed.");
100 
101  return out;
102 }
103 
105 {
106  auto task = Tasks::GenericTask::build([&]() {
108  return true;
109  });
110  kernel_.core_utils()->scheduler().enqueue(task, TargetThread::MAIN);
111  task->wait();
112  ASSERT_LOG(task->succeed(), "Requesting server restart failed.");
113 }
GlobalRegistry.hpp
Leosac::Kernel::restart_later
void restart_later()
Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration.
Definition: kernel.cpp:441
Leosac::CoreAPI::kernel_config
boost::property_tree::ptree kernel_config() const
Retrieve the property tree describing the Leosac's kernel configuration.
Definition: CoreAPI.cpp:45
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::CoreAPI::restart_server
void restart_server() const
Request that Leosac restarts.
Definition: CoreAPI.cpp:104
Scheduler.hpp
Mail.hpp
GetLocalConfigVersion.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
kernel.hpp
Leosac::Kernel::start_time
const std::chrono::steady_clock::time_point start_time() const
Return the time point at which Leosac started.
Definition: kernel.cpp:452
GenGuid.h
CoreAPI.hpp
Leosac::Tasks::GenericTask::build
static TaskPtr build(const Callable &callable)
Definition: GenericTask.hpp:41
Leosac::CoreAPI::config_version
uint64_t config_version() const
Retrieve the local configuration version.
Definition: CoreAPI.cpp:36
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::TargetThread::MAIN
@ MAIN
Leosac::CoreAPI::CoreAPI
CoreAPI(Kernel &k)
Definition: CoreAPI.cpp:31
Leosac::CoreAPI::uptime
uint64_t uptime() const
Returns the uptime, in seconds.
Definition: CoreAPI.cpp:60
Leosac::CoreAPI::kernel_
Kernel & kernel_
Definition: CoreAPI.hpp:84
Leosac::Kernel::core_utils
CoreUtilsPtr core_utils()
Returns a (smart) pointer to the core utils: some thread-safe utilities.
Definition: kernel.cpp:447
Leosac::Kernel::module_manager
const ModuleManager & module_manager() const
Returns a reference to the module manager object (const version).
Definition: kernel.cpp:405
Leosac::CoreAPI::instance_name
std::string instance_name() const
Returns the instance_name of Leosac.
Definition: CoreAPI.cpp:76
Leosac::Kernel::config_manager
ConfigManager & config_manager()
Retrieve a reference to the configuration manager object.
Definition: kernel.cpp:436
Leosac::CoreAPI::modules_names
std::vector< std::string > modules_names() const
Retrieve the names of all enabled modules.
Definition: CoreAPI.cpp:90