History

Renamed TSFPlatformLibrary to TimeSeriesPlatformLibrary and updated namespaces to reflect the new GSF::TimeSeries namespace.

StephenCWills committed 14 years ago

Commit 43d65a91 parent 473b71e25 files · +310 −606Browse files

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

Unified · Split

Source/Libraries/TimeSeriesPlatformLibrary/Samples/SimpleSubscribe.cpp+130−0
@@ -0,0 +1,130 @@
1+//******************************************************************************************************
2+// SimpleSubscribe.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/05/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+
37+void RunSubscriber(std::string hostname, gsfts::uint16_t port);
38+void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements);
39+
40+// Sample application to demonstrate the most simple use of the subscriber API.
41+//
42+// This application accepts the hostname and port of the publisher via command
43+// line arguments, connects to the publisher, subscribes, and displays information
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
46+// smoothly (no reconnection attempts are made if the connection fails).
47+//
48+// Measurements are transmitted via the TCP command channel.
49+int main(int argc, char* argv[])
50+{
51+ std::string hostname;
52+ gsfts::uint16_t port;
53+
54+ // Ensure that the necessary
55+ // command line arguments are given.
56+ if (argc < 3)
57+ {
58+ std::cout << "Usage:" << std::endl;
59+ std::cout << " SimpleSubscribe HOSTNAME PORT" << std::endl;
60+ return 0;
61+ }
62+
63+ // Get hostname and port.
64+ hostname = argv[1];
65+ std::stringstream(argv[2]) >> port;
66+
67+ // Run the subscriber.
68+ RunSubscriber(hostname, port);
69+
70+ // Wait until the user presses enter before quitting.
71+ std::string line;
72+ std::getline(std::cin, line);
73+
74+ // Disconnect the subscriber to stop background threads.
75+ Subscriber.Disconnect();
76+
77+ return 0;
78+}
79+
80+// The proper procedure when creating and running a subscriber is:
81+// - Create subscriber
82+// - Register callbacks
83+// - Connect to publisher
84+// - Subscribe
85+void RunSubscriber(std::string hostname, gsfts::uint16_t port)
86+{
87+ // SubscriptionInfo is a helper object which allows the user
88+ // to set up their subscription and reuse subscription settings.
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";
91+
92+ Subscriber.RegisterNewMeasurementsCallback(&ProcessMeasurements);
93+ Subscriber.Connect(hostname, port);
94+ Subscriber.Subscribe(info);
95+}
96+
97+// Callback which is called when the subscriber has
98+// received a new packet of measurements from the publisher.
99+void ProcessMeasurements(std::vector<gsfts::Measurement> newMeasurements)
100+{
101+ const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f";
102+ const std::size_t MaxTimestampSize = 80;
103+
104+ static int processCount = 0;
105+ std::size_t i;
106+
107+ char timestamp[MaxTimestampSize];
108+
109+ // Only display messages every five
110+ // seconds (assuming 30 calls per second).
111+ if (processCount % 150 == 0)
112+ {
113+ std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl;
114+
115+ if (newMeasurements.size() > 0)
116+ {
117+ if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp))
118+ std::cout << "Timestamp: " << std::string(timestamp) << std::endl;
119+
120+ std::cout << "Point\tValue" << std::endl;
121+
122+ for (i = 0; i < newMeasurements.size(); ++i)
123+ std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl;
124+
125+ std::cout << std::endl;
126+ }
127+ }
128+
129+ ++processCount;
130+}

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