|
1 | | -// |
2 | | -// RovParticipant.mm |
3 | | -// TridentVideoViewer |
4 | | -// |
5 | | -// Created by Dmitriy Borovikov on 06/09/2019. |
6 | | -// Copyright © 2019 Dmitriy Borovikov. All rights reserved. |
| 1 | +///// |
| 2 | +//// BridgedParticipant.mm |
| 3 | +/// Copyright © 2019 Dmitriy Borovikov. All rights reserved. |
7 | 4 | // |
8 | 5 |
|
9 | | -#include "RovParticipant.h" |
10 | | -#include "RovTopicListener.h" |
11 | | -#include "RovWriterListener.h" |
12 | | -#include "CustomParticipantListener.h" |
| 6 | +#include "BridgedParticipant.h" |
| 7 | +#include "BridgedReaderListener.h" |
| 8 | +#include "BridgedWriterListener.h" |
| 9 | +#include "BridgedParticipantListener.h" |
13 | 10 |
|
14 | 11 | #include <fastrtps/rtps/RTPSDomain.h> |
15 | 12 | #include <fastrtps/rtps/participant/RTPSParticipant.h> |
|
23 | 20 | #include <fastrtps/qos/ReaderQos.h> |
24 | 21 | #include <fastrtps/qos/WriterQos.h> |
25 | 22 | #include <fastrtps/log/Log.h> |
| 23 | +#include <fastrtps/transport/UDPv4TransportDescriptor.h> |
| 24 | +#include <memory> |
| 25 | +#include <arpa/inet.h> |
26 | 26 |
|
27 | 27 | using namespace eprosima::fastrtps; |
28 | 28 | using namespace eprosima::fastrtps::rtps; |
29 | 29 |
|
30 | | -RovParticipant::RovParticipant(): |
| 30 | +BridgedParticipant::BridgedParticipant(): |
31 | 31 | mp_participant(nullptr), |
32 | | -mp_listener(nullptr) |
| 32 | +mp_listener(nullptr), |
| 33 | +partitionName("*") |
33 | 34 | { |
34 | 35 | } |
35 | 36 |
|
36 | | -RovParticipant::~RovParticipant() |
| 37 | +BridgedParticipant::~BridgedParticipant() |
37 | 38 | { |
38 | | - mp_participant->stopRTPSParticipantAnnouncement(); |
39 | 39 | logInfo(ROV_PARTICIPANT, "Delete participant") |
| 40 | + mp_participant->stopRTPSParticipantAnnouncement(); |
40 | 41 | resignAll(); |
41 | 42 | RTPSDomain::removeRTPSParticipant(mp_participant); |
42 | 43 | delete mp_listener; |
43 | 44 | // RTPSDomain::stopAll(); |
44 | 45 | } |
45 | 46 |
|
46 | | -void RovParticipant::resignAll() { |
| 47 | +void BridgedParticipant::resignAll() { |
47 | 48 | for(auto it = readerList.begin(); it != readerList.end(); it++) |
48 | 49 | { |
49 | 50 | logInfo(ROV_PARTICIPANT, "Remove reader: " << it->first) |
|
63 | 64 | writerList.clear(); |
64 | 65 | } |
65 | 66 |
|
66 | | -bool RovParticipant::startRTPS() |
| 67 | +bool BridgedParticipant::createParticipant(const char* name, const char *interfaceIPv4, const char* networkAddress) |
67 | 68 | { |
68 | | - //CREATE PARTICIPANT |
69 | | - RTPSParticipantAttributes PParam; |
70 | | - PParam.builtin.use_WriterLivelinessProtocol = true; |
71 | | - PParam.builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol::SIMPLE; |
72 | | - PParam.builtin.discovery_config.leaseDuration_announcementperiod.seconds = 1; |
73 | | - PParam.builtin.discovery_config.leaseDuration.seconds = 20; |
74 | | - PParam.builtin.readerHistoryMemoryPolicy = PREALLOCATED_WITH_REALLOC_MEMORY_MODE; |
75 | | - PParam.builtin.writerHistoryMemoryPolicy = PREALLOCATED_WITH_REALLOC_MEMORY_MODE; |
76 | | - PParam.builtin.domainId = 0; |
77 | | - PParam.setName("TridentVideoViewer"); |
| 69 | + RTPSParticipantAttributes pattr; |
| 70 | + pattr.builtin.use_WriterLivelinessProtocol = true; |
| 71 | + pattr.builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol::SIMPLE; |
| 72 | + pattr.builtin.discovery_config.leaseDuration_announcementperiod.seconds = 1; |
| 73 | + pattr.builtin.discovery_config.leaseDuration.seconds = 10; |
| 74 | + pattr.builtin.discovery_config.ignoreParticipantFlags = FILTER_SAME_PROCESS; |
| 75 | + pattr.builtin.readerHistoryMemoryPolicy = PREALLOCATED_WITH_REALLOC_MEMORY_MODE; |
| 76 | + pattr.builtin.writerHistoryMemoryPolicy = PREALLOCATED_WITH_REALLOC_MEMORY_MODE; |
| 77 | + pattr.builtin.domainId = 0; |
| 78 | + pattr.setName(name); |
78 | 79 |
|
79 | | - mp_listener = new CustomParticipantListener(); |
80 | | - mp_participant = RTPSDomain::createParticipant(PParam, mp_listener); |
| 80 | + auto customTransport = std::make_shared<UDPv4TransportDescriptor>(); |
| 81 | + customTransport->sendBufferSize = 65536; |
| 82 | + customTransport->receiveBufferSize = 65536; |
| 83 | + if (interfaceIPv4 != nullptr) { |
| 84 | + customTransport->interfaceWhiteList.emplace_back(interfaceIPv4); |
| 85 | + } |
| 86 | + if (networkAddress != nullptr) { |
| 87 | + customTransport->remoteWhiteList.emplace_back(networkAddress); |
| 88 | + } |
| 89 | + pattr.userTransports.push_back(customTransport); |
| 90 | + pattr.useBuiltinTransports = false; |
| 91 | + |
| 92 | + mp_listener = new BridgedParticipantListener(); |
| 93 | + mp_participant = RTPSDomain::createParticipant(pattr, mp_listener); |
81 | 94 | if (mp_participant == nullptr) |
82 | 95 | return false; |
83 | 96 |
|
84 | 97 | return true; |
85 | 98 | } |
86 | 99 |
|
87 | | -bool RovParticipant::addReader(const char* name, |
| 100 | +bool BridgedParticipant::addReader(const char* name, |
88 | 101 | const char* dataType, |
89 | 102 | const bool keyed, |
90 | 103 | NSObject<PayloadDecoderInterface>* payloadDecoder) |
|
95 | 108 | // aready registered |
96 | 109 | return false; |
97 | 110 | } |
98 | | - //CREATE READER |
99 | 111 | ReaderAttributes readerAttributes; |
100 | 112 | readerAttributes.endpoint.topicKind = tKind; |
101 | | - auto listener = new RovTopicListener(name, payloadDecoder); |
102 | | - //CREATE READERHISTORY |
| 113 | + auto listener = new BridgedReaderListener(name, payloadDecoder); |
| 114 | + |
103 | 115 | HistoryAttributes hatt; |
104 | | - hatt.payloadMaxSize = 10000; |
105 | 116 | hatt.memoryPolicy = DYNAMIC_RESERVE_MEMORY_MODE; |
| 117 | + hatt.payloadMaxSize = 1000; |
| 118 | + hatt.initialReservedCaches = 5; |
106 | 119 | hatt.maximumReservedCaches = 0; |
107 | 120 | auto history = new ReaderHistory(hatt); |
108 | 121 | auto reader = RTPSDomain::createRTPSReader(mp_participant, readerAttributes, history, listener); |
|
111 | 124 | delete history; |
112 | 125 | return false; |
113 | 126 | } |
114 | | - |
| 127 | + |
115 | 128 | auto readerInfo = new ReaderInfo; |
116 | 129 | readerInfo->reader = reader; |
117 | 130 | readerInfo->history = history; |
118 | 131 | readerInfo->listener = listener; |
119 | 132 | readerList[topicName] = readerInfo; |
120 | | - |
| 133 | + |
121 | 134 | TopicAttributes Tatt(name, dataType, tKind); |
122 | 135 | ReaderQos Rqos; |
123 | | - Rqos.m_partition.push_back("*"); |
| 136 | + Rqos.m_partition.push_back(partitionName.c_str()); |
124 | 137 | auto rezult = mp_participant->registerReader(reader, Tatt, Rqos); |
125 | 138 | if (!rezult) { |
126 | 139 | RTPSDomain::removeRTPSReader(reader); |
|
132 | 145 | return true; |
133 | 146 | } |
134 | 147 |
|
135 | | -bool RovParticipant::removeReader(const char* name) |
| 148 | +bool BridgedParticipant::removeReader(const char* name) |
136 | 149 | { |
137 | 150 | logInfo(ROV_PARTICIPANT, "Remove reader: " << name) |
138 | 151 | auto topicName = std::string(name); |
|
147 | 160 | return true; |
148 | 161 | } |
149 | 162 |
|
150 | | -bool RovParticipant::addWriter(const char* name, |
| 163 | +bool BridgedParticipant::addWriter(const char* name, |
151 | 164 | const char* dataType, |
152 | 165 | const bool keyed) |
153 | 166 | { |
|
161 | 174 | WriterAttributes watt; |
162 | 175 | watt.endpoint.reliabilityKind = BEST_EFFORT; |
163 | 176 | watt.endpoint.topicKind = tKind; |
164 | | - auto listener = new RovWriterListener(name); |
165 | | - //CREATE WRITERHISTORY |
| 177 | + auto listener = new BridgedWriterListener(name); |
166 | 178 | HistoryAttributes hatt; |
167 | | -// hatt.payloadMaxSize = 10000; |
168 | 179 | hatt.memoryPolicy = DYNAMIC_RESERVE_MEMORY_MODE; |
| 180 | + hatt.payloadMaxSize = 1000; |
| 181 | + hatt.initialReservedCaches = 5; |
169 | 182 | hatt.maximumReservedCaches = 0; |
170 | 183 | auto history = new WriterHistory(hatt); |
171 | 184 | auto writer = RTPSDomain::createRTPSWriter(mp_participant, watt, history, listener); |
|
183 | 196 |
|
184 | 197 | TopicAttributes Tatt(name, dataType, tKind); |
185 | 198 | WriterQos Wqos; |
186 | | - Wqos.m_partition.push_back("*"); |
| 199 | + Wqos.m_partition.push_back(partitionName.c_str()); |
187 | 200 | Wqos.m_disablePositiveACKs.enabled = true; |
188 | 201 | auto rezult = mp_participant->registerWriter(writer, Tatt, Wqos); |
189 | 202 | if (!rezult) { |
|
196 | 209 | return true; |
197 | 210 | } |
198 | 211 |
|
199 | | -bool RovParticipant::removeWriter(const char* name) |
| 212 | +bool BridgedParticipant::removeWriter(const char* name) |
200 | 213 | { |
201 | 214 | logInfo(ROV_PARTICIPANT, "Remove writer: " << name) |
202 | 215 | auto topicName = std::string(name); |
|
211 | 224 | return true; |
212 | 225 | } |
213 | 226 |
|
214 | | -bool RovParticipant::send(const char* name, const uint8_t* data, uint32_t length, const void* key, uint32_t keyLength) |
| 227 | +bool BridgedParticipant::send(const char* name, const uint8_t* data, uint32_t length, const void* key, uint32_t keyLength) |
215 | 228 | { |
216 | 229 | static const octet header[] = {0, 1, 0, 0}; |
217 | 230 | auto topicName = std::string(name); |
|
231 | 244 | memcpy(instanceHandle.value, key, len); |
232 | 245 | change = writer->new_change([length]() -> uint32_t { return length+sizeof(header);}, ALIVE, instanceHandle); |
233 | 246 | if (!change) { // In the case history is full, remove some old changes |
234 | | - logWarning(ROV_PARTICIPANT, "cleaning history...") |
| 247 | + logInfo(ROV_PARTICIPANT, "cleaning history...") |
235 | 248 | writer->remove_older_changes(2); |
236 | 249 | change = writer->new_change([length]() -> uint32_t { return length+sizeof(header);}, ALIVE, instanceHandle); |
237 | 250 | } |
238 | 251 | } else { |
239 | 252 | change = writer->new_change([length]() -> uint32_t { return length+sizeof(header);}, ALIVE); |
240 | 253 | if (!change) { // In the case history is full, remove some old changes |
241 | | - logWarning(ROV_PARTICIPANT, "cleaning history...") |
| 254 | + logInfo(ROV_PARTICIPANT, "cleaning history...") |
242 | 255 | writer->remove_older_changes(2); |
243 | 256 | change = writer->new_change([length]() -> uint32_t { return length+sizeof(header);}, ALIVE); |
244 | 257 | } |
|
0 commit comments