Author: bleny Date: 2010-05-06 11:48:34 +0200 (Thu, 06 May 2010) New Revision: 24 Url: http://nuiton.org/repositories/revision/diswork/24 Log: implementation du time-out propre Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/SimpleDownload.java trunk/diswork-fs/src/main/resources/log4j.properties Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-06 08:25:12 UTC (rev 23) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-06 09:48:34 UTC (rev 24) @@ -52,7 +52,7 @@ } - public OutputStream read(String path) { + public OutputStream read(String path) throws InterruptedException, FileNotFoundException { log.info("trying to read " + path); @@ -63,15 +63,12 @@ // the file is not available // let's download it - try { - SimpleDownload simpleDownload = new SimpleDownload(path, lookUpService, downloadService); - boolean fileFound = simpleDownload.initiateDownload(); - if (fileFound) - simpleDownload.startDownload(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + SimpleDownload simpleDownload = new SimpleDownload(path, lookUpService, downloadService); + boolean fileFound = simpleDownload.initiateDownload(); + if (fileFound) + simpleDownload.startDownload(); + else + throw new FileNotFoundException("no look-up response received"); } Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/SimpleDownload.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/SimpleDownload.java 2010-05-06 08:25:12 UTC (rev 23) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/SimpleDownload.java 2010-05-06 09:48:34 UTC (rev 24) @@ -12,83 +12,81 @@ public class SimpleDownload implements DownloadObserver, LookUpObserver { + /** + * only used for synchronisation purpose + */ + protected final Object lock = new Object(); + + private Boolean lookUpResponseReceived = false; private Boolean downloadFinised = false; + private FileDescription fileDescription = null; + private DownloadService downloadService; private LookUpService lookUpService; private String filePath; private static final Log log = LogFactory.getLog(SimpleDownload.class); - // TODO timeout - public SimpleDownload(String filePath, LookUpService lookUpService, DownloadService downloadService) throws Exception { + public SimpleDownload(String filePath, LookUpService lookUpService, DownloadService downloadService) { this.filePath = filePath; this.downloadService = downloadService; this.lookUpService = lookUpService; } - public boolean initiateDownload() { + public boolean initiateDownload() throws InterruptedException { lookUpService.lookForFileName(filePath, this); - // FIXME bad implementation of a timeout - int numberOfSecondsWaited = 0; - - boolean responseReceived = false; - - // wait until there is a result or timeout - while (!responseReceived && numberOfSecondsWaited <= 10) { - // response not yet received, wait again... - try { - Thread.sleep(1000); - log.info("waiting for look-up response"); - numberOfSecondsWaited += 1; - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - responseReceived = this.fileDescription != null; - if (responseReceived) - log.info("look-up response received"); - } + + synchronized (lock) { + lock.wait(10 * 1000); // time out at 10 seconds + } + + // TODO throw file not found if timeout exceed + if (lookUpResponseReceived) { + log.info("look-up response received for " + filePath); + } else { + log.info("no look-up response received for " + filePath); + } - return responseReceived; + return lookUpResponseReceived; } - public File startDownload() { + public File startDownload() throws InterruptedException { if (log.isDebugEnabled()) log.info("starting download for " + fileDescription.getFileName()); downloadService.startDownload(fileDescription, this); - // TODO throw file not found if timeout exceed - while(! downloadFinised) { - try { - Thread.sleep(10 * 1000); - log.info("still waiting for " + fileDescription.getFileName() + " download to complete"); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + synchronized (lock) { + lock.wait(); + if (log.isDebugEnabled()) + log.info("download " + fileDescription.getFileName() + " is complete"); + } - if (log.isDebugEnabled()) - log.info("download " + fileDescription.getFileName() + " is complete"); + // FIXME may not return a good file return new File(fileDescription.getFileName()); } @Override public void updateDownloadStatus(DownloadService downloadService) { - synchronized (downloadFinised) { - downloadFinised = downloadService.isFinished(fileDescription); - } + downloadFinised = downloadService.isFinished(fileDescription); + if (downloadFinised) { + synchronized (lock) { + lock.notify(); + } + } } @Override public void receiveResult(FileDescription fileDescription) { - this.fileDescription = fileDescription; + synchronized (lock) { + lookUpResponseReceived = true; + this.fileDescription = fileDescription; + lock.notify(); + } } } Modified: trunk/diswork-fs/src/main/resources/log4j.properties =================================================================== --- trunk/diswork-fs/src/main/resources/log4j.properties 2010-05-06 08:25:12 UTC (rev 23) +++ trunk/diswork-fs/src/main/resources/log4j.properties 2010-05-06 09:48:34 UTC (rev 24) @@ -7,5 +7,5 @@ # package level log4j.logger.org.nuiton.disworkfs=DEBUG log4j.logger.org.nuiton.disworkfs.transport=WARN -#log4j.logger.org.nuiton.disworkfs.services.DownloadService=WARN -#log4j.logger.org.nuiton.disworkfs.services.UploadService=WARN +log4j.logger.org.nuiton.disworkfs.services.DownloadService=WARN +log4j.logger.org.nuiton.disworkfs.services.UploadService=WARN