VSProperties.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:12k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
  2.    Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
  3.    Software license is located in file "COPYING"
  4.    VideoServer application
  5.      $Id: VSProperties.java,v 1.13 1999/02/25 03:46:30 andrew Exp $
  6. */
  7. package edu.sunysb.cs.ecsl.videoserver;
  8. import java.io.*;
  9. import java.util.Properties;
  10. import java.util.Vector;
  11. import java.util.Dictionary;
  12. import java.util.Hashtable;
  13. import java.net.InetAddress;
  14. import java.net.UnknownHostException;
  15. /** 
  16.     holds pairs of keys and help descriptions for each key 
  17.     it will be stored in vector allProps
  18. */
  19. class KeyDescription
  20. {
  21.   String key;
  22.   String help;
  23.   public KeyDescription( String k, String h )
  24.     {
  25.       key = k;
  26.       help = h;
  27.     }
  28. }
  29. /**
  30.    manage all specific properties of video server
  31. */
  32. public class VSProperties implements TextProtocol {
  33.   static private VSProperties single_instance;
  34.   Properties defaultProps = null, applicationProps;
  35.   // default values of properties
  36.   /** the actual jdbc driver is customizable. by default we use postgresql
  37.    */
  38.   protected final String DatabaseEngine = "DatabaseEngine";
  39.   /** driver
  40.    */
  41.   protected final String DatabaseEngineClassName = "DatabaseEngineClassName";
  42.   /** Note: it holds only the default name, and each connection may change this
  43.       name to its own */
  44.   protected final String DatabaseName = "DatabaseName";
  45.   protected final String DatabaseUserName = "DatabaseUserName";
  46.   protected final String ListenSocket = "ListenSocket";
  47.   /** some parameters for acquisition server */
  48.   protected final String AcquisitionServersNumber = "AcquisitionServNumber";
  49.   protected final String AcquisitionServerN_IP = "AcquisitionServerIP";
  50.   /** default description */
  51.   protected final String AcquisitionServerN_DefDescr = 
  52.       "AcquisitionServerDefDescr";
  53.   /** configure several push servers */
  54.   protected final String PushServNumber = "PushServNumber";
  55.   /** the Id for corresponding acquisition server. The idea is that channel
  56.       ID is the acquisition server ID. But single acquisition server may
  57.       distribute data to several distantly located push servers - in case
  58.       that the distribution of network load is required. Each client, given
  59.       its IP address have a set of nearest push servers, one per channel.
  60.       This class should privide also the way to get all push servers for 
  61.       particular client. Remember - each push server may have only one 
  62.       incoming data stream from only one acquisition server.
  63.   */
  64.   protected final String PushServN_AcqServer = "PushServAcqServer";
  65.   protected final String PushServN_IP = "PushServIP";
  66.   /** Input control UDP port: a single computer may have several push server
  67.       running on it. The only way those servers distinguish from each other
  68.       is the incoming UDP control socket to listen. The control socket is
  69.       specified from command line on startup. 
  70.   */
  71.   protected final String PushServN_Control = "PushServControl";
  72.   protected final String PushServN_BroadcastInPort = "PushServBroadcastInPort";
  73.   /** path prefix for the database */
  74.   protected final String PushServN_PPrefix = "PushServPPrefix";
  75.   /** each push server represents a single channel for a set of local 
  76.       networks, may have several broadcast addresses - for this channel
  77.    */
  78.   protected final String PushServN_NumBroadcasts = "PushServNumBroadcasts";
  79.   /** the key is composed from base + pserv num + "_" + chan num 
  80.    */
  81.   protected final String PushServN_BroadcastsM_IP = "PushServBroadcastsIP";
  82.   protected final String PushServN_BroadcastsM_Port = "PushServBroadcastsPort";
  83.   protected Vector allProps = null;
  84.   /** Use this function to access the instance of properties
  85.    */
  86.   synchronized static public VSProperties GetPropertiesInstance() 
  87.     {
  88.       if( single_instance != null )
  89. return single_instance;
  90.       else {
  91. single_instance = new VSProperties();
  92. return single_instance;
  93.       }
  94.     }
  95.   public VSProperties() 
  96.     {
  97.       System.out.println( "Loading system properties." );
  98.       // create properties
  99.       defaultProps = new Properties();
  100.       allProps = new Vector();
  101.       // create program properties
  102.       applicationProps = new Properties(defaultProps);
  103.       // now load properties from last invocation
  104.       try {
  105. FileInputStream in = new FileInputStream("appProperties");
  106. applicationProps.load(in);
  107. in.close();
  108.       } catch ( FileNotFoundException e ) {
  109. System.out.println( e.toString() );
  110.       } catch ( IOException e ) {
  111.       }
  112.       // now fill not-existing yet properties with defaults
  113.       defaultPropsPut( DatabaseEngine, "jdbc:postgresql", "Name of " +
  114.        "database engine. For postgres jdbc:postgresql");
  115.       defaultPropsPut( DatabaseEngineClassName, "postgresql.Driver",
  116.        "JDBC driver implementation" );
  117.       defaultPropsPut( DatabaseName, "videoservertest", "Database name" );
  118.       defaultPropsPut( DatabaseUserName, "andrew", 
  119.        "User account name of server" );
  120.       defaultPropsPut( ListenSocket, "7077","Listen incoming user " + 
  121.        "connections");
  122.       defaultPropsPut( AcquisitionServersNumber, "1", "Number of valid " +
  123.        "acquisition servers" );
  124.       // loop on all acquisition servers
  125.       int as_num = getPropertyInt( AcquisitionServersNumber );
  126.       for( int i = 0; i < as_num; i++ )
  127.         {
  128.   String id = String.valueOf( i + 1 );
  129.   defaultPropsPut( AcquisitionServerN_IP + id, "130.245.22.9",
  130.    "IP or hostname of acquisition server (where " +
  131.    "mpeg encoder is located)" );
  132.   defaultPropsPut( AcquisitionServerN_DefDescr + id, 
  133.    "Our best channel",
  134.    "Default description of the channel from this " +
  135.    "acquisition server" );
  136. }
  137.       defaultPropsPut( PushServNumber, "1",
  138.        "How many push servers are in use" );
  139.       
  140.       // ensure that default props for every push server are set
  141.       int push_num = getPropertyInt( PushServNumber );
  142.       for( int i = 0; i < push_num; i++ )
  143. {
  144.   String id = String.valueOf( i + 1 );
  145.   defaultPropsPut( PushServN_AcqServer + id, "1",
  146.    "Id of acquisition server( channel ) " +
  147.    "for push server #" + id );
  148.   defaultPropsPut( PushServN_IP + id, "localhost",
  149.    "Address of push server #" + id );
  150.   defaultPropsPut( PushServN_Control + id, "7078",
  151.    "Port for control packets, push server #" + id );
  152.   defaultPropsPut
  153.     ( PushServN_BroadcastInPort + id, "7081", "Port in push " +
  154.       "server #" + id + " to accept broadcast TCP stream" );
  155.   defaultPropsPut( PushServN_PPrefix + id, 
  156.    "/home/videoserver/VideoData",
  157.    "Prefix path to store video files, push server #" 
  158.    + id );
  159.   defaultPropsPut( PushServN_NumBroadcasts + id, "1",
  160.    "Number of output broadcast channels for " +
  161.    "push server #" + id );
  162.   // loop through destinations
  163.   int broadcasts_num = getPropertyInt( PushServN_NumBroadcasts + id );
  164.   for( int c = 0; c < broadcasts_num; c++ )
  165.     {
  166.       String c_id = String.valueOf( c + 1 );
  167.       defaultPropsPut( PushServN_BroadcastsM_IP + id + "_" + c_id,
  168.        "127.0.0.1", "Output broadcast distination #" +
  169.        c_id + "for push server #" + id + " IP addr" );
  170.       defaultPropsPut( PushServN_BroadcastsM_Port + id + "_" + c_id,
  171.        "7080", "Output broadcast channel #" +
  172.        c_id + "for push server #" + id + " port" );
  173.     }
  174. }
  175.     }
  176.   private void defaultPropsPut( String key, String val, String help )
  177.     {
  178.       defaultProps.put( key, val );
  179.       KeyDescription kd = new KeyDescription( key, help );
  180.       allProps.addElement( kd );
  181.     }
  182.   protected String getProperty( String key )
  183.     {
  184.       return applicationProps.getProperty( key );
  185.     }
  186.   protected int getPropertyInt( String key )
  187.     {
  188.       String val = applicationProps.getProperty( key );
  189.       if( val != null )
  190. return Integer.decode( val ).intValue();
  191.       else return 0;
  192.     }
  193.   protected void setProperty( String key, String value )
  194.     {
  195.       applicationProps.put( key, value );
  196.     }
  197.   protected Vector getPropertiesList()
  198.     {
  199.       return allProps;
  200.     }
  201.   protected void list_properties( DataOutputStream outputStream )
  202.     throws IOException
  203.     {
  204.       outputStream.writeBytes( "->n" + allProps.size() + "n" );
  205.       for( int i = 0; i < allProps.size(); i++ )
  206. {
  207.   String key = ((KeyDescription)allProps.elementAt( i )).key;
  208.   outputStream.writeBytes( key + "n" );
  209.   outputStream.writeBytes( getProperty( key ) + "n" );
  210.   outputStream.writeBytes
  211.     ( ((KeyDescription)allProps.elementAt( i )).help + "n" );
  212. }
  213.       outputStream.writeBytes( "<-n" );
  214.     }
  215.   protected void set_property( BufferedReader inputReader, 
  216.        DataOutputStream outputStream )
  217.     throws IOException, SyntaxException
  218.     {
  219.       String key = "", value = "";
  220.       try {
  221. do {
  222.   key = inputReader.readLine();
  223. } while( key.trim().length() == 0 );
  224. do {
  225.   value = inputReader.readLine();
  226. } while( value.trim().length() == 0 );
  227.       } catch (IOException e) {
  228. throw new SyntaxException( _set_property_[KEY] + " " + 
  229.    _set_property_[LDESC] );
  230.       }
  231.       setProperty( key, value );
  232.     }
  233.   protected void finalize() throws Throwable 
  234.     {
  235.       save_app_defaults();
  236.     }
  237.   protected void save_app_defaults() throws Throwable 
  238.     {
  239.       FileOutputStream out = new FileOutputStream("appProperties");
  240.       applicationProps.save(out, "---No Comment---");
  241.       out.close();
  242.       super.finalize();
  243.     }
  244.   // --------- specific functions -------------
  245.   /** returns the Vector of Dictionaries of all push server Ids that broadcast
  246.       something on given IP address. Given push server ID you may get the 
  247.       corresponding channel.
  248.       Format: key = "id", key = "ip", key = "port"
  249.   */
  250.   protected Vector list_push_serv_ids_by_IP( String user_ip )
  251.     throws UnknownHostException
  252.     {
  253.       // vector of Strings, representing ID's
  254.       Vector res = new Vector();
  255.       InetAddress iser_ip_ia = InetAddress.getByName( user_ip );
  256.       byte user_ip_b[] = iser_ip_ia.getAddress();
  257.       int push_num = getPropertyInt( PushServNumber );
  258.       for( int i = 0; i < push_num; i++ )
  259. {
  260.   String id = String.valueOf( i + 1 );
  261.   // loop through destinations
  262.   int broadcasts_num = getPropertyInt( PushServN_NumBroadcasts + id );
  263. destinations_loop:
  264.   for( int c = 0; c < broadcasts_num; c++ )
  265.     {
  266.       String c_id = String.valueOf( c + 1 );
  267.       String ip = getProperty( PushServN_BroadcastsM_IP + id + "_" + 
  268.        c_id );
  269.       String port = getProperty( PushServN_BroadcastsM_Port + id + 
  270.  "_" + c_id );
  271.       // if it is targeted to broadcast to itself...
  272.       if( ip.startsWith( "127" ))
  273. ip = getProperty( PushServN_IP + id );
  274.       // we must convert both addresses to bytes and compare by mask
  275.       byte ip_b[] = InetAddress.getByName( ip ).getAddress();
  276.       for( int b = 0; b < ip_b.length; b ++ )
  277. {
  278.   if( ( user_ip_b[b] & ip_b[b] ) != user_ip_b[b] )
  279.     continue destinations_loop; // not what we need
  280. }
  281.       // add this push server ID to the Vector on proceed with next
  282.       Dictionary element = new Hashtable();
  283.       element.put( "id", id );
  284.       element.put( "ip", ip );
  285.       element.put( "port", port );
  286.       res.addElement( element );
  287.       break;
  288.     }
  289. }
  290.       return res;
  291.     }   
  292.   /** Each acquisition server may transmit it's channel data to several
  293.       push servers at the same time. Returns the Vector of String's.
  294.       Acquisition server id and channel id is the same
  295.   */
  296.   protected Vector list_push_serv_ids_by_ch_id( int channel_id )
  297.     throws UnknownHostException
  298.     {
  299.       Vector res = new Vector();
  300.       int push_num = getPropertyInt( PushServNumber );
  301.       for( int i = 0; i < push_num; i++ )
  302. {
  303.   String id = String.valueOf( i + 1 );
  304.   int ch_id = getPropertyInt( PushServN_AcqServer + id );
  305.   
  306.   if( ch_id != channel_id )
  307.     continue;
  308.   res.addElement( id );
  309. }
  310.       return res;
  311.     }
  312. }