VSProperties.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:12k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- VideoServer application
- $Id: VSProperties.java,v 1.13 1999/02/25 03:46:30 andrew Exp $
- */
- package edu.sunysb.cs.ecsl.videoserver;
- import java.io.*;
- import java.util.Properties;
- import java.util.Vector;
- import java.util.Dictionary;
- import java.util.Hashtable;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- /**
- holds pairs of keys and help descriptions for each key
- it will be stored in vector allProps
- */
- class KeyDescription
- {
- String key;
- String help;
- public KeyDescription( String k, String h )
- {
- key = k;
- help = h;
- }
- }
- /**
- manage all specific properties of video server
- */
- public class VSProperties implements TextProtocol {
- static private VSProperties single_instance;
- Properties defaultProps = null, applicationProps;
- // default values of properties
- /** the actual jdbc driver is customizable. by default we use postgresql
- */
- protected final String DatabaseEngine = "DatabaseEngine";
- /** driver
- */
- protected final String DatabaseEngineClassName = "DatabaseEngineClassName";
- /** Note: it holds only the default name, and each connection may change this
- name to its own */
- protected final String DatabaseName = "DatabaseName";
- protected final String DatabaseUserName = "DatabaseUserName";
- protected final String ListenSocket = "ListenSocket";
- /** some parameters for acquisition server */
- protected final String AcquisitionServersNumber = "AcquisitionServNumber";
- protected final String AcquisitionServerN_IP = "AcquisitionServerIP";
- /** default description */
- protected final String AcquisitionServerN_DefDescr =
- "AcquisitionServerDefDescr";
- /** configure several push servers */
- protected final String PushServNumber = "PushServNumber";
- /** the Id for corresponding acquisition server. The idea is that channel
- ID is the acquisition server ID. But single acquisition server may
- distribute data to several distantly located push servers - in case
- that the distribution of network load is required. Each client, given
- its IP address have a set of nearest push servers, one per channel.
- This class should privide also the way to get all push servers for
- particular client. Remember - each push server may have only one
- incoming data stream from only one acquisition server.
- */
- protected final String PushServN_AcqServer = "PushServAcqServer";
- protected final String PushServN_IP = "PushServIP";
- /** Input control UDP port: a single computer may have several push server
- running on it. The only way those servers distinguish from each other
- is the incoming UDP control socket to listen. The control socket is
- specified from command line on startup.
- */
- protected final String PushServN_Control = "PushServControl";
- protected final String PushServN_BroadcastInPort = "PushServBroadcastInPort";
- /** path prefix for the database */
- protected final String PushServN_PPrefix = "PushServPPrefix";
- /** each push server represents a single channel for a set of local
- networks, may have several broadcast addresses - for this channel
- */
- protected final String PushServN_NumBroadcasts = "PushServNumBroadcasts";
- /** the key is composed from base + pserv num + "_" + chan num
- */
- protected final String PushServN_BroadcastsM_IP = "PushServBroadcastsIP";
- protected final String PushServN_BroadcastsM_Port = "PushServBroadcastsPort";
- protected Vector allProps = null;
- /** Use this function to access the instance of properties
- */
- synchronized static public VSProperties GetPropertiesInstance()
- {
- if( single_instance != null )
- return single_instance;
- else {
- single_instance = new VSProperties();
- return single_instance;
- }
- }
- public VSProperties()
- {
- System.out.println( "Loading system properties." );
- // create properties
- defaultProps = new Properties();
- allProps = new Vector();
- // create program properties
- applicationProps = new Properties(defaultProps);
- // now load properties from last invocation
- try {
- FileInputStream in = new FileInputStream("appProperties");
- applicationProps.load(in);
- in.close();
- } catch ( FileNotFoundException e ) {
- System.out.println( e.toString() );
- } catch ( IOException e ) {
- }
- // now fill not-existing yet properties with defaults
- defaultPropsPut( DatabaseEngine, "jdbc:postgresql", "Name of " +
- "database engine. For postgres jdbc:postgresql");
- defaultPropsPut( DatabaseEngineClassName, "postgresql.Driver",
- "JDBC driver implementation" );
- defaultPropsPut( DatabaseName, "videoservertest", "Database name" );
- defaultPropsPut( DatabaseUserName, "andrew",
- "User account name of server" );
- defaultPropsPut( ListenSocket, "7077","Listen incoming user " +
- "connections");
- defaultPropsPut( AcquisitionServersNumber, "1", "Number of valid " +
- "acquisition servers" );
- // loop on all acquisition servers
- int as_num = getPropertyInt( AcquisitionServersNumber );
- for( int i = 0; i < as_num; i++ )
- {
- String id = String.valueOf( i + 1 );
- defaultPropsPut( AcquisitionServerN_IP + id, "130.245.22.9",
- "IP or hostname of acquisition server (where " +
- "mpeg encoder is located)" );
- defaultPropsPut( AcquisitionServerN_DefDescr + id,
- "Our best channel",
- "Default description of the channel from this " +
- "acquisition server" );
- }
- defaultPropsPut( PushServNumber, "1",
- "How many push servers are in use" );
-
- // ensure that default props for every push server are set
- int push_num = getPropertyInt( PushServNumber );
- for( int i = 0; i < push_num; i++ )
- {
- String id = String.valueOf( i + 1 );
- defaultPropsPut( PushServN_AcqServer + id, "1",
- "Id of acquisition server( channel ) " +
- "for push server #" + id );
- defaultPropsPut( PushServN_IP + id, "localhost",
- "Address of push server #" + id );
- defaultPropsPut( PushServN_Control + id, "7078",
- "Port for control packets, push server #" + id );
- defaultPropsPut
- ( PushServN_BroadcastInPort + id, "7081", "Port in push " +
- "server #" + id + " to accept broadcast TCP stream" );
- defaultPropsPut( PushServN_PPrefix + id,
- "/home/videoserver/VideoData",
- "Prefix path to store video files, push server #"
- + id );
- defaultPropsPut( PushServN_NumBroadcasts + id, "1",
- "Number of output broadcast channels for " +
- "push server #" + id );
- // loop through destinations
- int broadcasts_num = getPropertyInt( PushServN_NumBroadcasts + id );
- for( int c = 0; c < broadcasts_num; c++ )
- {
- String c_id = String.valueOf( c + 1 );
- defaultPropsPut( PushServN_BroadcastsM_IP + id + "_" + c_id,
- "127.0.0.1", "Output broadcast distination #" +
- c_id + "for push server #" + id + " IP addr" );
- defaultPropsPut( PushServN_BroadcastsM_Port + id + "_" + c_id,
- "7080", "Output broadcast channel #" +
- c_id + "for push server #" + id + " port" );
- }
- }
- }
- private void defaultPropsPut( String key, String val, String help )
- {
- defaultProps.put( key, val );
- KeyDescription kd = new KeyDescription( key, help );
- allProps.addElement( kd );
- }
- protected String getProperty( String key )
- {
- return applicationProps.getProperty( key );
- }
- protected int getPropertyInt( String key )
- {
- String val = applicationProps.getProperty( key );
- if( val != null )
- return Integer.decode( val ).intValue();
- else return 0;
- }
- protected void setProperty( String key, String value )
- {
- applicationProps.put( key, value );
- }
- protected Vector getPropertiesList()
- {
- return allProps;
- }
- protected void list_properties( DataOutputStream outputStream )
- throws IOException
- {
- outputStream.writeBytes( "->n" + allProps.size() + "n" );
- for( int i = 0; i < allProps.size(); i++ )
- {
- String key = ((KeyDescription)allProps.elementAt( i )).key;
- outputStream.writeBytes( key + "n" );
- outputStream.writeBytes( getProperty( key ) + "n" );
- outputStream.writeBytes
- ( ((KeyDescription)allProps.elementAt( i )).help + "n" );
- }
- outputStream.writeBytes( "<-n" );
- }
- protected void set_property( BufferedReader inputReader,
- DataOutputStream outputStream )
- throws IOException, SyntaxException
- {
- String key = "", value = "";
- try {
- do {
- key = inputReader.readLine();
- } while( key.trim().length() == 0 );
- do {
- value = inputReader.readLine();
- } while( value.trim().length() == 0 );
- } catch (IOException e) {
- throw new SyntaxException( _set_property_[KEY] + " " +
- _set_property_[LDESC] );
- }
- setProperty( key, value );
- }
- protected void finalize() throws Throwable
- {
- save_app_defaults();
- }
- protected void save_app_defaults() throws Throwable
- {
- FileOutputStream out = new FileOutputStream("appProperties");
- applicationProps.save(out, "---No Comment---");
- out.close();
- super.finalize();
- }
- // --------- specific functions -------------
- /** returns the Vector of Dictionaries of all push server Ids that broadcast
- something on given IP address. Given push server ID you may get the
- corresponding channel.
- Format: key = "id", key = "ip", key = "port"
- */
- protected Vector list_push_serv_ids_by_IP( String user_ip )
- throws UnknownHostException
- {
- // vector of Strings, representing ID's
- Vector res = new Vector();
- InetAddress iser_ip_ia = InetAddress.getByName( user_ip );
- byte user_ip_b[] = iser_ip_ia.getAddress();
- int push_num = getPropertyInt( PushServNumber );
- for( int i = 0; i < push_num; i++ )
- {
- String id = String.valueOf( i + 1 );
- // loop through destinations
- int broadcasts_num = getPropertyInt( PushServN_NumBroadcasts + id );
- destinations_loop:
- for( int c = 0; c < broadcasts_num; c++ )
- {
- String c_id = String.valueOf( c + 1 );
- String ip = getProperty( PushServN_BroadcastsM_IP + id + "_" +
- c_id );
- String port = getProperty( PushServN_BroadcastsM_Port + id +
- "_" + c_id );
- // if it is targeted to broadcast to itself...
- if( ip.startsWith( "127" ))
- ip = getProperty( PushServN_IP + id );
- // we must convert both addresses to bytes and compare by mask
- byte ip_b[] = InetAddress.getByName( ip ).getAddress();
- for( int b = 0; b < ip_b.length; b ++ )
- {
- if( ( user_ip_b[b] & ip_b[b] ) != user_ip_b[b] )
- continue destinations_loop; // not what we need
- }
- // add this push server ID to the Vector on proceed with next
- Dictionary element = new Hashtable();
- element.put( "id", id );
- element.put( "ip", ip );
- element.put( "port", port );
- res.addElement( element );
- break;
- }
- }
- return res;
- }
- /** Each acquisition server may transmit it's channel data to several
- push servers at the same time. Returns the Vector of String's.
- Acquisition server id and channel id is the same
- */
- protected Vector list_push_serv_ids_by_ch_id( int channel_id )
- throws UnknownHostException
- {
- Vector res = new Vector();
- int push_num = getPropertyInt( PushServNumber );
- for( int i = 0; i < push_num; i++ )
- {
- String id = String.valueOf( i + 1 );
- int ch_id = getPropertyInt( PushServN_AcqServer + id );
-
- if( ch_id != channel_id )
- continue;
- res.addElement( id );
- }
- return res;
- }
- }