VideoServerThread.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:29k
- /* 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: VideoServerThread.java,v 1.29 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.lang.reflect.Field;
- import java.util.Hashtable;
- import java.util.Calendar;
- /** When new connection is accepted, the VideoServerThread starts and
- establish the text-based protocol
- @author Andrew Shuvalov
- */
- class VideoServerThread extends Thread implements TextProtocol
- {
- protected VideoServer application = null;
- Socket socket;
- private DataOutputStream outputstream = null;
- private BufferedReader inputreader = null;
- /** VideoServerThread class provides also the debugging facilities to
- corresponding connected user. System-level debug is performed by the
- main thread, method "log"
- */
- private int debugLevel = 0;
- /** database connection
- */
- DatabaseConnection dbConnection = null;
- private boolean SuperuserStatus = false;
- /** share single copy
- */
- static VSProperties properties = null;
- /** holds all protocol items - wich function to call on which keyword
- */
- java.util.Hashtable TextProtocolItems = new java.util.Hashtable();
- /** types of this client
- */
- final int clientIsPlayer = 0;
- final int clientIsAcqServer = 1;
- /** default is player
- */
- private int typeOfClient = 0;
- /** if client is acquisition server */
- private int channelSelfId = -1;
- public int get_channel_id() { return channelSelfId; }
- /** 0 if not recording now, otherwise the movie ID */
- private int movieId = 0;
- /** Use the _local_ port number as ID of the thread
- */
- public VideoServerThread( VideoServer app, Socket sock,
- VSProperties props, ThreadGroup group )
- {
- super( group, String.valueOf( sock.getLocalPort() ) );
- socket = sock;
- application = app;
- properties = props;
-
- // init the protocol items
- Field fields[];
- try
- {
- fields = Class.forName
- ( "edu.sunysb.cs.ecsl.videoserver.TextProtocol" ).
- getDeclaredFields();
- String c_pattern[] = new String[3];
- for( int i = 0; i < fields.length; i++ )
- {
- Class c = fields[i].getType();
- if( c.isArray() == false )
- continue;
-
- try
- {
- String value[] = (String[])fields[i].get( c_pattern );
- String key = value[0].toLowerCase().trim();
- // now value[0] represents the string of our keyword
- TextProtocolItem item =
- new TextProtocolItem( this, value[0] );
- TextProtocolItems.put( key, item );
- } catch( Exception e ) {
- application.log( 2, e.toString() );
- }
- }
- } catch( ClassNotFoundException e ) {
- System.err.print( e.toString() );
- }
- }
- public void run()
- {
- try {
- outputstream = new DataOutputStream( socket.getOutputStream() );
- inputreader = new BufferedReader( new InputStreamReader
- ( socket.getInputStream() ));
- dbConnection = new DatabaseConnection( this, application,
- outputstream );
- } catch (IOException e) {
- return;
- } catch (SQLException e) {
- return;
- }
- InterfaceLoop();
-
- // remove itself from channel list ( this call is ignored if this
- // connection is not a channel
- if( typeOfClient == clientIsAcqServer &&
- application.channelRegistry != null )
- application.channelRegistry.removeChannel( channelSelfId );
-
- /** alwais close port and close database when going out
- */
- application.log( 1, "close connection " + socket.toString() );
- try {
- outputstream.writeBytes( "rn" );
- socket.close();
- } catch (IOException e) {
- return;
- }
- // when the 'run' method ends, thread is ended too
- }
- private void putHelp() {
- try {
- outputstream.writeBytes("n# Text protocoln" );
- outputstream.writeBytes(" Prefixes of server messages:n");
- outputstream.writeBytes
- ("t>>>tpromptnt->tfollowing text must be parsed");
- outputstream.writeBytes("nt#tcommentnt#!tErrorn");
- outputstream.writeBytes(" Keywords:n");
- outputstream.writeBytes( shortHelp( _help_ ));
- outputstream.writeBytes( shortHelp( _kill_server_ ));
- outputstream.writeBytes( shortHelp( _superuser_ ));
- outputstream.writeBytes( shortHelp( _debug_level_ ));
- outputstream.writeBytes( shortHelp( _database_name_ ));
- outputstream.writeBytes( shortHelp( _list_movies_ ));
- outputstream.writeBytes( shortHelp( _list_mov_files_ ));
- outputstream.writeBytes( shortHelp( _list_captions_ ));
- outputstream.writeBytes( shortHelp( _play_movie_ ));
- if( SuperuserStatus == true )
- {
- outputstream.writeBytes( " Superuser commands:n" );
- outputstream.writeBytes( shortHelp( _add_new_movie_ ));
- outputstream.writeBytes( shortHelp( _add_movie_file_ ));
- outputstream.writeBytes( shortHelp( _add_caption_ ));
- outputstream.writeBytes( shortHelp( _save_properties_ ));
- outputstream.writeBytes( shortHelp( _list_properties_ ));
- }
- } catch (IOException e) {
- }
- }
- private String shortHelp( String[] h )
- {
- return "t" + h[KEY] + "t" + h[SHDESC] + "n";
- }
- /** parse protocol
- */
- private void InterfaceLoop() {
- try {
- outputstream.writeBytes( "Video ServernConnection establishedn" );
- } catch (IOException e) {
- debug_forced( "!" + e.toString() );
- }
- while( true ) {
- // this is a prompt
- try {
- outputstream.writeBytes( _prompt_[KEY] );
- } catch (IOException e) {
- debug_forced( "!" + e.toString() );
- break;
- }
-
- String s = null;
- try {
- s = inputreader.readLine();
- if( s == null ) // connection broken?
- break;
- if( s.trim().length() == 0 )
- continue;
- } catch (IOException e) {
- debug_forced( "!" + e.toString() );
- break;
- }
- // the first word of s is keyword. Break s into the array of words
- Vector args = splitStringToWords( s );
- String keyCommand = ((String)args.elementAt( 0 )).
- toLowerCase().trim();
- application.log( 2, "command " + keyCommand );
- // try - block for syntax errors
- try
- {
- TextProtocolItem item =
- (TextProtocolItem)TextProtocolItems.get( keyCommand );
- if( item != null )
- {
- // here the parsed command is executed
- item.execute( args );
- }
- else
- {
- application.log( 1, "Unknown command " + keyCommand );
- throw new SyntaxException( "Unknown command " + keyCommand );
- }
- }
- catch ( KillRequestedException e ) {
- break;
- }
- catch ( SyntaxException e ) {
- debug_forced( "!" + e.toString() );
- }
- catch ( IOException e ) {
- debug_forced( "!" + e.toString() );
- break; // we end this thread
- }
- catch ( Exception e ) {
- // all other exceptions...
- debug_forced( "!" + e.toString() );
- break; // we end this thread
- }
- } // while
- try {
- outputstream.writeBytes( "->n" + _shutdown_[KEY] + "n<-n" );
- } catch (IOException e) {
- }
- }
- /** this is not called too often; so create datagram socket, send data
- and close the socket. Do not keep the connection
- */
- protected void commandToPushServer( String serverNum, String arg )
- throws IOException
- {
- byte[] byterepr = arg.getBytes();
- InetAddress addr;
- try
- {
- addr = InetAddress.getByName
- ( properties.getProperty( properties.PushServN_IP +
- serverNum ) );
- } catch ( UnknownHostException e ) {
- application.log( 1, e.toString() );
- throw new IOException( e.toString() );
- }
- int port = properties.getPropertyInt
- ( properties.PushServN_Control + serverNum );
- application.log( 2, "command to push server " + addr.toString() +
- " port " + String.valueOf( port ));
- DatagramPacket packet = new DatagramPacket( byterepr, byterepr.length,
- addr, port );
- DatagramSocket socket = new DatagramSocket();
- socket.send( packet );
- }
- /** utility to split the string
- */
- public static Vector splitStringToWords( String s ) {
- Vector res = new Vector();
- if( s == null )
- return res;
- int pos1 = 0, pos2 = 0;
- while( s.length() > pos1 ) {
- while( pos1 < s.length() && ( s.charAt( pos1 ) == ' '
- || s.charAt( pos1 ) == 't' ) )
- pos1 ++;
- pos2 = pos1;
- while( pos2 < s.length() && s.charAt( pos2 ) != ' '
- && s.charAt( pos2 ) != 't' )
- pos2 ++;
- String ts = s.substring( pos1, pos2 );
- res.addElement( ts );
- pos1 = pos2;
- }
- return res;
- }
- /** debugging is performed through protocol, debug strings begins
- by # */
- public void debug( String s, int level ) {
- if( debugLevel < 1 || debugLevel < level )
- return;
- try {
- outputstream.writeBytes( "#" + s );
- outputstream.writeBytes( "n" );
- } catch (IOException e) {
- }
- }
- public void debug( String s ) {
- debug( s, 1 );
- }
- /** debug message that is 'forced' to appear
- */
- public void debug_forced( String s ) {
- try {
- outputstream.writeBytes( "#" + s + "n" );
- } catch (IOException e) {
- }
- }
- public void kill_impl( Vector args )
- throws KillRequestedException
- {
- throw new KillRequestedException( "Explicit kill" );
- }
- public void superuser_impl( Vector args )
- throws KillRequestedException
- {
- // if authorization fails - break connection immediately
- if( args.size() != 2 )
- throw new KillRequestedException( "Explicit kill" );
- if( false == application.testPassward
- ( (String)args.elementAt( 1 ) ))
- throw new KillRequestedException( "Explicit kill" );
- SuperuserStatus = true;
- }
- public void debug_impl( Vector args )
- {
- if( args.size() == 1 ) // no digit argument -> default 1
- {
- debugLevel = 1;
- debug( "Deafault debug level 1" );
- return;
- }
- else {
- try {
- debugLevel = Integer.parseInt( ((String)args.elementAt( 1 )) );
- debug( "Debug level " + debugLevel );
- } catch (NumberFormatException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- }
- public void help_impl( Vector args )
- {
- putHelp();
- }
- public void database_impl( Vector args )
- throws SyntaxException, IOException
- {
- try {
- dbConnection.database_impl( args, inputreader, outputstream,
- SuperuserStatus );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void listmovies_impl( Vector args )
- throws SyntaxException, IOException
- {
- try {
- dbConnection.listmovies_impl( args, inputreader, outputstream,
- SuperuserStatus );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void listmovfiles_impl( Vector args )
- throws SyntaxException, IOException
- {
- try {
- dbConnection.listmovfiles_impl( args, inputreader, outputstream,
- SuperuserStatus );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void listcaptions_impl( Vector args )
- throws SyntaxException, IOException
- {
- try {
- dbConnection.list_captions( args, inputreader, outputstream,
- SuperuserStatus );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void addnewmovie_impl( Vector args )
- throws SyntaxException, IOException
- {
- int id = 0;
- try {
- if( args.size() != 1 )
- throw new SyntaxException( _add_new_movie_[KEY] + " " +
- _add_new_movie_[LDESC] );
- String movieName = "", startTime = "", stopTime = "";
- try {
- do {
- movieName = inputreader.readLine();
- } while( movieName.trim().length() == 0 );
- do {
- startTime = inputreader.readLine();
- } while( startTime.trim().length() == 0 );
- do {
- stopTime = inputreader.readLine();
- } while( stopTime.trim().length() == 0 );
- } catch (IOException e) {
- throw new SyntaxException( _add_new_movie_[KEY] + " " +
- _add_new_movie_[LDESC] );
- }
- if( SuperuserStatus == false )
- {
- debug_forced
- ("!You are not authorized to add movien");
- outputstream.writeBytes( "->n" + _disconnect_[KEY] + "n<-n" );
- return;
- }
- id = dbConnection.add_new_movie( movieName, startTime, stopTime );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- outputstream.writeBytes( "->n" + String.valueOf( id ) + "n<-n" );
- }
- public void addmoviefile_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( args.size() != 1 )
- throw new SyntaxException( _add_movie_file_[KEY] + " " +
- _add_movie_file_[LDESC] );
- try {
- String mov_id = "", pushserv_id = "", mpegName = "",
- startTime = "", stopTime = "";
-
- try {
- do {
- mov_id = inputreader.readLine();
- } while( mov_id.trim().length() == 0 );
- do {
- pushserv_id = inputreader.readLine();
- } while( pushserv_id.trim().length() == 0 );
- do {
- mpegName = inputreader.readLine();
- } while( mpegName.trim().length() == 0 );
- do {
- startTime = inputreader.readLine();
- } while( startTime.trim().length() == 0 );
- do {
- stopTime = inputreader.readLine();
- } while( stopTime.trim().length() == 0 );
- } catch (IOException e) {
- throw new SyntaxException( _add_movie_file_[KEY] + " " +
- _add_movie_file_[LDESC] );
- }
- if( SuperuserStatus == false )
- {
- debug_forced("!You are not authorized to " +
- "add movie filesn");
- outputstream.writeBytes( "->n" + _disconnect_[KEY] + "n<-n" );
- return;
- }
- dbConnection.add_movie_file( mov_id, pushserv_id, mpegName,
- startTime, stopTime );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void addcaption_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( args.size() != 1 )
- throw new SyntaxException( _add_caption_[KEY] + " " +
- _add_caption_[LDESC] );
- String id = "", time = "", text = "";
- try {
- do {
- id = inputreader.readLine();
- } while( id.trim().length() == 0 );
- do {
- time = inputreader.readLine();
- } while( time.trim().length() == 0 );
- // text may be empty string. don't trim it
- text = inputreader.readLine();
- } catch (IOException e) {
- throw new SyntaxException( _add_caption_[KEY] + " " +
- _add_caption_[LDESC] );
- }
- if( SuperuserStatus == false && typeOfClient != clientIsAcqServer )
- {
- debug_forced
- ("!You are not authorized to add captions");
- outputstream.writeBytes( "->n" + _disconnect_[KEY] + "n<-n" );
- return;
- }
- try {
- dbConnection.add_caption( id, time, text );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- /** that's a variant of the same command but without the movie ID. This
- may come only from acquisition server and movie ID is known. It
- may be zero as well if no recording happens */
- public void addcaptionnoid_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( args.size() != 1 )
- throw new SyntaxException( _add_caption_[KEY] + " " +
- _add_caption_[LDESC] );
- String time = "", text = "";
- try {
- do {
- time = inputreader.readLine();
- } while( time.trim().length() == 0 );
- // text may be empty string. don't trim it
- text = inputreader.readLine();
- } catch (IOException e) {
- throw new SyntaxException( _add_caption_[KEY] + " " +
- _add_caption_[LDESC] );
- }
- if( typeOfClient != clientIsAcqServer )
- {
- debug_forced
- ("!You are not authorized to add captions");
- outputstream.writeBytes( "->n" + _disconnect_[KEY] + "n<-n" );
- return;
- }
- // what is the movie id?
- if( movieId == 0 )
- return;
- try {
- dbConnection.add_caption( String.valueOf( movieId ), time, text );
- } catch ( SQLException e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- public void saveproperties_impl( Vector args )
- {
- if( SuperuserStatus == true )
- {
- try {
- application.properties.save_app_defaults();
- }
- catch( Throwable e ) {
- debug_forced( "!" + e.toString() );
- }
- }
- }
- public void listproperties_impl( Vector args )
- throws IOException
- {
- if( SuperuserStatus == true )
- {
- properties.list_properties( outputstream );
- }
- }
- public void setproperty_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( SuperuserStatus == true )
- {
- properties.set_property( inputreader, outputstream );
- }
- }
- public void startrecording_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( SuperuserStatus == true )
- {
- // get channel ID from input
- String ch_id = "";
- try {
- do {
- ch_id = inputreader.readLine();
- } while( ch_id.trim().length() == 0 );
- } catch (IOException e) {
- throw new SyntaxException( _start_recording_[KEY] +
- " " +
- _start_recording_[LDESC] );
- }
- application.log( 1, "Start recording the channel " + ch_id );
- // we got channel id and wants to start the recording
- // let first find all push servers who receive it
- Vector pushserv_ids =
- properties.list_push_serv_ids_by_ch_id( Integer.
- parseInt( ch_id ));
- if( pushserv_ids.size() == 0 )
- {
- outputstream.writeBytes( "->n" );
- debug( "Nobody can record this channel", 0 );
- outputstream.writeBytes( "<-n" );
- return;
- }
- // first add the movie title to the database and receive
- // the movie ID.
- String description = properties.getProperty
- ( properties.AcquisitionServerN_DefDescr + ch_id );
- java.util.Calendar rightNow = Calendar.getInstance();
-
- try {
- int movie_id = dbConnection.add_new_movie
- ( description, rightNow, null ); // stop time is null (unknown)
- application.log( 2, "New movie id is " +
- String.valueOf( movie_id ) );
- for( int i = 0; i < pushserv_ids.size(); i++ )
- {
- String push_id = (String) pushserv_ids.elementAt( i );
- // tell this push server to start recording
- String filename = set_pushserv_mpeg_filename( push_id );
- // then push server already started recording - store this
- // filename into the database
- dbConnection.add_movie_file
- ( String.valueOf( movie_id ),
- push_id, filename, rightNow, null );
- }
- // that's not all yet. We got the new movie ID and we want the
- // acquisition server to send close captions to the database
- // and they will be stored by movie ID. So find the acq. server
- // thread and tell it the movie ID.
- application.set_acq_serv_movie_id( Integer.parseInt( ch_id ),
- movie_id );
- application.log( 3, "recording started" );
- outputstream.writeBytes( "->n" );
- debug( "recording started", 1 );
- outputstream.writeBytes( "<-n" );
- } catch ( SQLException e) {
- outputstream.writeBytes( "->n" );
- debug_forced( "Can't start recording: " + e.toString() );
- outputstream.writeBytes( "<-n" );
- } catch ( Exception e) {
- outputstream.writeBytes( "->n" );
- outputstream.writeBytes( "<-n" );
- application.log( 0, "recording: " + e.toString() );
- }
- }
- else
- {
- outputstream.writeBytes( "->n" );
- debug_forced( "You are not authorized to start recording" );
- outputstream.writeBytes( "<-n" );
- application.log( 0, "client not authorized to start recording" );
- }
- }
- /** the single parameter is the channel id */
- public void stoprecording_impl( Vector args )
- throws SyntaxException, IOException
- {
- if( SuperuserStatus == true )
- {
- // get channel ID from input
- String ch_id = "";
- try {
- do {
- ch_id = inputreader.readLine();
- } while( ch_id.trim().length() == 0 );
- } catch (IOException e) {
- throw new SyntaxException( _start_recording_[KEY] +
- " " +
- _start_recording_[LDESC] );
- }
- application.log( 1, "Stop recording the channel " + ch_id );
- // we got channel id and wants to stop the recording
- // let first find all push servers who receive it
- Vector pushserv_ids =
- properties.list_push_serv_ids_by_ch_id( Integer.
- parseInt( ch_id ));
- if( pushserv_ids.size() == 0 )
- {
- outputstream.writeBytes( "->n" );
- debug( "Nobody is recording this channel", 0 );
- outputstream.writeBytes( "<-n" );
- return;
- }
- // tell each push server to stop the recording
- for( int i = 0; i < pushserv_ids.size(); i++ )
- {
- String push_id = (String) pushserv_ids.elementAt( i );
- reset_pushserv_mpeg_filename( push_id );
- }
- int movie_id =
- application.get_acq_serv_movie_id( Integer.parseInt( ch_id ) );
- application.set_acq_serv_movie_id( Integer.parseInt( ch_id ), 0 );
- try {
- // database should store the time when movie did ends
- // this call will modify the database to setup the
- // stop time for this movie
- dbConnection.mark_movie_stop_time( movie_id );
- } catch ( SQLException e) {
- debug_forced( "Stop time remains empty: " + e.toString() );
- }
- outputstream.writeBytes( "->n" );
- debug( "recording stopped", 1 );
- outputstream.writeBytes( "<-n" );
- }
- else
- {
- outputstream.writeBytes( "->n" );
- debug_forced( "You are not authorized to stop recording" );
- outputstream.writeBytes( "<-n" );
- application.log( 0, "client not authorized to stop recording" );
- }
- }
- /**
- if the id of movie is not zero, that means that this acquisition server
- thread is in process of recording. If zero - recording stopped
- */
- protected void set_movie_id( int id )
- {
- movieId = id;
- }
-
- protected int get_movie_id()
- {
- return movieId;
- }
-
- public void listchannels_impl( Vector args )
- throws IOException
- {
- // get this client IP from socket and pass it to the channelRegistry
- // to find all channels that are broadcasted here
- String ip = socket.getInetAddress().getHostAddress();
- ip = convert_gen_127_addr( ip );
- outputstream.writeBytes( "->n" );
- if( application.channelRegistry != null )
- application.channelRegistry.listChannels( outputstream, ip );
- outputstream.writeBytes( "<-n" );
- }
- // ---------- Acquisition server commands --------------
- /** look at the incoming connection address ( it was stored in socket )
- and determine this channel Id. It should be known from properties.
- If this acquisition server is not authorized to connect - return
- -1 as Id
- */
- public void registeracquisitionserver_impl( Vector args )
- throws IOException, KillRequestedException
- {
- typeOfClient = clientIsAcqServer;
- InetAddress iaddr = socket.getInetAddress();
- String saddr = iaddr.getHostAddress();
- int ch_nums = properties.getPropertyInt( properties.
- AcquisitionServersNumber );
- channelSelfId = -1;
- for( int i = 0; i < ch_nums; i++ )
- {
- String id = String.valueOf( i + 1 );
- String s_ip = properties.getProperty( properties.
- AcquisitionServerN_IP + id);
- // if this ip address is equal to incoming - Id is determined
- if( s_ip.equals( saddr ))
- {
- channelSelfId = i + 1;
- break;
- }
- }
- if( channelSelfId != -1 )
- application.channelRegistry.addNewChannel( this, channelSelfId );
- // reply the ID only
- outputstream.writeBytes( "->n" );
- outputstream.writeBytes( String.valueOf( channelSelfId ) + "n" );
- outputstream.writeBytes( "<-n" );
- application.log( 2, "new acq. server, #"
- + String.valueOf( channelSelfId ) );
-
- // if authorization failed
- if( channelSelfId == -1 )
- throw new KillRequestedException( "Authorization failed" );
- // at that point reply nothing else, client will request the broadcast
- // parameters with separate command
- }
- /** reply format: num of push servers, for each server: ip, control port,
- broadcast port
- */
- public void requestbroadcastparameters_impl( Vector args )
- throws IOException
- {
- Vector push_ids = properties.list_push_serv_ids_by_ch_id(channelSelfId);
- outputstream.writeBytes( "->n" );
- outputstream.writeBytes( push_ids.size() + "n" ); // number of servers
- application.log( 2, "reporting " + push_ids.size() + " push servers " +
- "for channel #" + String.valueOf( channelSelfId ));
- if( push_ids.size() == 0 )
- {
- // sorry, nothing
- outputstream.writeBytes( "<-n" );
- return;
- }
- for( int i = 0; i < push_ids.size(); i++ )
- {
- String id = (String) push_ids.elementAt( i );
- outputstream.writeBytes( id + "n" );
- outputstream.writeBytes( properties.getProperty
- ( properties.PushServN_IP + id ) + "n" );
- outputstream.writeBytes( properties.getProperty
- ( properties.PushServN_Control
- + id ) + "n" );
- outputstream.writeBytes( properties.getProperty
- ( properties.PushServN_BroadcastInPort
- + id ) + "n" );
- application.log( 2, "push server #" + id );
- // push server should wait for incoming data connection on start-up,
- // if it does not - acq server may troubleshut that by sending
- // a command
- }
- outputstream.writeBytes( "<-n" );
- }
- /** everything is ok - so inform the push server what to do next */
- public void pushserverok_impl( Vector args )
- throws IOException
- {
- try {
- String s_id = "";
- try {
- do {
- s_id = inputreader.readLine();
-
- } while( s_id.trim().length() == 0 );
- // tell server to broadcast
- set_pushserv_broadcasts( s_id );
- // and then report success
- outputstream.writeBytes( "->n" );
- outputstream.writeBytes( "1n" );
- outputstream.writeBytes( "<-n" );
- application.log( 2, "push server #" + s_id + " is reported ok" );
- } catch (IOException e) {
- // report error back
- outputstream.writeBytes( "->n" );
- outputstream.writeBytes( "0n" );
- outputstream.writeBytes( "<-n" );
- throw new SyntaxException( _push_server_ok_[KEY] +
- " " + _push_server_ok_[LDESC] );
- }
- } catch ( Exception e ) {
- System.err.println("Exception: " + e.toString());
- }
- }
- /** this method requests to construct the filename for the mpeg data file
- stored on the particular push server and send this command to push
- server.
- Returns relative path of the new mpeg filename - store it in
- database
- */
- private String set_pushserv_mpeg_filename( String push_server_id )
- throws IOException
- {
- String pprefix = properties.getProperty
- ( properties.PushServN_PPrefix + push_server_id );
- java.util.Calendar rightNow = Calendar.getInstance();
- String subpath =
- String.valueOf( rightNow.get(Calendar.YEAR) ) +
- padZero( rightNow.get(Calendar.MONTH), 2 ) +
- padZero( rightNow.get(Calendar.DAY_OF_MONTH), 2 );
- // file name must be unique for this day
- String fname = padZero( rightNow.get(Calendar.HOUR_OF_DAY), 2 ) +
- padZero( rightNow.get(Calendar.MINUTE), 2 );
- // this is what will be recorded in database:
- fname = subpath + "/" + fname;
- // concat
- String full_fname = pprefix + "/" + fname;
- commandToPushServer
- ( push_server_id, push__set_filename_[KEY] + "n" +
- full_fname + ".mpgn" );
- return fname + ".mpg";
- }
- /** tell push server 'id' to stop recording. Each push server may record
- only its own well known channel, so no more information is needed
- */
- private void reset_pushserv_mpeg_filename( String push_server_id )
- throws IOException
- {
- commandToPushServer
- ( push_server_id, push__stop_recording_[KEY] + "n" );
- }
-
- /** instruct push server to broadcast what it have on input to those
- broadcast destinations
- */
- protected void set_pushserv_broadcasts( String push_server_id )
- throws IOException
- {
- // push server not only store file to disk, but also broadcast it
- // outside
- int num_of_broadcasts = properties.getPropertyInt
- ( properties.PushServN_NumBroadcasts + push_server_id );
- application.log( 2, "Set " + String.valueOf( num_of_broadcasts ) +
- " broadcasts for push server #" + push_server_id );
- for( int c = 0; c < num_of_broadcasts; c++ )
- {
- try {
- String c_id = String.valueOf( c + 1 );
- String broadcast_addr = properties.getProperty
- ( properties.PushServN_BroadcastsM_IP + push_server_id +
- "_" + c_id );
- String broadcast_port = properties.getProperty
- ( properties.PushServN_BroadcastsM_Port + push_server_id +
- "_" + c_id );
- commandToPushServer( push_server_id,
- push__add_broadcast_destination_[KEY] + "n" +
- broadcast_addr + "n" + broadcast_port+"n");
- } catch ( IOException e ) {
- application.log( 1, "can't send command to push server " +
- e.toString() );
- }
- }
- }
- /** utility to pad zeroes before integer, if integer representation
- is shorter than len */
- public String padZero( int value, int len )
- {
- String repr = String.valueOf( value );
- while( repr.length() < len )
- repr = new String( "0" ) + repr;
- return repr;
- }
- public String convert_gen_127_addr( String ip )
- {
- try {
- if( ip.equals( "127.0.0.1" ))
- {
- ip = InetAddress.getLocalHost().getHostAddress();
- return ip;
- }
- } catch ( Exception e ) {
- application.log( 2, e.toString() );
- }
- return ip;
- }
- }