Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp (1 of 32 files). Show all
Unified · Split
| @@ -26,16 +26,18 @@ · 25 unmodified lines | |||
| 26 | #include <vector> | 26 | #include <vector> |
| 27 | 27 | ||
| 28 | #include "../Common/Convert.h" | 28 | #include "../Common/Convert.h" |
| 29 | #include "../Common/Measurement.h" | ||
| 30 | #include "../Transport/DataSubscriber.h" | 29 | #include "../Transport/DataSubscriber.h" |
| 31 | 30 | ||
| 32 | namespace gsfts = GSF::TimeSeries; | 31 | using namespace std; |
| 33 | namespace tst = gsfts::Transport; | 32 | using namespace GSF::TimeSeries; |
| 33 | using namespace GSF::TimeSeries::Transport; | ||
| 34 | 34 | ||
| 35 | tst::DataSubscriber Subscriber; | 35 | DataSubscriber Subscriber; |
| 36 | 36 | ||
| 37 | void RunSubscriber(std::string hostname, gsfts::uint16_t port); | 37 | void RunSubscriber(string hostname, uint16_t port); |
| 38 | void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements); | 38 | void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements); |
| 39 | void DisplayStatusMessage(DataSubscriber* source, string message); | ||
| 40 | void DisplayErrorMessage(DataSubscriber* source, string message); | ||
| 39 | 41 | ||
| 40 | // Sample application to demonstrate the most simple use of the subscriber API. | 42 | // Sample application to demonstrate the most simple use of the subscriber API. |
| 41 | // | 43 | // |
| @@ -48,31 +50,32 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 6 unmodified lines | |||
| 48 | // Measurements are transmitted via the TCP command channel. | 50 | // Measurements are transmitted via the TCP command channel. |
| 49 | int main(int argc, char* argv[]) | 51 | int main(int argc, char* argv[]) |
| 50 | { | 52 | { |
| 51 | std::string hostname; | 53 | string hostname; |
| 52 | gsfts::uint16_t port; | 54 | uint16_t port; |
| 53 | 55 | ||
| 54 | // Ensure that the necessary | 56 | // Ensure that the necessary |
| 55 | // command line arguments are given. | 57 | // command line arguments are given. |
| 56 | if (argc < 3) | 58 | if (argc < 3) |
| 57 | { | 59 | { |
| 58 | std::cout << "Usage:" << std::endl; | 60 | cout << "Usage:" << endl; |
| 59 | std::cout << " SimpleSubscribe HOSTNAME PORT" << std::endl; | 61 | cout << " SimpleSubscribe HOSTNAME PORT" << endl; |
| 60 | return 0; | 62 | return 0; |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | // Get hostname and port. | 65 | // Get hostname and port. |
| 64 | hostname = argv[1]; | 66 | hostname = argv[1]; |
| 65 | std::stringstream(argv[2]) >> port; | 67 | stringstream(argv[2]) >> port; |
| 66 | 68 | ||
| 67 | // Run the subscriber. | 69 | // Run the subscriber. |
| 68 | RunSubscriber(hostname, port); | 70 | RunSubscriber(hostname, port); |
| 69 | 71 | ||
| 70 | // Wait until the user presses enter before quitting. | 72 | // Wait until the user presses enter before quitting. |
| 71 | std::string line; | 73 | string line; |
| 72 | std::getline(std::cin, line); | 74 | getline(cin, line); |
| 73 | 75 | ||
| 74 | // Disconnect the subscriber to stop background threads. | 76 | // Disconnect the subscriber to stop background threads. |
| 75 | Subscriber.Disconnect(); | 77 | Subscriber.Disconnect(); |
| 78 | cout << "Disconnected." << endl; | ||
| 76 | 79 | ||
| 77 | return 0; | 80 | return 0; |
| 78 | } | 81 | } |
| @@ -82,27 +85,34 @@ int main(int argc, char* argv[]) · 3 unmodified lines | |||
| 82 | // - Register callbacks | 85 | // - Register callbacks |
| 83 | // - Connect to publisher | 86 | // - Connect to publisher |
| 84 | // - Subscribe | 87 | // - Subscribe |
| 85 | void RunSubscriber(std::string hostname, gsfts::uint16_t port) | 88 | void RunSubscriber(string hostname, uint16_t port) |
| 86 | { | 89 | { |
| 87 | // SubscriptionInfo is a helper object which allows the user | 90 | // SubscriptionInfo is a helper object which allows the user |
| 88 | // to set up their subscription and reuse subscription settings. | 91 | // to set up their subscription and reuse subscription settings. |
| 89 | tst::SubscriptionInfo info; | 92 | SubscriptionInfo info; |
| 90 | 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"; |
| 91 | 94 | ||
| 95 | // Register callbacks | ||
| 96 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | ||
| 97 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | ||
| 92 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); | 98 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); |
| 99 | |||
| 100 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | ||
| 93 | Subscriber.Connect(hostname, port); | 101 | Subscriber.Connect(hostname, port); |
| 102 | |||
| 103 | cout << "Connected! Subscribing to data..." << endl << endl; | ||
| 94 | Subscriber.Subscribe(info); | 104 | Subscriber.Subscribe(info); |
| 95 | } | 105 | } |
| 96 | 106 | ||
| 97 | // Callback which is called when the subscriber has | 107 | // Callback which is called when the subscriber has |
| 98 | // received a new packet of measurements from the publisher. | 108 | // received a new packet of measurements from the publisher. |
| 99 | void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements) | 109 | void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements) |
| 100 | { | 110 | { |
| 101 | const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | 111 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; |
| 102 | const std::size_t MaxTimestampSize = 80; | 112 | const size_t MaxTimestampSize = 80; |
| 103 | 113 | ||
| 104 | static int processCount = 0; | 114 | static int processCount = 0; |
| 105 | std::size_t i; | 115 | size_t i; |
| 106 | 116 | ||
| 107 | char timestamp[MaxTimestampSize]; | 117 | char timestamp[MaxTimestampSize]; |
| 108 | 118 | ||
| @@ -110,21 +120,33 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 1 unmodified line | |||
| 110 | // seconds (assuming 30 calls per second). | 120 | // seconds (assuming 30 calls per second). |
| 111 | if (processCount % 150 == 0) | 121 | if (processCount % 150 == 0) |
| 112 | { | 122 | { |
| 113 | std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl; | 123 | cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << endl; |
| 114 | 124 | ||
| 115 | if (newMeasurements.size() > 0) | 125 | if (newMeasurements.size() > 0) |
| 116 | { | 126 | { |
| 117 | if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) | 127 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) |
| 118 | std::cout << "Timestamp: " << std::string(timestamp) << std::endl; | 128 | cout << "Timestamp: " << string(timestamp) << endl; |
| 119 | 129 | ||
| 120 | std::cout << "Point\tValue" << std::endl; | 130 | cout << "Point\tValue" << endl; |
| 121 | 131 | ||
| 122 | for (i = 0; i < newMeasurements.size(); ++i) | 132 | for (i = 0; i < newMeasurements.size(); ++i) |
| 123 | std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl; | 133 | cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << endl; |
| 124 | 134 | ||
| 125 | std::cout << std::endl; | 135 | cout << endl; |
| 126 | } | 136 | } |
| 127 | } | 137 | } |
| 128 | 138 | ||
| 129 | ++processCount; | 139 | ++processCount; |
| 140 | } | ||
| 141 | |||
| 142 | // Callback which is called to display status messages from the subscriber. | ||
| 143 | void DisplayStatusMessage(DataSubscriber* source, string message) | ||
| 144 | { | ||
| 145 | cout << message << endl << endl; | ||
| 146 | } | ||
| 147 | |||
| 148 | // Callback which is called to display error messages from the connector and subscriber. | ||
| 149 | void DisplayErrorMessage(DataSubscriber* source, string message) | ||
| 150 | { | ||
| 151 | cerr << message << endl << endl; | ||
| 130 | } | 152 | } |