mavtables  0.2.1
MAVLink router and firewall.
InterfaceThreader.hpp
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 #ifndef INTERFACETHREADER_HPP_
19 #define INTERFACETHREADER_HPP_
20 
21 
22 #include <atomic>
23 #include <memory>
24 #include <thread>
25 
26 #include "Interface.hpp"
27 
28 
29 /** A multithreaded interface runner.
30  *
31  * This class runs a given interface. It does this by calling the \ref
32  * Interface's \ref Interface::send_packet and \ref Interface::receive_packet
33  * methods repeatedly until the runner is shutdown. This is a threaded runner
34  * and thus it runs these methods in two separate threads which are managed by
35  * the \ref InterfaceThreader class.
36  *
37  * %Call the \ref shutdown method to stop running the interface and cleanup the
38  * threads.
39  *
40  * \note This is the only place where threading occurs in mavtables.
41  */
43 {
44  public:
45  enum Threads
46  {
47  START, //!< Start the interface (and worker threads) immediately.
48  DELAY_START //!< Delay starting, use \ref start to launch threads.
49  };
51  std::unique_ptr<Interface> interface,
52  std::chrono::microseconds = std::chrono::microseconds(100000),
53  Threads start_threads = InterfaceThreader::START);
54  InterfaceThreader(const InterfaceThreader &other) = delete;
55  InterfaceThreader(InterfaceThreader &&other) = delete;
57  void start();
58  void shutdown();
59  InterfaceThreader &operator=(const InterfaceThreader &other) = delete;
61 
62  private:
63  // Variables
64  std::unique_ptr<Interface> interface_;
65  std::thread tx_thread_;
66  std::thread rx_thread_;
67  std::chrono::microseconds timeout_;
68  std::atomic<bool> running_;
69  // Methods
70  void tx_runner_();
71  void rx_runner_();
72 };
73 
74 
75 #endif // INTERFACETHREADER_HPP_
Start the interface (and worker threads) immediately.
InterfaceThreader(std::unique_ptr< Interface > interface, std::chrono::microseconds=std::chrono::microseconds(100000), Threads start_threads=InterfaceThreader::START)
Delay starting, use start to launch threads.
InterfaceThreader & operator=(const InterfaceThreader &other)=delete