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

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: ChannelRegistry.java,v 1.6 1999/03/01 03:01:10 andrew Exp $
  6. */
  7. package edu.sunysb.cs.ecsl.videoserver;
  8. import java.io.*;
  9. import java.util.Hashtable;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. import java.util.Dictionary;
  13. import java.util.Hashtable;
  14. /** main parent class should hold the single instance of this class
  15.  */
  16. class ChannelRegistry 
  17. {
  18.   protected VideoServer parentThread = null;
  19.   /** vector of channels
  20.    */
  21.   private Hashtable channels = new Hashtable();
  22.   /** specify the port range as well */
  23.   ChannelRegistry( VideoServer parent )
  24.     {
  25.       parentThread = parent;
  26.     }
  27.   /**  
  28.    */
  29.   protected synchronized void addNewChannel( VideoServerThread ch_thread, 
  30.      int id )
  31.     {
  32.       // use the next ID in sequence
  33.       channels.put( new Integer( id ), 
  34.     new ChannelRegistryItem( ch_thread ) );
  35.     }
  36.   /** returns the number of channels to list and then 4 lines per channel:
  37.       Id number, port, description and status (active/inactive). Do not leave 
  38.       description field blanc! This method also check for each channel to be
  39.       valid at this time; if channel have problems reply with inactive;
  40.       delete channel from list if it's dead.
  41.       Algorithm: first find all push servers that broadcasts to that dest_ip,
  42.       then find the channel for each push server. It is legal (but stupid) 
  43.       to have the same channel to be broadcasted to the same LAN by different
  44.       push servers
  45.   */
  46.   protected synchronized void listChannels( DataOutputStream outputstream,
  47.     String dest_ip )
  48.     throws IOException
  49.     {
  50.       // get the list of push servers
  51.       Vector push_ids = parentThread.properties.list_push_serv_ids_by_IP
  52. ( dest_ip );
  53.       // all push servers will be listed, but some will be reported "inactive"
  54.       outputstream.writeBytes( push_ids.size() + "n" );
  55.       parentThread.log( 2, "report " + push_ids.size() + " channels for " +
  56. "the address " + dest_ip );
  57.       for(int i = 0; i < push_ids.size(); i++ )
  58. {
  59.   Dictionary push_info = (Dictionary)push_ids.elementAt( i );
  60.   String push_id = (String)push_info.get( "id" );
  61.   String channel_id = parentThread.properties.getProperty
  62.     ( parentThread.properties.PushServN_AcqServer + push_id );
  63.   // output channel id
  64.   outputstream.writeBytes( channel_id + "n" );
  65.   // output channel port
  66.   String push_port = (String)push_info.get( "port" );
  67.   outputstream.writeBytes( push_port + "n" );
  68.   
  69.   // output channel description ( not empty )
  70.   ChannelRegistryItem ci = (ChannelRegistryItem) channels.get
  71.       ( Integer.valueOf( push_id ) );
  72.   // "hot" description has the priority
  73.   if( ci == null || ci.description == null || 
  74.       ci.description.trim().length() == 0 )
  75.     {
  76.       // try description from system setup
  77.       String descr = parentThread.properties.getProperty
  78. ( parentThread.properties.AcquisitionServerN_DefDescr + 
  79.   channel_id );
  80.       if( descr == null || descr.trim().length() == 0 )
  81. outputstream.writeBytes( "Channel #" + channel_id + "n" );
  82.       else
  83. outputstream.writeBytes( descr + "n" );
  84.     }
  85.   else
  86.     outputstream.writeBytes( ci.description + "n" );
  87.   // how to determine status?
  88.   // if this channel does not exists in channel registry - it is 
  89.   // inactive
  90.   String active = null;
  91.   if( ci == null )
  92.     active = "inactive";
  93.   else
  94.     active = "active";
  95.   outputstream.writeBytes( active + "n" );
  96.   parentThread.log( 2, "report channel " + channel_id+" "+push_port
  97.     + ", " + active );
  98. }
  99.     }
  100.     /** if this is a valid channel - remove it. No exceptions - stay silent
  101. if fail */
  102.     protected synchronized void removeChannel( int Id )
  103.     {
  104.       channels.remove( new Integer( Id ));
  105.     }
  106. }
  107. class ChannelRegistryItem
  108. {
  109.   protected String description = null;
  110.   VideoServerThread channelThread;
  111.   protected ChannelRegistryItem( VideoServerThread ch_thread )
  112.     {
  113.       channelThread = ch_thread;
  114.     }
  115. }