-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
142 lines (112 loc) · 3.47 KB
/
main.cpp
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
/*!
* \author Nico Kratky
* \file main.cpp
* \copyright Copyright 2018 Nico Kratky.
* This project is released under the Boost Software License.
*
* matnr: i13083
* catnr: 13
* class: 5cHIF
*/
#include <csignal>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <thread>
#include "asio.hpp"
#include "spdlog/spdlog.h"
#include "fmt/format.h"
#include "json.hpp"
#include "clipp.h"
#include "router.h"
using nlohmann::json;
/*!
* \brief Check if a file exists
* \param filename path to file
* \return true if the file exists, else false
*/
bool file_exists(const std::string& filename) {
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
}
int main(int argc, char** argv) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
std::string input;
std::string id;
int interval{5};
bool verbose{false};
auto cli = (
clipp::value("topology file", input),
clipp::value("router id", id),
clipp::option("-i", "--interval")
.doc(fmt::format("router update interval (default: {})", interval))
& clipp::value("seconds", interval),
clipp::option("-v", "--verbose")
.doc("print additional debug information").set(verbose));
if (!clipp::parse(argc, argv, cli)) {
std::cout << clipp::make_man_page(cli, argv[0]);
return 1;
}
auto logger = spdlog::stdout_color_mt("logger");
if (verbose) {
spdlog::set_level(spdlog::level::trace);
} else {
spdlog::set_level(spdlog::level::info);
}
// start thread that listens for for SIGTERM and SIGINT
// shutdown protobuf library gracefully and exit
std::thread{[&]() {
// Block SIGTERM and SIGINT at process-level
::sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGTERM);
sigaddset(&sigset, SIGINT);
sigprocmask(SIG_BLOCK, &sigset, nullptr);
// wait for SIGTERM or SIGINT
int sig;
sigwait(&sigset, &sig);
logger->info("Bye!");
google::protobuf::ShutdownProtobufLibrary();
std::exit(0);
}}.detach();
logger->debug("Topology file: {}", input);
logger->debug("Router ID: {}", id);
if (!file_exists(input)) {
logger->error("{} does not exist.", input);
return 1;
}
std::ifstream f{input};
json j;
try {
f >> j;
} catch(json::parse_error& e) {
logger->error("Topology file malformed");
logger->error("File could not be parsed");
return 1;
}
json nodes = j["nodes"];
json links = j["links"];
// If either no nodes or links are available, abort
if (nodes.empty() || links.empty()) {
logger->error("Topology file malformed");
logger->error("Nodes or links are missing");
return 1;
}
if (nodes.count(id) != 1) {
logger->error("{} does not exist or exists multiple times.", id);
return 1;
}
logger->info("Nodes in topology: {}", nodes.size());
// extract ip_address and port from topology file
std::string ip_address{nodes[id]["ip_address"].get<std::string>()};
unsigned short port{nodes[id]["port"].get<unsigned short>()};
asio::io_context io_context;
Router router{id, ip_address, port, io_context,
std::chrono::seconds(interval)};
router.initialize_from_json(nodes, links);
logger->info("Router initialized");
// start routing algorithm
router.start();
}