Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[21324] Adapt example generation to Fast DDS examples refactor #377

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/main/java/com/eprosima/fastdds/fastddsgen.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,10 @@ private Project parseIDL(

if (m_exampleOption != null)
{
// Load Application templates
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSApplicationHeader.stg");
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSApplicationSource.stg");

// Load Publisher templates
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSPublisherHeader.stg");
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSPublisherSource.stg");
Expand All @@ -795,7 +799,7 @@ private Project parseIDL(
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSSubscriberHeader.stg");
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSSubscriberSource.stg");

// Load PubSubMain template
// Load main template
tmanager.addGroup("com/eprosima/fastdds/idl/templates/DDSPubSubMain.stg");
}

Expand Down Expand Up @@ -1017,40 +1021,54 @@ private Project parseIDL(

if (m_exampleOption != null)
{
System.out.println("Generating Publisher files...");
System.out.println("Generating Application files...");
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Application.hpp",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSApplicationHeader.stg"), m_replace))
{
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Application.cxx",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSApplicationSource.stg"), m_replace))
{
project.addProjectIncludeFile(relative_dir + ctx.getFilename() + "Application.hpp");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "Application.cxx");
}
}

System.out.println("Generating PublisherApp files...");
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Publisher.hpp",
Utils.writeFile(output_dir + ctx.getFilename() + "PublisherApp.hpp",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSPublisherHeader.stg"), m_replace))
{
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Publisher.cxx",
Utils.writeFile(output_dir + ctx.getFilename() + "PublisherApp.cxx",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSPublisherSource.stg"), m_replace))
{
project.addProjectIncludeFile(relative_dir + ctx.getFilename() + "Publisher.hpp");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "Publisher.cxx");
project.addProjectIncludeFile(relative_dir + ctx.getFilename() + "PublisherApp.hpp");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "PublisherApp.cxx");
}
}

System.out.println("Generating Subscriber files...");
System.out.println("Generating SubscriberApp files...");
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Subscriber.hpp",
Utils.writeFile(output_dir + ctx.getFilename() + "SubscriberApp.hpp",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSSubscriberHeader.stg"), m_replace))
{
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "Subscriber.cxx",
Utils.writeFile(output_dir + ctx.getFilename() + "SubscriberApp.cxx",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSSubscriberSource.stg"), m_replace))
{
project.addProjectIncludeFile(relative_dir + ctx.getFilename() + "Subscriber.hpp");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "Subscriber.cxx");
project.addProjectIncludeFile(relative_dir + ctx.getFilename() + "SubscriberApp.hpp");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "SubscriberApp.cxx");
}
}

