VideoServer.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:5k
- /* 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: VideoServer.java,v 1.17 1999/03/13 03:06:34 andrew Exp $
- */
- package edu.sunysb.cs.ecsl.videoserver;
- import java.io.*;
- import java.net.*;
- import java.sql.*;
- import java.util.Vector;
- import java.util.Calendar;
- /**
- The main entry point to the program. The main thread.
- @author Andrew Shuvalov
- */
- public class VideoServer {
- static VSProperties properties;
- /** Video server listen for a port and create new instance of
- VideoServerThread class for each newcoming connection
- */
- // static Vector instances = null;
- /** the single instance of VideoServer class
- */
- static private VideoServer application;
- private String RootPassword = null;
- String logFileName = "log/db_videoserver";
- /** print all logging to stream
- */
- private PrintWriter logOutputStream = null;
- private int verboseLevel = 2;
- /** holds the list of all channels
- */
- protected ChannelRegistry channelRegistry = null;
- /** all incoming connections goes into that group */
- private static ThreadGroup connectionsThreadGroup;
- public static void main( String[] args )
- {
- // loading package
- try
- {
- Class.forName("edu.sunysb.cs.ecsl.videoserver.VSProperties");
- } catch (ClassNotFoundException e) {
- System.err.println("Exception: " + e.toString());
- // if not found - go out, let user fix this
- return;
- }
- // loading properties
- properties = VSProperties.GetPropertiesInstance();
- // we must have the database package, or go away!
- try
- {
- Class.forName( properties.getProperty
- ( properties.DatabaseEngineClassName ));
- } catch (ClassNotFoundException e) {
- System.err.println("Exception: " + e.toString());
- return;
- }
- application = new VideoServer();
- }
- /** constructor */
- public VideoServer()
- {
- try
- {
- logOutputStream = new PrintWriter
- ( new FileOutputStream( new File( logFileName) ), true );
- } catch (IOException e) {
- System.err.println("Exception: " + e.toString());
- // if not found - go out, let user fix this
- return;
- }
- log( 0, "Starting video server application." );
- // remember the root password
- System.out.print("Enter root password>");
- try {
- BufferedReader r =
- new BufferedReader(new InputStreamReader(System.in));
- RootPassword = r.readLine();
- } catch ( IOException e ) {
- System.err.println("Exception: " + e.toString());
- return;
- }
- // ask the socket number and listen it
- int sock = properties.getPropertyInt( properties.ListenSocket );
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket( sock );
- } catch (IOException e) {
- System.err.println("Could not listen on port.");
- System.exit(1);
- }
- // create the single instance of this channel registry
- channelRegistry = new ChannelRegistry( this );
- // create thread group to hold the list of incoming connections
- connectionsThreadGroup = new ThreadGroup( "connections" );
- while( true ) { // loop of accepting connections
- Socket cl_sock;
- try {
- cl_sock = serverSocket.accept();
- System.err.println("New connection " + cl_sock.toString() );
- log( 0, "New connection " + cl_sock.toString() );
- // all those threads goes into the same group - "connections"
- VideoServerThread thread =
- new VideoServerThread( this, cl_sock, properties,
- connectionsThreadGroup );
- thread.start();
- } catch (IOException e) {
- log( 0, "This thread stop after " + e.toString() );
- }
- }
- }
- synchronized protected void finalize() throws Throwable {
- properties.save_app_defaults();
- }
- synchronized protected boolean testPassward( String p )
- {
- if( 0 == p.compareTo( RootPassword ))
- return true;
- return false;
- }
- /** loop over all threads, search for that with specified channel ID
- and tell it the movie ID to set. While movie ID is equal 0 -
- that means that this channel is not recorded right now
- */
- protected void set_acq_serv_movie_id
- ( int channel_id, int movie_id )
- {
- Thread list[] = new Thread[ connectionsThreadGroup.activeCount() ];
- int count = connectionsThreadGroup.enumerate( list, false );
- for( int i = 0; i < count; i++ )
- {
- VideoServerThread thread = (VideoServerThread) list[ i ];
- synchronized( thread )
- {
- if( thread.get_channel_id() == channel_id )
- thread.set_movie_id( movie_id );
- }
- }
- }
- protected int get_acq_serv_movie_id( int channel_id )
- {
- Thread list[] = new Thread[ connectionsThreadGroup.activeCount() ];
- int count = connectionsThreadGroup.enumerate( list, false );
- for( int i = 0; i < count; i++ )
- {
- VideoServerThread thread = (VideoServerThread) list[ i ];
- synchronized( thread )
- {
- if( thread.get_channel_id() == channel_id )
- return thread.get_movie_id();
- }
- }
- return 0;
- }
- public void log( int level, String msg )
- {
- if( level > verboseLevel )
- return;
- synchronized( logOutputStream )
- {
- logOutputStream.println( Calendar.getInstance().getTime().
- toString() + ": " + msg );
- }
- }
- }