History

Converted TFS repository to Git.

StephenCWills committed 11 years ago

Commit dd143d71 parent ab7f4b62684 files · +766031 −765809Browse files

Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp (1 of 2684 files). Show all

Unified · Split

Source/Libraries/TimeSeriesPlatformLibrary/Samples/AdvancedSubscribe.cpp+234−234
@@ -1,235 +1,235 @@
1//******************************************************************************************************1//******************************************************************************************************
2// AdvancedSubscribe.cpp - Gbtc2// AdvancedSubscribe.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. See6// 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 may8// 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.php11// 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 an13// 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 the14// "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/06/2012 - Stephen C. Wills19// 04/06/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
32namespace gsfts = GSF::TimeSeries;32namespace gsfts = GSF::TimeSeries;
33namespace tst = gsfts::Transport;33namespace tst = gsfts::Transport;
34 34
35tst::DataSubscriber Subscriber;35tst::DataSubscriber Subscriber;
36tst::SubscriptionInfo Info;36tst::SubscriptionInfo Info;
37 37
38// Create helper objects for subscription.38// Create helper objects for subscription.
39tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, uint16_t port);39tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, uint16_t port);
40tst::SubscriptionInfo CreateSubscriptionInfo();40tst::SubscriptionInfo CreateSubscriptionInfo();
41 41
42// Handlers for subscriber callbacks.42// Handlers for subscriber callbacks.
43void Resubscribe(tst::DataSubscriber* source);43void Resubscribe(tst::DataSubscriber* source);
44void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements);44void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements);
45void DisplayStatusMessage(std::string message);45void DisplayStatusMessage(std::string message);
46void DisplayErrorMessage(std::string message);46void DisplayErrorMessage(std::string message);
47 47
48// Runs the subscriber.48// Runs the subscriber.
49void RunSubscriber(std::string hostname, gsfts::uint16_t port);49void RunSubscriber(std::string hostname, gsfts::uint16_t port);
50 50
51// Sample application to demonstrate the more advanced use of the subscriber API.51// Sample application to demonstrate the more advanced use of the subscriber API.
52//52//
53// This application accepts the hostname and port of the publisher via command53// This application accepts the hostname and port of the publisher via command
54// line arguments, connects to the publisher, subscribes, and displays information54// line arguments, connects to the publisher, subscribes, and displays information
55// about the measurements it receives. It assumes that the publisher is providing55// about the measurements it receives. It assumes that the publisher is providing
56// fourteen measurements (PPA:1 through PPA:14) and will make a maximum of five56// fourteen measurements (PPA:1 through PPA:14) and will make a maximum of five
57// connection attempts before giving up. It will also auto-reconnect if the connection57// connection attempts before giving up. It will also auto-reconnect if the connection
58// is terminated.58// is terminated.
59//59//
60// Measurements are transmitted via a separate UDP data channel.60// Measurements are transmitted via a separate UDP data channel.
61int main(int argc, char* argv[])61int main(int argc, char* argv[])
62{62{
63 std::string hostname;63 std::string hostname;
64 gsfts::uint16_t port;64 gsfts::uint16_t port;
65 65
66 // Ensure that the necessary66 // Ensure that the necessary
67 // command line arguments are given.67 // command line arguments are given.
68 if (argc < 3)68 if (argc < 3)
69 {69 {
70 std::cout << "Usage:" << std::endl;70 std::cout << "Usage:" << std::endl;
71 std::cout << " AdvancedSubscribe HOSTNAME PORT" << std::endl;71 std::cout << " AdvancedSubscribe HOSTNAME PORT" << std::endl;
72 return 0;72 return 0;
73 }73 }
74 74
75 // Get hostname and port.75 // Get hostname and port.
76 hostname = argv[1];76 hostname = argv[1];
77 std::stringstream(argv[2]) >> port;77 std::stringstream(argv[2]) >> port;
78 78
79 // Run the subscriber.79 // Run the subscriber.
80 RunSubscriber(hostname, port);80 RunSubscriber(hostname, port);
81 81
82 // Wait until the user presses enter before quitting.82 // Wait until the user presses enter before quitting.
83 std::string line;83 std::string line;
84 std::getline(std::cin, line);84 std::getline(std::cin, line);
85 85
86 // Disconnect the subscriber to stop background threads.86 // Disconnect the subscriber to stop background threads.
87 Subscriber.Disconnect();87 Subscriber.Disconnect();
88 88
89 return 0;89 return 0;
90}90}
91 91
92// The proper procedure when creating and running a subscriber is:92// The proper procedure when creating and running a subscriber is:
93// - Create subscriber93// - Create subscriber
94// - Register callbacks94// - Register callbacks
95// - Connect to publisher95// - Connect to publisher
96// - Subscribe96// - Subscribe
97void RunSubscriber(std::string hostname, gsfts::uint16_t port)97void RunSubscriber(std::string hostname, gsfts::uint16_t port)
98{98{
99 // The connector is declared here because it99 // The connector is declared here because it
100 // is only needed for the initial connection100 // is only needed for the initial connection
101 tst::SubscriberConnector connector;101 tst::SubscriberConnector connector;
102 102
103 // Set up helper objects103 // Set up helper objects
104 connector = CreateSubscriberConnector(hostname, port);104 connector = CreateSubscriberConnector(hostname, port);
105 Info = CreateSubscriptionInfo();105 Info = CreateSubscriptionInfo();
106 106
107 // Register callbacks107 // Register callbacks
108 Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage);108 Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage);
109 Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage);109 Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage);
110 110
111 // Connect and subscribe to publisher111 // Connect and subscribe to publisher
112 if (connector.Connect(Subscriber))112 if (connector.Connect(Subscriber))
113 Subscriber.Subscribe(Info);113 Subscriber.Subscribe(Info);
114 else114 else
115 std::cerr << "All connection attempts failed" << std::endl;115 std::cerr << "All connection attempts failed" << std::endl;
116}116}
117 117
118tst::SubscriptionInfo CreateSubscriptionInfo()118tst::SubscriptionInfo CreateSubscriptionInfo()
119{119{
120 // SubscriptionInfo is a helper object which allows the user120 // SubscriptionInfo is a helper object which allows the user
121 // to set up their subscription and reuse subscription settings.121 // to set up their subscription and reuse subscription settings.
122 tst::SubscriptionInfo info;122 tst::SubscriptionInfo info;
123 123
124 // The following filter expression formats are also available:124 // The following filter expression formats are also available:
125 //125 //
126 // - Signal ID list -126 // - Signal ID list -
127 //info.FilterExpression = "7aaf0a8f-3a4f-4c43-ab43-ed9d1e64a255;"127 //info.FilterExpression = "7aaf0a8f-3a4f-4c43-ab43-ed9d1e64a255;"
128 // "93673c68-d59d-4926-b7e9-e7678f9f66b4;"128 // "93673c68-d59d-4926-b7e9-e7678f9f66b4;"
129 // "65ac9cf6-ae33-4ece-91b6-bb79343855d5;"129 // "65ac9cf6-ae33-4ece-91b6-bb79343855d5;"
130 // "3647f729-d0ed-4f79-85ad-dae2149cd432;"130 // "3647f729-d0ed-4f79-85ad-dae2149cd432;"
131 // "069c5e29-f78a-46f6-9dff-c92cb4f69371;"131 // "069c5e29-f78a-46f6-9dff-c92cb4f69371;"
132 // "25355a7b-2a9d-4ef2-99ba-4dd791461379";132 // "25355a7b-2a9d-4ef2-99ba-4dd791461379";
133 //133 //
134 // - Filter pattern -134 // - Filter pattern -
135 //info.FilterExpression = "FILTER ActiveMeasurements WHERE ID LIKE 'PPA:*'";135 //info.FilterExpression = "FILTER ActiveMeasurements WHERE ID LIKE 'PPA:*'";
136 //info.FilterExpression = "FILTER ActiveMeasurements WHERE Device = 'SHELBY' AND SignalType = 'FREQ'";136 //info.FilterExpression = "FILTER ActiveMeasurements WHERE Device = 'SHELBY' AND SignalType = 'FREQ'";
137 137
138 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";138 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";
139 info.NewMeasurementsCallback = &ProcessMeasurements;139 info.NewMeasurementsCallback = &ProcessMeasurements;
140 140
141 // To set up a remotely synchronized subscription, set this flag141 // To set up a remotely synchronized subscription, set this flag
142 // to true and add the framesPerSecond parameter to the142 // to true and add the framesPerSecond parameter to the
143 // ExtraConnectionStringParameters. Additionally, the following143 // ExtraConnectionStringParameters. Additionally, the following
144 // example demonstrates the use of some other useful parameters144 // example demonstrates the use of some other useful parameters
145 // when setting up remotely synchronized subscriptions.145 // when setting up remotely synchronized subscriptions.
146 //146 //
147 //info.RemotelySynchronized = true;147 //info.RemotelySynchronized = true;
148 //info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest";148 //info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest";
149 149
150 info.RemotelySynchronized = false;150 info.RemotelySynchronized = false;
151 info.Throttled = false;151 info.Throttled = false;
152 152
153 info.UdpDataChannel = true;153 info.UdpDataChannel = true;
154 info.DataChannelLocalPort = 9600;154 info.DataChannelLocalPort = 9600;
155 155
156 info.IncludeTime = true;156 info.IncludeTime = true;
157 info.LagTime = 3.0;157 info.LagTime = 3.0;
158 info.LeadTime = 1.0;158 info.LeadTime = 1.0;
159 info.UseLocalClockAsRealTime = false;159 info.UseLocalClockAsRealTime = false;
160 info.UseMillisecondResolution = true;160 info.UseMillisecondResolution = true;
161 161
162 return info;162 return info;
163}163}
164 164
165tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts::uint16_t port)165tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts::uint16_t port)
166{166{
167 // SubscriberConnector is another helper object which allows the167 // SubscriberConnector is another helper object which allows the
168 // user to modify settings for auto-reconnects and retry cycles.168 // user to modify settings for auto-reconnects and retry cycles.
169 tst::SubscriberConnector connector;169 tst::SubscriberConnector connector;
170 170
171 connector.RegisterErrorMessageCallback(&DisplayErrorMessage);171 connector.RegisterErrorMessageCallback(&DisplayErrorMessage);
172 connector.RegisterReconnectCallback(&Resubscribe);172 connector.RegisterReconnectCallback(&Resubscribe);
173 173
174 connector.SetHostname(hostname);174 connector.SetHostname(hostname);
175 connector.SetPort(port);175 connector.SetPort(port);
176 connector.SetMaxRetries(5);176 connector.SetMaxRetries(5);
177 connector.SetRetryInterval(1000);177 connector.SetRetryInterval(1000);
178 connector.SetAutoReconnect(true);178 connector.SetAutoReconnect(true);
179 179
180 return connector;180 return connector;
181}181}
182 182
183// Callback which is called when the subscriber has183// Callback which is called when the subscriber has
184// received a new packet of measurements from the publisher.184// received a new packet of measurements from the publisher.
185void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements)185void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements)
186{186{
187 const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";187 const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
188 const std::size_t MaxTimestampSize = 80;188 const std::size_t MaxTimestampSize = 80;
189 189
190 static int processCount = 0;190 static int processCount = 0;
191 std::size_t i;191 std::size_t i;
192 192
193 char timestamp[MaxTimestampSize];193 char timestamp[MaxTimestampSize];
194 194
195 // Only display messages every five195 // Only display messages every five
196 // seconds (assuming 30 calls per second).196 // seconds (assuming 30 calls per second).
197 if (processCount % 150 == 0)197 if (processCount % 150 == 0)
198 {198 {
199 std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl;199 std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl;
200 200
201 if (newMeasurements.size() > 0)201 if (newMeasurements.size() > 0)
202 {202 {
203 if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))203 if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
204 std::cout << "Timestamp: " << std::string(timestamp) << std::endl;204 std::cout << "Timestamp: " << std::string(timestamp) << std::endl;
205 205
206 std::cout << "Point\tValue" << std::endl;206 std::cout << "Point\tValue" << std::endl;
207 207
208 for (i = 0; i < newMeasurements.size(); ++i)208 for (i = 0; i < newMeasurements.size(); ++i)
209 std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl;209 std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl;
210 210
211 std::cout << std::endl;211 std::cout << std::endl;
212 }212 }
213 }213 }
214 214
215 ++processCount;215 ++processCount;
216}216}
217 217
218// Callback that is called when the subscriber auto-reconnects.218// Callback that is called when the subscriber auto-reconnects.
219void Resubscribe(tst::DataSubscriber* source)219void Resubscribe(tst::DataSubscriber* source)
220{220{
221 if (source->IsConnected())221 if (source->IsConnected())
222 source->Subscribe(Info);222 source->Subscribe(Info);
223}223}
224 224
225// Callback which is called to display status messages from the subscriber.225// Callback which is called to display status messages from the subscriber.
226void DisplayStatusMessage(std::string message)226void DisplayStatusMessage(std::string message)
227{227{
228 std::cout << message << std::endl << std::endl;228 std::cout << message << std::endl << std::endl;
229}229}
230 230
231// Callback which is called to display error messages from the connector and subscriber.231// Callback which is called to display error messages from the connector and subscriber.
232void DisplayErrorMessage(std::string message)232void DisplayErrorMessage(std::string message)
233{233{
234 std::cerr << message << std::endl << std::endl;234 std::cerr << message << std::endl << std::endl;
235}235}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t