Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp (1 of 24 files). Show all
Unified · Split
| @@ -50,34 +50,34 @@ void DisplayErrorMessage(DataSubscriber* source, const string& message); · 49 unmodified lines | |||
| 50 | // Measurements are transmitted via the TCP command channel. | 50 | // Measurements are transmitted via the TCP command channel. |
| 51 | int main(int argc, char* argv[]) | 51 | int main(int argc, char* argv[]) |
| 52 | { | 52 | { |
| 53 | string hostname; | 53 | string hostname; |
| 54 | uint16_t port; | 54 | uint16_t port; |
| 55 | 55 | ||
| 56 | // Ensure that the necessary | 56 | // Ensure that the necessary |
| 57 | // command line arguments are given. | 57 | // command line arguments are given. |
| 58 | if (argc < 3) | 58 | if (argc < 3) |
| 59 | { | 59 | { |
| 60 | cout << "Usage:" << endl; | 60 | cout << "Usage:" << endl; |
| 61 | cout << " SimpleSubscribe HOSTNAME PORT" << endl; | 61 | cout << " SimpleSubscribe HOSTNAME PORT" << endl; |
| 62 | return 0; | 62 | return 0; |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | // Get hostname and port. | 65 | // Get hostname and port. |
| 66 | hostname = argv[1]; | 66 | hostname = argv[1]; |
| 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 | 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 | // Disconnect the subscriber to stop background threads. | 76 | // Disconnect the subscriber to stop background threads. |
| 77 | Subscriber.Disconnect(); | 77 | Subscriber.Disconnect(); |
| 78 | cout << "Disconnected." << endl; | 78 | cout << "Disconnected." << endl; |
| 79 | 79 | ||
| 80 | return 0; | 80 | return 0; |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | // The proper procedure when creating and running a subscriber is: | 83 | // The proper procedure when creating and running a subscriber is: |
| @@ -87,66 +87,66 @@ int main(int argc, char* argv[]) · 3 unmodified lines | |||
| 87 | // - Subscribe | 87 | // - Subscribe |
| 88 | void RunSubscriber(string hostname, uint16_t port) | 88 | void RunSubscriber(string hostname, uint16_t port) |
| 89 | { | 89 | { |
| 90 | // SubscriptionInfo is a helper object which allows the user | 90 | // SubscriptionInfo is a helper object which allows the user |
| 91 | // to set up their subscription and reuse subscription settings. | 91 | // to set up their subscription and reuse subscription settings. |
| 92 | SubscriptionInfo info; | 92 | SubscriptionInfo info; |
| 93 | 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"; | 93 | 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"; |
| 94 | 94 | ||
| 95 | // Register callbacks | 95 | // Register callbacks |
| 96 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | 96 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); |
| 97 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | 97 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 98 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); | 98 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); |
| 99 | 99 | ||
| 100 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | 100 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; |
| 101 | Subscriber.Connect(hostname, port); | 101 | Subscriber.Connect(hostname, port); |
| 102 | 102 | ||
| 103 | cout << "Connected! Subscribing to data..." << endl << endl; | 103 | cout << "Connected! Subscribing to data..." << endl << endl; |
| 104 | Subscriber.Subscribe(info); | 104 | Subscriber.Subscribe(info); |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | // Callback which is called when the subscriber has | 107 | // Callback which is called when the subscriber has |
| 108 | // received a new packet of measurements from the publisher. | 108 | // received a new packet of measurements from the publisher. |
| 109 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements) | 109 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements) |
| 110 | { | 110 | { |
| 111 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | 111 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; |
| 112 | const size_t MaxTimestampSize = 80; | 112 | const size_t MaxTimestampSize = 80; |
| 113 | 113 | ||
| 114 | static int processCount = 0; | 114 | static int processCount = 0; |
| 115 | size_t i; | 115 | size_t i; |
| 116 | 116 | ||
| 117 | char timestamp[MaxTimestampSize]; | 117 | char timestamp[MaxTimestampSize]; |
| 118 | 118 | ||
| 119 | // Only display messages every five | 119 | // Only display messages every five |
| 120 | // seconds (assuming 30 calls per second). | 120 | // seconds (assuming 30 calls per second). |
| 121 | if (processCount % 150 == 0) | 121 | if (processCount % 150 == 0) |
| 122 | { | 122 | { |
| 123 | cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | 123 | cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; |
| 124 | 124 | ||
| 125 | if (!newMeasurements.empty()) | 125 | if (!newMeasurements.empty()) |
| 126 | { | 126 | { |
| 127 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) | 127 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) |
| 128 | cout << "Timestamp: " << string(timestamp) << endl; | 128 | cout << "Timestamp: " << string(timestamp) << endl; |
| 129 | 129 | ||
| 130 | cout << "Point\tValue" << endl; | 130 | cout << "Point\tValue" << endl; |
| 131 | 131 | ||
| 132 | for (i = 0; i < newMeasurements.size(); ++i) | 132 | for (i = 0; i < newMeasurements.size(); ++i) |
| 133 | cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; | 133 | cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; |
| 134 | 134 | ||
| 135 | cout << endl; | 135 | cout << endl; |
| 136 | } | 136 | } |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | ++processCount; | 139 | ++processCount; |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | // Callback which is called to display status messages from the subscriber. | 142 | // Callback which is called to display status messages from the subscriber. |
| 143 | void DisplayStatusMessage(DataSubscriber* source, const string& message) | 143 | void DisplayStatusMessage(DataSubscriber* source, const string& message) |
| 144 | { | 144 | { |
| 145 | cout << message << endl << endl; | 145 | cout << message << endl << endl; |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | // Callback which is called to display error messages from the connector and subscriber. | 148 | // Callback which is called to display error messages from the connector and subscriber. |
| 149 | void DisplayErrorMessage(DataSubscriber* source, const string& message) | 149 | void DisplayErrorMessage(DataSubscriber* source, const string& message) |
| 150 | { | 150 | { |
| 151 | cerr << message << endl << endl; | 151 | cerr << message << endl << endl; |
| 152 | } | 152 | } |