1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file Subscriber.cpp
*
*/
#include <atomic>
#include <future>
#include <string>
#include <utility>
#include <asio.hpp>
#include <fastdds/dds/core/ReturnCode.hpp>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/subscriber/SubscriberListener.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicData.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicType.hpp>
#include <fastdds/dds/xtypes/type_representation/ITypeObjectRegistry.hpp>
#include <fastdds/dds/xtypes/type_representation/TypeObject.hpp>
using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::dds::xtypes;
using namespace eprosima::fastdds::rtps;
class ParListener : public DomainParticipantListener
{
public:
ParListener()
{
remote_names_ = is_worth_a_type_.get_future();
}
/**
* This method is called when a new Participant is discovered, or a previously discovered participant changes its QOS or is removed.
* @param p Pointer to the Participant
* @param info DiscoveryInfo.
*/
void on_participant_discovery(
DomainParticipant* /*participant*/,
ParticipantDiscoveryStatus status,
const ParticipantBuiltinTopicData& info,
bool& /*should_be_ignored*/) override
{
if (status == ParticipantDiscoveryStatus::DISCOVERED_PARTICIPANT)
{
std::cout << "Subscriber participant " << //participant->getGuid() <<
" discovered participant " << info.guid << std::endl;
}
else if (status == ParticipantDiscoveryStatus::CHANGED_QOS_PARTICIPANT)
{
std::cout << "Subscriber participant " << //participant->getGuid() <<
" detected changes on participant " << info.guid << std::endl;
}
else if (status == ParticipantDiscoveryStatus::REMOVED_PARTICIPANT)
{
std::cout << "Subscriber participant " << //participant->getGuid() <<
" removed participant " << info.guid << std::endl;
}
else if (status == ParticipantDiscoveryStatus::DROPPED_PARTICIPANT)
{
std::cout << "Subscriber participant " << //participant->getGuid() <<
" dropped participant " << info.guid << std::endl;
}
}
#if HAVE_SECURITY
void onParticipantAuthentication(
DomainParticipant* /*participant*/,
ParticipantAuthenticationInfo&& info) override
{
if (ParticipantAuthenticationInfo::AUTHORIZED_PARTICIPANT == info.status)
{
std::cout << "Subscriber participant " << //participant->guid() <<
" authorized participant " << info.guid << std::endl;
}
else
{
std::cout << "Subscriber participant " << //participant->guid() <<
" unauthorized participant " << info.guid << std::endl;
}
}
#endif // if HAVE_SECURITY
using topic_type_names = std::pair<std::string, std::string>;
std::future<topic_type_names> remote_names_;
private:
using DomainParticipantListener::on_participant_discovery;
std::promise<topic_type_names> is_worth_a_type_;
};
class SubListener : public SubscriberListener
{
public:
void on_subscription_matched(
DataReader* /*reader*/,
const SubscriptionMatchedStatus& info) override
{
if (info.current_count_change == 1)
{
std::cout << "Subscriber matched with publisher " << info.last_publication_handle << std::endl;
}
else if (info.current_count_change == -1)
{
std::cout << "Subscriber unmatched with publisher " << info.last_publication_handle << std::endl;
}
else
{
std::cout << info.current_count_change
<< " is not a valid value for SubscriptionMatchedStatus current count change" << std::endl;
}
}
void on_liveliness_changed(
DataReader* /*reader*/,
const eprosima::fastdds::dds::LivelinessChangedStatus& status) override
{
if (status.alive_count_change == 1)
{
std::cout << "Publisher recovered liveliness" << std::endl;
}
else if (status.not_alive_count_change == 1)
{
std::cout << "Publisher lost liveliness" << std::endl;
run_ = false;
}
}
std::atomic_bool run_ = { true };
};
int main(
int argc,
char** argv)
{
int result = 0;
int arg_count = 1;
bool notexit = false;
uint32_t seed = 7800;
uint32_t samples = 4;
std::string magic;
while (arg_count < argc)
{
if (strcmp(argv[arg_count], "--notexit") == 0)
{
notexit = true;
}
else if (strcmp(argv[arg_count], "--seed") == 0)
{
if (++arg_count >= argc)
{
std::cout << "--seed expects a parameter" << std::endl;
return -1;
}
seed = strtol(argv[arg_count], nullptr, 10);
}
else if (strcmp(argv[arg_count], "--samples") == 0)
{
if (++arg_count >= argc)
{
std::cout << "--samples expects a parameter" << std::endl;
return -1;
}
samples = strtol(argv[arg_count], nullptr, 10);
}
else if (strcmp(argv[arg_count], "--magic") == 0)
{
if (++arg_count >= argc)
{
std::cout << "--magic expects a parameter" << std::endl;
return -1;
}
magic = argv[arg_count];
}
else if (strcmp(argv[arg_count], "--xmlfile") == 0)
{
std::cout << "--xmlfile option isn't implemented yet." << std::endl;
if (++arg_count >= argc)
{
std::cout << "--xmlfile expects a parameter" << std::endl;
return -1;
}
}
++arg_count;
}
ParListener participant_listener;
DomainParticipant* participant = nullptr;
SubListener listener;
Subscriber* subscriber = nullptr;
Topic* topic = nullptr;
DataReader* reader = nullptr;
try
{
DomainParticipantQos participant_qos;
StatusMask participant_mask = StatusMask::none();
participant =
DomainParticipantFactory::get_instance()->create_participant(seed % 230, participant_qos,
&participant_listener, participant_mask);
if (participant == nullptr)
{
std::cout << "Error creating subscriber participant" << std::endl;
throw 1;
}
StatusMask mask = StatusMask::subscription_matched()
<< StatusMask::liveliness_changed();
// Create the Subscriber
subscriber = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, &listener, mask);
if (subscriber == nullptr)
{
std::cout << "Error creating subscriber" << std::endl;
throw 1;
}
// Get the dynamic type factory
auto remote_names = participant_listener.remote_names_.get();
auto& topic_name = remote_names.first;
auto& type_name = remote_names.second;
DynamicType::_ref_type type;
{
TypeObjectPair type_objects;
if (RETCODE_OK != DomainParticipantFactory::get_instance()->type_object_registry().get_type_objects(
type_name, type_objects))
{
std::cout << "ERROR: TypeObject cannot be retrieved for type: "
<< type_name << std::endl;
throw 1;
}
type = DynamicTypeBuilderFactory::get_instance()->create_type_w_type_object(
type_objects.complete_type_object)->build();
if (!type)
{
std::cout << "ERROR: DynamicType cannot be created for type: " << type_name << std::endl;
throw 1;
}
}
// Create the Topic & DataReader
TopicDescription* desc = participant->lookup_topicdescription(topic_name);
if (desc != nullptr)
{
std::cout << "ERROR: Cannot create Topic with name " << topic_name
<< " - Topic already exists" << std::endl;
topic = static_cast<Topic*>(desc);
}
else
{
topic = participant->create_topic( topic_name, type_name, TOPIC_QOS_DEFAULT);
if (topic == nullptr)
{
std::cout << "ERROR: Could not create topic " << topic_name << std::endl;
throw 1;
}
}
DataReaderQos qos;
reader = subscriber->create_datareader(topic, qos, nullptr);
if (reader == nullptr)
{
std::cout << "ERROR: Could not create reader for topic " << topic_name << std::endl;
throw 1;
}
// samples received
uint32_t number_samples = 0;
while ((notexit || number_samples < samples ) && listener.run_)
{
// loop taking samples
DynamicPubSubType pst(type);
DynamicData* sample {static_cast<DynamicData*>(pst.createData())};
eprosima::fastdds::dds::SampleInfo info;
if (RETCODE_OK == reader->take_next_sample(sample, &info))
{
if (info.valid_data)
{
std::string message;
uint32_t index;
octet count;
++number_samples;
sample->get_string_value(message, 0);
sample->get_uint32_value(index, 1);
DynamicData::_ref_type inner {sample->loan_value(2)};
inner->get_byte_value(count, 0);
sample->return_loaned_value(inner);
std::cout << "Received sample: index(" << index << "), message("
<< message << "), inner_count(" << std::hex << (uint32_t)count << ")" << std::endl;
}
}
}
}
catch (int res)
{
result = res;
}
if (participant != nullptr)
{
if (RETCODE_OK != participant->delete_contained_entities() && !result)
{
std::cout << "ERROR: precondition not met on participant entities removal" << std::endl;
result = 1;
}
DomainParticipantFactory::get_instance()->delete_participant(participant);
}
return result;
}
|