Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp (1 of 32 files). Show all
Unified · Split
| @@ -26,27 +26,26 @@ · 25 unmodified lines | ||
| 26 | 26 | #include <vector> |
| 27 | 27 | |
| 28 | 28 | #include "../Common/Convert.h" |
| 29 | −#include "../Common/Measurement.h" | |
| 30 | 29 | #include "../Transport/DataSubscriber.h" |
| 31 | 30 | |
| 32 | −namespace gsfts = GSF::TimeSeries; | |
| 33 | −namespace tst = gsfts::Transport; | |
| 31 | +using namespace GSF::TimeSeries; | |
| 32 | +using namespace GSF::TimeSeries::Transport; | |
| 34 | 33 | |
| 35 | −tst::DataSubscriber Subscriber; | |
| 36 | −tst::SubscriptionInfo Info; | |
| 34 | +DataSubscriber Subscriber; | |
| 35 | +SubscriptionInfo Info; | |
| 37 | 36 | |
| 38 | 37 | // 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(); | |
| 41 | 40 | |
| 42 | 41 | // 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); | |
| 47 | 46 | |
| 48 | 47 | // Runs the subscriber. |
| 49 | −void RunSubscriber(std::string hostname, gsfts::uint16_t port); | |
| 48 | +void RunSubscriber(string hostname, uint16_t port); | |
| 50 | 49 | |
| 51 | 50 | // Sample application to demonstrate the more advanced use of the subscriber API. |
| 52 | 51 | // |
| @@ -60,31 +59,32 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port); · 7 unmodified lines | ||
| 60 | 59 | // Measurements are transmitted via a separate UDP data channel. |
| 61 | 60 | int main(int argc, char* argv[]) |
| 62 | 61 | { |
| 63 | − std::string hostname; | |
| 64 | − gsfts::uint16_t port; | |
| 62 | + string hostname; | |
| 63 | + uint16_t port; | |
| 65 | 64 | |
| 66 | 65 | // Ensure that the necessary |
| 67 | 66 | // command line arguments are given. |
| 68 | 67 | if (argc < 3) |
| 69 | 68 | { |
| 70 | − std::cout << "Usage:" << std::endl; | |
| 71 | − std::cout << " AdvancedSubscribe HOSTNAME PORT" << std::endl; | |
| 69 | + cout << "Usage:" << endl; | |
| 70 | + cout << " AdvancedSubscribe HOSTNAME PORT" << endl; | |
| 72 | 71 | return 0; |
| 73 | 72 | } |
| 74 | 73 | |
| 75 | 74 | // Get hostname and port. |
| 76 | 75 | hostname = argv[1]; |
| 77 | − std::stringstream(argv[2]) >> port; | |
| 76 | + stringstream(argv[2]) >> port; | |
| 78 | 77 | |
| 79 | 78 | // Run the subscriber. |
| 80 | 79 | RunSubscriber(hostname, port); |
| 81 | 80 | |
| 82 | 81 | // 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); | |
| 85 | 84 | |
| 86 | 85 | // Disconnect the subscriber to stop background threads. |
| 87 | 86 | Subscriber.Disconnect(); |
| 87 | + cout << "Disconnected." << endl; | |
| 88 | 88 | |
| 89 | 89 | return 0; |
| 90 | 90 | } |
| @@ -94,11 +94,11 @@ int main(int argc, char* argv[]) · 3 unmodified lines | ||
| 94 | 94 | // - Register callbacks |
| 95 | 95 | // - Connect to publisher |
| 96 | 96 | // - Subscribe |
| 97 | −void RunSubscriber(std::string hostname, gsfts::uint16_t port) | |
| 97 | +void RunSubscriber(string hostname, uint16_t port) | |
| 98 | 98 | { |
| 99 | 99 | // The connector is declared here because it |
| 100 | 100 | // is only needed for the initial connection |
| 101 | − tst::SubscriberConnector connector; | |
| 101 | + SubscriberConnector connector; | |
| 102 | 102 | |
| 103 | 103 | // Set up helper objects |
| 104 | 104 | connector = CreateSubscriberConnector(hostname, port); |
| @@ -108,18 +108,25 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port) · 3 unmodified lines | ||
| 108 | 108 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); |
| 109 | 109 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 110 | 110 | |
| 111 | + cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | |
| 112 | + | |
| 111 | 113 | // Connect and subscribe to publisher |
| 112 | 114 | if (connector.Connect(Subscriber)) |
| 115 | + { | |
| 116 | + cout << "Connected! Subscribing to data..." << endl << endl; | |
| 113 | 117 | Subscriber.Subscribe(Info); |
| 118 | + } | |
| 114 | 119 | else |
| 115 | − std::cerr << "All connection attempts failed" << std::endl; | |
| 120 | + { | |
| 121 | + cout << "Connection attempts exceeded. Press enter to exit." << endl; | |
| 122 | + } | |
| 116 | 123 | } |
| 117 | 124 | |
| 118 | −tst::SubscriptionInfo CreateSubscriptionInfo() | |
| 125 | +SubscriptionInfo CreateSubscriptionInfo() | |
| 119 | 126 | { |
| 120 | 127 | // SubscriptionInfo is a helper object which allows the user |
| 121 | 128 | // to set up their subscription and reuse subscription settings. |
| 122 | − tst::SubscriptionInfo info; | |
| 129 | + SubscriptionInfo info; | |
| 123 | 130 | |
| 124 | 131 | // The following filter expression formats are also available: |
| 125 | 132 | // |
| @@ -162,11 +169,11 @@ tst::SubscriptionInfo CreateSubscriptionInfo() · 36 unmodified lines | ||
| 162 | 169 | return info; |
| 163 | 170 | } |
| 164 | 171 | |
| 165 | −tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts::uint16_t port) | |
| 172 | +SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port) | |
| 166 | 173 | { |
| 167 | 174 | // SubscriberConnector is another helper object which allows the |
| 168 | 175 | // user to modify settings for auto-reconnects and retry cycles. |
| 169 | − tst::SubscriberConnector connector; | |
| 176 | + SubscriberConnector connector; | |
| 170 | 177 | |
| 171 | 178 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 172 | 179 | connector.RegisterReconnectCallback(&Resubscribe); |
| @@ -174,7 +181,7 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 1 unmodified line | ||
| 174 | 181 | connector.SetHostname(hostname); |
| 175 | 182 | connector.SetPort(port); |
| 176 | 183 | connector.SetMaxRetries(5); |
| 177 | − connector.SetRetryInterval(1000); | |
| 184 | + connector.SetRetryInterval(1500); | |
| 178 | 185 | connector.SetAutoReconnect(true); |
| 179 | 186 | |
| 180 | 187 | return connector; |
| @@ -182,13 +189,13 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 1 unmodified line | ||
| 182 | 189 | |
| 183 | 190 | // Callback which is called when the subscriber has |
| 184 | 191 | // 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) | |
| 186 | 193 | { |
| 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; | |
| 189 | 196 | |
| 190 | 197 | static int processCount = 0; |
| 191 | − std::size_t i; | |
| 198 | + size_t i; | |
| 192 | 199 | |
| 193 | 200 | char timestamp[MaxTimestampSize]; |
| 194 | 201 | |
| @@ -196,19 +203,19 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line | ||
| 196 | 203 | // seconds (assuming 30 calls per second). |
| 197 | 204 | if (processCount % 150 == 0) |
| 198 | 205 | { |
| 199 | − std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl; | |
| 206 | + cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | |
| 200 | 207 | |
| 201 | 208 | if (newMeasurements.size() > 0) |
| 202 | 209 | { |
| 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; | |
| 205 | 212 | |
| 206 | − std::cout << "Point\tValue" << std::endl; | |
| 213 | + cout << "Point\tValue" << endl; | |
| 207 | 214 | |
| 208 | 215 | 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; | |
| 210 | 217 | |
| 211 | − std::cout << std::endl; | |
| 218 | + cout << endl; | |
| 212 | 219 | } |
| 213 | 220 | } |
| 214 | 221 | |
| @@ -216,20 +223,28 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line | ||
| 216 | 223 | } |
| 217 | 224 | |
| 218 | 225 | // Callback that is called when the subscriber auto-reconnects. |
| 219 | −void Resubscribe(tst::DataSubscriber* source) | |
| 226 | +void Resubscribe(DataSubscriber* source) | |
| 220 | 227 | { |
| 221 | 228 | if (source->IsConnected()) |
| 229 | + { | |
| 230 | + cout << "Reconnected! Subscribing to data..." << endl << endl; | |
| 222 | 231 | source->Subscribe(Info); |
| 232 | + } | |
| 233 | + else | |
| 234 | + { | |
| 235 | + source->Disconnect(); | |
| 236 | + cout << "Connection retry attempts exceeded. Press enter to exit." << endl; | |
| 237 | + } | |
| 223 | 238 | } |
| 224 | 239 | |
| 225 | 240 | // 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) | |
| 227 | 242 | { |
| 228 | − std::cout << message << std::endl << std::endl; | |
| 243 | + cout << message << endl << endl; | |
| 229 | 244 | } |
| 230 | 245 | |
| 231 | 246 | // 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) | |
| 233 | 248 | { |
| 234 | − std::cerr << message << std::endl << std::endl; | |
| 249 | + cerr << message << endl << endl; | |
| 235 | 250 | } |