Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp (1 of 9 files). Show all
Unified · Split
| @@ -34,7 +34,7 @@ using namespace GSF::TimeSeries::Transport; · 33 unmodified lines | |||
| 34 | 34 | ||
| 35 | DataSubscriber Subscriber; | 35 | DataSubscriber Subscriber; |
| 36 | 36 | ||
| 37 | void RunSubscriber(string hostname, uint16_t port); | 37 | bool RunSubscriber(string hostname, uint16_t port); |
| 38 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements); | 38 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements); |
| 39 | void DisplayStatusMessage(DataSubscriber* source, const string& message); | 39 | void DisplayStatusMessage(DataSubscriber* source, const string& message); |
| 40 | void DisplayErrorMessage(DataSubscriber* source, const string& message); | 40 | void DisplayErrorMessage(DataSubscriber* source, const string& message); |
| @@ -67,11 +67,12 @@ int main(int argc, char* argv[]) · 26 unmodified lines | |||
| 67 | stringstream(argv[2]) >> port; | 67 | stringstream(argv[2]) >> port; |
| 68 | 68 | ||
| 69 | // Run the subscriber. | 69 | // Run the subscriber. |
| 70 | RunSubscriber(hostname, port); | 70 | if (RunSubscriber(hostname, port)) |
| 71 | 71 | { | |
| 72 | // Wait until the user presses enter before quitting. | 72 | // Wait until the user presses enter before quitting. |
| 73 | string line; | 73 | string line; |
| 74 | getline(cin, line); | 74 | getline(cin, line); |
| 75 | } | ||
| 75 | 76 | ||
| 76 | // Disconnect the subscriber to stop background threads. | 77 | // Disconnect the subscriber to stop background threads. |
| 77 | Subscriber.Disconnect(); | 78 | Subscriber.Disconnect(); |
| @@ -85,7 +86,7 @@ int main(int argc, char* argv[]) · 7 unmodified lines | |||
| 85 | // - Register callbacks | 86 | // - Register callbacks |
| 86 | // - Connect to publisher | 87 | // - Connect to publisher |
| 87 | // - Subscribe | 88 | // - Subscribe |
| 88 | void RunSubscriber(string hostname, uint16_t port) | 89 | bool RunSubscriber(string hostname, uint16_t port) |
| 89 | { | 90 | { |
| 90 | // SubscriptionInfo is a helper object which allows the user | 91 | // SubscriptionInfo is a helper object which allows the user |
| 91 | // to set up their subscription and reuse subscription settings. | 92 | // to set up their subscription and reuse subscription settings. |
| @@ -98,10 +99,39 @@ void RunSubscriber(string hostname, uint16_t port) · 6 unmodified lines | |||
| 98 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); | 99 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); |
| 99 | 100 | ||
| 100 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | 101 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; |
| 101 | Subscriber.Connect(hostname, port); | ||
| 102 | 102 | ||
| 103 | cout << "Connected! Subscribing to data..." << endl << endl; | 103 | string errorMessage; |
| 104 | Subscriber.Subscribe(info); | 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; | ||
| 105 | } | 135 | } |
| 106 | 136 | ||
| 107 | // Callback which is called when the subscriber has | 137 | // Callback which is called when the subscriber has |