History

TimeSeriesPlatFormLibrary: Improved auto-reconnect handling.

J. Ritchie Carroll committed 8 years ago

Commit b2b5ad51 parent 8d7dcfe9 files · +336 −292Browse files

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

Unified · Split

Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp+40−10
@@ -34,7 +34,7 @@ using namespace GSF::TimeSeries::Transport; · 33 unmodified lines
3434
3535 DataSubscriber Subscriber;
3636
37−void RunSubscriber(string hostname, uint16_t port);
37+bool RunSubscriber(string hostname, uint16_t port);
3838 void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements);
3939 void DisplayStatusMessage(DataSubscriber* source, const string& message);
4040 void DisplayErrorMessage(DataSubscriber* source, const string& message);
@@ -67,11 +67,12 @@ int main(int argc, char* argv[]) · 26 unmodified lines
6767 stringstream(argv[2]) >> port;
6868
6969 // Run the subscriber.
70− RunSubscriber(hostname, port);
71−
72− // Wait until the user presses enter before quitting.
73− string line;
74− getline(cin, line);
70+ if (RunSubscriber(hostname, port))
71+ {
72+ // Wait until the user presses enter before quitting.
73+ string line;
74+ getline(cin, line);
75+ }
7576
7677 // Disconnect the subscriber to stop background threads.
7778 Subscriber.Disconnect();
@@ -85,7 +86,7 @@ int main(int argc, char* argv[]) · 7 unmodified lines
8586 // - Register callbacks
8687 // - Connect to publisher
8788 // - Subscribe
88−void RunSubscriber(string hostname, uint16_t port)
89+bool RunSubscriber(string hostname, uint16_t port)
8990 {
9091 // SubscriptionInfo is a helper object which allows the user
9192 // to set up their subscription and reuse subscription settings.
@@ -98,10 +99,39 @@ void RunSubscriber(string hostname, uint16_t port) · 6 unmodified lines
9899 Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements);
99100
100101 cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl;
101− Subscriber.Connect(hostname, port);
102102
103− cout << "Connected! Subscribing to data..." << endl << endl;
104− Subscriber.Subscribe(info);
103+ string errorMessage;
104+ bool connected = false;
105+
106+ try
107+ {
108+ Subscriber.Connect(hostname, port);
109+ connected = true;
110+ }
111+ catch (SubscriberException& ex)
112+ {
113+ errorMessage = ex.what();
114+ }
115+ catch (SystemError& ex)
116+ {
117+ errorMessage = ex.what();
118+ }
119+ catch (...)
120+ {
121+ errorMessage = current_exception_diagnostic_information(true);
122+ }
123+
124+ if (connected)
125+ {
126+ cout << "Connected! Subscribing to data..." << endl << endl;
127+ Subscriber.Subscribe(info);
128+ }
129+ else
130+ {
131+ cerr << "Failed to connect to \"" << hostname << ":" << port << "\": " << errorMessage;
132+ }
133+
134+ return connected;
105135 }
106136
107137 // Callback which is called when the subscriber has

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