Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 08f43db

Browse files
committed
Registry now supports command line options and sigs
1 parent db6badb commit 08f43db

File tree

8 files changed

+51
-17
lines changed

8 files changed

+51
-17
lines changed

projects/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ else()
2929
endforeach(OUTPUTCONFIG)
3030
endif()
3131

32+
set(Boost_USE_STATIC_LIBS OFF)
33+
find_package(Boost COMPONENTS system thread program_options signals log REQUIRED)
34+
3235
###############################################################################
3336
# Step 2: Project Includes are generally order dependent. So manually maintain
3437
# this list

projects/libpfc_net/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ set(${PREFIX}_UNIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/unit" PARENT_SCOPE)
2323
# Requirments
2424
###############################################################################
2525
find_package(nanomsg REQUIRED)
26-
set(Boost_USE_STATIC_LIBS OFF)
27-
find_package(Boost COMPONENTS system thread REQUIRED)
2826
###############################################################################
2927
#Code Generation
3028
###############################################################################
@@ -96,6 +94,7 @@ set(${PREFIX}_LIBS
9694
${CMAKE_DL_LIBS}
9795
nanomsg
9896
Boost::disable_autolinking
97+
Boost::dynamic_linking
9998
Boost::system
10099
Boost::thread
101100
)

projects/libpfc_net/cpp/net/Multicast_Receiver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <boost/asio/ip/multicast.hpp>
66
#include <boost/asio/ip/udp.hpp>
7+
#include <boost/asio/signal_set.hpp>
8+
#include <boost/log/trivial.hpp>
79
#include <boost/system/error_code.hpp>
810

911
namespace pfc {
@@ -129,8 +131,8 @@ void Multicast_Receiver::async_receive(std::function<void(std::istream&)> proces
129131
//! This function waits until no pending work is done.
130132
void Multicast_Receiver::join()
131133
{
132-
if( _impl->multicast_async_receive_thread.joinable() )
133-
_impl->multicast_async_receive_thread.join();
134+
if (_impl->multicast_async_receive_thread.joinable())
135+
_impl->multicast_async_receive_thread.join();
134136
}
135137
//-----------------------------------------------------------------------------
136138
//! This function waits until no pending work is done.

projects/libpfc_net/cpp/net/Multicast_Sender.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <boost/system/error_code.hpp>
99

1010
#include <boost/asio/buffered_stream.hpp>
11+
#include <boost/asio/signal_set.hpp>
1112
#include <boost/asio/streambuf.hpp>
1213

1314
namespace pfc {

projects/registry_server/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ set(${PREFIX}_LIBS
9191
${CMAKE_THREAD_LIBS_INIT}
9292
${CMAKE_DL_LIBS}
9393
sustain::pfc_nw
94+
Boost::program_options
95+
Boost::log
96+
9497
)
9598

9699
set(${PREFIX}_LIBS ${${PREFIX}_LIBS} PARENT_SCOPE)

projects/registry_server/cpp/Registry.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ void Registry::start()
9494
using namespace std::placeholders;
9595
_impl->subscription_listiner.async_receive(std::bind(&Implementation::process_subscription_message, _impl.get(), _1));
9696
}
97+
void Registry::wait()
98+
{
99+
_impl->subscription_listiner.join();
100+
_impl->service_broadcaster.join();
101+
}
97102
//-----------------------------------------------------------------------------
98103
void Registry::shutdown()
99104
{

projects/registry_server/cpp/Registry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Registry {
2525
~Registry();
2626

2727
void start();
28+
void wait();
2829
void shutdown();
2930

3031
bool is_valid();

projects/registry_server/cpp/registry_client_main.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,49 @@
22
#include <thread>
33

44
#include "Registry.h"
5-
#include <boost/system/error_code.hpp>
5+
6+
#include <boost/log/trivial.hpp>
7+
#include <boost/program_options.hpp>
8+
#include <boost/asio/io_context.hpp>
9+
#include <boost/asio/signal_set.hpp>
610

711
int main(int argc, const char* argv[])
812
{
13+
namespace bpo = boost::program_options;
14+
bpo::options_description options("Allowed options");
15+
options.add_options()("help", "Produce help message") //
16+
("bind", bpo::value<std::string>()->default_value("0::0"), "Server port bind address [0::0]") //
17+
("multicast", bpo::value<std::string>()->default_value("ff31::8000:1234"), "Server multicast broadcast address [ff31::8000:1234]");
18+
19+
bpo::variables_map vm;
20+
bpo::store(bpo::parse_command_line(argc, argv, options), vm);
21+
bpo::notify(vm);
22+
923
std::unique_ptr<pfc::Registry> reg;
1024
try {
11-
reg = std::make_unique<pfc::Registry>("0::0", "ff31::8000:1234");
25+
reg = std::make_unique<pfc::Registry>(vm["bind"].as<std::string>(), vm["multicast"].as<std::string>());
1226

13-
} catch (boost::system::error_code& e) {
14-
std::cout << e << std::endl;
15-
} catch (...) {
16-
std::cout << "Unknown Exception Caught\n";
27+
} catch (std::exception& e) {
28+
std::cerr << e.what();
1729
}
1830

19-
if (reg) {
20-
reg->start();
21-
}
31+
boost::asio::io_context context;
32+
boost::asio::signal_set signals(context);
2233

23-
std::cout << "Sleeping for 15 seconds\n";
24-
std::this_thread::sleep_for(std::chrono::seconds(15));
25-
std::cout << "Done Sleeping\n";
34+
signals.add(SIGINT);
35+
signals.add(SIGTERM);
36+
#if defined(SIGQUIT)
37+
signals_.add(SIGQUIT);
38+
#endif
39+
signals.async_wait([&](const boost::system::error_code& /*ec*/, int /*no*/) {
40+
BOOST_LOG_TRIVIAL(info) << "Stopping PFC Registry\n";
41+
reg->shutdown();
42+
});
2643

44+
// Start an asynchronous wait for one of the signals to occur.
2745
if (reg) {
28-
reg->shutdown();
46+
BOOST_LOG_TRIVIAL(info) << "Starting PFC Registry on " << vm["bind"].as<std::string>() << " broadcasting on " << vm["multicast"].as<std::string>() << "\n";
47+
reg->start();
2948
}
49+
context.run();
3050
}

0 commit comments

Comments
 (0)