Revision 5378

View differences:

branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/test/org/gvsig/remoteClient/taskplanning/retrieving/URLRetrieveTest.java
43 43
 *
44 44
 * $Id$
45 45
 * $Log$
46
 * Revision 1.3.2.2  2006-05-22 12:19:12  jaume
46
 * Revision 1.3.2.3  2006-05-24 08:28:08  jaume
47 47
 * *** empty log message ***
48 48
 *
49
 * Revision 1.3.2.2  2006/05/22 12:19:12  jaume
50
 * *** empty log message ***
51
 *
49 52
 * Revision 1.3.2.1  2006/05/17 09:21:49  jaume
50 53
 * *** empty log message ***
51 54
 *
......
199 202
		
200 203
	}
201 204
	
202
	public void testRetrieve() {
203
		queue.put(task1);
204
		queue.put(task2);
205
		
206
		while(!queue.isEmpty()) {		
207
		}
208
	}
205
//	public void testRetrieve() {
206
//		queue.put(task1);
207
//		queue.put(task2);
208
//		
209
//		while(!queue.isEmpty()) {		
210
//		}
211
//	}
209 212
	
210 213
	public void testCancelling() {
211 214
		long time = System.currentTimeMillis();
212 215
		
213
		queue.put(task1);
216
		queue.put(task3);
214 217
		queue.put(task2);
215 218
		boolean more = true;
216 219
		while (!queue.isEmpty()) {
217
			if (more && System.currentTimeMillis()-time > 1000) { // wait 1 seconds and cancel
218
				task2.cancel();
220
			if (more && System.currentTimeMillis()-time > 5000) { // wait 1 seconds and cancel
221
				task3.cancel();
219 222
				more = false;
220 223
			}
221 224
		}
......
230 233
		// http://192.168.0.223/prueba.rar
231 234
		RequestManager manager = RequestManager.getInstance();
232 235
		manager.addURLRequest(request3, listener3);
233
		manager.addURLRequest(request4, listener4);
234
		manager.addURLRequest(request4, listener1);
235
		while (!(finished1 && finished3 && finished4)) { 
236
		/*manager.addURLRequest(request4, listener4);
237
		manager.addURLRequest(request4, listener1);*/
238
		while (!(finished3 /*&& finished1 && finished4*/)) { 
236 239
			
237 240
		}
238 241
	}
......
248 251
		manager.addURLRequest(request3, listener2);
249 252
		manager.addURLRequest(request3, listener3);
250 253
		
251
		manager.addURLRequest(request3, listener4);
252
		while (!(finished1 && finished2 && finished3 && finished4)) { 
254
		while (!(finished1 && finished2 && finished3)) { 
253 255
			
254 256
		}
255 257
	}
256 258
	
257
	public void printMessage(String who, int code) {
259
	private void printMessage(String who, int code) {
258 260
		switch (code) {
259 261
		case RetrieveEvent.CONNECTING:
260 262
			System.out.println(who+": connecting");
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/taskplanning/retrieving/RetrieveEvent.java
31 31
	/**
32 32
	 * The request failed retreiving.
33 33
	 */
34
	public static final int REQUEST_FAILED = 4;
34
	public static final int REQUEST_FAILED = -4;
35 35
	
36 36
	/**
37 37
	 * The request was cancelled by somewho.
38 38
	 */
39
	public static final int REQUEST_CANCELED = 5;
39
	public static final int REQUEST_CANCELED = -5;
40 40
	
41 41
	/**
42 42
	 * The task is postprocessing the request. For instance, it is closing the connection.
......
44 44
	public static final int POSTPROCESSING = 6;
45 45
	
46 46
	/**
47
	 * The task returned a non communication error but a server-kind specific error.
47
	 * The task returned a non communication error but a protocol specific error.
48 48
	 */
49
	public static final int ERROR = 11;
49
	public static final int ERROR = -11;
50

  
51
	/**
52
	 * When the url is not valid.
53
	 */
54
	public static final int REQUEST_FAILED_INVALID_URL = -7;
55

  
56
	/**
57
	 * No route to the host.
58
	 */
59
	public static final int REQUEST_FAILED_UNKNOWN_HOST = -8;
60

  
61
	/**
62
	 * No answer from the host, or the connection was refused.
63
	 */
64
	public static final int REQUEST_FAILED_CONNECTION_REFUSED = -9;
50 65
	
51 66
	private int eventType;
52 67
	private String fileName;
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/taskplanning/retrieving/URLRequest.java
8 8
import java.util.Iterator;
9 9
import java.util.Vector;
10 10

  
11
import org.gvsig.remoteClient.ICancellable;
12

  
11 13
/**
12 14
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
13 15
 */
......
26 28
	private Vector listeners = new Vector();
27 29
	private RetrieveEvent event = new RetrieveEvent();
28 30
	private URLRetrieveTask worker;
31
	private ICancellable cancel;
29 32
	
30 33
	public URL getUrl() throws MalformedURLException {
31 34
		String u = protocol;
......
147 150
		if (listeners.size()==0) {
148 151
			// If no body listens at, then no body cares about the result;
149 152
			// cancel the job
153
			event.setType(RetrieveEvent.REQUEST_CANCELED);
150 154
			worker.cancel();
151 155
			worker = null;
152 156
		}
......
219 223
		if (this.requestType != other.requestType) return false;
220 224
		return true;
221 225
	}
226

  
227
	public void setCancel(ICancellable cancel) {
228
		this.cancel = cancel;
229
	}
230

  
231
	public boolean isCanceled() {
232
		if (cancel!=null)
233
			return cancel.isCanceled() || event.getType() == RetrieveEvent.REQUEST_CANCELED;
234
		else 
235
			return event.getType() == RetrieveEvent.REQUEST_CANCELED;
236
	}
222 237
}
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/taskplanning/retrieving/URLRetrieveTask.java
10 10
import java.io.FileOutputStream;
11 11
import java.io.IOException;
12 12
import java.io.InputStream;
13
import java.net.ConnectException;
13 14
import java.net.MalformedURLException;
14 15
import java.net.URL;
16
import java.net.UnknownHostException;
15 17
import java.util.Iterator;
16 18
import java.util.Vector;
17 19

  
......
71 73
				for (int i = is.read(buffer); !canceled && i>0; i = is.read(buffer)){
72 74
					dos.write(buffer, 0, i);
73 75
					readed += i;
76
					if (!canceled) {
77
						canceled = request.isCanceled();
78
					}
74 79
				}
75 80
				
76 81
				dos.close();
......
78 83
			
79 84
			if (canceled) {
80 85
				// Bad incomplete file, delete it.
81
				System.out.println("\n[RemoteClients:URLRetrieveTask] download cancelled ("+url+")\n");
86
				System.err.println("\n[RemoteClients:URLRetrieveTask] download cancelled ("+url+")\n");
82 87
				f.delete();
83
			}
84
			
85
			running = false;
86
			if (canceled)
87 88
				request.setStatus(RetrieveEvent.REQUEST_CANCELED);
88
			else
89
				cancel();
90
			} else
89 91
				request.setStatus(RetrieveEvent.REQUEST_FINISHED);
90
//		} catch (ConnectException e) {
91
//        	JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this,"server_timeout"));
92
//			e.printStackTrace();
93
//			return;
94
//		} catch (IOException e) {
95
//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "cant_connect"));
96
//			e.printStackTrace();
97
//			return;
98
//		}
99 92
		} catch (MalformedURLException e) {
100
			e.printStackTrace();
101
			request.setStatus(RetrieveEvent.REQUEST_FAILED);
93
			request.setStatus(RetrieveEvent.REQUEST_FAILED_INVALID_URL);
102 94
		} catch (IOException e) {
103
			e.printStackTrace();
104
//			event.setMessage("cant_connect");
105
			if (!canceled)
106
				request.setStatus(RetrieveEvent.REQUEST_FAILED);
95
			if (!canceled) {
96
				if (e instanceof UnknownHostException)
97
					request.setStatus(RetrieveEvent.REQUEST_FAILED_UNKNOWN_HOST);
98
				else if (e instanceof ConnectException)
99
					request.setStatus(RetrieveEvent.REQUEST_FAILED_CONNECTION_REFUSED);
100
				else 
101
					request.setStatus(RetrieveEvent.ERROR);
102
			}
107 103
		}
104
		running = false;
108 105
	}
109 106

  
110 107
	public void cancel() {
108
		canceled = true;
111 109
		try {
110
			
112 111
			synchronized (this) {
113 112
				if (is != null) {
114 113
					is.close();
......
118 117
		} catch (IOException e) {
119 118
			e.printStackTrace();
120 119
		}
121
		canceled = true;
120
		
122 121
	}
123 122

  
124 123
	public boolean isRunning() {
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/taskplanning/retrieving/RequestManager.java
38 38
					if (r.equals(request)) {
39 39
						System.out.println(request.getUrl()+" is cached at '"+r.getFileName()+"'");
40 40
						request.setFileName(r.getFileName()); /* Uaaah! 
41
															   * Si no pose a?? l'event es 
41
															   * Si no pose aixo l'event es 
42 42
															   * dispara abans que haja 
43 43
															   * retornat el request 
44 44
															   * reasignat i confon el 
......
49 49
					}
50 50
				}
51 51
				
52
				
53 52
				synchronized (this) {
54
					if (request.getStatus() == RetrieveEvent.REQUEST_CANCELED ||
55
						request.getStatus() == RetrieveEvent.REQUEST_FAILED) {
53
					if (request.getStatus() < 0) { // Error codes are less than 0
56 54
						// The last requested URL was cancelled or it failed but now it is required again
57 55
						// so, I have to remove it and add again.
58 56
						removeURLRequest(request);
59 57
						return addURLRequest(request, listener);
60 58
					} else if (request.getStatus() == RetrieveEvent.REQUEST_FINISHED) {
59
						File f = new File(request.getFileName());
60
						if (!f.exists()) {
61
							/* 
62
							 * Maybe a multiple instance of the 
63
							 * application erased previous cached files.
64
							 * 
65
							 * Remove from the table, and try again.
66
							 */
67
							removeURLRequest(request);
68
							return addURLRequest(request, listener);
69
						}
61 70
						if (debug)
62 71
							System.err.println("done job found ("+request.getFileName()+")");
72
						
63 73
						RetrieveEvent event = new RetrieveEvent();
64 74
						event.setFileName(request.getFileName());
65 75
						event.setType(RetrieveEvent.REQUEST_FINISHED);
......
81 91
				if (!tempDir.exists())
82 92
					tempDir.mkdir();
83 93
				String fileNamePrefix = tempDirectoryPath + 
84
											File.separator + host + 
94
											File.separator + host.toString().replaceAll("[.]", "_") + /* GDAL gets confused if there is dots in the filename */
85 95
											"-" ;
86 96
				if (fileName.startsWith(fileNamePrefix))
87 97
					fileName = fileName.substring(fileNamePrefix.length(), fileName.length());
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSClient.java
2 2
package org.gvsig.remoteClient.wms;
3 3

  
4 4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6 5
import java.io.IOException;
7 6
import java.net.ConnectException;
8 7
import java.util.TreeMap;
9 8
import java.util.Vector;
10 9

  
10
import org.gvsig.remoteClient.ICancellable;
11 11
import org.gvsig.remoteClient.exceptions.ServerErrorException;
12 12
import org.gvsig.remoteClient.exceptions.WMSException;
13 13
import org.gvsig.remoteClient.utils.BoundaryBox;
......
86 86
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
87 87
     * @throws ServerErrorException 
88 88
     */
89
    public void getMap(WMSStatus status) throws WMSException, ServerErrorException{   
90
        handler.getMap(status);
89
    public void getMap(WMSStatus status, ICancellable cancel) {   
90
        handler.getMap(status, cancel);
91 91
    } 
92 92
    
93 93
    /**
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_1_0/WMSProtocolHandler1_1_0.java
13 13

  
14 14
import org.gvsig.remoteClient.utils.CapabilitiesTags;
15 15
import org.gvsig.remoteClient.utils.ExceptionTags;
16
import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation;
16 17
import org.kxml2.io.KXmlParser;
17 18
import org.xmlpull.v1.XmlPullParserException;
18 19

  
......
29 30
	{
30 31
		this.version = "1.1.0";
31 32
		this.name = "WMS1.1.0";
32
		this.serviceInfo = new ServiceInformation(); 
33 33
		this.layers = new TreeMap();
34 34
	}
35 35
    
......
37 37
// Parsing methods....    
38 38
//------------------------------------------------------------------------------    
39 39
/**
40
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
40
 * <p>Parses the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
41 41
 * 
42 42
 */
43 43
    public void parse(File f)
44
    {       
45
    	FileReader reader = null;  	
44
    {     
45
    	layers.clear();
46
    	rootLayer = null;
47
    	serviceInfo = new ServiceInformation(); 
48
		
49
    	FileReader reader = null;
50
    	this.serviceInfo = new ServiceInformation(); 
46 51
    	try
47 52
    	{
48 53
    		reader = new FileReader(f);
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSProtocolHandler.java
4 4
import java.io.DataInputStream;
5 5
import java.io.File;
6 6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8 7
import java.io.IOException;
9
import java.io.InputStream;
10 8
import java.io.StringReader;
11 9
import java.net.MalformedURLException;
12 10
import java.net.URL;
13
import java.net.URLConnection;
14 11
import java.nio.ByteBuffer;
15 12
import java.nio.channels.FileChannel;
16 13
import java.util.ArrayList;
......
18 15
import java.util.TreeMap;
19 16
import java.util.Vector;
20 17

  
18
import org.gvsig.remoteClient.ICancellable;
21 19
import org.gvsig.remoteClient.exceptions.ServerErrorException;
22 20
import org.gvsig.remoteClient.exceptions.WMSException;
23 21
import org.gvsig.remoteClient.taskplanning.retrieving.RequestManager;
24 22
import org.gvsig.remoteClient.taskplanning.retrieving.RetrieveEvent;
25 23
import org.gvsig.remoteClient.taskplanning.retrieving.RetrieveListener;
26 24
import org.gvsig.remoteClient.taskplanning.retrieving.URLRequest;
27
import org.gvsig.remoteClient.taskplanning.retrieving.URLRetrieveTask;
28 25
import org.gvsig.remoteClient.utils.CapabilitiesTags;
29 26
import org.gvsig.remoteClient.utils.ExceptionTags;
30 27
import org.gvsig.remoteClient.utils.Utilities;
......
305 302
    	}
306 303
    }
307 304
    
308
    public void getMap(WMSStatus status) throws ServerErrorException, WMSException
305
    public void getMap(WMSStatus status, ICancellable cancel) 
309 306
    {      
310 307
		try
311 308
		{
312
			mapRequest = buildMapRequest(status);
309
			mapRequest = buildMapRequest(status, cancel);
313 310
			mapRequest.setFileName("wmsGetMap");
314 311
			mapRequest = RequestManager.getInstance().addURLRequest(mapRequest, getMapListener);
315 312
			
......
445 442
     * Builds the GetMapRequest according to the OGC WMS Specifications
446 443
     * @throws MalformedURLException 
447 444
     */
448
    private URLRequest buildMapRequest(WMSStatus status) throws MalformedURLException
445
    private URLRequest buildMapRequest(WMSStatus status, ICancellable cancel) throws MalformedURLException
449 446
    { 
450 447
		URLRequest req = new URLRequest();
451 448
		StringBuffer u = new StringBuffer();
......
471 468
		int myPort = url.getPort();
472 469
		if (myPort != -1 && myPort != 80 )
473 470
			req.setPort(myPort);
474
		
471
		req.setCancel(cancel);
475 472
		return req;
476 473
    }
477 474
    
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_1_1/WMSProtocolHandler1_1_1.java
28 28
	{
29 29
		this.version = "1.1.1";
30 30
		this.name = "WMS1.1.1";
31
		this.serviceInfo = new ServiceInformation(); 
32 31
		this.layers = new TreeMap();
33 32
	}
34 33
    
......
41 40
 */
42 41
    public void parse(File f)
43 42
    {      
44
    	FileReader reader = null;    	
43
    	layers.clear();
44
    	rootLayer = null;
45
    	serviceInfo = new ServiceInformation(); 
46
    	
47
    	FileReader reader = null; 
45 48
    	try
46 49
    	{
47 50
    		reader = new FileReader(f);
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_3_0/WMSProtocolHandler1_3_0.java
13 13

  
14 14
import org.gvsig.remoteClient.utils.CapabilitiesTags;
15 15
import org.gvsig.remoteClient.utils.ExceptionTags;
16
import org.gvsig.remoteClient.wms.WMSLayer;
17
import org.gvsig.remoteClient.wms.WMSProtocolHandler.ServiceInformation;
16 18
import org.kxml2.io.KXmlParser;
17 19
import org.xmlpull.v1.XmlPullParserException;
18 20

  
......
28 30
	{
29 31
		this.version = "1.3.0";
30 32
		this.name = "WMS1.3.0";
31
		this.serviceInfo = new ServiceInformation(); 
32
		this.layers = new TreeMap();		   
33
		this.layers = new TreeMap();
33 34
	}
34 35
    
35 36
//------------------------------------------------------------------------------
......
40 41
 * 
41 42
 */
42 43
    public void parse(File f)
43
    {       
44
    {      
45
    	layers.clear();
46
    	rootLayer = null;
47
    	serviceInfo = new ServiceInformation(); 
44 48
    	FileReader reader = null;    	
45 49
    	try
46 50
    	{
branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/ICancellable.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.1.2.1  2006-05-24 08:28:08  jaume
47
* *** empty log message ***
48
*
49
*
50
*/
51
package org.gvsig.remoteClient;
52

  
53
public interface ICancellable {
54

  
55
	public boolean isCanceled();
56
	
57
}
0 58

  

Also available in: Unified diff