Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9f75338009bc8bbdd1e7ffb815a611a5325ea070 (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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package org.eclipse.team.internal.ccvs.core.response;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Iterator;

import org.eclipse.core.internal.utils.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
import org.eclipse.team.internal.ccvs.core.connection.Connection;
import org.eclipse.team.internal.ccvs.core.resources.ICVSFolder;

/**
 * The ResponseContainer manages the respones of the server and
 * pipes them to the appropiate handlers.
 * 
 * It also takes care about registering handlers for response-tokens
 * form the server. Standard-handlers are loaded on creation.
 */
public class ResponseDispatcher {
	
	public static final String OK = "ok";	
	public static final String ERROR = "error";
	
	private Hashtable standardResponsePool;
	private Hashtable replaceResponsePool;
	private Connection connection;
	// Idea: private IResponse[] addResponsePool;
	
	/**
	 * Puts all the Request in a container in order to have access
	 * to them when they come from the stream
	 * 
	 * Generic approach to "plug in" new responses just by adding them
	 * to this constructor
	 */	
	public ResponseDispatcher(Connection connection, IResponseHandler[] customHandlers) {
		
		ModTimeHandler modTimeHandler = new ModTimeHandler();
		
		this.connection = connection;
		
		standardResponsePool = new Hashtable();
		replaceResponsePool = new Hashtable();
		
		registerStandardHandler(modTimeHandler);
		registerStandardHandler(new CopyHandler());
		registerStandardHandler(new RemoveEntry());
		registerStandardHandler(new Updated(modTimeHandler,true));
		registerStandardHandler(new Updated(modTimeHandler,false));
		// registerStandardHandler(new UpdateExisting(modTimeHandler));
		registerStandardHandler(new UnsupportedHandler("Valid-requests"));
		registerStandardHandler(new CheckedIn());
		registerStandardHandler(new Removed());
		registerStandardHandler(new MessageOutputHandler("M"));
		registerStandardHandler(new MessageOutputHandler("E"));
		registerStandardHandler(new StaticHandler(true));
		registerStandardHandler(new StaticHandler(false));
		registerStandardHandler(new StickyHandler(true));
		registerStandardHandler(new StickyHandler(false));

		if (customHandlers != null) {
			for (int i=0;i<customHandlers.length;i++) {
				registerResponseHandler(customHandlers[i]);
			}
		}
		
	}
	
	/**
	 * Get the handler matching the string. Take it from the replaceResponsePool if 
	 * possible, otherwise take it from the standardResponsePool.
	 * 
	 * If there is no matching handler at all, return a standard-handler
	 */
	private IResponseHandler getHandler(String responseToken) {
		
		IResponseHandler responseHandler = (IResponseHandler) replaceResponsePool.get(responseToken);
		
		if (responseHandler == null) {
			responseHandler = (IResponseHandler) standardResponsePool.get(responseToken);
		}
		
		if (responseHandler == null) {
			responseHandler = new DefaultHandler();
		}
		
		return responseHandler;
	}
	
	/**
	 * Give a list of all registered Responses from the Server.
	 * 
	 * (ok, error is added, because they are not fromal 
	 * registerd as handler)
	 * 
	 */
	public String makeResponseList() {
		
		StringBuffer result = new StringBuffer("ok error");		
		
		/* We are only looking into the standardResponsePool
		   all the registerd responses must be here as well,
		   otherwise you are not allowed to register special 
		   handler
		*/
		Iterator elements = standardResponsePool.values().iterator();
		while (elements.hasNext()) {
			IResponseHandler handler = (IResponseHandler) elements.next();
			result.append(' ');
			result.append(handler.getName());
		}
		
		return result.toString();
	}
	
	/**
	 * Given a token of response from the server, this method
	 * reacts on it and does the appropiate handling with the
	 * responseHandler, that are loaded in it.
	 */
	public void handle(String responseToken,
							Connection connection, 
							PrintStream messageOutput,
							ICVSFolder mRoot,
							IProgressMonitor monitor) 
							throws CVSException {
		
		IResponseHandler responseHandler;
		
		responseHandler = getHandler(responseToken);
		responseHandler.handle(connection, messageOutput, mRoot, monitor);
		
	}
	
	/**
	 * To register a non-standard responseHandler.
	 * 
	 * Replaces the preloaded responshandler for one response of the 
	 * server. The name of the replaced response is response.getName().
	 * 
	 * If the response is not known to the server, then the call crashes.
	 * 
	 */
	public void registerResponseHandler(IResponseHandler responseHandler) {
		
		Assert.isNotNull(standardResponsePool.get(responseHandler.getName()));
		Assert.isTrue(replaceResponsePool.get(responseHandler.getName()) == null);

		replaceResponsePool.put(responseHandler.getName(),responseHandler);
	}
	
	/**
	 * To unregister a non-standard responseHandler.
	 * 
	 */
	public void unregisterResponseHandler(IResponseHandler responseHandler) {
		
		Assert.isNotNull(standardResponsePool.get(responseHandler.getName()));
		Assert.isNotNull(replaceResponsePool.get(responseHandler.getName()));
		
		replaceResponsePool.remove(responseHandler.getName());
	}	
	
	/**
	 * Registers a standard-handler while doing the 
	 * init.
	 */
	private void registerStandardHandler(IResponseHandler responseHandler) {
		
		Assert.isTrue(standardResponsePool.get(responseHandler.getName()) == null);
		
		standardResponsePool.put(responseHandler.getName(),responseHandler);
	}

	/**
	 * Runs the response event loop.
	 * 
	 * If OK is in the pipe       => stop looping.
	 * In Error is in the pipe    => throw error, stop looping.
	 * Something else in the pipe => handle it with handle(response) 
	 * 									and continou looping
	 * 
	 * Does the work with the monitor
	 */	
	public void manageResponse(IProgressMonitor monitor, 
								ICVSFolder mRoot,
								PrintStream messageOutput) 
								throws CVSException {

		// Processing responses contributes 70% of total work.
		// This depends on the caller to have picked the magic number of 100
		// as the amount of work for the operation!!!
		IProgressMonitor subMonitor = Policy.subMonitorFor(monitor, 70);
		
		// This number can be tweaked if the monitor is judged to move too
		// quickly or too slowly. After some experimentation this is a good
		// number for both large projects (it doesn't move so quickly as to
		// give a false sense of speed) and smaller projects (it actually does
		// move some rather than remaining still and then jumping to 100).
		final int TOTAL_WORK = 300;
		subMonitor.beginTask(Policy.bind("ResponseDispatcher.receiving"), TOTAL_WORK);
		
		int halfWay = TOTAL_WORK / 2;
		int currentIncrement = 4;
		int nextProgress = currentIncrement;
		int worked = 0;

		connection.flush();
				
		try {
			while (true) {
				String response = connection.readToken();
				
				// Update monitor work amount
				if (--nextProgress <= 0) {
					subMonitor.worked(1);
					worked++;
					if (worked >= halfWay) {
						//we have passed the current halfway point, so double the
						//increment and reset the halfway point.
						currentIncrement *= 2;
						halfWay += (TOTAL_WORK - halfWay) / 2;				
					}
					//reset the progress counter to another full increment
					nextProgress = currentIncrement;
				}			
				Policy.checkCanceled(subMonitor);
				
				// Distiguage between three different tokens:
				//   OK    => break
				//   ERROR => throw error (implicit break)
				//   rest  => handle it
				if (response.startsWith(OK)) {
					break;
				} else if (ERROR.equals(response)) {
					throw serverErrorConnection(connection);
				} else {
					handle(response,connection,messageOutput,mRoot,monitor);
				}
			}
		} finally {
			subMonitor.done();
		}
	}
	
	/**
	 * Reads error-data from the server and throws an
	 * exception.
	 */
	private static CVSException serverErrorConnection(Connection connection) {
		
		String message = ERROR;
		
		// The error tag can be followed by an error message too
		if (connection.getLastUsedDelimiterToken() == IResponseHandler.BLANK_DELIMITER) {
			try {
				message = connection.readLine();
			} catch (CVSException e) {
				// We get nothing and go on sending the standard-message
			}
		}
		
		if (message.equals("") || message.equals(" ")) {
			message = Policy.bind("ResponseDispatcher.serverError");
		}
		
		return new CVSServerException(message);
	}
}

Back to the top