Skip to content

Commit

Permalink
Merge pull request OpenDDS#4491 from jrw972/env-config
Browse files Browse the repository at this point in the history
OpenDDS cannot be configured with environment variables
  • Loading branch information
jrw972 authored Mar 6, 2024
2 parents af0b2b7 + 2605ead commit 19abd11
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bin/PerlDDS/Run_Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ sub _process_common {
my $self = shift;
my $name = shift;
my $params = shift;
my $debug_logging = 1;
my $debug_logging = $self->{dcps_log_level};

if ($$params !~ /-DCPSLogLevel / && $self->{dcps_log_level}) {
$$params .= " -DCPSLogLevel $self->{dcps_log_level}";
Expand Down
56 changes: 56 additions & 0 deletions dds/DCPS/Service_Participant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
#include "Service_Participant.inl"
#endif /* __ACE_INLINE__ */

#if !defined (ACE_WIN32)
extern char **environ;
#endif

namespace {

void set_log_file_name(const char* fname)
Expand Down Expand Up @@ -388,6 +392,8 @@ Service_Participant::get_domain_participant_factory(int &argc,
}
}

parse_env();

if (parse_args(argc, argv) != 0) {
return DDS::DomainParticipantFactory::_nil();
}
Expand Down Expand Up @@ -578,6 +584,56 @@ Service_Participant::get_domain_participant_factory(int &argc,
return DDS::DomainParticipantFactory::_duplicate(dp_factory_servant_.in());
}



void Service_Participant::parse_env()
{
#if defined (ACE_WIN32)
LPTCH env_strings = GetEnvironmentStrings();

// If the returned pointer is NULL, exit.
if (!env_strings) {
if (log_level >= LogLevel::Error) {
ACE_ERROR((LM_ERROR,
"(%P|%t) ERROR: Service_Participant::parse_env: Could not get environment strings\n"));
}
return;
}

LPTSTR env_string = (LPTSTR) env_strings;

while (*env_string) {
parse_env(ACE_TEXT_ALWAYS_CHAR(env_string));
env_string += lstrlen(env_string) + 1;
}
FreeEnvironmentStrings(env_strings);

#else

for (char** e = environ; *e; ++e) {
parse_env(*e);
}

#endif
}

void Service_Participant::parse_env(const String& p)
{
// Only parse environment variables starting with OPENDDS_.
if (p.substr(0, 8) == "OPENDDS_") {
// Extract everything after OPENDDS_.
const String q = p.substr(8);
// q should have the form key=value
String::size_type pos = q.find('=');
if (pos != String::npos) {
// Split into key and value.
const String key = q.substr(0, pos);
const String value = q.substr(pos + 1);
config_store_->set(key.c_str(), value);
}
}
}

int Service_Participant::parse_args(int& argc, ACE_TCHAR* argv[])
{
// Process logging options first, so they are in effect if we need to log
Expand Down
4 changes: 4 additions & 0 deletions dds/DCPS/Service_Participant.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ class OpenDDS_Dcps_Export Service_Participant {
/// Initialize the thread scheduling and initial priority.
void initializeScheduling();

/// Parse environment variables.
void parse_env();
void parse_env(const String& s);

/**
* Parse the command line for user options. e.g. "-DCPSInfoRepo <iorfile>".
* It consumes -DCPS* options and their arguments
Expand Down
4 changes: 4 additions & 0 deletions tests/DCPS/ConfigFile/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ACE_TMAIN(int argc, ACE_TCHAR* argv[])
TheParticipantFactoryWithArgs(argc, argv);
TEST_CHECK(dpf.in() != 0);

// From commandline
TEST_CHECK(TheServiceParticipant->config_store()->get("MY_CONFIG_KEY1", "") == "value1");
// From environment variable
TEST_CHECK(TheServiceParticipant->config_store()->get("MY_CONFIG_KEY2", "") == "value2");
TEST_CHECK(OpenDDS::DCPS::DCPS_debug_level == 1);
TEST_CHECK(TheServiceParticipant->n_chunks() == 10);
TEST_CHECK(TheServiceParticipant->association_chunk_multiplier() == 5);
Expand Down
43 changes: 10 additions & 33 deletions tests/DCPS/ConfigFile/run_test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

# -*- perl -*-

use Sys::Hostname;

use Env (DDS_ROOT);
use lib "$DDS_ROOT/bin";
use Env (ACE_ROOT);
Expand All @@ -16,50 +14,29 @@
use File::Copy;
use strict;

my $status = 0;
my $test = new PerlDDS::TestFramework();
$test->{add_pending_timeout} = 0;
$test->setup_discovery();

my $dcpsrepo_ior = "repo.ior";
my $dcpsrepo2_ior = "repo2.ior";
my $dcpsrepo3_ior = "repo3.ior";


unlink $dcpsrepo_ior;
my $DCPSREPO = PerlDDS::create_process("$ENV{DDS_ROOT}/bin/DCPSInfoRepo",
" -o $dcpsrepo_ior");
print $DCPSREPO->CommandLine() . "\n";
$DCPSREPO->Spawn ();
if (PerlACE::waitforfile_timed ($dcpsrepo_ior, 30) == -1) {
print STDERR "ERROR: waiting for Info Repo IOR file\n";
$DCPSREPO->Kill ();
exit 1;
}

copy($dcpsrepo_ior, $dcpsrepo2_ior);
copy($dcpsrepo_ior, $dcpsrepo3_ior);

my $cfg = new PerlACE::ConfigList->check_config('NO_BUILT_IN_TOPICS')
? 'test1_nobits.ini' : 'test1.ini';
my $TST = PerlDDS::create_process("ConfigFile", "-DCPSConfigFile $cfg");
print $TST->CommandLine() . "\n";
my $retcode = $TST->SpawnWaitKill(60);
if ($retcode != 0) {
$status = 1;
}

my $ir = $DCPSREPO->TerminateWaitKill(5);
if ($ir != 0) {
print STDERR "ERROR: DCPSInfoRepo returned $ir\n";
$status = 1;
}
$test->process('ConfigFile', 'ConfigFile', "-DCPSConfigFile $cfg -OpenDDSMyConfigKey1 value1");

$ENV{'OPENDDS_MY_CONFIG_KEY2'} = "value2";

$test->start_process('ConfigFile');

my $status = $test->finish(5);

unlink $dcpsrepo_ior;
unlink $dcpsrepo2_ior;
unlink $dcpsrepo3_ior;
if ($status == 0) {
print "test PASSED.\n";
}
else {
print STDERR "test FAILED.\n";
}

exit $status;

0 comments on commit 19abd11

Please sign in to comment.