You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I modified part of example Fast-DDS/Fast-DDS/examples/C++/HelloWorldExample for publishe-subscribe test.
Found sometimes subscriber are slow to match and slow to print the first message.
Steps to reproduce
Deploy 1 publisher and 1 subscriber, run HelloWorldExample.
Estimate the time(unit:us) spent in subscriber match and received first message(after matched)
Restart the subscriber process several times, compare those times.
Fast DDS version/commit and platform
Fast-dds VERSION 2.5.0
Fast-cdr VERSION 1.0.23
platform: ubuntu 18.04
Modified codes:
HelloWorld_main.cpp
// 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 HelloWorld_main.cpp **/
#include"HelloWorldPublisher.h"
#include"HelloWorldSubscriber.h"
#include<fastrtps/Domain.h>
#include<fastrtps/log/Log.h>usingnamespaceeprosima;usingnamespacefastrtps;usingnamespacertps;intmain(int argc, char** argv)
{
std::cout << "Starting "<< std::endl;
int type = 1;
int count = 0;
longsleep = 10;
if(argc > 1)
{
if(strcmp(argv[1],"publisher")==0)
{
type = 1;
if (argc >= 3)
{
count = atoi(argv[2]);
if (argc == 4)
{
sleep = atoi(argv[3]);
}
}
}
elseif(strcmp(argv[1],"subscriber")==0)
type = 2;
}
else
{
std::cout << "publisher OR subscriber argument needed" << std::endl;
Log::Reset();
return0;
}
switch(type)
{
case1:
{
HelloWorldPublisher mypub;
if(mypub.init())
{
mypub.run(count, sleep);
}
break;
}
case2:
{
HelloWorldSubscriber mysub;
if(mysub.init())
{
mysub.run();
}
break;
}
}
Domain::stopAll();
Log::Reset();
return0;
}
HelloWorldPublisher.cpp
// 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 HelloWorldPublisher.cpp **/
#include"HelloWorldPublisher.h"
#include<fastrtps/participant/Participant.h>
#include<fastrtps/attributes/ParticipantAttributes.h>
#include<fastrtps/attributes/PublisherAttributes.h>
#include<fastrtps/publisher/Publisher.h>
#include<fastrtps/Domain.h>
#include<thread>usingnamespaceeprosima::fastrtps;usingnamespaceeprosima::fastrtps::rtps;HelloWorldPublisher::HelloWorldPublisher()
: mp_participant(nullptr)
, mp_publisher(nullptr)
{
}
boolHelloWorldPublisher::init()
{
m_Hello.index(0);
m_Hello.message("HelloWorld");
ParticipantAttributes PParam;
PParam.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
PParam.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
PParam.rtps.builtin.discovery_config.leaseDuration = 2;
PParam.rtps.builtin.discovery_config.leaseDuration_announcementperiod = eprosima::fastrtps::Duration_t(1, 0);
PParam.rtps.setName("Participant_pub");
mp_participant = Domain::createParticipant(PParam);
if (mp_participant == nullptr)
{
returnfalse;
}
//REGISTER THE TYPEDomain::registerType(mp_participant, &m_type);
//CREATE THE PUBLISHER
PublisherAttributes Wparam;
Wparam.topic.topicKind = NO_KEY;
Wparam.topic.topicDataType = "HelloWorld";
Wparam.topic.topicName = "HelloWorldTopic";
Wparam.topic.historyQos.kind = KEEP_LAST_HISTORY_QOS;
Wparam.topic.historyQos.depth = 30;
Wparam.topic.resourceLimitsQos.max_samples = 50;
Wparam.topic.resourceLimitsQos.allocated_samples = 20;
Wparam.times.heartbeatPeriod.seconds = 0;
Wparam.times.heartbeatPeriod.nanosec = 500 * 1000 * 1000;
Wparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
Wparam.qos.m_durability.kind = VOLATILE_DURABILITY_QOS;
mp_publisher = Domain::createPublisher(mp_participant, Wparam, (PublisherListener*)&m_listener);
if (mp_publisher == nullptr)
{
returnfalse;
}
returntrue;
}
HelloWorldPublisher::~HelloWorldPublisher()
{
// TODO Auto-generated destructor stubDomain::removeParticipant(mp_participant);
}
voidHelloWorldPublisher::PubListener::onPublicationMatched(
Publisher* /*pub*/,
MatchingInfo& info)
{
if (info.status == MATCHED_MATCHING)
{
n_matched++;
firstConnected = true;
std::cout << "Publisher matched" << std::endl;
}
else
{
n_matched--;
std::cout << "Publisher unmatched" << std::endl;
}
}
voidHelloWorldPublisher::runThread(
uint32_t samples,
uint32_t sleep)
{
if (samples == 0)
{
while (!stop)
{
if (publish(false))
{
// std::cout << "Message: " << m_Hello.message() << " with index: " << m_Hello.index() << " SENT" <<std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
}
}
else
{
for (uint32_t i = 0; i < samples; ++i)
{
if (!publish())
{
--i;
}
else
{
std::cout << "Message: " << m_Hello.message() << " with index: " << m_Hello.index() << " SENT" <<
std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
}
}
}
voidHelloWorldPublisher::run(
uint32_t samples,
uint32_t sleep)
{
stop = false;
std::thread thread(&HelloWorldPublisher::runThread, this, samples, sleep);
// if (samples == 0)// {// std::cout << "Publisher running. Please press enter to stop the Publisher at any time." << std::endl;// std::cin.ignore();// stop = true;// }// else// {// std::cout << "Publisher running " << samples << " samples." << std::endl;// }
thread.join();
}
boolHelloWorldPublisher::publish(
bool waitForListener)
{
if (m_listener.n_matched > 0)
{
m_Hello.index(m_Hello.index() + 1);
mp_publisher->write((void*)&m_Hello);
returntrue;
}
returnfalse;
}
HelloWorldSubscriber.h
// 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 HelloWorldSubscriber.h **/
#ifndef HELLOWORLDSUBSCRIBER_H_
#defineHELLOWORLDSUBSCRIBER_H_
#include"HelloWorldPubSubTypes.h"
#include<fastrtps/fastrtps_fwd.h>
#include<fastrtps/attributes/SubscriberAttributes.h>
#include<fastrtps/subscriber/SubscriberListener.h>
#include<fastrtps/subscriber/SampleInfo.h>
#include<chrono>
#include"HelloWorld.h"classHelloWorldSubscriber
{
public:HelloWorldSubscriber();
virtual~HelloWorldSubscriber();
//!Initialize the subscriberboolinit();
//!RUN the subscribervoidrun();
//!Run the subscriber until number samples have been received.voidrun(
uint32_t number);
private:
eprosima::fastrtps::Participant* mp_participant;
eprosima::fastrtps::Subscriber* mp_subscriber;
public:classSubListener : publiceprosima::fastrtps::SubscriberListener
{
public:SubListener()
: n_matched(0)
, n_samples(0)
{
first_matched = false;
first_received = false;
}
~SubListener()
{
}
voidonSubscriptionMatched(
eprosima::fastrtps::Subscriber* sub,
eprosima::fastrtps::rtps::MatchingInfo& info);
voidonNewDataMessage(
eprosima::fastrtps::Subscriber* sub);
HelloWorld m_Hello;
eprosima::fastrtps::SampleInfo_t m_info;
int n_matched;
uint32_t n_samples;
std::chrono::microseconds start_timestamp;
std::chrono::microseconds first_matched_timestamp;
std::chrono::microseconds first_receive_timestamp;
bool first_matched;
bool first_received;
} m_listener;
private:
HelloWorldPubSubType m_type;
};
#endif/* HELLOWORLDSUBSCRIBER_H_ */
HelloWorldSubscriber.cpp
// 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 HelloWorldSubscriber.cpp **/
#include"HelloWorldSubscriber.h"
#include<fastrtps/participant/Participant.h>
#include<fastrtps/attributes/ParticipantAttributes.h>
#include<fastrtps/attributes/SubscriberAttributes.h>
#include<fastrtps/subscriber/Subscriber.h>
#include<fastrtps/Domain.h>usingnamespaceeprosima::fastrtps;usingnamespaceeprosima::fastrtps::rtps;HelloWorldSubscriber::HelloWorldSubscriber()
: mp_participant(nullptr)
, mp_subscriber(nullptr)
{
}
boolHelloWorldSubscriber::init()
{
ParticipantAttributes PParam;
PParam.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
PParam.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
PParam.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
PParam.rtps.builtin.discovery_config.leaseDuration = 2;
PParam.rtps.builtin.discovery_config.leaseDuration_announcementperiod = eprosima::fastrtps::Duration_t(1, 0);
PParam.rtps.setName("Participant_sub");
mp_participant = Domain::createParticipant(PParam);
if (mp_participant == nullptr)
{
returnfalse;
}
//REGISTER THE TYPEDomain::registerType(mp_participant, &m_type);
//CREATE THE SUBSCRIBER
SubscriberAttributes Rparam;
Rparam.topic.topicKind = NO_KEY;
Rparam.topic.topicDataType = "HelloWorld";
Rparam.topic.topicName = "HelloWorldTopic";
Rparam.topic.historyQos.kind = KEEP_LAST_HISTORY_QOS;
Rparam.topic.historyQos.depth = 30;
Rparam.topic.resourceLimitsQos.max_samples = 50;
Rparam.topic.resourceLimitsQos.allocated_samples = 20;
Rparam.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
Rparam.qos.m_durability.kind = VOLATILE_DURABILITY_QOS;
mp_subscriber = Domain::createSubscriber(mp_participant, Rparam, (SubscriberListener*)&m_listener);
if (mp_subscriber == nullptr)
{
returnfalse;
}
returntrue;
}
HelloWorldSubscriber::~HelloWorldSubscriber()
{
// TODO Auto-generated destructor stubDomain::removeParticipant(mp_participant);
}
voidHelloWorldSubscriber::SubListener::onSubscriptionMatched(
Subscriber* /*sub*/,
MatchingInfo& info)
{
if (info.status == MATCHED_MATCHING)
{
n_matched++;
std::cout << "Subscriber matched" << std::endl;
if(!first_matched){
first_matched_timestamp = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
first_matched = true;
auto matched_cost = first_matched_timestamp.count() - start_timestamp.count();
std::cout << "first match cost(us): " << matched_cost << std::endl;
}
}
else
{
n_matched--;
std::cout << "Subscriber unmatched" << std::endl;
}
}
voidHelloWorldSubscriber::SubListener::onNewDataMessage(
Subscriber* sub)
{
if (sub->takeNextData((void*)&m_Hello, &m_info))
{
if (m_info.sampleKind == ALIVE)
{
this->n_samples++;
// Print your structure data here.// std::cout << "Message " << m_Hello.message() << " " << m_Hello.index() << " RECEIVED" << std::endl;if(!first_received){
first_receive_timestamp=std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
auto first_received_cost = first_receive_timestamp.count() - first_matched_timestamp.count();
std::cout << "first received cost(us): " << first_received_cost << std::endl;
first_received = true;
}
}
}
}
voidHelloWorldSubscriber::run()
{
std::cout << "Subscriber running. Please press enter to stop the Subscriber" << std::endl;
m_listener.start_timestamp = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
std::cin.ignore();
}
voidHelloWorldSubscriber::run(
uint32_t number)
{
std::cout << "Subscriber running until " << number << "samples have been received" << std::endl;
while (number > this->m_listener.n_samples)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I modified part of example
Fast-DDS/Fast-DDS/examples/C++/HelloWorldExample
for publishe-subscribe test.Found sometimes subscriber are slow to match and slow to print the first message.
Steps to reproduce
HelloWorldExample
.Fast DDS version/commit and platform
Fast-dds VERSION 2.5.0
Fast-cdr VERSION 1.0.23
platform: ubuntu 18.04
Modified codes:
HelloWorld_main.cpp
HelloWorldPublisher.cpp
HelloWorldSubscriber.h
HelloWorldSubscriber.cpp
Please let me know if you need any info. 😸
Beta Was this translation helpful? Give feedback.
All reactions