mavtables  0.2.1
MAVLink router and firewall.
QueuedPacket.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 <limits>
19 #include <memory>
20 #include <ostream>
21 
22 #include "Packet.hpp"
23 #include "QueuedPacket.hpp"
24 
25 
26 /** Construct a queued packet.
27  *
28  * \param packet The packet to store in the queue.
29  * \param priority The priority to send the packet with, higher numbers result
30  * in a higher priority.
31  * \param ticket_number A number that should always be incremented for each
32  * queued packet created per packet queue, even if this increment causes an
33  * unsigned integer wraparound.
34  * \throws std::invalid_argument if the given packet pointer is nullptr.
35  */
37  std::shared_ptr<const Packet> packet, int priority,
38  unsigned long long ticket_number)
39  : packet_(std::move(packet)), priority_(priority),
40  ticket_number_(ticket_number)
41 {
42  if (packet_ == nullptr)
43  {
44  throw std::invalid_argument("Given packet pointer is null.");
45  }
46 }
47 
48 
49 /** Return the contained packet.
50  *
51  * \returns The contained MAVLink packet.
52  */
53 std::shared_ptr<const Packet> QueuedPacket::packet() const
54 {
55  return packet_;
56 }
57 
58 
59 /** Equality comparison.
60  *
61  * \note It should never be the case that two queued packets have the same
62  * ticket number and thus two queued packets should never be equal.
63  *
64  * \relates QueuedPacket
65  * \param lhs The left hand side queued packet.
66  * \param rhs The right hand side queued packet.
67  * \retval true if \p lhs and \p rhs have the same priority and ticket number.
68  * \retval false if \p lhs and \p rhs do not have the same priority and
69  * ticket number.
70  */
71 bool operator==(const QueuedPacket &lhs, const QueuedPacket &rhs)
72 {
73  return (lhs.priority_ == rhs.priority_) &&
74  (lhs.ticket_number_ == rhs.ticket_number_);
75 }
76 
77 
78 /** Inequality comparison.
79  *
80  * \note It should never be the case that two queued packets have the same
81  * ticket number and thus two queued packets should never be equal.
82  *
83  * \relates QueuedPacket
84  * \param lhs The left hand side queued packet.
85  * \param rhs The right hand side queued packet.
86  * \retval true if \p lhs and \p rhs do not have the same priority and ticket
87  * number.
88  * \retval false if \p lhs and \p rhs have the same priority and ticket number.
89  */
90 bool operator!=(const QueuedPacket &lhs, const QueuedPacket &rhs)
91 {
92  return (lhs.priority_ != rhs.priority_) ||
93  (lhs.ticket_number_ != rhs.ticket_number_);
94 }
95 
96 
97 /** Less than comparison.
98  *
99  * The priority is considered first, followed by the ticket number in reverse
100  * order (lower ticket number is greater).
101  *
102  * \note The ticket number is considered to be a wrapping integer and thus
103  * numbers that are within `std::numeric_limits<unsigned long
104  * long>::%max()/2` of each other are considered in the same range. In
105  * this way 0 is greater than `std::numeric_limits<unsigned long
106  * long>::%max()`. Because of this it is important that anything relying
107  * on ordering must not contain a range of ticket numbers equal to or
108  * grater than `std::numeric_limits<unsigned long long>::%max()/2`.
109  *
110  * \relates QueuedPacket
111  * \param lhs The left hand side queued packet.
112  * \param rhs The right hand side queued packet.
113  * \retval true if \p lhs is less than \p rhs.
114  * \retval false if \p lhs is not less than \p rhs.
115  */
116 bool operator<(const QueuedPacket &lhs, const QueuedPacket &rhs)
117 {
118  return (lhs.priority_ < rhs.priority_) || (lhs.priority_ == rhs.priority_ &&
119  (rhs.ticket_number_ - lhs.ticket_number_ >
120  std::numeric_limits<unsigned long long>::max() / 2));
121 }
122 
123 
124 /** Greater than comparison.
125  *
126  * The priority is considered first, followed by the ticket number in reverse
127  * order (lower ticket number is greater).
128  *
129  * \note The ticket number is considered to be a wrapping integer and thus
130  * numbers that are within `std::numeric_limits<unsigned long
131  * long>::%max()/2` of each other are considered in the same range. In
132  * this way 0 is greater than `std::numeric_limits<unsigned long
133  * long>::%max()`. Because of this it is important that anything relying
134  * on ordering must not contain a range of ticket numbers equal to or
135  * grater than `std::numeric_limits<unsigned long long>::%max()/2`.
136  *
137  * \relates QueuedPacket
138  * \param lhs The left hand side queued packet.
139  * \param rhs The right hand side queued packet.
140  * \retval true if \p lhs is less than \p rhs.
141  * \retval false if \p lhs is not less than \p rhs.
142  */
143 bool operator>(const QueuedPacket &lhs, const QueuedPacket &rhs)
144 {
145  return (lhs.priority_ > rhs.priority_) || (lhs.priority_ == rhs.priority_ &&
146  (lhs.ticket_number_ - rhs.ticket_number_ >
147  std::numeric_limits<unsigned long long>::max() / 2));
148 }
149 
150 
151 /** Less than or equal comparison.
152  *
153  * \relates QueuedPacket
154  * \param lhs The left hand side queued packet.
155  * \param rhs The right hand side queued packet.
156  * \retval true if \p lhs is less than or eqaul to \p rhs.
157  * \retval false if \p lhs is greater than \p rhs.
158  */
159 bool operator<=(const QueuedPacket &lhs, const QueuedPacket &rhs)
160 {
161  return lhs == rhs || lhs < rhs;
162 }
163 
164 
165 /** Greater than or equal comparison.
166  *
167  * \relates QueuedPacket
168  * \param lhs The left hand side queued packet.
169  * \param rhs The right hand side queued packet.
170  * \retval true if \p lhs is greater than or equal to \p rhs.
171  * \retval false if \p lhs is less than \p rhs.
172  */
173 bool operator>=(const QueuedPacket &lhs, const QueuedPacket &rhs)
174 {
175  return lhs == rhs || lhs > rhs;
176 }
177 
178 
179 /** Print the packet to the given output stream.
180  *
181  * Some examples are:
182  * - `HEARTBEAT (#1) from 16.8 (v1.0) with priority -3`
183  * - `PING (#4) from 128.4 to 16.8 (v2.0) with priority 0`
184  * - `DATA_TRANSMISSION_HANDSHAKE (#130) from 16.8 (v2.0) with priority 3`
185  * - `ENCAPSULATED_DATA (#131) from 128.4 (v2.0) with priority 1`
186  *
187  * \relates QueuedPacket
188  * \param os The output stream to print to.
189  * \param queued_packet The queued packet to print.
190  * \returns The output stream.
191  */
192 std::ostream &operator<<(std::ostream &os, const QueuedPacket &queued_packet)
193 {
194  os << *queued_packet.packet_;
195  os << " with priority " << queued_packet.priority_;
196  return os;
197 }
QueuedPacket(const QueuedPacket &other)=default
STL namespace.
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
bool operator==(const Action &lhs, const Action &rhs)
Definition: Action.cpp:154
bool operator<=(const IPAddress &lhs, const IPAddress &rhs)
Definition: IPAddress.cpp:297
bool operator!=(const Action &lhs, const Action &rhs)
Definition: Action.cpp:168
bool operator>=(const IPAddress &lhs, const IPAddress &rhs)
Definition: IPAddress.cpp:313
bool operator>(const IPAddress &lhs, const IPAddress &rhs)
Definition: IPAddress.cpp:280
bool operator<(const IPAddress &lhs, const IPAddress &rhs)
Definition: IPAddress.cpp:263
std::shared_ptr< const Packet > packet() const