Added the possibility of caching stored race data - helps when internet connection is very slow and races have been loaded before

This commit is contained in:
Simon Pamies committed 2013-11-13 15:10:44 -02:00
1 parent dd96e0be24
commit 2841dec7d7
1 file changed
+54
@@ -1,6 +1,10 @@
package com.sap.sailing.domain.tractracadapter.impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
@@ -203,6 +207,9 @@ public class TracTracRaceTrackerImpl extends AbstractRaceTrackerImpl implements
logger.info("Starting race tracker: " + tractracEvent.getName() + " " + paramURL + " " + liveURI + " "
+ storedURI + " startOfTracking:" + (startOfTracking != null ? startOfTracking.asMillis() : "n/a") + " endOfTracking:" + (endOfTracking != null ? endOfTracking.asMillis() : "n/a"));
// check if there is a directory configured where stored data files can be cached
storedURI = checkForCachedStoredData(storedURI);
// Initialize data controller using live and stored data sources
controller = new DataController(liveURI, storedURI, this);
// Start live and stored data streams
@@ -237,6 +244,53 @@ public class TracTracRaceTrackerImpl extends AbstractRaceTrackerImpl implements
controlPointPositionPoller = scheduleClientParamsPHPPoller(paramURL, simulator, tracTracUpdateURI, delayToLiveInMillis, tracTracUsername, tracTracPassword);
}
private URI checkForCachedStoredData(URI storedURI){
if (System.getProperty("cache.dir") != null) {
final String directory = System.getProperty("cache.dir");
if (new File(directory).exists()) {
final String[] pathFragments = storedURI.getPath().split("\\/");
final String mtbFileName = pathFragments[pathFragments.length-1];
final String directoryAndFileName = directory+"/"+mtbFileName;
if (!new File(directoryAndFileName).exists()) {
FileOutputStream mtbOutStream = null;
try {
logger.info("Starting to download " + storedURI + " to cache dir " + directoryAndFileName);
InputStream in = storedURI.toURL().openStream();
mtbOutStream = new FileOutputStream(new File(directoryAndFileName));
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
{
mtbOutStream.write(data, 0, count);
}
logger.info("Finished downloading file to cache!");
} catch (Exception ex) {
// never throw but display
ex.printStackTrace();
} finally {
if (mtbOutStream != null) {
try {
mtbOutStream.close();
} catch (IOException e) {
// ignore
}
}
}
} else {
logger.info("Found file " + directoryAndFileName + "! Reusing it for this race!");
}
try {
return new URI("file:///" + directoryAndFileName);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
return storedURI;
}
@Override
public DynamicTrackedRegatta getTrackedRegatta() {
return trackedRegatta;