Skip to content

Json object list construction #36

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

Merged
merged 2 commits into from
Oct 31, 2016
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
5 changes: 3 additions & 2 deletions Echo/Echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,14 +45,15 @@
*
* @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[] )
{
// 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 ) )
Expand Down
56 changes: 48 additions & 8 deletions Echo/EchoNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


void PolySyncEcho::initStateEvent()
{
{
_applicationStartTime = polysync::getTimestamp();

if( _inputHandler.messageTypesWereFiltered() )
Expand All @@ -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\":[";
}
}
}


Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -87,7 +111,7 @@ void PolySyncEcho::messageEvent( std::shared_ptr< polysync::Message > message )
return;
}

if( _inputHandler.fileWasSpecified() )
if( _openUserFile )
{
printToFile( message );
}
Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions Echo/EchoNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#ifndef POLYSYNC_ECHO_HPP
#define POLYSYNC_ECHO_HPP

#include <fstream>

#include <PolySyncDataModel.hpp>

#include "ApplicationInputHandler.hpp"
#include "EchoHelp.hpp"


/**
* @brief PolySyncEcho class
*
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -96,7 +99,6 @@ class PolySyncEcho : public polysync::Node
*/
void printEchoHelpPage() const;


private:

/**
Expand Down Expand Up @@ -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


Expand Down