aboutsummaryrefslogtreecommitdiffstats
blob: 90ce809ad1f043ac1c4734e7cfe159043d7cbb56 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/********************************************************************************
 * Copyright (c) 2009 Motorola Inc. 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
 *
 * Initial Contributor:
 * Otavio Ferranti (Motorola)
 *
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.tml.linuxtools.network.telnet;

import java.io.IOException;

import org.eclipse.tml.common.utilities.logger.ILogger;

import org.eclipse.tml.linuxtools.LinuxToolsPlugin;
import org.eclipse.tml.linuxtools.network.IConnectionProvider;
import org.eclipse.tml.linuxtools.network.IConstants.CommandCode;
import org.eclipse.tml.linuxtools.network.IConstants.EventCode;
import org.eclipse.tml.linuxtools.network.IConstants.OperationCode;
import org.eclipse.tml.linuxtools.tools.AbstractNotifier;

/**
 * @author Ot�vio Ferranti
 */
public class TelnetProvider extends AbstractNotifier implements IConnectionProvider {

	final private String CMD_FETCH_PREFIX = "cat "; //$NON-NLS-1$
	
	private TelnetWrapper connectionWrapper = null;
	private ILogger logger = null;
	
	private Thread connectThread = null;
	private Thread loginThread = null;
	private Thread sendCommandThread = null;
	
	/**
	 * The constructor.
	 */
	public TelnetProvider() {
		connectionWrapper = new TelnetWrapper();
		logger = LinuxToolsPlugin.getLogger();
	}

	/**
	 * This method will be executed in a separated thread and will produce
	 * an event to be sent to the registered listeners. 
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#connect(java.lang.String, int)
	 */
	public void connect(String host, int port) throws IOException {
		final String hostAux = host;
		final int portAux = port;
		final TelnetWrapper connectionWrapperAux = connectionWrapper;

		/*TODO: Enhance this. It would be great if all these executed-in-other threads
				were located under a synchronized block */ 
		
		connectThread = new Thread() {
			public void run() {
				try {
					OperationCode opCode;
					opCode = connectionWrapperAux.connect(hostAux, portAux);
					TelnetProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_CONNECT_FINISHED, opCode);
				} catch (IOException ie) {
					TelnetProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_CONNECT_ERROR,
							OperationCode.UNEXPECTED_RESULT);					
				}
				
			}
		};
		connectThread.start();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#disconnect()
	 */
	public void disconnect() throws IOException {
		if (null != connectThread &&
				connectThread.isAlive()) {
			connectThread.interrupt();
		}
		if (null != loginThread &&
				loginThread.isAlive()) {
			loginThread.interrupt();
		}
		if (null != sendCommandThread &&
				sendCommandThread.isAlive()) {
			sendCommandThread.interrupt();
		}
		connectionWrapper.disconnect();
		notifyListeners(
				EventCode.EVT_PROVIDER_DISCONNECT_FINISHED, OperationCode.SUCCESS);
	}

	/**
	 * This method will be executed in a separated thread and will produce
	 * an event to be sent to the registered listeners. 
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#login(java.lang.String, java.lang.String)
	 */
	public void login(String user, String password) throws IOException {
		final String userAux = user;
		final String passwordAux = password;
		final TelnetWrapper connectionWrapperAux = connectionWrapper;

		/*TODO: Enhance this. It would be great if all these executed-in-other threads
				were located under a synchronized block */ 
		loginThread = new Thread() {
			public void run() {
				try {
					OperationCode opCode;
					opCode = connectionWrapperAux.login(userAux, passwordAux);
					TelnetProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_LOGIN_FINISHED, opCode);
				} catch (IOException ie) {
					TelnetProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_LOGIN_ERROR,
							OperationCode.UNEXPECTED_RESULT);					
				}
			}
		};
		loginThread.start();
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#getLastResponde()
	 */
	public StringBuffer getLastResponde() {
		return connectionWrapper.getLastResponde();
	}

	/**
	 * This method will be executed in a separated thread and will produce
	 * an event to be sent to the registered listeners. 
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#sendCommand(java.lang.String)
	 */
	public void sendCommand(CommandCode cmd, String cmdStr) throws IOException {
		
		if (CommandCode.FETCH_FILE != cmd) {
			return;
		}

		final String commandAux = CMD_FETCH_PREFIX + cmdStr + "\n"; //$NON-NLS-1$
		
		final TelnetWrapper connectionWrapperAux = connectionWrapper;

		/*TODO: Enhance this. It would be great if all these executed-in-other threads
				were located under a synchronized block */ 
		sendCommandThread = new Thread() {
			public void run() {
				try {
					connectionWrapperAux.setResponseLength(2048);
					if (null != connectionWrapperAux.sendCommand(commandAux)) {
						StringBuffer result = connectionWrapperAux.getLastResponde();
						TelnetProvider.this.notifyListeners(
								EventCode.EVT_PROVIDER_SENDCOMMAND_FINISHED, result);
					} else {
						logger.info("##### NULL ######");
					}
				} catch (IOException ie) {
					TelnetProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_SENDCOMMAND_ERROR, null);					
				}
				
			}
		};
		sendCommandThread.start();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#sendData(java.lang.String)
	 */
	public void sendData(String out) {
		connectionWrapper.sendData(out);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxmemorymapviewer.network.IConnectionProvider#setResponseLength(int)
	 */
	public void setResponseLength(int maxLength) {
		connectionWrapper.setResponseLength(maxLength);
	}
}