History

TimeSeriesPlatformLibrary: (1) Updated library and samples so that they run on Windows, (2) Overall code cleanup, bug fixes and improvements,

J. Ritchie Carroll committed 8 years ago

Commit 4c65c881 parent 713fa6232 files · +1496 −869Browse files

Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp (1 of 32 files). Show all

Unified · Split

Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp+47−25
@@ -26,16 +26,18 @@ · 25 unmodified lines
2626 #include <vector>
2727
2828 #include "../Common/Convert.h"
29−#include "../Common/Measurement.h"
3029 #include "../Transport/DataSubscriber.h"
3130
32−namespace gsfts = GSF::TimeSeries;
33−namespace tst = gsfts::Transport;
31+using namespace std;
32+using namespace GSF::TimeSeries;
33+using namespace GSF::TimeSeries::Transport;
3434
35−tst::DataSubscriber Subscriber;
35+DataSubscriber Subscriber;
3636
37−void RunSubscriber(std::string hostname, gsfts::uint16_t port);
38−void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements);
37+void RunSubscriber(string hostname, uint16_t port);
38+void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements);
39+void DisplayStatusMessage(DataSubscriber* source, string message);
40+void DisplayErrorMessage(DataSubscriber* source, string message);
3941
4042 // Sample application to demonstrate the most simple use of the subscriber API.
4143 //
@@ -48,31 +50,32 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 6 unmodified lines
4850 // Measurements are transmitted via the TCP command channel.
4951 int main(int argc, char* argv[])
5052 {
51− std::string hostname;
52− gsfts::uint16_t port;
53+ string hostname;
54+ uint16_t port;
5355
5456 // Ensure that the necessary
5557 // command line arguments are given.
5658 if (argc < 3)
5759 {
58− std::cout << "Usage:" << std::endl;
59− std::cout << " SimpleSubscribe HOSTNAME PORT" << std::endl;
60+ cout << "Usage:" << endl;
61+ cout << " SimpleSubscribe HOSTNAME PORT" << endl;
6062 return 0;
6163 }
6264
6365 // Get hostname and port.
6466 hostname = argv[1];
65− std::stringstream(argv[2]) >> port;
67+ stringstream(argv[2]) >> port;
6668
6769 // Run the subscriber.
6870 RunSubscriber(hostname, port);
6971
7072 // Wait until the user presses enter before quitting.
71− std::string line;
72− std::getline(std::cin, line);
73+ string line;
74+ getline(cin, line);
7375
7476 // Disconnect the subscriber to stop background threads.
7577 Subscriber.Disconnect();
78+ cout << "Disconnected." << endl;
7679
7780 return 0;
7881 }
@@ -82,27 +85,34 @@ int main(int argc, char* argv[]) · 3 unmodified lines
8285 // - Register callbacks
8386 // - Connect to publisher
8487 // - Subscribe
85−void RunSubscriber(std::string hostname, gsfts::uint16_t port)
88+void RunSubscriber(string hostname, uint16_t port)
8689 {
8790 // SubscriptionInfo is a helper object which allows the user
8891 // to set up their subscription and reuse subscription settings.
89− tst::SubscriptionInfo info;
92+ SubscriptionInfo info;
9093 info.FilterExpression = "PPA:1;PPA:2;PPA:3;PPA:4;PPA:5;PPA:6;PPA:7;PPA:8;PPA:9;PPA:10;PPA:11;PPA:12;PPA:13;PPA:14";
9194
95+ // Register callbacks
96+ Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage);
97+ Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage);
9298 Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements);
99+
100+ cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl;
93101 Subscriber.Connect(hostname, port);
102+
103+ cout << "Connected! Subscribing to data..." << endl << endl;
94104 Subscriber.Subscribe(info);
95105 }
96106
97107 // Callback which is called when the subscriber has
98108 // received a new packet of measurements from the publisher.
99−void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements)
109+void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements)
100110 {
101− const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
102− const std::size_t MaxTimestampSize = 80;
111+ const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
112+ const size_t MaxTimestampSize = 80;
103113
104114 static int processCount = 0;
105− std::size_t i;
115+ size_t i;
106116
107117 char timestamp[MaxTimestampSize];
108118
@@ -110,21 +120,33 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line
110120 // seconds (assuming 30 calls per second).
111121 if (processCount % 150 == 0)
112122 {
113− std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl;
123+ cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << endl;
114124
115125 if (newMeasurements.size() > 0)
116126 {
117− if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
118− std::cout << "Timestamp: " << std::string(timestamp) << std::endl;
127+ if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
128+ cout << "Timestamp: " << string(timestamp) << endl;
119129
120− std::cout << "Point\tValue" << std::endl;
130+ cout << "Point\tValue" << endl;
121131
122132 for (i = 0; i < newMeasurements.size(); ++i)
123− std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl;
133+ cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << endl;
124134
125− std::cout << std::endl;
135+ cout << endl;
126136 }
127137 }
128138
129139 ++processCount;
140+}
141+
142+// Callback which is called to display status messages from the subscriber.
143+void DisplayStatusMessage(DataSubscriber* source, string message)
144+{
145+ cout << message << endl << endl;
146+}
147+
148+// Callback which is called to display error messages from the connector and subscriber.
149+void DisplayErrorMessage(DataSubscriber* source, string message)
150+{
151+ cerr << message << endl << endl;
130152 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t