mavtables  0.2.1
MAVLink router and firewall.
Connection.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 CONNECTION_HPP_
19 #define CONNECTION_HPP_
20 
21 
22 #include <memory>
23 #include <string>
24 
25 #include "AddressPool.hpp"
26 #include "config.hpp"
27 #include "Filter.hpp"
28 #include "MAVAddress.hpp"
29 #include "Packet.hpp"
30 #include "PacketQueue.hpp"
31 
32 
33 /** Represents a connection that packets can be sent over.
34  *
35  * The connection class does not actually send anything. It filters and sorts
36  * packets in a queue for sending by an \ref Interface. It also maintains a
37  * list of MAVLink addresses reachable on this connection.
38  */
40 {
41  public:
42  Connection(
43  std::string name,
44  std::shared_ptr<Filter> filter, bool mirror = false,
45  std::unique_ptr<AddressPool<>> pool =
46  std::make_unique<AddressPool<>>(),
47  std::unique_ptr<PacketQueue> queue =
48  std::make_unique<PacketQueue>());
49  // LCOV_EXCL_START
50  TEST_VIRTUAL ~Connection() = default;
51  // LCOV_EXCL_STOP
52  TEST_VIRTUAL void add_address(MAVAddress address);
53  TEST_VIRTUAL std::shared_ptr<const Packet> next_packet(
54  const std::chrono::nanoseconds &timeout =
55  std::chrono::nanoseconds(0));
56  TEST_VIRTUAL void send(std::shared_ptr<const Packet> packet);
57 
58  friend std::ostream &operator<<(
59  std::ostream &os, const Connection &connection);
60 
61  private:
62  // Variables
63  std::string name_;
64  std::shared_ptr<Filter> filter_;
65  std::unique_ptr<AddressPool<>> pool_;
66  std::unique_ptr<PacketQueue> queue_;
67  bool mirror_;
68  // Methods
69  void log_(bool accept, const Packet &packet);
70  void send_to_address_(
71  std::shared_ptr<const Packet> packet, const MAVAddress &dest);
72  void send_to_all_(std::shared_ptr<const Packet> packet);
73  void send_to_system_(
74  std::shared_ptr<const Packet> packet, unsigned int system);
75 };
76 
77 
78 std::ostream &operator<<(std::ostream &os, const Connection &connection);
79 
80 
81 #endif // CONNECTION_HPP_
TEST_VIRTUAL ~Connection()=default
Connection(std::string name, std::shared_ptr< Filter > filter, bool mirror=false, std::unique_ptr< AddressPool<>> pool=std::make_unique< AddressPool<>>(), std::unique_ptr< PacketQueue > queue=std::make_unique< PacketQueue >())
Definition: Connection.cpp:244
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
friend std::ostream & operator<<(std::ostream &os, const Connection &connection)
Definition: Connection.cpp:368
TEST_VIRTUAL std::shared_ptr< const Packet > next_packet(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds(0))
Definition: Connection.cpp:303
TEST_VIRTUAL void add_address(MAVAddress address)
Definition: Connection.cpp:281
TEST_VIRTUAL void send(std::shared_ptr< const Packet > packet)
Definition: Connection.cpp:327