Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp (1 of 24 files). Show all
Unified · Split
| @@ -59,34 +59,34 @@ void RunSubscriber(string hostname, uint16_t port); · 58 unmodified lines | ||
| 59 | 59 | // Measurements are transmitted via a separate UDP data channel. |
| 60 | 60 | int main(int argc, char* argv[]) |
| 61 | 61 | { |
| 62 | − string hostname; | |
| 63 | − uint16_t port; | |
| 64 | − | |
| 65 | − // Ensure that the necessary | |
| 66 | − // command line arguments are given. | |
| 67 | − if (argc < 3) | |
| 68 | − { | |
| 69 | − cout << "Usage:" << endl; | |
| 70 | − cout << " AdvancedSubscribe HOSTNAME PORT" << endl; | |
| 71 | − return 0; | |
| 72 | − } | |
| 73 | − | |
| 74 | − // Get hostname and port. | |
| 75 | − hostname = argv[1]; | |
| 76 | − stringstream(argv[2]) >> port; | |
| 77 | − | |
| 78 | − // Run the subscriber. | |
| 79 | − RunSubscriber(hostname, port); | |
| 80 | − | |
| 81 | − // Wait until the user presses enter before quitting. | |
| 82 | − string line; | |
| 83 | − getline(cin, line); | |
| 84 | − | |
| 85 | − // Disconnect the subscriber to stop background threads. | |
| 86 | − Subscriber.Disconnect(); | |
| 87 | − cout << "Disconnected." << endl; | |
| 88 | − | |
| 89 | − return 0; | |
| 62 | + string hostname; | |
| 63 | + uint16_t port; | |
| 64 | + | |
| 65 | + // Ensure that the necessary | |
| 66 | + // command line arguments are given. | |
| 67 | + if (argc < 3) | |
| 68 | + { | |
| 69 | + cout << "Usage:" << endl; | |
| 70 | + cout << " AdvancedSubscribe HOSTNAME PORT" << endl; | |
| 71 | + return 0; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Get hostname and port. | |
| 75 | + hostname = argv[1]; | |
| 76 | + stringstream(argv[2]) >> port; | |
| 77 | + | |
| 78 | + // Run the subscriber. | |
| 79 | + RunSubscriber(hostname, port); | |
| 80 | + | |
| 81 | + // Wait until the user presses enter before quitting. | |
| 82 | + string line; | |
| 83 | + getline(cin, line); | |
| 84 | + | |
| 85 | + // Disconnect the subscriber to stop background threads. | |
| 86 | + Subscriber.Disconnect(); | |
| 87 | + cout << "Disconnected." << endl; | |
| 88 | + | |
| 89 | + return 0; | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // The proper procedure when creating and running a subscriber is: |
| @@ -96,155 +96,155 @@ int main(int argc, char* argv[]) · 3 unmodified lines | ||
| 96 | 96 | // - Subscribe |
| 97 | 97 | void RunSubscriber(string hostname, uint16_t port) |
| 98 | 98 | { |
| 99 | − // The connector is declared here because it | |
| 100 | − // is only needed for the initial connection | |
| 101 | − SubscriberConnector connector; | |
| 102 | − | |
| 103 | − // Set up helper objects | |
| 104 | − connector = CreateSubscriberConnector(hostname, port); | |
| 105 | − Info = CreateSubscriptionInfo(); | |
| 106 | − | |
| 107 | − // Register callbacks | |
| 108 | − Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | |
| 109 | − Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 110 | − | |
| 111 | − cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | |
| 112 | − | |
| 113 | − // Connect and subscribe to publisher | |
| 114 | − if (connector.Connect(Subscriber)) | |
| 115 | − { | |
| 116 | − cout << "Connected! Subscribing to data..." << endl << endl; | |
| 117 | − Subscriber.Subscribe(Info); | |
| 118 | − } | |
| 119 | − else | |
| 120 | − { | |
| 121 | − cout << "Connection attempts exceeded. Press enter to exit." << endl; | |
| 122 | − } | |
| 99 | + // The connector is declared here because it | |
| 100 | + // is only needed for the initial connection | |
| 101 | + SubscriberConnector connector; | |
| 102 | + | |
| 103 | + // Set up helper objects | |
| 104 | + connector = CreateSubscriberConnector(hostname, port); | |
| 105 | + Info = CreateSubscriptionInfo(); | |
| 106 | + | |
| 107 | + // Register callbacks | |
| 108 | + Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | |
| 109 | + Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 110 | + | |
| 111 | + cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | |
| 112 | + | |
| 113 | + // Connect and subscribe to publisher | |
| 114 | + if (connector.Connect(Subscriber)) | |
| 115 | + { | |
| 116 | + cout << "Connected! Subscribing to data..." << endl << endl; | |
| 117 | + Subscriber.Subscribe(Info); | |
| 118 | + } | |
| 119 | + else | |
| 120 | + { | |
| 121 | + cout << "Connection attempts exceeded. Press enter to exit." << endl; | |
| 122 | + } | |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | SubscriptionInfo CreateSubscriptionInfo() |
| 126 | 126 | { |
| 127 | − // SubscriptionInfo is a helper object which allows the user | |
| 128 | − // to set up their subscription and reuse subscription settings. | |
| 129 | − SubscriptionInfo info; | |
| 130 | − | |
| 131 | − // The following filter expression formats are also available: | |
| 132 | − // | |
| 133 | − // - Signal ID list - | |
| 134 | − //info.FilterExpression = "7aaf0a8f-3a4f-4c43-ab43-ed9d1e64a255;" | |
| 135 | − // "93673c68-d59d-4926-b7e9-e7678f9f66b4;" | |
| 136 | − // "65ac9cf6-ae33-4ece-91b6-bb79343855d5;" | |
| 137 | − // "3647f729-d0ed-4f79-85ad-dae2149cd432;" | |
| 138 | − // "069c5e29-f78a-46f6-9dff-c92cb4f69371;" | |
| 139 | − // "25355a7b-2a9d-4ef2-99ba-4dd791461379"; | |
| 140 | − // | |
| 141 | − // - Filter pattern - | |
| 142 | − //info.FilterExpression = "FILTER ActiveMeasurements WHERE ID LIKE 'PPA:*'"; | |
| 143 | − //info.FilterExpression = "FILTER ActiveMeasurements WHERE Device = 'SHELBY' AND SignalType = 'FREQ'"; | |
| 144 | − | |
| 145 | − info.FilterExpression = "PPA:1;PPA:2;PPA:3;PPA:4;PPA:5;PPA:6;PPA:7;PPA:8;PPA:9;PPA:10;PPA:11;PPA:12;PPA:13;PPA:14"; | |
| 146 | − info.NewMeasurementsCallback = &ProcessMeasurements; | |
| 147 | − | |
| 148 | − // To set up a remotely synchronized subscription, set this flag | |
| 149 | − // to true and add the framesPerSecond parameter to the | |
| 150 | − // ExtraConnectionStringParameters. Additionally, the following | |
| 151 | − // example demonstrates the use of some other useful parameters | |
| 152 | − // when setting up remotely synchronized subscriptions. | |
| 153 | − // | |
| 154 | − //info.RemotelySynchronized = true; | |
| 155 | − //info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest"; | |
| 156 | − | |
| 157 | − info.RemotelySynchronized = false; | |
| 158 | − info.Throttled = false; | |
| 159 | − | |
| 160 | − info.UdpDataChannel = true; | |
| 161 | − info.DataChannelLocalPort = 9600; | |
| 162 | − | |
| 163 | − info.IncludeTime = true; | |
| 164 | − info.LagTime = 3.0; | |
| 165 | − info.LeadTime = 1.0; | |
| 166 | − info.UseLocalClockAsRealTime = false; | |
| 167 | − info.UseMillisecondResolution = true; | |
| 168 | − | |
| 169 | − return info; | |
| 127 | + // SubscriptionInfo is a helper object which allows the user | |
| 128 | + // to set up their subscription and reuse subscription settings. | |
| 129 | + SubscriptionInfo info; | |
| 130 | + | |
| 131 | + // The following filter expression formats are also available: | |
| 132 | + // | |
| 133 | + // - Signal ID list - | |
| 134 | + //info.FilterExpression = "7aaf0a8f-3a4f-4c43-ab43-ed9d1e64a255;" | |
| 135 | + // "93673c68-d59d-4926-b7e9-e7678f9f66b4;" | |
| 136 | + // "65ac9cf6-ae33-4ece-91b6-bb79343855d5;" | |
| 137 | + // "3647f729-d0ed-4f79-85ad-dae2149cd432;" | |
| 138 | + // "069c5e29-f78a-46f6-9dff-c92cb4f69371;" | |
| 139 | + // "25355a7b-2a9d-4ef2-99ba-4dd791461379"; | |
| 140 | + // | |
| 141 | + // - Filter pattern - | |
| 142 | + //info.FilterExpression = "FILTER ActiveMeasurements WHERE ID LIKE 'PPA:*'"; | |
| 143 | + //info.FilterExpression = "FILTER ActiveMeasurements WHERE Device = 'SHELBY' AND SignalType = 'FREQ'"; | |
| 144 | + | |
| 145 | + info.FilterExpression = "PPA:1;PPA:2;PPA:3;PPA:4;PPA:5;PPA:6;PPA:7;PPA:8;PPA:9;PPA:10;PPA:11;PPA:12;PPA:13;PPA:14"; | |
| 146 | + info.NewMeasurementsCallback = &ProcessMeasurements; | |
| 147 | + | |
| 148 | + // To set up a remotely synchronized subscription, set this flag | |
| 149 | + // to true and add the framesPerSecond parameter to the | |
| 150 | + // ExtraConnectionStringParameters. Additionally, the following | |
| 151 | + // example demonstrates the use of some other useful parameters | |
| 152 | + // when setting up remotely synchronized subscriptions. | |
| 153 | + // | |
| 154 | + //info.RemotelySynchronized = true; | |
| 155 | + //info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest"; | |
| 156 | + | |
| 157 | + info.RemotelySynchronized = false; | |
| 158 | + info.Throttled = false; | |
| 159 | + | |
| 160 | + info.UdpDataChannel = true; | |
| 161 | + info.DataChannelLocalPort = 9600; | |
| 162 | + | |
| 163 | + info.IncludeTime = true; | |
| 164 | + info.LagTime = 3.0; | |
| 165 | + info.LeadTime = 1.0; | |
| 166 | + info.UseLocalClockAsRealTime = false; | |
| 167 | + info.UseMillisecondResolution = true; | |
| 168 | + | |
| 169 | + return info; | |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port) |
| 173 | 173 | { |
| 174 | − // SubscriberConnector is another helper object which allows the | |
| 175 | − // user to modify settings for auto-reconnects and retry cycles. | |
| 176 | − SubscriberConnector connector; | |
| 174 | + // SubscriberConnector is another helper object which allows the | |
| 175 | + // user to modify settings for auto-reconnects and retry cycles. | |
| 176 | + SubscriberConnector connector; | |
| 177 | 177 | |
| 178 | − connector.RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 179 | − connector.RegisterReconnectCallback(&Resubscribe); | |
| 178 | + connector.RegisterErrorMessageCallback(&DisplayErrorMessage); | |
| 179 | + connector.RegisterReconnectCallback(&Resubscribe); | |
| 180 | 180 | |
| 181 | − connector.SetHostname(hostname); | |
| 182 | − connector.SetPort(port); | |
| 183 | − connector.SetMaxRetries(5); | |
| 184 | − connector.SetRetryInterval(1500); | |
| 185 | − connector.SetAutoReconnect(true); | |
| 181 | + connector.SetHostname(hostname); | |
| 182 | + connector.SetPort(port); | |
| 183 | + connector.SetMaxRetries(5); | |
| 184 | + connector.SetRetryInterval(1500); | |
| 185 | + connector.SetAutoReconnect(true); | |
| 186 | 186 | |
| 187 | − return connector; | |
| 187 | + return connector; | |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | // Callback which is called when the subscriber has |
| 191 | 191 | // received a new packet of measurements from the publisher. |
| 192 | 192 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements) |
| 193 | 193 | { |
| 194 | − const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | |
| 195 | − const size_t MaxTimestampSize = 80; | |
| 194 | + const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | |
| 195 | + const size_t MaxTimestampSize = 80; | |
| 196 | 196 | |
| 197 | − static int processCount = 0; | |
| 198 | − size_t i; | |
| 197 | + static int processCount = 0; | |
| 198 | + size_t i; | |
| 199 | 199 | |
| 200 | − char timestamp[MaxTimestampSize]; | |
| 200 | + char timestamp[MaxTimestampSize]; | |
| 201 | 201 | |
| 202 | − // Only display messages every five | |
| 203 | − // seconds (assuming 30 calls per second). | |
| 204 | − if (processCount % 150 == 0) | |
| 205 | − { | |
| 206 | − cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | |
| 202 | + // Only display messages every five | |
| 203 | + // seconds (assuming 30 calls per second). | |
| 204 | + if (processCount % 150 == 0) | |
| 205 | + { | |
| 206 | + cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | |
| 207 | 207 | |
| 208 | − if (!newMeasurements.empty()) | |
| 209 | − { | |
| 210 | − if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) | |
| 211 | − cout << "Timestamp: " << string(timestamp) << endl; | |
| 208 | + if (!newMeasurements.empty()) | |
| 209 | + { | |
| 210 | + if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) | |
| 211 | + cout << "Timestamp: " << string(timestamp) << endl; | |
| 212 | 212 | |
| 213 | − cout << "Point\tValue" << endl; | |
| 213 | + cout << "Point\tValue" << endl; | |
| 214 | 214 | |
| 215 | − for (i = 0; i < newMeasurements.size(); ++i) | |
| 216 | − cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; | |
| 215 | + for (i = 0; i < newMeasurements.size(); ++i) | |
| 216 | + cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; | |
| 217 | 217 | |
| 218 | − cout << endl; | |
| 219 | − } | |
| 220 | − } | |
| 218 | + cout << endl; | |
| 219 | + } | |
| 220 | + } | |
| 221 | 221 | |
| 222 | − ++processCount; | |
| 222 | + ++processCount; | |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | // Callback that is called when the subscriber auto-reconnects. |
| 226 | 226 | void Resubscribe(DataSubscriber* source) |
| 227 | 227 | { |
| 228 | − if (source->IsConnected()) | |
| 229 | − { | |
| 230 | − cout << "Reconnected! Subscribing to data..." << endl << endl; | |
| 231 | − source->Subscribe(Info); | |
| 232 | − } | |
| 233 | − else | |
| 234 | − { | |
| 235 | − source->Disconnect(); | |
| 236 | − cout << "Connection retry attempts exceeded. Press enter to exit." << endl; | |
| 237 | − } | |
| 228 | + if (source->IsConnected()) | |
| 229 | + { | |
| 230 | + cout << "Reconnected! Subscribing to data..." << endl << endl; | |
| 231 | + source->Subscribe(Info); | |
| 232 | + } | |
| 233 | + else | |
| 234 | + { | |
| 235 | + source->Disconnect(); | |
| 236 | + cout << "Connection retry attempts exceeded. Press enter to exit." << endl; | |
| 237 | + } | |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | // Callback which is called to display status messages from the subscriber. |
| 241 | 241 | void DisplayStatusMessage(DataSubscriber* source, const string& message) |
| 242 | 242 | { |
| 243 | − cout << message << endl << endl; | |
| 243 | + cout << message << endl << endl; | |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Callback which is called to display error messages from the connector and subscriber. |
| 247 | 247 | void DisplayErrorMessage(DataSubscriber* source, const string& message) |
| 248 | 248 | { |
| 249 | − cerr << message << endl << endl; | |
| 249 | + cerr << message << endl << endl; | |
| 250 | 250 | } |