System.out.println("Generating main file...");
if (returnedValue =
Utils.writeFile(output_dir + ctx.getFilename() + "PubSubMain.cxx",
Utils.writeFile(output_dir + ctx.getFilename() + "main.cxx",
maintemplates.getTemplate("com/eprosima/fastdds/idl/templates/DDSPubSubMain.stg"), m_replace))
{
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "PubSubMain.cxx");
project.addProjectSrcFile(relative_dir + ctx.getFilename() + "main.cxx");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

group ProtocolHeader;

import "eprosima.stg"

main(ctx, definitions) ::= <<
$fileHeader(ctx=ctx, file=[ctx.filename, "Application.hpp"], description=["This header file contains the declaration of the application functions."])$

#ifndef FAST_DDS_GENERATED__$ctx.headerGuardName$APPLICATION_HPP
#define FAST_DDS_GENERATED__$ctx.headerGuardName$APPLICATION_HPP

#include <atomic>
#include <memory>
#include <string>

class $ctx.filename$Application
{
public:

//! Virtual destructor
virtual ~$ctx.filename$Application() = default;

//! Run application
virtual void run() = 0;

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

//! Factory method to create applications based on configuration
static std::shared_ptr<$ctx.filename$Application> make_app(
const int& domain_id,
const std::string& entity_kind);
};

#endif // FAST_DDS_GENERATED__$ctx.headerGuardName$APPLICATION_HPP
>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

group ProtocolHeader;

import "eprosima.stg"

main(ctx, definitions) ::= <<
$fileHeader(ctx=ctx, file=[ctx.filename, "Application.cxx"], description=["This file contains the implementation of the application functions."])$

#include "$ctx.filename$Application.hpp"

#include "$ctx.filename$PublisherApp.hpp"
#include "$ctx.filename$SubscriberApp.hpp"

//! Factory method to create a publisher or subscriber
std::shared_ptr<$ctx.filename$Application> $ctx.filename$Application::make_app(
const int& domain_id,
const std::string& entity_kind)
{
std::shared_ptr<$ctx.filename$Application> entity;
if (strcmp(entity_kind.c_str(), "publisher") == 0)
{
entity = std::make_shared<$ctx.filename$PublisherApp>(domain_id);
}
else if (strcmp(entity_kind.c_str(), "subscriber") == 0)
{
entity = std::make_shared<$ctx.filename$SubscriberApp>(domain_id);
}
else
{
throw std::runtime_error("Entity initialization failed");
}
return entity;
}
>>
112 changes: 72 additions & 40 deletions src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubMain.stg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
// 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.
Expand All @@ -17,65 +17,97 @@ group ProtocolHeader;
import "eprosima.stg"

main(ctx, definitions) ::= <<
$fileHeader(ctx=ctx, file=[ctx.filename, "PubSubMain.cpp"], description=["This file acts as a main entry point to the application."])$
$fileHeader(ctx=ctx, file=[ctx.filename, "main.cxx"], description=["This file acts as a main entry point to the application."])$

#include <csignal>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <thread>

#include "$ctx.filename$Publisher.hpp"
#include "$ctx.filename$Subscriber.hpp"
#include <fastdds/dds/log/Log.hpp>

int main(
int argc,
char** argv)
#include "$ctx.filename$Application.hpp"

using eprosima::fastdds::dds::Log;

std::function<void(int)> stop_handler;
void signal_handler(
int signum)
{
int type = 0;
stop_handler(signum);
}

if (argc == 2)
std::string parse_signal(
const int& signum)
{
switch (signum)
{
if (strcmp(argv[1], "publisher") == 0)
{
type = 1;
}
else if (strcmp(argv[1], "subscriber") == 0)
{
type = 2;
}
case SIGINT:
return "SIGINT";
case SIGTERM:
return "SIGTERM";
#ifndef _WIN32
case SIGQUIT:
return "SIGQUIT";
case SIGHUP:
return "SIGHUP";
#endif // _WIN32
default:
return "UNKNOWN SIGNAL";
}
}

if (type == 0)
int main(
int argc,
char** argv)
{
auto ret = EXIT_SUCCESS;
int domain_id = 0;
std::shared_ptr<$ctx.filename$Application> app;

if (argc != 2 || (strcmp(argv[1], "publisher") != 0 && strcmp(argv[1], "subscriber") != 0))
{
std::cout << "Error: Incorrect arguments." << std::endl;
std::cout << "Usage: " << std::endl << std::endl;
std::cout << argv[0] << " publisher|subscriber" << std::endl << std::endl;
return 0;
ret = EXIT_FAILURE;
}

std::cout << "Starting " << std::endl;

// Register the type being used

switch (type)
else
{
case 1:
try
{
$ctx.filename$Publisher mypub;
if (mypub.init())
{
mypub.run();
}
break;
app = $ctx.filename$Application::make_app(domain_id, argv[1]);
}
case 2:
catch (const std::runtime_error& e)
{
$ctx.filename$Subscriber mysub;
if (mysub.init())
{
mysub.run();
}
break;
EPROSIMA_LOG_ERROR(app_name, e.what());
ret = EXIT_FAILURE;
}

std::thread thread(&$ctx.filename$Application::run, app);

std::cout << argv[1] << " running. Please press Ctrl+C to stop the " << argv[1] << " at any time." << std::endl;

stop_handler = [&](int signum)
{
std::cout << "\n" << parse_signal(signum) << " received, stopping " << argv[1]
<< " execution." << std::endl;
app->stop();
};

signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
#endif // _WIN32

thread.join();
}

return 0;
Log::Reset();
return ret;
}

>>
Loading
Loading