Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010, 2011 SAP AG and others All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Lazar Kirchev, SAP AG - initial API and implementation IBM Corporation - ongoing development /
 
 package org.eclipse.equinox.console.telnet;
 
 import java.util.List;
 import java.util.Map;
 
A telnet server, which listens for telnet connections and starts a telnet connection manager when a connection is accepted. If there are multiple CommandProcessor, a telnet connection is created for each of them.
 
 public class TelnetServer extends Thread {
 	
 	private ServerSocket server;
     private boolean isRunning = true;
     private List<CommandProcessorprocessors = null;
     private BundleContext context;
     private List<Socketsockets = new ArrayList<Socket>();
     
     public TelnetServer(BundleContext contextList<CommandProcessorprocessorsString hostint portthrows IOException {
     	this. = context;
     	this. = processors;
     	if(host != null) {
     		 = new ServerSocket(port, 0, InetAddress.getByName(host));
     	} else {
     		 = new ServerSocket(port);
     	}
     }
     
 	public void run()
     {
         try
         {
             while ()
             {
                 final Socket socket = .accept();
                 .add(socket);
                 for (CommandProcessor processor : ) {
                 	TelnetConnection telnetConnection = new TelnetConnection(socketprocessor);
                 	List<TelnetConnectiontelnetConnections = .get(processor);
                 	if (telnetConnections == null) {
                 		telnetConnections = new ArrayList<TelnetConnection>();
                 		.put(processortelnetConnections);
                 	}
                 	telnetConnections.add(telnetConnection);
                 	telnetConnection.start();
                 }
             }
         } catch (IOException e) {
             if ( == true) {
                 e.printStackTrace();
             }
         } finally {
         	 = false;
             try {
                 if ( != null) {
                     .close();
                 }
             } catch (IOException e){
             	// do nothing
             }
         }
     }
 	
 	public synchronized void addCommandProcessor(CommandProcessor processor) {
 		.add(processor);
 		if (!.isEmpty()) {
 			List<TelnetConnectiontelnetConnections = new ArrayList<TelnetConnection>();
 			for (Socket socket : ) {
 				TelnetConnection telnetConnection = new TelnetConnection(socketprocessor);
 				telnetConnections.add(telnetConnection);
 				telnetConnection.start();
 			}
 			.put(processortelnetConnections);
 		}
 	}
 	
 	public synchronized void removeCommandProcessor(CommandProcessor processor) {
		.remove(processor);
		List<TelnetConnectiontelnetConnections = .remove(processor);
		if (telnetConnections != null) {
			for (TelnetConnection telnetConnection : telnetConnections) {
				telnetConnection.close();
			}
		}
	}
	public synchronized void stopTelnetServer() {
		 = false;
		try {
            if ( != null) {
                .close();
            }    
        } catch (IOException e){
        	// do nothing
        }
        
        for(List<TelnetConnectiontelnetConnections : .values()) {
        	for (TelnetConnection telnetConnection : telnetConnections) {
        		telnetConnection.close();
        	}
        }
        
		this.interrupt();
	}
New to GrepCode? Check out our FAQ X