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

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