Skip to content

Commit

Permalink
Refs #20543: Refactor entities
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
  • Loading branch information
JesusPoderoso committed Mar 20, 2024
1 parent fbb9c0e commit b2a2839
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 198 deletions.
84 changes: 84 additions & 0 deletions examples/cpp/hello_world/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 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 Application.cpp
*
*/

#include "Application.hpp"


#include "CLIParser.hpp"
#include "ListenerSubscriberApp.hpp"
#include "PublisherApp.hpp"
#include "WaitsetSubscriberApp.hpp"

using namespace eprosima::fastdds::dds;

namespace eprosima {
namespace fastdds {
namespace examples {
namespace hello_world {

Application::Application()
{
}

void Application::run()
{
}

void Application::stop()
{
}

bool Application::is_stopped()
{
return false;
}

//! Factory method to create a publisher or subscriber
std::shared_ptr<Application> Application::make_app(
const CLIParser::hello_world_config& config,
const std::string& topic_name)
{
std::shared_ptr<Application> entity;
switch (config.entity)
{
case CLIParser::EntityKind::PUBLISHER:
entity = std::make_shared<PublisherApp>(config.pub_config, topic_name);
break;
case CLIParser::EntityKind::SUBSCRIBER:
if (config.sub_config.use_waitset)
{
entity = std::make_shared<WaitsetSubscriberApp>(config.sub_config, topic_name);
}
else
{
entity = std::make_shared<ListenerSubscriberApp>(config.sub_config, topic_name);
}
break;
case CLIParser::EntityKind::UNDEFINED:
default:
throw std::runtime_error("Entity initialization failed");
break;
}
return entity;
}

} // namespace hello_world
} // namespace examples
} // namespace fastdds
} // namespace eprosima
61 changes: 61 additions & 0 deletions examples/cpp/hello_world/Application.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 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 Application.hpp
*
*/

#ifndef _FASTDDS_HELLO_WORLD_APPLICATION_HPP_
#define _FASTDDS_HELLO_WORLD_APPLICATION_HPP_

#include <atomic>

#include "CLIParser.hpp"

namespace eprosima {
namespace fastdds {
namespace examples {
namespace hello_world {

class Application
{
public:

Application();

//! Run entity (publisher or subscriber)
virtual void run();

//! Trigger the end of execution
virtual void stop();

//! Factory method to create a publisher or subscriber
static std::shared_ptr<Application> make_app(
const CLIParser::hello_world_config& config,
const std::string& topic_name);

private:

//! Return the current state of execution
virtual bool is_stopped();

};

} // namespace hello_world
} // namespace examples
} // namespace fastdds
} // namespace eprosima

#endif /* _FASTDDS_HELLO_WORLD_APPLICATION_HPP_ */
15 changes: 15 additions & 0 deletions examples/cpp/hello_world/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ class CLIParser
}
}

static std::string parse_entity_kind(
EntityKind entity)
{
switch (entity)
{
case EntityKind::PUBLISHER:
return "Publisher";
case EntityKind::SUBSCRIBER:
return "Subscriber";
case EntityKind::UNDEFINED:
default:
return "Undefined entity";
}
}

};

} // namespace hello_world
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

/**
* @file Subscriber.cpp
* @file ListenerSubscriber.cpp
*
*/

#include "Subscriber.hpp"
#include "ListenerSubscriberApp.hpp"

#include <condition_variable>
#include <csignal>
Expand All @@ -31,12 +31,22 @@
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>

#include "CLIParser.hpp"
#include "HelloWorldPubSubTypes.h"
#include "Application.hpp"

using namespace eprosima::fastdds::dds;

