Skip to content

Commit ea8031a

Browse files
authored
Merge pull request #36 from PolySync/json-object-list-construction
Json object list construction
2 parents 8492c6c + 21037f0 commit ea8031a

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

Echo/Echo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* \example Echo.cpp
2929
*
3030
* PolySync Echo C++ API example application
31-
*
31+
*
3232
* Demonstrates a command line utility for echoing PolySync messges to stdout
3333
*
3434
* @file Echo.cpp
@@ -45,14 +45,15 @@
4545
*
4646
* @param argc - int, the number of parameters on the command-line
4747
* @param argv - char* [], the parsed command-line arguments
48-
*
48+
*
4949
* @return int - exit code
5050
*/
5151
int main( int argc, char *argv[] )
5252
{
5353
// Create an instance of the PolySyncEcho and connect it to PolySync.
5454
PolySyncEcho echo;
5555

56+
5657
// Nodes will only connect if help option -h not used,
5758
// and, if all arguments are valid.
5859
if ( echo.optionsParse( argc, argv ) )

Echo/EchoNode.cpp

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
void PolySyncEcho::initStateEvent()
10-
{
10+
{
1111
_applicationStartTime = polysync::getTimestamp();
1212

1313
if( _inputHandler.messageTypesWereFiltered() )
@@ -22,6 +22,18 @@ void PolySyncEcho::initStateEvent()
2222
{
2323
registerListenerToAllMessageTypes();
2424
}
25+
26+
std::cout << "{\"polysync-echo\":[";
27+
28+
if( _inputHandler.fileWasSpecified() )
29+
{
30+
_openUserFile.open( _inputHandler.getFileName(), std::ios::app );
31+
32+
if( _openUserFile )
33+
{
34+
_openUserFile << "{\"polysync-echo\":[";
35+
}
36+
}
2537
}
2638

2739

@@ -50,6 +62,18 @@ void PolySyncEcho::okStateEvent()
5062
polysync::sleepMicro( SecondsToMicro );
5163
}
5264

65+
void PolySyncEcho::releaseStateEvent()
66+
{
67+
std::cout << "]}" << std::endl;
68+
69+
if( _openUserFile )
70+
{
71+
_openUserFile << "]}" << std::endl;
72+
}
73+
74+
_openUserFile.close();
75+
}
76+
5377

5478
void PolySyncEcho::registerFilteredMessages()
5579
{
@@ -87,7 +111,7 @@ void PolySyncEcho::messageEvent( std::shared_ptr< polysync::Message > message )
87111
return;
88112
}
89113

90-
if( _inputHandler.fileWasSpecified() )
114+
if( _openUserFile )
91115
{
92116
printToFile( message );
93117
}
@@ -99,26 +123,42 @@ void PolySyncEcho::messageEvent( std::shared_ptr< polysync::Message > message )
99123
void PolySyncEcho::printToFile(
100124
std::shared_ptr < polysync:: Message > message )
101125
{
102-
std::ofstream openUserFile;
126+
static bool is_first_print = true;
103127

104-
openUserFile.open( _inputHandler.getFileName(), std::ios::app );
128+
if( is_first_print )
129+
{
130+
is_first_print = false;
131+
}
132+
else
133+
{
134+
_openUserFile << ",";
135+
}
105136

106137
if( _inputHandler.headersWereRequested() )
107138
{
108-
message->printHeader( openUserFile );
139+
message->printHeader( _openUserFile );
109140
}
110141
else
111142
{
112-
message->print( openUserFile );
143+
message->print( _openUserFile );
113144
}
114-
115-
openUserFile.close();
116145
}
117146

118147

119148
void PolySyncEcho::printMessage(
120149
std::shared_ptr < polysync:: Message > message ) const
121150
{
151+
static bool is_first_print = true;
152+
153+
if( is_first_print )
154+
{
155+
is_first_print = false;
156+
}
157+
else
158+
{
159+
std::cout << ",";
160+
}
161+
122162
if( _inputHandler.headersWereRequested() )
123163
{
124164
message->printHeader();

Echo/EchoNode.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#ifndef POLYSYNC_ECHO_HPP
99
#define POLYSYNC_ECHO_HPP
1010

11+
#include <fstream>
12+
1113
#include <PolySyncDataModel.hpp>
1214

1315
#include "ApplicationInputHandler.hpp"
1416
#include "EchoHelp.hpp"
1517

16-
1718
/**
1819
* @brief PolySyncEcho class
1920
*
@@ -41,6 +42,8 @@ class PolySyncEcho : public polysync::Node
4142
*/
4243
void okStateEvent() override;
4344

45+
void releaseStateEvent() override;
46+
4447
/**
4548
* @brief Register filtered message type(s) per cmd line input.
4649
*/
@@ -96,7 +99,6 @@ class PolySyncEcho : public polysync::Node
9699
*/
97100
void printEchoHelpPage() const;
98101

99-
100102
private:
101103

102104
/**
@@ -162,6 +164,10 @@ class PolySyncEcho : public polysync::Node
162164
*/
163165
ps_timestamp _applicationStartTime;
164166

167+
/**
168+
* Output file stream if option set.
169+
*/
170+
std::ofstream _openUserFile;
165171
}; // END polysync::EchoNode class
166172

167173

0 commit comments

Comments
 (0)