Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp (1 of 2684 files). Show all
Unified · Split
| @@ -1,130 +1,130 @@ | |||
| 1 | //****************************************************************************************************** | 1 | //****************************************************************************************************** |
| 2 | // SimpleSubscribe.cpp - Gbtc | 2 | // SimpleSubscribe.cpp - Gbtc |
| 3 | // | 3 | // |
| 4 | // Copyright � 2010, Grid Protection Alliance. All Rights Reserved. | 4 | // Copyright � 2010, Grid Protection Alliance. All Rights Reserved. |
| 5 | // | 5 | // |
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See |
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. |
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may |
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: |
| 10 | // | 10 | // |
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | 11 | // http://www.opensource.org/licenses/eclipse-1.0.php |
| 12 | // | 12 | // |
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an |
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the |
| 15 | // License for the specific language governing permissions and limitations. | 15 | // License for the specific language governing permissions and limitations. |
| 16 | // | 16 | // |
| 17 | // Code Modification History: | 17 | // Code Modification History: |
| 18 | // ---------------------------------------------------------------------------------------------------- | 18 | // ---------------------------------------------------------------------------------------------------- |
| 19 | // 04/05/2012 - Stephen C. Wills | 19 | // 04/05/2012 - Stephen C. Wills |
| 20 | // Generated original version of source code. | 20 | // Generated original version of source code. |
| 21 | // | 21 | // |
| 22 | //****************************************************************************************************** | 22 | //****************************************************************************************************** |
| 23 | 23 | ||
| 24 | #include <iostream> | 24 | #include <iostream> |
| 25 | #include <string> | 25 | #include <string> |
| 26 | #include <vector> | 26 | #include <vector> |
| 27 | 27 | ||
| 28 | #include "../Common/Convert.h" | 28 | #include "../Common/Convert.h" |
| 29 | #include "../Common/Measurement.h" | 29 | #include "../Common/Measurement.h" |
| 30 | #include "../Transport/DataSubscriber.h" | 30 | #include "../Transport/DataSubscriber.h" |
| 31 | 31 | ||
| 32 | namespace gsfts = GSF::TimeSeries; | 32 | namespace gsfts = GSF::TimeSeries; |
| 33 | namespace tst = gsfts::Transport; | 33 | namespace tst = gsfts::Transport; |
| 34 | 34 | ||
| 35 | tst::DataSubscriber Subscriber; | 35 | tst::DataSubscriber Subscriber; |
| 36 | 36 | ||
| 37 | void RunSubscriber(std::string hostname, gsfts::uint16_t port); | 37 | void RunSubscriber(std::string hostname, gsfts::uint16_t port); |
| 38 | void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements); | 38 | void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements); |
| 39 | 39 | ||
| 40 | // Sample application to demonstrate the most simple use of the subscriber API. | 40 | // Sample application to demonstrate the most simple use of the subscriber API. |
| 41 | // | 41 | // |
| 42 | // This application accepts the hostname and port of the publisher via command | 42 | // This application accepts the hostname and port of the publisher via command |
| 43 | // line arguments, connects to the publisher, subscribes, and displays information | 43 | // line arguments, connects to the publisher, subscribes, and displays information |
| 44 | // about the measurements it receives. It assumes that the publisher is providing | 44 | // about the measurements it receives. It assumes that the publisher is providing |
| 45 | // fourteen measurements (PPA:1 through PPA:14) and that the publisher is running | 45 | // fourteen measurements (PPA:1 through PPA:14) and that the publisher is running |
| 46 | // smoothly (no reconnection attempts are made if the connection fails). | 46 | // smoothly (no reconnection attempts are made if the connection fails). |
| 47 | // | 47 | // |
| 48 | // Measurements are transmitted via the TCP command channel. | 48 | // Measurements are transmitted via the TCP command channel. |
| 49 | int main(int argc, char* argv[]) | 49 | int main(int argc, char* argv[]) |
| 50 | { | 50 | { |
| 51 | std::string hostname; | 51 | std::string hostname; |
| 52 | gsfts::uint16_t port; | 52 | gsfts::uint16_t port; |
| 53 | 53 | ||
| 54 | // Ensure that the necessary | 54 | // Ensure that the necessary |
| 55 | // command line arguments are given. | 55 | // command line arguments are given. |
| 56 | if (argc < 3) | 56 | if (argc < 3) |
| 57 | { | 57 | { |
| 58 | std::cout << "Usage:" << std::endl; | 58 | std::cout << "Usage:" << std::endl; |
| 59 | std::cout << " SimpleSubscribe HOSTNAME PORT" << std::endl; | 59 | std::cout << " SimpleSubscribe HOSTNAME PORT" << std::endl; |
| 60 | return 0; | 60 | return 0; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | // Get hostname and port. | 63 | // Get hostname and port. |
| 64 | hostname = argv[1]; | 64 | hostname = argv[1]; |
| 65 | std::stringstream(argv[2]) >> port; | 65 | std::stringstream(argv[2]) >> port; |
| 66 | 66 | ||
| 67 | // Run the subscriber. | 67 | // Run the subscriber. |
| 68 | RunSubscriber(hostname, port); | 68 | RunSubscriber(hostname, port); |
| 69 | 69 | ||
| 70 | // Wait until the user presses enter before quitting. | 70 | // Wait until the user presses enter before quitting. |
| 71 | std::string line; | 71 | std::string line; |
| 72 | std::getline(std::cin, line); | 72 | std::getline(std::cin, line); |
| 73 | 73 | ||
| 74 | // Disconnect the subscriber to stop background threads. | 74 | // Disconnect the subscriber to stop background threads. |
| 75 | Subscriber.Disconnect(); | 75 | Subscriber.Disconnect(); |
| 76 | 76 | ||
| 77 | return 0; | 77 | return 0; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | // The proper procedure when creating and running a subscriber is: | 80 | // The proper procedure when creating and running a subscriber is: |
| 81 | // - Create subscriber | 81 | // - Create subscriber |
| 82 | // - Register callbacks | 82 | // - Register callbacks |
| 83 | // - Connect to publisher | 83 | // - Connect to publisher |
| 84 | // - Subscribe | 84 | // - Subscribe |
| 85 | void RunSubscriber(std::string hostname, gsfts::uint16_t port) | 85 | void RunSubscriber(std::string hostname, gsfts::uint16_t port) |
| 86 | { | 86 | { |
| 87 | // SubscriptionInfo is a helper object which allows the user | 87 | // SubscriptionInfo is a helper object which allows the user |
| 88 | // to set up their subscription and reuse subscription settings. | 88 | // to set up their subscription and reuse subscription settings. |
| 89 | tst::SubscriptionInfo info; | 89 | tst::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"; | 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"; |
| 91 | 91 | ||
| 92 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); | 92 | Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements); |
| 93 | Subscriber.Connect(hostname, port); | 93 | Subscriber.Connect(hostname, port); |
| 94 | Subscriber.Subscribe(info); | 94 | Subscriber.Subscribe(info); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | // Callback which is called when the subscriber has | 97 | // Callback which is called when the subscriber has |
| 98 | // received a new packet of measurements from the publisher. | 98 | // received a new packet of measurements from the publisher. |
| 99 | void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements) | 99 | void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements) |
| 100 | { | 100 | { |
| 101 | const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | 101 | const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; |
| 102 | const std::size_t MaxTimestampSize = 80; | 102 | const std::size_t MaxTimestampSize = 80; |
| 103 | 103 | ||
| 104 | static int processCount = 0; | 104 | static int processCount = 0; |
| 105 | std::size_t i; | 105 | std::size_t i; |
| 106 | 106 | ||
| 107 | char timestamp[MaxTimestampSize]; | 107 | char timestamp[MaxTimestampSize]; |
| 108 | 108 | ||
| 109 | // Only display messages every five | 109 | // Only display messages every five |
| 110 | // seconds (assuming 30 calls per second). | 110 | // seconds (assuming 30 calls per second). |
| 111 | if (processCount % 150 == 0) | 111 | if (processCount % 150 == 0) |
| 112 | { | 112 | { |
| 113 | std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl; | 113 | std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl; |
| 114 | 114 | ||
| 115 | if (newMeasurements.size() > 0) | 115 | if (newMeasurements.size() > 0) |
| 116 | { | 116 | { |
| 117 | if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) | 117 | if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) |
| 118 | std::cout << "Timestamp: " << std::string(timestamp) << std::endl; | 118 | std::cout << "Timestamp: " << std::string(timestamp) << std::endl; |
| 119 | 119 | ||
| 120 | std::cout << "Point\tValue" << std::endl; | 120 | std::cout << "Point\tValue" << std::endl; |
| 121 | 121 | ||
| 122 | for (i = 0; i < newMeasurements.size(); ++i) | 122 | for (i = 0; i < newMeasurements.size(); ++i) |
| 123 | std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl; | 123 | std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl; |
| 124 | 124 | ||
| 125 | std::cout << std::endl; | 125 | std::cout << std::endl; |
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | ++processCount; | 129 | ++processCount; |
| 130 | } | 130 | } |