diff --git a/Echo/Echo.cpp b/Echo/Echo.cpp index d1f3d39..c453a9a 100644 --- a/Echo/Echo.cpp +++ b/Echo/Echo.cpp @@ -28,7 +28,7 @@ * \example Echo.cpp * * PolySync Echo C++ API example application - * + * * Demonstrates a command line utility for echoing PolySync messges to stdout * * @file Echo.cpp @@ -45,7 +45,7 @@ * * @param argc - int, the number of parameters on the command-line * @param argv - char* [], the parsed command-line arguments - * + * * @return int - exit code */ int main( int argc, char *argv[] ) @@ -53,6 +53,7 @@ int main( int argc, char *argv[] ) // Create an instance of the PolySyncEcho and connect it to PolySync. PolySyncEcho echo; + // Nodes will only connect if help option -h not used, // and, if all arguments are valid. if ( echo.optionsParse( argc, argv ) ) diff --git a/Echo/EchoNode.cpp b/Echo/EchoNode.cpp index a20a5fd..c0aa465 100644 --- a/Echo/EchoNode.cpp +++ b/Echo/EchoNode.cpp @@ -7,7 +7,7 @@ void PolySyncEcho::initStateEvent() -{ +{ _applicationStartTime = polysync::getTimestamp(); if( _inputHandler.messageTypesWereFiltered() ) @@ -22,6 +22,18 @@ void PolySyncEcho::initStateEvent() { registerListenerToAllMessageTypes(); } + + std::cout << "{\"polysync-echo\":["; + + if( _inputHandler.fileWasSpecified() ) + { + _openUserFile.open( _inputHandler.getFileName(), std::ios::app ); + + if( _openUserFile ) + { + _openUserFile << "{\"polysync-echo\":["; + } + } } @@ -50,6 +62,18 @@ void PolySyncEcho::okStateEvent() polysync::sleepMicro( SecondsToMicro ); } +void PolySyncEcho::releaseStateEvent() +{ + std::cout << "]}" << std::endl; + + if( _openUserFile ) + { + _openUserFile << "]}" << std::endl; + } + + _openUserFile.close(); +} + void PolySyncEcho::registerFilteredMessages() { @@ -87,7 +111,7 @@ void PolySyncEcho::messageEvent( std::shared_ptr< polysync::Message > message ) return; } - if( _inputHandler.fileWasSpecified() ) + if( _openUserFile ) { printToFile( message ); } @@ -99,26 +123,42 @@ void PolySyncEcho::messageEvent( std::shared_ptr< polysync::Message > message ) void PolySyncEcho::printToFile( std::shared_ptr < polysync:: Message > message ) { - std::ofstream openUserFile; + static bool is_first_print = true; - openUserFile.open( _inputHandler.getFileName(), std::ios::app ); + if( is_first_print ) + { + is_first_print = false; + } + else + { + _openUserFile << ","; + } if( _inputHandler.headersWereRequested() ) { - message->printHeader( openUserFile ); + message->printHeader( _openUserFile ); } else { - message->print( openUserFile ); + message->print( _openUserFile ); } - - openUserFile.close(); } void PolySyncEcho::printMessage( std::shared_ptr < polysync:: Message > message ) const { + static bool is_first_print = true; + + if( is_first_print ) + { + is_first_print = false; + } + else + { + std::cout << ","; + } + if( _inputHandler.headersWereRequested() ) { message->printHeader(); diff --git a/Echo/EchoNode.hpp b/Echo/EchoNode.hpp index 29079eb..cd82fbc 100644 --- a/Echo/EchoNode.hpp +++ b/Echo/EchoNode.hpp @@ -8,12 +8,13 @@ #ifndef POLYSYNC_ECHO_HPP #define POLYSYNC_ECHO_HPP +#include + #include #include "ApplicationInputHandler.hpp" #include "EchoHelp.hpp" - /** * @brief PolySyncEcho class * @@ -41,6 +42,8 @@ class PolySyncEcho : public polysync::Node */ void okStateEvent() override; + void releaseStateEvent() override; + /** * @brief Register filtered message type(s) per cmd line input. */ @@ -96,7 +99,6 @@ class PolySyncEcho : public polysync::Node */ void printEchoHelpPage() const; - private: /** @@ -162,6 +164,10 @@ class PolySyncEcho : public polysync::Node */ ps_timestamp _applicationStartTime; + /** + * Output file stream if option set. + */ + std::ofstream _openUserFile; }; // END polysync::EchoNode class