Skip to content
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set(AGENT_VERSION_MAJOR 2)
set(AGENT_VERSION_MINOR 6)
set(AGENT_VERSION_PATCH 0)
set(AGENT_VERSION_BUILD 3)
set(AGENT_VERSION_BUILD 4)
set(AGENT_VERSION_RC "")

# This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent
Expand Down
33 changes: 21 additions & 12 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ namespace mtconnect {
catch (std::runtime_error &e)
{
LOG(fatal) << "Cannot start server: " << e.what();
std::exit(1);
throw FatalException(e.what());
}

m_started = true;
Expand Down Expand Up @@ -436,14 +436,14 @@ namespace mtconnect {
LOG(fatal) << "Error loading xml configuration: " + deviceFile;
LOG(fatal) << "Error detail: " << e.what();
cerr << e.what() << endl;
throw e;
throw FatalException(e.what());
}
catch (exception &f)
{
LOG(fatal) << "Error loading xml configuration: " + deviceFile;
LOG(fatal) << "Error detail: " << f.what();
cerr << f.what() << endl;
throw f;
throw FatalException(f.what());
}
}

Expand Down Expand Up @@ -525,6 +525,10 @@ namespace mtconnect {
if (changed)
loadCachedProbe();
}
catch (FatalException &e)
{
throw e;
}
catch (runtime_error &e)
{
for (auto device : devices)
Expand Down Expand Up @@ -832,7 +836,7 @@ namespace mtconnect {
{
for (auto &e : errors)
LOG(fatal) << "Error creating the agent device: " << e->what();
throw EntityError("Cannot create AgentDevice");
throw FatalException("Cannot create AgentDevice");
}
addDevice(m_agentDevice);
}
Expand Down Expand Up @@ -870,14 +874,14 @@ namespace mtconnect {
LOG(fatal) << "Error loading xml configuration: " + configXmlPath;
LOG(fatal) << "Error detail: " << e.what();
cerr << e.what() << endl;
throw e;
throw FatalException(e.what());
}
catch (exception &f)
{
LOG(fatal) << "Error loading xml configuration: " + configXmlPath;
LOG(fatal) << "Error detail: " << f.what();
cerr << f.what() << endl;
throw f;
throw FatalException(f.what());
}

return {};
Expand Down Expand Up @@ -976,9 +980,13 @@ namespace mtconnect {
auto di = m_dataItemMap[d->getId()].lock();
if (di && di != d)
{
LOG(fatal) << "Duplicate DataItem id " << d->getId()
<< " for device: " << *device->getComponentName();
std::exit(1);
stringstream msg;

msg << "Duplicate DataItem id " << d->getId()
<< " for device: " << *device->getComponentName()
<< ". Try using configuration option CreateUniqueIds to resolve.";
LOG(fatal) << msg.str();
throw FatalException(msg.str());
}
}
else
Expand Down Expand Up @@ -1008,9 +1016,10 @@ namespace mtconnect {
if (old != idx.end())
{
// Update existing device
LOG(fatal) << "Device " << *device->getUuid() << " already exists. "
<< " Update not supported yet";
std::exit(1);
stringstream msg;
msg << "Device " << *device->getUuid() << " already exists. "
<< " Update not supported yet";
throw msg;
}
else
{
Expand Down
23 changes: 11 additions & 12 deletions src/mtconnect/configuration/agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ namespace mtconnect::configuration {
buffer << file.rdbuf();

FileFormat fmt = MTCONNECT;
if (ends_with(m_configFile.string(), "json"))
if (m_configFile.string().ends_with("json"))
{
LOG(debug) << "Parsing json configuration";
fmt = JSON;
}
else if (ends_with(m_configFile.string(), "xml"))
else if (m_configFile.string().ends_with("xml"))
{
LOG(debug) << "Parsing xml configuration";
fmt = XML;
Expand Down Expand Up @@ -392,7 +392,7 @@ namespace mtconnect::configuration {
m_monitorTimer.async_wait(boost::bind(&AgentConfiguration::monitorFiles, this, _1));
}

void AgentConfiguration::start()
int AgentConfiguration::start()
{
if (m_monitorFiles)
{
Expand All @@ -411,7 +411,7 @@ namespace mtconnect::configuration {
m_context->setThreadCount(m_workerThreadCount);
m_beforeStartHooks.exec(*this);
m_agent->start();
m_context->start();
return m_context->start();
}

void AgentConfiguration::stop()
Expand Down Expand Up @@ -879,12 +879,12 @@ namespace mtconnect::configuration {
LOG(fatal) << "Cannot find device configuration file";
logPaths(LOG_LEVEL(fatal), m_configPaths);

throw runtime_error(((string) "Please make sure the configuration "
"file probe.xml or Devices.xml is in the current "
"directory or specify the correct file "
"in the configuration file " +
m_configFile.string() + " using Devices = <file>")
.c_str());
throw FatalException(((string) "Please make sure the configuration "
"file probe.xml or Devices.xml is in the current "
"directory or specify the correct file "
"in the configuration file " +
m_configFile.string() + " using Devices = <file>")
.c_str());
}

m_name = get<string>(options[configuration::ServiceName]);
Expand Down Expand Up @@ -958,7 +958,7 @@ namespace mtconnect::configuration {
if (host.empty())
{
LOG(fatal) << "Malformed URL in configuration file: '" << url << "', exiting";
exit(1);
throw FatalException();
}
options[configuration::Host] = host;

Expand Down Expand Up @@ -1252,4 +1252,3 @@ namespace mtconnect::configuration {
return false;
}
} // namespace mtconnect::configuration

3 changes: 2 additions & 1 deletion src/mtconnect/configuration/agent_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ namespace mtconnect {
/// @brief stops the agent. Used in daemons.
void stop() override;
/// @brief starts the agent. Used in daemons.
void start() override;
/// @return 0 on success
int start() override;
/// @brief initializes the configuration of the agent from the command line parameters
/// @param[in] options command line parameters
void initialize(const boost::program_options::variables_map &options) override;
Expand Down
110 changes: 81 additions & 29 deletions src/mtconnect/configuration/async_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "mtconnect/config.hpp"
#include "mtconnect/logging.hpp"
#include "mtconnect/utilities.hpp"

namespace mtconnect::configuration {

Expand Down Expand Up @@ -63,46 +64,96 @@ namespace mtconnect::configuration {
void setThreadCount(int threads) { m_threadCount = threads; }

/// @brief start `m_threadCount` worker threads
void start()
/// @returns the exit code
int start()
{
m_running = true;
m_paused = false;
do
m_exitCode = 0;
try
{
for (int i = 0; i < m_threadCount; i++)
{
m_workers.emplace_back(boost::thread([this]() { m_context.run(); }));
}
auto &first = m_workers.front();
while (m_running && !m_paused)
m_running = true;
m_paused = false;
do
{
if (!first.try_join_for(boost::chrono::seconds(5)) && !m_running)
for (int i = 0; i < m_threadCount; i++)
{
m_workers.emplace_back(boost::thread([this]() {
try
{
m_context.run();
}

catch (FatalException &e)
{
LOG(fatal) << "Fatal exception occurred: " << e.what();
stop();
m_exitCode = 1;
}
catch (std::exception &e)
{
LOG(fatal) << "Uncaught exception occurred: " << e.what();
stop();
m_exitCode = 1;
}
catch (...)
{
LOG(fatal) << "Unknown fatal exception occurred";
stop();
m_exitCode = 1;
}
}));
}
auto &first = m_workers.front();
while (m_running && !m_paused)
{
if (!first.try_join_for(boost::chrono::seconds(5)))
m_context.stop();
if (!first.try_join_for(boost::chrono::seconds(5)) && !m_running)
{
if (!first.try_join_for(boost::chrono::seconds(5)))
m_context.stop();
}
}
}

for (auto &w : m_workers)
{
w.join();
}
m_workers.clear();
for (auto &w : m_workers)
{
w.join();
}
m_workers.clear();

if (m_delayedStop.joinable())
m_delayedStop.join();
if (m_delayedStop.joinable())
m_delayedStop.join();

if (m_syncCallback)
{
m_syncCallback(*this);
if (m_running)
if (m_syncCallback && m_exitCode == 0)
{
m_syncCallback = nullptr;
restart();
m_syncCallback(*this);
if (m_running)
{
m_syncCallback = nullptr;
restart();
}
}
}

} while (m_running);
} while (m_running);
}

catch (FatalException &e)
{
LOG(fatal) << "Fatal exception occurred: " << e.what();
stop();
m_exitCode = 1;
}
catch (std::exception &e)
{
LOG(fatal) << "Uncaught exception occurred: " << e.what();
stop();
m_exitCode = 1;
}
catch (...)
{
LOG(fatal) << "Unknown fatal exception occurred";
stop();
m_exitCode = 1;
}

return m_exitCode;
}

/// @brief pause the worker threads. Sets a callback when the threads are paused.
Expand Down Expand Up @@ -190,6 +241,7 @@ namespace mtconnect::configuration {
int m_threadCount = 1;
bool m_running = false;
bool m_paused = false;
int m_exitCode = 0;
};

} // namespace mtconnect::configuration
13 changes: 8 additions & 5 deletions src/mtconnect/configuration/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ namespace mtconnect {

using namespace std;

int res = 0;

try
{
// If command-line parameter is "install", install the service. If debug or run
Expand Down Expand Up @@ -208,9 +210,9 @@ namespace mtconnect {
m_isDebug = true;

initialize(options);
start();
res = start();
std::thread cmd(commandLine);
return 0;
return res;
}
else
{
Expand All @@ -236,14 +238,16 @@ namespace mtconnect {
{
LOG(fatal) << "Agent top level exception: " << e.what();
std::cerr << "Agent top level exception: " << e.what() << std::endl;
res = 1;
}
catch (std::string &s)
{
LOG(fatal) << "Agent top level exception: " << s;
std::cerr << "Agent top level exception: " << s << std::endl;
res = 1;
}

return 0;
return res;
}

bool MTConnectService::isElevated()
Expand Down Expand Up @@ -841,8 +845,7 @@ namespace mtconnect {
usage(1);
}

start();
return 0;
return start();
}

void MTConnectService::install() {}
Expand Down
2 changes: 1 addition & 1 deletion src/mtconnect/configuration/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace mtconnect {
/// @brief stop the srvice
virtual void stop() = 0;
/// @brief start the service
virtual void start() = 0;
virtual int start() = 0;

/// @brief set the name of the service
/// @param[in] name name of the service
Expand Down
1 change: 1 addition & 0 deletions src/mtconnect/device_model/agent_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace mtconnect {
{
LOG(fatal) << "Cannot create AgentDevice: " << e->what();
}
throw FatalException();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mtconnect/device_model/data_item/data_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace mtconnect {
auto &category = get<string>("category");

auto units = maybeGet<string>("units");
if (units && ends_with(*units, "3D"))
if (units && units->ends_with("3D"))
m_specialClass = THREE_SPACE_CLS;

if (category == "SAMPLE")
Expand Down
Loading