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

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: VideoServer.java,v 1.17 1999/03/13 03:06:34 andrew Exp $
  6. */
  7. package edu.sunysb.cs.ecsl.videoserver;
  8. import java.io.*;
  9. import java.net.*;
  10. import java.sql.*;
  11. import java.util.Vector;
  12. import java.util.Calendar;
  13. /**
  14.    The main entry point to the program. The main thread.
  15.    @author Andrew Shuvalov
  16. */
  17. public class VideoServer {
  18.   static VSProperties properties;
  19.   /** Video server listen for a port and create new instance of 
  20.       VideoServerThread class for each newcoming connection
  21.   */
  22.   // static Vector instances = null;
  23.   /** the single instance of VideoServer class
  24.    */
  25.   static private VideoServer application;
  26.   private String RootPassword = null;
  27.   String logFileName = "log/db_videoserver";
  28.   /** print all logging to stream
  29.    */
  30.   private PrintWriter logOutputStream = null;
  31.   private int verboseLevel = 2;
  32.   /** holds the list of all channels 
  33.    */
  34.   protected ChannelRegistry channelRegistry = null;
  35.   /** all incoming connections goes into that group */
  36.   private static ThreadGroup connectionsThreadGroup;
  37.   public static void main( String[] args ) 
  38.     {
  39.       // loading package
  40.       try
  41. {
  42.   Class.forName("edu.sunysb.cs.ecsl.videoserver.VSProperties");
  43. } catch (ClassNotFoundException e) {
  44.   System.err.println("Exception: " + e.toString());
  45.   // if not found - go out, let user fix this
  46.   return;
  47. }
  48.       // loading properties
  49.       properties = VSProperties.GetPropertiesInstance();
  50.       // we must have the database package, or go away!
  51.       try
  52. {
  53.   Class.forName( properties.getProperty
  54.  ( properties.DatabaseEngineClassName ));
  55. } catch (ClassNotFoundException e) {
  56.   System.err.println("Exception: " + e.toString());
  57.   return;
  58. }
  59.       application = new VideoServer();
  60.     }
  61.   /** constructor */
  62.   public VideoServer() 
  63.     {
  64.       try
  65. {
  66.   logOutputStream = new PrintWriter
  67.     ( new FileOutputStream( new File( logFileName) ), true );
  68. } catch (IOException e) {
  69.   System.err.println("Exception: " + e.toString());
  70.   // if not found - go out, let user fix this
  71.   return;
  72. }   
  73.       log( 0, "Starting video server application." );
  74.       // remember the root password
  75.       System.out.print("Enter root password>");
  76.       try {
  77. BufferedReader r = 
  78.   new BufferedReader(new InputStreamReader(System.in));
  79. RootPassword = r.readLine();
  80.       } catch ( IOException e ) {
  81. System.err.println("Exception: " + e.toString());
  82. return;
  83.       }
  84.       // ask the socket number and listen it
  85.       int sock = properties.getPropertyInt( properties.ListenSocket );
  86.       ServerSocket serverSocket = null;
  87.       try {
  88. serverSocket = new ServerSocket( sock );
  89.       } catch (IOException e) {
  90. System.err.println("Could not listen on port.");
  91. System.exit(1);
  92.       }
  93.       // create the single instance of this channel registry
  94.       channelRegistry = new ChannelRegistry( this );
  95.       // create thread group to hold the list of incoming connections
  96.       connectionsThreadGroup = new ThreadGroup( "connections" );
  97.       while( true ) {  // loop of accepting connections
  98. Socket cl_sock;
  99. try {
  100.   cl_sock = serverSocket.accept();
  101.   System.err.println("New connection " + cl_sock.toString() );
  102.   log( 0, "New connection " + cl_sock.toString() );
  103.   // all those threads goes into the same group - "connections"
  104.   VideoServerThread thread = 
  105.     new VideoServerThread( this, cl_sock, properties, 
  106.    connectionsThreadGroup );
  107.   thread.start();
  108. } catch (IOException e) {
  109.   log( 0, "This thread stop after " + e.toString() );
  110. }
  111.       }
  112.     }
  113.   synchronized protected void finalize() throws Throwable {
  114.     properties.save_app_defaults();
  115.   }
  116.   synchronized protected boolean testPassward( String p ) 
  117.     {
  118.       if( 0 == p.compareTo( RootPassword ))
  119. return true;
  120.       return false;
  121.     }
  122.   /** loop over all threads, search for that with specified channel ID
  123.       and tell it the movie ID to set. While movie ID is equal 0 - 
  124.       that means that this channel is not recorded right now
  125.   */
  126.   protected void set_acq_serv_movie_id
  127.       ( int channel_id, int movie_id )
  128.     {
  129.       Thread list[] = new Thread[ connectionsThreadGroup.activeCount() ];
  130.       int count = connectionsThreadGroup.enumerate( list, false );
  131.       for( int i = 0; i < count; i++ )
  132. {
  133.   VideoServerThread thread = (VideoServerThread) list[ i ];
  134.   synchronized( thread )
  135.     {
  136.       if( thread.get_channel_id() == channel_id )
  137. thread.set_movie_id( movie_id );
  138.     }
  139. }
  140.     }
  141.   protected int get_acq_serv_movie_id( int channel_id )
  142.     {
  143.       Thread list[] = new Thread[ connectionsThreadGroup.activeCount() ];
  144.       int count = connectionsThreadGroup.enumerate( list, false );
  145.       for( int i = 0; i < count; i++ )
  146. {
  147.   VideoServerThread thread = (VideoServerThread) list[ i ];
  148.   synchronized( thread )
  149.     {
  150.       if( thread.get_channel_id() == channel_id )
  151. return thread.get_movie_id();
  152.     }
  153. }
  154.       return 0;
  155.     }
  156.   public void log( int level, String msg )
  157.     {
  158.       if( level > verboseLevel )
  159. return;
  160.       synchronized( logOutputStream )
  161. {
  162.   logOutputStream.println( Calendar.getInstance().getTime().
  163.    toString() + ": " + msg );
  164. }
  165.     }
  166. }