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/AdvancedSubscribe.cpp (1 of 32 files). Show all

Unified · Split

Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp+57−42
@@ -26,27 +26,26 @@ · 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 GSF::TimeSeries;
32+using namespace GSF::TimeSeries::Transport;
3433
35−tst::DataSubscriber Subscriber;
36−tst::SubscriptionInfo Info;
34+DataSubscriber Subscriber;
35+SubscriptionInfo Info;
3736
3837 // Create helper objects for subscription.
39−tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, uint16_t port);
40−tst::SubscriptionInfo CreateSubscriptionInfo();
38+SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port);
39+SubscriptionInfo CreateSubscriptionInfo();
4140
4241 // Handlers for subscriber callbacks.
43−void Resubscribe(tst::DataSubscriber* source);
44−void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements);
45−void DisplayStatusMessage(tst::DataSubscriber* source, std::string message);
46−void DisplayErrorMessage(tst::DataSubscriber* source, std::string message);
42+void Resubscribe(DataSubscriber* source);
43+void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements);
44+void DisplayStatusMessage(DataSubscriber* source, string message);
45+void DisplayErrorMessage(DataSubscriber* source, string message);
4746
4847 // Runs the subscriber.
49−void RunSubscriber(std::string hostname, gsfts::uint16_t port);
48+void RunSubscriber(string hostname, uint16_t port);
5049
5150 // Sample application to demonstrate the more advanced use of the subscriber API.
5251 //
@@ -60,31 +59,32 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port); · 7 unmodified lines
6059 // Measurements are transmitted via a separate UDP data channel.
6160 int main(int argc, char* argv[])
6261 {
63− std::string hostname;
64− gsfts::uint16_t port;
62+ string hostname;
63+ uint16_t port;
6564
6665 // Ensure that the necessary
6766 // command line arguments are given.
6867 if (argc < 3)
6968 {
70− std::cout << "Usage:" << std::endl;
71− std::cout << " AdvancedSubscribe HOSTNAME PORT" << std::endl;
69+ cout << "Usage:" << endl;
70+ cout << " AdvancedSubscribe HOSTNAME PORT" << endl;
7271 return 0;
7372 }
7473
7574 // Get hostname and port.
7675 hostname = argv[1];
77− std::stringstream(argv[2]) >> port;
76+ stringstream(argv[2]) >> port;
7877
7978 // Run the subscriber.
8079 RunSubscriber(hostname, port);
8180
8281 // Wait until the user presses enter before quitting.
83− std::string line;
84− std::getline(std::cin, line);
82+ string line;
83+ getline(cin, line);
8584
8685 // Disconnect the subscriber to stop background threads.
8786 Subscriber.Disconnect();
87+ cout << "Disconnected." << endl;
8888
8989 return 0;
9090 }
@@ -94,11 +94,11 @@ int main(int argc, char* argv[]) · 3 unmodified lines
9494 // - Register callbacks
9595 // - Connect to publisher
9696 // - Subscribe
97−void RunSubscriber(std::string hostname, gsfts::uint16_t port)
97+void RunSubscriber(string hostname, uint16_t port)
9898 {
9999 // The connector is declared here because it
100100 // is only needed for the initial connection
101− tst::SubscriberConnector connector;
101+ SubscriberConnector connector;
102102
103103 // Set up helper objects
104104 connector = CreateSubscriberConnector(hostname, port);
@@ -108,18 +108,25 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port) · 3 unmodified lines
108108 Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage);
109109 Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage);
110110
111+ cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl;
112+
111113 // Connect and subscribe to publisher
112114 if (connector.Connect(Subscriber))
115+ {
116+ cout << "Connected! Subscribing to data..." << endl << endl;
113117 Subscriber.Subscribe(Info);
118+ }
114119 else
115− std::cerr << "All connection attempts failed" << std::endl;
120+ {
121+ cout << "Connection attempts exceeded. Press enter to exit." << endl;
122+ }
116123 }
117124
118−tst::SubscriptionInfo CreateSubscriptionInfo()
125+SubscriptionInfo CreateSubscriptionInfo()
119126 {
120127 // SubscriptionInfo is a helper object which allows the user
121128 // to set up their subscription and reuse subscription settings.
122− tst::SubscriptionInfo info;
129+ SubscriptionInfo info;
123130
124131 // The following filter expression formats are also available:
125132 //
@@ -162,11 +169,11 @@ tst::SubscriptionInfo CreateSubscriptionInfo() · 36 unmodified lines
162169 return info;
163170 }
164171
165−tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts::uint16_t port)
172+SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port)
166173 {
167174 // SubscriberConnector is another helper object which allows the
168175 // user to modify settings for auto-reconnects and retry cycles.
169− tst::SubscriberConnector connector;
176+ SubscriberConnector connector;
170177
171178 connector.RegisterErrorMessageCallback(&DisplayErrorMessage);
172179 connector.RegisterReconnectCallback(&Resubscribe);
@@ -174,7 +181,7 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 1 unmodified line
174181 connector.SetHostname(hostname);
175182 connector.SetPort(port);
176183 connector.SetMaxRetries(5);
177− connector.SetRetryInterval(1000);
184+ connector.SetRetryInterval(1500);
178185 connector.SetAutoReconnect(true);
179186
180187 return connector;
@@ -182,13 +189,13 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 1 unmodified line
182189
183190 // Callback which is called when the subscriber has
184191 // received a new packet of measurements from the publisher.
185−void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements)
192+void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements)
186193 {
187− const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
188− const std::size_t MaxTimestampSize = 80;
194+ const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
195+ const size_t MaxTimestampSize = 80;
189196
190197 static int processCount = 0;
191− std::size_t i;
198+ size_t i;
192199
193200 char timestamp[MaxTimestampSize];
194201
@@ -196,19 +203,19 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line
196203 // seconds (assuming 30 calls per second).
197204 if (processCount % 150 == 0)
198205 {
199− std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl;
206+ cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << endl;
200207
201208 if (newMeasurements.size() > 0)
202209 {
203− if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
204− std::cout << "Timestamp: " << std::string(timestamp) << std::endl;
210+ if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
211+ cout << "Timestamp: " << string(timestamp) << endl;
205212
206− std::cout << "Point\tValue" << std::endl;
213+ cout << "Point\tValue" << endl;
207214
208215 for (i = 0; i < newMeasurements.size(); ++i)
209− std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl;
216+ cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << endl;
210217
211− std::cout << std::endl;
218+ cout << endl;
212219 }
213220 }
214221
@@ -216,20 +223,28 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line
216223 }
217224
218225 // Callback that is called when the subscriber auto-reconnects.
219−void Resubscribe(tst::DataSubscriber* source)
226+void Resubscribe(DataSubscriber* source)
220227 {
221228 if (source->IsConnected())
229+ {
230+ cout << "Reconnected! Subscribing to data..." << endl << endl;
222231 source->Subscribe(Info);
232+ }
233+ else
234+ {
235+ source->Disconnect();
236+ cout << "Connection retry attempts exceeded. Press enter to exit." << endl;
237+ }
223238 }
224239
225240 // Callback which is called to display status messages from the subscriber.
226−void DisplayStatusMessage(tst::DataSubscriber* source, std::string message)
241+void DisplayStatusMessage(DataSubscriber* source, string message)
227242 {
228− std::cout << message << std::endl << std::endl;
243+ cout << message << endl << endl;
229244 }
230245
231246 // Callback which is called to display error messages from the connector and subscriber.
232−void DisplayErrorMessage(tst::DataSubscriber* source, std::string message)
247+void DisplayErrorMessage(DataSubscriber* source, string message)
233248 {
234− std::cerr << message << std::endl << std::endl;
249+ cerr << message << endl << endl;
235250 }

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