mavtables  0.2.1
MAVLink router and firewall.
App.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <chrono>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include <signal.h>
24 
25 #include "App.hpp"
26 #include "Interface.hpp"
27 #include "InterfaceThreader.hpp"
28 
29 
30 using namespace std::chrono_literals;
31 
32 
33 /** Construct mavtables application from a vector of interfaces.
34  *
35  * Neither the interfaces, nor the application will be started until the \ref
36  * run method is called.
37  *
38  * \param interfaces A vector of interfaces.
39  */
40 App::App(std::vector<std::unique_ptr<Interface>> interfaces)
41 {
42  // Create threader for each interface.
43  for (auto &interface : interfaces)
44  {
45  threaders_.push_back(
46  std::make_unique<InterfaceThreader>(
47  std::move(interface), 100ms, InterfaceThreader::DELAY_START));
48  }
49 }
50 
51 
52 /** Start the application.
53  *
54  * This starts listening on all interfaces.
55  *
56  * \throws std::system_error if an error is generated while waiting for Ctrl+C.
57  * \throws std::runtime_error if run on Microsoft Windows.
58  */
59 void App::run()
60 {
61  // Start interfaces.
62  for (auto &interface : threaders_)
63  {
64  interface->start();
65  }
66 
67  #ifdef UNIX
68  // Wait for SIGINT (Ctrl+C).
69  sigset_t waitset;
70  sigemptyset(&waitset);
71  sigaddset(&waitset, SIGINT);
72  sigprocmask(SIG_BLOCK, &waitset, nullptr);
73  int sig;
74 
75  if (sigwait(&waitset, &sig) < 0)
76  {
77  throw std::system_error(std::error_code(errno, std::system_category()));
78  }
79 
80  #elif WINDOWS
81  throw std::runtime_error("Microsoft Windows is not currently supported.")
82  #endif
83 
84  // Shutdown interfaces.
85  for (auto &interface : threaders_)
86  {
87  interface->shutdown();
88  }
89 }
Delay starting, use start to launch threads.
App(std::vector< std::unique_ptr< Interface >> interfaces)
Definition: App.cpp:40
void run()
Definition: App.cpp:59