Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 75aee71bee89c24457f107bf35c1922ad8b0bf29 (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
128
129
130
131
132
133
134
135
136
137
138
139
/********************************************************************************
 * 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.linuxtools.sequoyah.device.network.tcf;

import java.io.IOException;

import org.eclipse.linuxtools.sequoyah.device.LinuxToolsPlugin;
import org.eclipse.linuxtools.sequoyah.device.network.IConnectionProvider;
import org.eclipse.linuxtools.sequoyah.device.network.IConstants.CommandCode;
import org.eclipse.linuxtools.sequoyah.device.network.IConstants.EventCode;
import org.eclipse.linuxtools.sequoyah.device.network.IConstants.OperationCode;
import org.eclipse.linuxtools.sequoyah.device.tools.AbstractNotifier;
import org.eclipse.sequoyah.device.common.utilities.logger.ILogger;

public class TCFProvider extends AbstractNotifier implements
		IConnectionProvider {

	private TCFWrapper connectionWrapper = null;
	private ILogger logger = null;
	
	private Thread connectThread = null;
	private Thread sendCommandThread = null;
	
	/**
	 * The constructor.
	 */
	public TCFProvider() {
		connectionWrapper = new TCFWrapper();
		logger = LinuxToolsPlugin.getLogger();
	}

	/**
	 * This method will be executed in a separated thread and will produce
	 * an event to be sent to the registered listeners. 
	 */
	public void connect(String host, int port) throws IOException {
		final String hostAux = host;
		final int portAux = port;
		final TCFWrapper 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 {
					connectionWrapperAux.connect(hostAux, portAux);
					TCFProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_CONNECT_FINISHED,
							OperationCode.SUCCESS);
				} catch (IOException ie) {
					TCFProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_CONNECT_ERROR,
							OperationCode.UNEXPECTED_RESULT);					
				}
				
			}
		};
		connectThread.start();
	}

	public void disconnect() throws IOException {
		if (null != connectThread &&
				connectThread.isAlive()) {
			connectThread.interrupt();
		}

		if (null != sendCommandThread &&
				sendCommandThread.isAlive()) {
			sendCommandThread.interrupt();
		}
		connectionWrapper.disconnect();
		notifyListeners(
				EventCode.EVT_PROVIDER_DISCONNECT_FINISHED, OperationCode.SUCCESS);
	}

	public StringBuffer getLastResponde() {
		return connectionWrapper.getLastResponde();
	}

	public void login(String user, String password) throws IOException {

	}

	/**
	 * This method will be executed in a separated thread and will produce
	 * an event to be sent to the registered listeners. 
	 */
	public void sendCommand(CommandCode cmd, String cmdStr) throws IOException {

		if (CommandCode.FETCH_FILE != cmd) {
			return;
		}

		final TCFWrapper connectionWrapperAux = connectionWrapper;
	
		final String path = cmdStr.substring(0, cmdStr.lastIndexOf("/"));
		final String fileName = cmdStr.substring(cmdStr.lastIndexOf("/"));

		sendCommandThread = new Thread() {
			public void run() {
				try {
					
					if (null != connectionWrapperAux.fetchFile(path, fileName)) {
						StringBuffer result = connectionWrapperAux.getLastResponde();
						TCFProvider.this.notifyListeners(
								EventCode.EVT_PROVIDER_SENDCOMMAND_FINISHED, result);
					} else {
						logger.info("##### NULL ######");
					}
				} catch (IOException ie) {
					TCFProvider.this.notifyListeners(
							EventCode.EVT_PROVIDER_SENDCOMMAND_ERROR, null);					
				}
				
			}
		};
		sendCommandThread.start();
	}

	public void sendData(String out) {
		connectionWrapper.sendData(out);
	}

	public void setResponseLength(int maxLength) {
		connectionWrapper.setResponseLength(maxLength);
	}
}

Back to the top