Skip to content

Commit

Permalink
Merge pull request #410 from mtconnect/configurable_sender_in_header
Browse files Browse the repository at this point in the history
Added Sender configuration setting and tests closing #409
  • Loading branch information
wsobel authored Feb 13, 2024
2 parents 4a189d7 + 5643cd4 commit 01b97ae
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 534 deletions.
7 changes: 7 additions & 0 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ namespace mtconnect {
for (auto &[k, pr] : m_printers)
pr->setSchemaVersion(*m_schemaVersion);
}

auto sender = GetOption<string>(options, config::Sender);
if (sender)
{
for (auto &[k, pr] : m_printers)
pr->setSenderName(*sender);
}
}

void Agent::initialize(pipeline::PipelineContextPtr context)
Expand Down
19 changes: 18 additions & 1 deletion src/mtconnect/configuration/agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ namespace mtconnect::configuration {
{configuration::LogStreams, false},
{configuration::ShdrVersion, 1},
{configuration::WorkerThreads, 1},
{configuration::Sender, ""s},
{configuration::TlsCertificateChain, ""s},
{configuration::TlsPrivateKey, ""s},
{configuration::TlsDHKey, ""s},
Expand Down Expand Up @@ -855,6 +856,22 @@ namespace mtconnect::configuration {
// Check for schema version
auto port = get<int>(options[configuration::Port]);
LOG(info) << "Starting agent on port " << int(port);

// Get the name of the sender
auto sender = GetOption<string>(options, configuration::Sender);
if (sender)
{
options[configuration::Sender] = *sender;
}
else
{
boost::system::error_code ec;
auto name = boost::asio::ip::host_name(ec);
if (ec)
options[configuration::Sender] = "localhost";
else
options[configuration::Sender] = name;
}

// Make the Agent
m_agent = make_unique<Agent>(getAsyncContext(), m_devicesFile, options);
Expand All @@ -868,7 +885,7 @@ namespace mtconnect::configuration {

m_agent->initialize(m_pipelineContext);
m_version = *m_agent->getSchemaVersion();

DevicePtr device;
if (IsOptionSet(options, configuration::PreserveUUID))
{
Expand Down
1 change: 1 addition & 0 deletions src/mtconnect/configuration/config_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace mtconnect {
DECLARE_CONFIGURATION(SchemaVersion);
DECLARE_CONFIGURATION(ServerIp);
DECLARE_CONFIGURATION(ServiceName);
DECLARE_CONFIGURATION(Sender);
DECLARE_CONFIGURATION(TlsCertificateChain);
DECLARE_CONFIGURATION(TlsCertificatePassword);
DECLARE_CONFIGURATION(TlsClientCAs);
Expand Down
24 changes: 4 additions & 20 deletions src/mtconnect/printer/json_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ namespace mtconnect::printer {
m_version = appVersion;
}

const string &JsonPrinter::hostname() const
{
if (m_hostname.empty())
{
string name;
boost::system::error_code ec;
name = boost::asio::ip::host_name(ec);
if (ec)
name = "localhost";
// Breaking the rules, this is a one off
const_cast<JsonPrinter *>(this)->m_hostname = name;
}

return m_hostname;
}

template <typename T>
inline void header(AutoJsonObject<T> &obj, const string &version, const string &hostname,
const uint64_t instanceId, const unsigned int bufferSize,
Expand Down Expand Up @@ -138,7 +122,7 @@ namespace mtconnect::printer {

{
AutoJsonObject obj(writer, "Header");
header(obj, m_version, hostname(), instanceId, bufferSize, *m_schemaVersion,
header(obj, m_version, m_senderName, instanceId, bufferSize, *m_schemaVersion,
m_modelChangeTime);
}
{
Expand Down Expand Up @@ -195,7 +179,7 @@ namespace mtconnect::printer {
obj.AddPairs("jsonVersion", m_jsonVersion, "schemaVersion", *m_schemaVersion);
{
AutoJsonObject obj(writer, "Header");
probeAssetHeader(obj, m_version, hostname(), instanceId, bufferSize, assetBufferSize,
probeAssetHeader(obj, m_version, m_senderName, instanceId, bufferSize, assetBufferSize,
assetCount, *m_schemaVersion, m_modelChangeTime);
}
{
Expand All @@ -222,7 +206,7 @@ namespace mtconnect::printer {
obj.AddPairs("jsonVersion", m_jsonVersion, "schemaVersion", *m_schemaVersion);
{
AutoJsonObject obj(writer, "Header");
probeAssetHeader(obj, m_version, hostname(), instanceId, 0, bufferSize, assetCount,
probeAssetHeader(obj, m_version, m_senderName, instanceId, 0, bufferSize, assetCount,
*m_schemaVersion, m_modelChangeTime);
}
{
Expand Down Expand Up @@ -420,7 +404,7 @@ namespace mtconnect::printer {
obj.AddPairs("jsonVersion", m_jsonVersion, "schemaVersion", *m_schemaVersion);
{
AutoJsonObject obj(writer, "Header");
streamHeader(obj, m_version, hostname(), instanceId, bufferSize, nextSeq, firstSeq, lastSeq,
streamHeader(obj, m_version, m_senderName, instanceId, bufferSize, nextSeq, firstSeq, lastSeq,
*m_schemaVersion, m_modelChangeTime);
}

Expand Down
1 change: 0 additions & 1 deletion src/mtconnect/printer/json_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace mtconnect::printer {
uint32_t getJsonVersion() const { return m_jsonVersion; }

protected:
const std::string &hostname() const;
std::string m_version;
std::string m_hostname;
uint32_t m_jsonVersion;
Expand Down
9 changes: 9 additions & 0 deletions src/mtconnect/printer/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ namespace mtconnect {
/// @brief Get the schema version
/// @return the schema version
const auto &getSchemaVersion() const { return m_schemaVersion; }

/// @brief sets the sener name for the header
/// @param name the name of the sender
void setSenderName(const std::string &s) { m_senderName = s; }

/// @brief gets the sender name
/// @returns the name of the sender in the header
const auto &getSenderName() const { return m_senderName; }

/// @brief Use the agent version to default the schema version
void defaultSchemaVersion() const
Expand All @@ -142,6 +150,7 @@ namespace mtconnect {
bool m_pretty;
std::string m_modelChangeTime;
std::optional<std::string> m_schemaVersion;
std::string m_senderName { "localhost" };
};
} // namespace printer
} // namespace mtconnect
10 changes: 1 addition & 9 deletions src/mtconnect/printer/xml_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,7 @@ namespace mtconnect::printer {

addAttribute(writer, "creationTime", getCurrentTime(GMT));

static std::string sHostname;
if (sHostname.empty())
{
boost::system::error_code ec;
sHostname = boost::asio::ip::host_name(ec);
if (ec)
sHostname = "localhost";
}
addAttribute(writer, "sender", sHostname);
addAttribute(writer, "sender", m_senderName);
addAttribute(writer, "instanceId", to_string(instanceId));

char version[32] = {0};
Expand Down
19 changes: 19 additions & 0 deletions test_package/agent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3023,3 +3023,22 @@ TEST_F(AgentTest, should_not_add_spaces_to_output)
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:Block", "");
}
}

TEST_F(AgentTest, should_set_sender_from_config_in_XML_header)
{
auto agent = m_agentTestHelper->createAgent("/samples/test_config.xml", 8, 4, "2.0", 4, false, true, {{configuration::Sender, "MachineXXX"s}});
ASSERT_TRUE(agent);
{
PARSE_XML_RESPONSE("/probe");
ASSERT_XML_PATH_EQUAL(doc, "//m:Header@sender", "MachineXXX");
}

{
PARSE_XML_RESPONSE("/current");
ASSERT_XML_PATH_EQUAL(doc, "//m:Header@sender", "MachineXXX");
}


}


Loading

0 comments on commit 01b97ae

Please sign in to comment.