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

Pr/21 commit2 #26

Draft
wants to merge 10 commits into
base: devel
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build
*.user
.vscode
.vs/
out/
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ link_directories(

add_definitions(${PCL_DEFINITIONS})


set(client_SRCS
src/common/notifications.cpp
src/modules/polar_to_cart_converter.cpp
src/modules/distance_filter.cpp
src/modules/ring_intensity_filter.cpp
Expand Down Expand Up @@ -142,7 +143,8 @@ if (PACKAGE_FOR_DEV)
if(WIN32)
install(TARGETS quanergy_client
EXPORT QuanergyClientTargets
RUNTIME DESTINATION "${INSTALL_LIB_DIR}" COMPONENT shlib)
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT shlib
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT shlib)
else()
install(TARGETS quanergy_client
EXPORT QuanergyClientTargets
Expand Down
3 changes: 3 additions & 0 deletions apps/dynamic_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// sensor pipeline
#include <quanergy/pipelines/sensor_pipeline.h>

// handle notifications
#include <quanergy/common/notifications.h>

int main(int argc, char** argv)
{
namespace po = boost::program_options;
Expand Down
3 changes: 3 additions & 0 deletions apps/visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// sensor pipeline
#include <quanergy/pipelines/sensor_pipeline.h>

// handle notifications
#include <quanergy/common/notifications.h>

int main(int argc, char** argv)
{
namespace po = boost::program_options;
Expand Down
20 changes: 16 additions & 4 deletions include/quanergy/client/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,27 @@ namespace quanergy
};

/** \brief error parsing header */
struct InvalidHeaderError : public std::exception
struct ParseTimeoutError : public std::exception
{
virtual const char* what() const throw() { return "Invalid header"; }
virtual const char* what() const throw() { return "Parse timeout error"; }
};

/** \brief error parsing header */
struct InvalidHeaderError : public std::runtime_error
{
explicit InvalidHeaderError(const std::string& message)
: std::runtime_error("Invalid header! Details: " + message) {}
explicit InvalidHeaderError()
: std::runtime_error("Invalid header!") {}
};

/** \brief packet size doesn't match data description */
struct SizeMismatchError : public std::exception
struct SizeMismatchError : public std::runtime_error
{
virtual const char* what() const throw() { return "Packet sizes don't match"; }
explicit SizeMismatchError(const std::string& message)
: std::runtime_error("Packet sizes don't match! Details: " + message) {}
explicit SizeMismatchError()
: std::runtime_error("Packet sizes don't match!") {}
};

/** \brief Invalid packet */
Expand Down
31 changes: 17 additions & 14 deletions include/quanergy/client/impl/tcp_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ namespace quanergy
template <class HEADER>
void TCPClient<HEADER>::startDataConnect()
{
std::cout << "Attempting to connect (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")..." << std::endl;
log.info << "Attempting to connect (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")..." << std::endl;
boost::asio::ip::tcp::resolver resolver(io_service_);

try
Expand All @@ -138,23 +138,25 @@ namespace quanergy
}
else if (error)
{
std::cerr << "Unable to bind to socket (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")! "
<< error.message() << std::endl;
log.error << "Unable to bind to socket (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")! "
<< error.message() << std::endl;

throw SocketBindError(error.message());
}
else
{
std::cout << "Connection established" << std::endl;
log.info << "Connection established" << std::endl;

startDataRead();
}
});
}
catch (boost::system::system_error& e)
{
std::cerr << "Unable to resolve host (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")! "
<< e.what() << std::endl;
log.error << "Unable to resolve host (" << host_query_.host_name()
<< ":" << host_query_.service_name() << ")! "
<< e.what() << std::endl;
throw SocketBindError(e.what());
}
}
Expand All @@ -177,8 +179,8 @@ namespace quanergy
}
else if (error)
{
std::cerr << "Error reading header: "
<< error.message() << std::endl;
log.error << "Error reading header: "
<< error.message() << std::endl;
throw SocketReadError(error.message());
}
else
Expand Down Expand Up @@ -213,8 +215,9 @@ namespace quanergy
}
else if (error)
{
std::cerr << "Error reading body: "
<< error.message() << std::endl;
log.error << "Error reading body: "
<< error.message() << std::endl;

throw SocketReadError(error.message());
}
else
Expand All @@ -227,7 +230,7 @@ namespace quanergy
while (buff_queue_.size() > max_queue_size_)
{
buff_queue_.pop();
std::cout << "Warning: Client dropped packet due to full buffer" << std::endl;
log.warn << "Client dropped packet due to full buffer" << std::endl;
}
lk.unlock();

Expand Down
7 changes: 4 additions & 3 deletions include/quanergy/client/packet_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <quanergy/client/exceptions.h>

#include <quanergy/common/notifications.h>

#include <iostream>

