Line data Source code
1 : #ifndef OPENDDS_RTPSRELAYLIB_UTILITY_H
2 : #define OPENDDS_RTPSRELAYLIB_UTILITY_H
3 :
4 : #include "RelayC.h"
5 :
6 : #include "dds/DCPS/GuidUtils.h"
7 : #include "dds/DCPS/TimeDuration.h"
8 :
9 : #include <ace/INET_Addr.h>
10 :
11 : #include <cstring>
12 : #include <set>
13 : #include <sstream>
14 : #include <string>
15 : #include <unordered_set>
16 :
17 : namespace RtpsRelay {
18 :
19 : typedef std::set<std::string> StringSet;
20 :
21 1 : inline std::string guid_to_string(const OpenDDS::DCPS::GUID_t& a_guid)
22 : {
23 1 : std::stringstream ss;
24 1 : ss << a_guid;
25 2 : return ss.str();
26 1 : }
27 :
28 : enum Port {
29 : SPDP,
30 : SEDP,
31 : DATA
32 : };
33 :
34 : enum class MessageType {
35 : Unknown,
36 : Rtps,
37 : Stun,
38 : };
39 :
40 : struct AddrPort {
41 : ACE_INET_Addr addr;
42 : Port port;
43 :
44 1 : AddrPort() : port(SPDP) {}
45 17 : AddrPort(const ACE_INET_Addr& a, Port p) : addr(a), port(p) {}
46 :
47 11 : bool operator==(const AddrPort& other) const
48 : {
49 11 : return addr == other.addr && port == other.port;
50 : }
51 :
52 8 : bool operator!=(const AddrPort& other) const
53 : {
54 8 : return !(*this == other);
55 : }
56 :
57 5 : bool operator<(const AddrPort& other) const
58 : {
59 5 : return addr < other.addr || (addr == other.addr && port < other.port);
60 : }
61 : };
62 :
63 : struct GuidAddr {
64 : OpenDDS::DCPS::GUID_t guid;
65 : AddrPort address;
66 :
67 : GuidAddr() : guid(OpenDDS::DCPS::GUID_UNKNOWN) {}
68 11 : GuidAddr(const OpenDDS::DCPS::GUID_t& a_guid, const AddrPort& a_address)
69 11 : : guid(a_guid)
70 11 : , address(a_address)
71 11 : {}
72 :
73 1 : bool operator==(const GuidAddr& other) const
74 : {
75 1 : return guid == other.guid && address == other.address;
76 : }
77 :
78 6 : bool operator!=(const GuidAddr& other) const
79 : {
80 6 : return guid != other.guid || address != other.address;
81 : }
82 :
83 3 : bool operator<(const GuidAddr& other) const
84 : {
85 3 : if (guid != other.guid) {
86 : static const OpenDDS::DCPS::GUID_tKeyLessThan gc;
87 1 : return gc(guid, other.guid);
88 : }
89 2 : return address < other.address;
90 : }
91 : };
92 :
93 3 : inline bool operator==(const EntityId_t& x, const EntityId_t& y)
94 : {
95 3 : return std::memcmp(&x, &y, sizeof(x)) == 0;
96 : }
97 :
98 1 : inline bool operator!=(const EntityId_t& x, const EntityId_t& y)
99 : {
100 1 : return std::memcmp(&x, &y, sizeof(x)) != 0;
101 : }
102 :
103 2 : inline void assign(EntityId_t& eid, const OpenDDS::DCPS::EntityId_t& a_eid)
104 : {
105 2 : std::memcpy(&eid._entityKey[0], a_eid.entityKey, sizeof(a_eid.entityKey));
106 2 : eid.entityKind(a_eid.entityKind);
107 2 : }
108 :
109 4 : inline bool operator==(const GUID_t& x, const GUID_t& y)
110 : {
111 4 : return std::memcmp(&x, &y, sizeof(x)) == 0;
112 : }
113 :
114 1 : inline bool operator!=(const GUID_t& x, const GUID_t& y)
115 : {
116 1 : return std::memcmp(&x, &y, sizeof(x)) != 0;
117 : }
118 :
119 1 : inline void assign(GUID_t& guid, const OpenDDS::DCPS::GUID_t& a_guid)
120 : {
121 1 : std::memcpy(&guid._guidPrefix[0], a_guid.guidPrefix, sizeof(a_guid.guidPrefix));
122 1 : assign(guid.entityId(), a_guid.entityId);
123 1 : }
124 :
125 1 : inline Duration_t time_diff_to_duration(const OpenDDS::DCPS::TimeDuration& d)
126 : {
127 1 : Duration_t duration;
128 1 : const auto x = d.to_dds_duration();
129 1 : duration.sec(x.sec);
130 1 : duration.nanosec(x.nanosec);
131 1 : return duration;
132 : }
133 :
134 3 : inline bool operator==(const Duration_t& x, const Duration_t& y)
135 : {
136 3 : return x.sec() == y.sec() && x.nanosec() == y.nanosec();
137 : }
138 :
139 1 : inline bool operator!=(const Duration_t& x, const Duration_t& y)
140 : {
141 1 : return x.sec() != y.sec() || x.nanosec() != y.nanosec();
142 : }
143 :
144 2 : inline bool operator<(const Duration_t& x, const Duration_t& y)
145 : {
146 2 : if (x.sec() != y.sec()) {
147 1 : return x.sec() < y.sec();
148 : }
149 1 : return x.nanosec() < y.nanosec();
150 : }
151 :
152 1 : inline OpenDDS::DCPS::GUID_t relay_guid_to_rtps_guid(const GUID_t& a_guid)
153 : {
154 : OpenDDS::DCPS::GUID_t retval;
155 1 : std::memcpy(&retval, &a_guid, sizeof(OpenDDS::DCPS::GUID_t));
156 1 : return retval;
157 : }
158 :
159 1 : inline GUID_t rtps_guid_to_relay_guid(const OpenDDS::DCPS::GUID_t& a_guid)
160 : {
161 1 : GUID_t retval;
162 1 : std::memcpy(&retval.guidPrefix(), a_guid.guidPrefix, sizeof(a_guid.guidPrefix));
163 1 : std::memcpy(&retval.entityId().entityKey(), a_guid.entityId.entityKey, sizeof(a_guid.entityId.entityKey));
164 1 : retval.entityId().entityKind(a_guid.entityId.entityKind);
165 1 : return retval;
166 : }
167 :
168 : struct GuidHash {
169 80 : std::size_t operator() (const OpenDDS::DCPS::GUID_t& guid) const
170 : {
171 : return
172 80 : (static_cast<std::size_t>(guid.guidPrefix[0]) << 15) ^
173 80 : (static_cast<std::size_t>(guid.guidPrefix[1]) << 14) ^
174 80 : (static_cast<std::size_t>(guid.guidPrefix[2]) << 13) ^
175 80 : (static_cast<std::size_t>(guid.guidPrefix[3]) << 12) ^
176 80 : (static_cast<std::size_t>(guid.guidPrefix[4]) << 11) ^
177 80 : (static_cast<std::size_t>(guid.guidPrefix[5]) << 10) ^
178 80 : (static_cast<std::size_t>(guid.guidPrefix[6]) << 9) ^
179 80 : (static_cast<std::size_t>(guid.guidPrefix[7]) << 8) ^
180 80 : (static_cast<std::size_t>(guid.guidPrefix[8]) << 7) ^
181 80 : (static_cast<std::size_t>(guid.guidPrefix[9]) << 6) ^
182 80 : (static_cast<std::size_t>(guid.guidPrefix[10]) << 5) ^
183 80 : (static_cast<std::size_t>(guid.guidPrefix[11]) << 4) ^
184 80 : (static_cast<std::size_t>(guid.entityId.entityKey[0]) << 3) ^
185 80 : (static_cast<std::size_t>(guid.entityId.entityKey[1]) << 2) ^
186 80 : (static_cast<std::size_t>(guid.entityId.entityKey[2]) << 1) ^
187 80 : (static_cast<std::size_t>(guid.entityId.entityKind) << 0);
188 : }
189 : };
190 : typedef std::unordered_set<OpenDDS::DCPS::GUID_t, GuidHash> GuidSet;
191 :
192 : inline GuidSet relay_guids_to_set(const RtpsRelay::GuidSequence& seq)
193 : {
194 : GuidSet set;
195 : for (const auto& guid : seq) {
196 : set.insert(relay_guid_to_rtps_guid(guid));
197 : }
198 : return set;
199 : }
200 :
201 : }
202 :
203 : #endif // RTPSRELAY_UTILITY_H_
|