Showing the changes to Source/Libraries/TSFJavaLibrary/src/org/gpa/tsf/sample/SimpleSubscribe.java (1 of 915 files). Show all
Unified · Split
| @@ -0,0 +1,111 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // SimpleSubscribe.java - 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/19/2012 - Stephen C. Wills | ||
| 20 | // Generated original version of source code. | ||
| 21 | // | ||
| 22 | //****************************************************************************************************** | ||
| 23 | |||
| 24 | package org.gpa.tsf.sample; | ||
| 25 | |||
| 26 | import java.io.IOException; | ||
| 27 | import java.util.Scanner; | ||
| 28 | |||
| 29 | import org.gpa.tsf.transport.DataSubscriber; | ||
| 30 | import org.gpa.tsf.transport.SubscriptionInfo; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * Provides an example of how to use the Subscriber API. | ||
| 34 | * | ||
| 35 | * @see DataSubscriber | ||
| 36 | */ | ||
| 37 | public class SimpleSubscribe | ||
| 38 | { | ||
| 39 | private static DataSubscriber s_subscriber; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Main method. Entry point into the program. | ||
| 43 | * | ||
| 44 | * @param args command line arguments | ||
| 45 | */ | ||
| 46 | public static void main(String[] args) | ||
| 47 | { | ||
| 48 | String hostname; | ||
| 49 | int port; | ||
| 50 | |||
| 51 | // Ensure that the necessary | ||
| 52 | // command line arguments are given | ||
| 53 | if (args.length < 2) | ||
| 54 | { | ||
| 55 | System.err.println("Error: requires two command line arguments"); | ||
| 56 | System.err.println(" 1. hostname of publisher"); | ||
| 57 | System.err.println(" 2. port used to initiate connection"); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | // Get hostname and port | ||
| 62 | hostname = args[0]; | ||
| 63 | port = Integer.parseInt(args[1]); | ||
| 64 | |||
| 65 | // Run the subscriber | ||
| 66 | runSubscriber(hostname, port); | ||
| 67 | |||
| 68 | // Wait until user presses enter before quitting | ||
| 69 | waitForUserInput(); | ||
| 70 | |||
| 71 | // Disconnect the subscriber to stop background threads | ||
| 72 | if (s_subscriber != null) | ||
| 73 | s_subscriber.disconnect(); | ||
| 74 | } | ||
| 75 | |||
| 76 | // The proper procedure when creating and running a subscriber is: | ||
| 77 | // - Create subscriber | ||
| 78 | // - Add listeners | ||
| 79 | // - Connect to publisher | ||
| 80 | // - Subscribe | ||
| 81 | private static void runSubscriber(String hostname, int port) | ||
| 82 | { | ||
| 83 | try | ||
| 84 | { | ||
| 85 | s_subscriber = new DataSubscriber(); | ||
| 86 | s_subscriber.addSubscriberListener(new SampleSubscriberListener()); | ||
| 87 | s_subscriber.connect(hostname, port); | ||
| 88 | s_subscriber.subscribe(createSubscriptionInfo()); | ||
| 89 | } | ||
| 90 | catch (IOException ex) | ||
| 91 | { | ||
| 92 | ex.printStackTrace(); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | // SubscriptionInfo is a helper object which allows the user | ||
| 97 | // to set up their subscription and reuse subscription settings | ||
| 98 | private static SubscriptionInfo createSubscriptionInfo() | ||
| 99 | { | ||
| 100 | SubscriptionInfo info = new SubscriptionInfo(); | ||
| 101 | info.setFilterExpression("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"); | ||
| 102 | return info; | ||
| 103 | } | ||
| 104 | |||
| 105 | // Waits for the user to press Enter before proceeding | ||
| 106 | private static void waitForUserInput() | ||
| 107 | { | ||
| 108 | Scanner scan = new Scanner(System.in); | ||
| 109 | scan.nextLine(); | ||
| 110 | } | ||
| 111 | } | ||