namespace quanergy
Expand Down Expand Up @@ -98,9 +100,8 @@ namespace quanergy
{
if (deserialize(object.signature) != SIGNATURE)
{
std::cerr << "Invalid header signature: " << std::hex << std::showbase
<< object.signature << std::dec << std::noshowbase << std::endl;

log.error << "Invalid header signature: " << std::hex << std::showbase
<< object.signature << std::dec << std::noshowbase << std::endl;
return false;
}

Expand Down
120 changes: 120 additions & 0 deletions include/quanergy/common/notifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/****************************************************************
** **
** Copyright(C) 2020 Quanergy Systems. All Rights Reserved. **
** Contact: http://www.quanergy.com **
** **
****************************************************************/

/** \file notifications.h
* \brief Define classes for handling notifications.
*
*/

#ifndef QUANERGY_NOTIFICATIONS_H
#define QUANERGY_NOTIFICATIONS_H

#include <iostream>
#include <sstream>

#include <quanergy/common/dll_export.h>

namespace quanergy
{
/** \brief severity level of the notification */
enum class NotificationLevel { Trace, Debug, Info, Warn, Error };

/** \brief string buffer for notifier */
class DLLEXPORT NotifierBuf : public std::stringbuf
{
public:
/** \brief constructor
* \param name notifier name printed in output
* \param level notifier level printed in output
*/
NotifierBuf(std::string name, NotificationLevel level);

/** \brief called on flush allowing us to forward to sink_ */
virtual int sync() override;

/** \brief set the downstream sink */
void SetSink(std::ostream* sink) { sink_ = sink; }

/** \brief enable formatting on output (time, level, name) */
void EnableFormatting(bool enable = true) { add_formatting_ = enable; }

protected:
/** \brief sink to stream to on flush */
std::ostream* sink_ = nullptr;

/** \brief flag determining whether to include time, level, and name */
bool add_formatting_ = true;

/** \brief name used for formatting */
std::string name_;

/** \brief level string used for formatting */
std::string level_string_;
};

/** \brief ostream for notifier */
class DLLEXPORT NotifierStream : public std::ostream
{
public:
/** \brief constructor
* \param name notifier name printed in output
* \param level notifier level printed in output
*/
NotifierStream(std::string name, NotificationLevel level);

/** \brief set the downstream sink */
void SetSink(std::ostream* sink) { buf_.SetSink(sink); }

/** \brief enable formatting on output (time, level, name) */
void EnableFormatting(bool enable = true) { buf_.EnableFormatting(enable); }

protected:
/** \brief the notification buffer for this stream */
NotifierBuf buf_;
};

/** \brief class to keep the various streams
* by default, error streams to cerr and info and warn stream to cout
*/
class DLLEXPORT Notifier
{
public:
/** \brief constructor
* \param name notifier name printed in output
*/
Notifier(std::string name);

/** \brief set the downstream sink for one or more streams */
void SetSinks(std::ostream* sink, NotificationLevel minLevel = NotificationLevel::Trace, NotificationLevel maxLevel = NotificationLevel::Error);
/** \brief set the downstream sink for one stream */
void SetSink(std::ostream* sink, NotificationLevel level);

/** \brief clear the downstream sink for one or more streams */
void ClearSinks(NotificationLevel minLevel = NotificationLevel::Trace, NotificationLevel maxLevel = NotificationLevel::Error);
/** \brief clear the downstream sink for one stream */
void ClearSink(NotificationLevel level);

/** \brief enable formatting on output (time, level, name) */
void EnableFormatting(bool enable = true);

/** \brief the error stream */
NotifierStream error;
/** \brief the warn stream */
NotifierStream warn;
/** \brief the info stream */
NotifierStream info;
/** \brief the debug stream */
NotifierStream debug;
/** \brief the info stream */
NotifierStream trace;
};

static Notifier log {"QuanergyClient"};

} // namespace quanergy

#endif
5 changes: 3 additions & 2 deletions include/quanergy/parsers/data_packet_01.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ namespace quanergy
sizeof(DataHeader01) +
object.data_header.point_count * sizeof(DataPoint01))
{
std::cerr << "Invalid sizes: " << object.data_header.point_count
std::stringstream ss;
ss << "Invalid sizes: " << object.data_header.point_count
<< " points and " << object.packet_header.size << " bytes" << std::endl;
throw SizeMismatchError();
throw SizeMismatchError(ss.str());
}

object.data_points.resize(object.data_header.point_count);
Expand Down
4 changes: 3 additions & 1 deletion include/quanergy/pipelines/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <atomic>
#include <iostream>

#include <quanergy/common/notifications.h>

namespace quanergy
{
namespace pipeline
Expand Down Expand Up @@ -88,7 +90,7 @@ namespace quanergy
// while shouldn't be necessary but doesn't hurt just to be sure
while (input_queue_.size() > max_queue_size_)
{
std::cerr << "Warning: AsyncModule dropped input due to full buffer" << std::endl;
log.warn << "AsyncModule dropped input due to full buffer" << std::endl;
input_queue_.pop();
}

Expand Down
7 changes: 5 additions & 2 deletions src/client/device_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

// Handle notifications
#include <quanergy/common/notifications.h>

using namespace quanergy::client;

DeviceInfo::DeviceInfo(const std::string& host)
Expand All @@ -25,7 +28,7 @@ DeviceInfo::DeviceInfo(const std::string& host)
std::stringstream device_info_stream;

// get deviceInfo from sensor for calibration
std::cout << "Attempting to get device info from " << host << std::endl;
quanergy::log.info << "Attempting to get device info from " << host << "..." << std::endl;
http_client.read(device_info_path_, device_info_stream);
boost::property_tree::ptree device_info_tree;
boost::property_tree::read_xml(device_info_stream, device_info_tree);
Expand Down Expand Up @@ -63,7 +66,7 @@ DeviceInfo::DeviceInfo(const std::string& host)
} // if laser data

} // if cal data

quanergy::log.info << "Device info retrieval complete." << std::endl;
} // constructor


Expand Down
Loading