FileDocCategorySizeDatePackage
PluginSingleInstanceHandler.javaAPI DocAzureus 3.0.3.45369Thu Feb 09 19:43:08 GMT 2006org.gudy.azureus2.pluginsimpl.local.launch

PluginSingleInstanceHandler

public class PluginSingleInstanceHandler extends Object

Fields Summary
private static boolean
active
private static int
port
private static org.gudy.azureus2.plugins.PluginManagerArgumentHandler
handler
Constructors Summary
Methods Summary
protected static java.lang.StringgetHeader()

		return( SystemProperties.getApplicationName() + " Single Instance Handler" );
	
public static voidinitialise(int _port, org.gudy.azureus2.plugins.PluginManagerArgumentHandler _handler)

		port		= _port;
		handler		= _handler;
		
		String	multi_instance = System.getProperty( "MULTI_INSTANCE");
		
		if ( multi_instance != null && multi_instance.equalsIgnoreCase( "true" )){
			
			return;
		}
		
		active = true;
	
protected static booleanprocess(org.gudy.azureus2.plugins.logging.LoggerChannelListener log, java.lang.String[] args)

		if ( active ){
			
			if ( startListener( log )){
				
				return( false );
				
			}else{
				
				sendArguments( log, args );
				
				return( true );
			}
		}else{
			
			return( false );
		}
	
protected static voidsendArguments(org.gudy.azureus2.plugins.logging.LoggerChannelListener log, java.lang.String[] args)

		Socket	socket = null;
	
		try{
			socket = new Socket( "127.0.0.1", port );
		       
			ObjectOutputStream	oos = new ObjectOutputStream( socket.getOutputStream());
			
			oos.writeInt( 0 );
			
			oos.writeObject( getHeader());
			
			oos.writeObject( args );
			
			log.messageLogged( LoggerChannel.LT_INFORMATION, "SingleInstanceHandler: arguments passed to existing process" );
			
    	}catch( Throwable e ){
    		
    		log.messageLogged( "SingleInstanceHandler: send error", e );

    	}finally{
    		
    		if ( socket != null ){
    			try{
    				socket.close();
    				
    			}catch( Throwable e ){
    			}	
    		}	
    	}
	
protected static booleanstartListener(org.gudy.azureus2.plugins.logging.LoggerChannelListener log)

		try{
			final ServerSocket server_socket = new ServerSocket( port, 50, InetAddress.getByName("127.0.0.1"));
	              
			log.messageLogged( 
					  LoggerChannel.LT_INFORMATION,
					  "SingleInstanceHandler: listening on 127.0.0.1:" + port + " for passed arguments");
	    
			Thread t = 
				new Thread("Single Instance Handler")
				{
		    		public void 
		    		run()
					{
		    		    while ( true ){
		    		    	
		    		    	Socket socket			= null;
		    		    	ObjectInputStream	ois	= null;
		    		    	
		    		    	try{
		    		    		socket = server_socket.accept();
		    		    		
		    		    		String address = socket.getInetAddress().getHostAddress();
		    		    		
		    		    		if ( !( address.equals("localhost") || address.equals("127.0.0.1"))){
		    		    			
		    		    			socket.close();
		    		    			
		    		    			continue;
		    		    		}
		    		    		
		    		    		ois = new ObjectInputStream( socket.getInputStream());
		    		    		
		    		    		ois.readInt();	// version
		    		    		
		    		    		String	header = (String)ois.readObject();
		    		    		
		    		    		if ( !header.equals( getHeader())){
		    		    			
		    		    			log.messageLogged( 
		    		  					  LoggerChannel.LT_ERROR,
		    		  					  "SingleInstanceHandler: invalid header - " + header );
		    		    			
		    		    			continue;
		    		    		}

		    		    		String[]	args = (String[])ois.readObject();
		    		    		
		    		    		handler.processArguments( args );
		    		    		
		    		    	}catch( Throwable e ){
		    		    		
		    		    		log.messageLogged( "SingleInstanceHandler: receive error", e );

		    		    	}finally{
		    		    		
		    		    		if ( ois != null ){
		    		    			try{
		    		    				ois.close();
		    		    				
		    		    			}catch( Throwable e ){
		    		    			}
		    		    		}
		    		    		
		    		    		if ( socket != null ){
		    		    			try{
		    		    				socket.close();
		    		    				
		    		    			}catch( Throwable e ){
		    		    			}	
		    		    		}
		    		    	}
		    		    }
					}
				};
		    
			t.setDaemon( true );
				
			t.start(); 
			
			return( true );
			
		}catch( Throwable e ){
		
			return( false );
		}