HelloWorldSubscriber::HelloWorldSubscriber(
const eprosima::fastdds::examples::hello_world::CLIParser::subscriber_config& config,
namespace eprosima {
namespace fastdds {
namespace examples {
namespace hello_world {

ListenerSubscriberApp::ListenerSubscriberApp(
const CLIParser::subscriber_config& config,
const std::string& topic_name)
: participant_(nullptr)
: Application ()
, participant_(nullptr)
, subscriber_(nullptr)
, topic_(nullptr)
, reader_(nullptr)
Expand Down Expand Up @@ -84,7 +94,7 @@ HelloWorldSubscriber::HelloWorldSubscriber(
}
}

HelloWorldSubscriber::~HelloWorldSubscriber()
ListenerSubscriberApp::~ListenerSubscriberApp()
{
if (nullptr != participant_)
{
Expand All @@ -96,7 +106,7 @@ HelloWorldSubscriber::~HelloWorldSubscriber()
}
}

void HelloWorldSubscriber::on_subscription_matched(
void ListenerSubscriberApp::on_subscription_matched(
DataReader* /*reader*/,
const SubscriptionMatchedStatus& info)
{
Expand All @@ -115,7 +125,7 @@ void HelloWorldSubscriber::on_subscription_matched(
}
}

void HelloWorldSubscriber::on_data_available(
void ListenerSubscriberApp::on_data_available(
DataReader* reader)
{
SampleInfo info;
Expand All @@ -135,7 +145,7 @@ void HelloWorldSubscriber::on_data_available(
}
}

void HelloWorldSubscriber::run()
void ListenerSubscriberApp::run()
{
std::unique_lock<std::mutex> lck(terminate_cv_mtx_);
terminate_cv_.wait(lck, [&]
Expand All @@ -144,13 +154,18 @@ void HelloWorldSubscriber::run()
});
}

bool HelloWorldSubscriber::is_stopped()
bool ListenerSubscriberApp::is_stopped()
{
return stop_.load();
}

void HelloWorldSubscriber::stop()
void ListenerSubscriberApp::stop()
{
stop_.store(true);
terminate_cv_.notify_all();
}

} // namespace hello_world
} // namespace examples
} // namespace fastdds
} // namespace eprosima
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// limitations under the License.

/**
* @file Subscriber.hpp
* @file ListenerSubscriberApp.hpp
*
*/

#ifndef _FASTDDS_HELLO_WORLD_SUBSCRIBER_HPP_
#define _FASTDDS_HELLO_WORLD_SUBSCRIBER_HPP_
#ifndef _FASTDDS_HELLO_WORLD_LISTENER_SUBSCRIBER_APP_HPP_
#define _FASTDDS_HELLO_WORLD_LISTENER_SUBSCRIBER_APP_HPP_

#include <condition_variable>

Expand All @@ -28,18 +28,24 @@

#include "CLIParser.hpp"
#include "HelloWorldPubSubTypes.h"
#include "Application.hpp"

using namespace eprosima::fastdds::dds;

class HelloWorldSubscriber : public DataReaderListener
namespace eprosima {
namespace fastdds {
namespace examples {
namespace hello_world {

class ListenerSubscriberApp : public Application, public DataReaderListener
{
public:

HelloWorldSubscriber(
const eprosima::fastdds::examples::hello_world::CLIParser::subscriber_config& config,
ListenerSubscriberApp(
const CLIParser::subscriber_config& config,
const std::string& topic_name);

virtual ~HelloWorldSubscriber();
~ListenerSubscriberApp();

//! Subscription callback
void on_data_available(
Expand All @@ -51,15 +57,15 @@ class HelloWorldSubscriber : public DataReaderListener
const SubscriptionMatchedStatus& info) override;

//! Run subscriber
void run();
virtual void run();

//! Trigger the end of execution
void stop();
virtual void stop();

private:

//! Return the current state of execution
bool is_stopped();
virtual bool is_stopped();

HelloWorld hello_;

Expand All @@ -84,4 +90,9 @@ class HelloWorldSubscriber : public DataReaderListener
std::condition_variable terminate_cv_;
};

#endif /* _FASTDDS_HELLO_WORLD_SUBSCRIBER_HPP_ */
} // namespace hello_world
} // namespace examples
} // namespace fastdds
} // namespace eprosima

#endif /* _FASTDDS_HELLO_WORLD_LISTENER_SUBSCRIBER_APP_HPP_ */
Loading

0 comments on commit b2a2839

Please sign in to comment.