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