Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b7dd14b2fef3dd0ec3b9339338148a50da4222b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * 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.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.felix.service.command.CommandProcessor;
import org.osgi.framework.BundleContext;

/**
 * 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<CommandProcessor> processors = null;
    private BundleContext context;
    private List<Socket> sockets = new ArrayList<Socket>();
    private Map<CommandProcessor, List<TelnetConnection>> processorToConnectionsMapping = new HashMap<CommandProcessor, List<TelnetConnection>>();
    
    public TelnetServer(BundleContext context, List<CommandProcessor> processors, String host, int port) throws IOException {
    	this.context = context;
    	this.processors = processors;
    	if(host != null) {
    		server = new ServerSocket(port, 0, InetAddress.getByName(host));
    	} else {
    		server = new ServerSocket(port);
    	}
    }
    
	public void run()
    {
        try
        {
            while (isRunning)
            {
                final Socket socket = server.accept();
                sockets.add(socket);
                for (CommandProcessor processor : processors) {
                	TelnetConnection telnetConnection = new TelnetConnection(socket, processor, context);
                	List<TelnetConnection> telnetConnections = processorToConnectionsMapping.get(processor);
                	if (telnetConnections == null) {
                		telnetConnections = new ArrayList<TelnetConnection>();
                		processorToConnectionsMapping.put(processor, telnetConnections);
                	}
                	telnetConnections.add(telnetConnection);
                	telnetConnection.start();
                }
            }
        } catch (IOException e) {
            if (isRunning == true) {
                e.printStackTrace();
            }
        } finally {
        	isRunning = false;
            try {
                if (server != null) {
                    server.close();
                }
            } catch (IOException e){
            	// do nothing
            }
        }
    }
	
	public synchronized void addCommandProcessor(CommandProcessor processor) {
		processors.add(processor);
		if (!sockets.isEmpty()) {
			List<TelnetConnection> telnetConnections = new ArrayList<TelnetConnection>();
			for (Socket socket : sockets) {
				TelnetConnection telnetConnection = new TelnetConnection(socket, processor, context);
				telnetConnections.add(telnetConnection);
				telnetConnection.start();
			}
			processorToConnectionsMapping.put(processor, telnetConnections);
		}
	}
	
	public synchronized void removeCommandProcessor(CommandProcessor processor) {
		processors.remove(processor);
		List<TelnetConnection> telnetConnections = processorToConnectionsMapping.remove(processor);
		if (telnetConnections != null) {
			for (TelnetConnection telnetConnection : telnetConnections) {
				telnetConnection.close();
			}
		}
	}
	
	public synchronized void stopTelnetServer() {
		isRunning = false;
		try {
            if (server != null) {
                server.close();
            }    
        } catch (IOException e){
        	// do nothing
        }
        
        for(List<TelnetConnection> telnetConnections : processorToConnectionsMapping.values()) {
        	for (TelnetConnection telnetConnection : telnetConnections) {
        		telnetConnection.close();
        	}
        }
        
		this.interrupt();
	}
}

Back to the top