Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp (1 of 8 files). Show all
Unified · Split
| @@ -33,11 +33,11 @@ using namespace GSF; · 32 unmodified lines | ||
| 33 | 33 | using namespace GSF::TimeSeries; |
| 34 | 34 | using namespace GSF::TimeSeries::Transport; |
| 35 | 35 | |
| 36 | −DataSubscriber Subscriber; | |
| 36 | +DataSubscriber* Subscriber; | |
| 37 | 37 | SubscriptionInfo Info; |
| 38 | 38 | |
| 39 | 39 | // Create helper objects for subscription. |
| 40 | −void SetupSubscriberConnector(SubscriberConnector& connector, string hostname, uint16_t port); | |
| 40 | +void SetupSubscriberConnector(SubscriberConnector& connector, const string& hostname, uint16_t port); | |
| 41 | 41 | SubscriptionInfo CreateSubscriptionInfo(); |
| 42 | 42 | |
| 43 | 43 | // Handlers for subscriber callbacks. |
| @@ -47,7 +47,7 @@ void DisplayStatusMessage(DataSubscriber* source, const string& message); · 3 unmodified lines | ||
| 47 | 47 | void DisplayErrorMessage(DataSubscriber* source, const string& message); |
| 48 | 48 | |
| 49 | 49 | // Runs the subscriber. |
| 50 | −void RunSubscriber(string hostname, uint16_t port); | |
| 50 | +void RunSubscriber(const string& hostname, uint16_t port); | |
| 51 | 51 | |
| 52 | 52 | // Sample application to demonstrate the more advanced use of the subscriber API. |
| 53 | 53 | // |
| @@ -77,6 +77,9 @@ int main(int argc, char* argv[]) · 23 unmodified lines | ||
| 77 | 77 | hostname = argv[1]; |
| 78 | 78 | stringstream(argv[2]) >> port; |
| 79 | 79 | |
| 80 | + // Maintain the life-time of DataSubscriber within main | |
| 81 | + Subscriber = new DataSubscriber(); | |
| 82 | + | |
| 80 | 83 | // Run the subscriber. |
| 81 | 84 | RunSubscriber(hostname, port); |
| 82 | 85 | |
| @@ -85,9 +88,11 @@ int main(int argc, char* argv[]) · 2 unmodified lines | ||
| 85 | 88 | getline(cin, line); |
| 86 | 89 | |
| 87 | 90 | // Disconnect the subscriber to stop background threads. |
| 88 | − Subscriber.Disconnect(); | |
| 91 | + Subscriber->Disconnect(); | |
| 89 | 92 | cout << "Disconnected." << endl; |
| 90 | 93 | |
| 94 | + delete Subscriber; | |
| 95 | + | |
| 91 | 96 | return 0; |
| 92 | 97 | } |
| 93 | 98 | |
| @@ -96,26 +101,26 @@ int main(int argc, char* argv[]) · 2 unmodified lines | ||
| 96 | 101 | // - Register callbacks |
| 97 | 102 | // - Connect to publisher |
| 98 | 103 | // - Subscribe |
| 99 | −void RunSubscriber(string hostname, uint16_t port) | |
| 104 | +void RunSubscriber(const string& hostname, uint16_t port) | |
| 100 | 105 | { |
| 101 | − SubscriberConnector& connector = Subscriber.GetSubscriberConnector(); | |
| 106 | + SubscriberConnector& connector = Subscriber->GetSubscriberConnector(); | |
| 102 | 107 | |
| 103 | 108 | // Set up helper objects |
| 104 | 109 | SetupSubscriberConnector(connector, hostname, port); |
| 105 | 110 | Info = CreateSubscriptionInfo(); |
| 106 | 111 | |
| 107 | 112 | // Register callbacks |
| 108 | − Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | |
| 109 | − Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 110 | − Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); | |
| 113 | + Subscriber->RegisterStatusMessageCallback(&DisplayStatusMessage); | |
| 114 | + Subscriber->RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 115 | + Subscriber->RegisterNewMeasurementsCallback(&ProcessMeasurements); | |
| 111 | 116 | |
| 112 | 117 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; |
| 113 | 118 | |
| 114 | 119 | // Connect and subscribe to publisher |
| 115 | − if (connector.Connect(Subscriber, Info)) | |
| 120 | + if (connector.Connect(*Subscriber, Info)) | |
| 116 | 121 | { |
| 117 | 122 | cout << "Connected! Subscribing to data..." << endl << endl; |
| 118 | − Subscriber.Subscribe(); | |
| 123 | + Subscriber->Subscribe(); | |
| 119 | 124 | } |
| 120 | 125 | else |
| 121 | 126 | { |
| @@ -169,7 +174,7 @@ SubscriptionInfo CreateSubscriptionInfo() · 47 unmodified lines | ||
| 169 | 174 | return info; |
| 170 | 175 | } |
| 171 | 176 | |
| 172 | −void SetupSubscriberConnector(SubscriberConnector& connector, string hostname, uint16_t port) | |
| 177 | +void SetupSubscriberConnector(SubscriberConnector& connector, const string& hostname, uint16_t port) | |
| 173 | 178 | { |
| 174 | 179 | // SubscriberConnector is another helper object which allows the |
| 175 | 180 | // user to modify settings for auto-reconnects and retry cycles. |