use of nl.uva.vlet.io.CircularStreamBufferTransferer in project lobcder by skoulouzis.
the class WebDataFileResource method transferer.
private PDRI transferer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
InputStream in = null;
PDRI pdri = null;
try {
PDRIDescr pdriDescr = selectBestPDRI(pdris);
pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
if (pdri != null) {
in = pdri.getData();
WebDataFileResource.log.log(Level.FINE, "sendContent() for {0}--------- {1}", new Object[] { getPath(), pdri.getFileName() });
if (!pdri.getEncrypted()) {
if (doCircularStreamBufferTransferer) {
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
cBuff.startTransfer((long) -1);
} else {
int read;
byte[] copyBuffer = new byte[Constants.BUF_SIZE];
while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
out.write(copyBuffer, 0, read);
}
}
} else {
DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
encrypter.decrypt(in, out);
}
} else {
sleepTime = 5;
throw new NotFoundException("Physical resource not found");
}
} catch (Exception ex) {
if (ex instanceof NotFoundException) {
throw (NotFoundException) ex;
}
if (ex.getMessage().contains("Resource not found")) {
throw new NotFoundException(ex.getMessage());
}
try {
sleepTime = sleepTime + 20;
Thread.sleep(sleepTime);
if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
transferer(pdris, out, tryCount, false);
} else if (++tryCount < Constants.RECONNECT_NTRY) {
transferer(pdris, out, tryCount, true);
} else {
transferer(pdris, out, 0, true);
}
} catch (InterruptedException ex1) {
sleepTime = 5;
throw new IOException(ex1);
}
} finally {
if (in != null) {
in.close();
}
}
sleepTime = 5;
return pdri;
}
use of nl.uva.vlet.io.CircularStreamBufferTransferer in project lobcder by skoulouzis.
the class VPDRI method doCopy.
private void doCopy(VFile file, Range range, OutputStream out, boolean decript) throws VlException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
long len = range.getFinish() - range.getStart() + 1;
InputStream in = null;
int buffSize;
Long start = range.getStart();
if (len <= Constants.BUF_SIZE) {
buffSize = (int) len;
} else {
buffSize = Constants.BUF_SIZE;
}
DesEncrypter en = null;
if (decript) {
en = new DesEncrypter(getKeyInt());
}
int read = 0;
try {
if (file instanceof VRandomReadable) {
VRandomReadable ra = (VRandomReadable) file;
byte[] buff = new byte[buffSize];
int totalBytesRead = 0;
while (totalBytesRead < len || read != -1) {
long startT = System.currentTimeMillis();
read = ra.readBytes(start, buff, 0, buff.length);
Logger.getLogger(VPDRI.class.getName()).log(Level.FINEST, "speed: {0} kb/s", (read / 1024.0) / ((System.currentTimeMillis() - startT) / 1000.0));
if (read == -1 || totalBytesRead == len) {
break;
}
totalBytesRead += read;
start += buff.length;
if (decript) {
byte[] tmp = en.decrypt(buff);
buff = tmp;
}
out.write(buff, 0, read);
}
} else {
if (start > 0) {
throw new IOException("Backend at " + vrl.getScheme() + "://" + vrl.getHostname() + "does not support random reads");
// long skiped = in.skip(start);
// if (skiped != start) {
// long n = start;
// int buflen = (int) Math.min(Constants.BUF_SIZE, n);
// byte data[] = new byte[buflen];
// while (n > 0) {
// int r = in.read(data, 0, (int) Math.min((long) buflen, n));
// if (r < 0) {
// break;
// }
// n -= r;
// }
// }
}
// int totalBytesRead = 0;
// byte[] buff = new byte[buffSize];
// while (totalBytesRead < len || read != -1) {
// if (read == -1 || totalBytesRead == len) {
// break;
// }
// read = in.read(buff, 0, buff.length);
// totalBytesRead += read;
// start += buff.length;
// out.write(buff, 0, read);
// }
in = getData();
if (decript) {
InputStream tmp = en.wrapInputStream(in);
in = tmp;
}
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer(buffSize, in, out);
cBuff.startTransfer(len);
}
} finally {
if (in != null) {
in.close();
}
}
}
use of nl.uva.vlet.io.CircularStreamBufferTransferer in project lobcder by skoulouzis.
the class WebDataFileResource method transfer.
private PDRI transfer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
InputStream in = null;
PDRI pdri = null;
double start = 0;
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start for {0}", getLogicalData().getName());
try {
PDRIDescr pdriDescr = selectBestPDRI(pdris);
pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "pdri: {0}", pdri.getURI());
if (pdri != null) {
start = System.currentTimeMillis();
in = pdri.getData();
if (!pdri.getEncrypted()) {
if (doCircularStreamBufferTransferer) {
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
cBuff.startTransfer((long) -1);
} else {
int read;
byte[] copyBuffer = new byte[Constants.BUF_SIZE];
while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
out.write(copyBuffer, 0, read);
// tos.write(copyBuffer, 0, read);
}
}
} else {
DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
encrypter.decrypt(in, out);
}
} else {
sleepTime = 5;
throw new NotFoundException("Physical resource not found");
}
} catch (Exception ex) {
if (ex instanceof NotFoundException) {
throw (NotFoundException) ex;
}
if (ex.getMessage().contains("Resource not found")) {
throw new NotFoundException(ex.getMessage());
}
if (pdri != null) {
Double speed = weightPDRIMap.get(pdri.getHost());
if (speed != null) {
speed = speed - 10;
} else {
speed = Double.valueOf(-10);
}
weightPDRIMap.put(pdri.getHost(), speed);
}
try {
sleepTime = sleepTime + 5;
Thread.sleep(sleepTime);
if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
transfer(pdris, out, tryCount, false);
} else if (++tryCount < Constants.RECONNECT_NTRY) {
transfer(pdris, out, tryCount, false);
} else {
transfer(pdris, out, 0, false);
}
} catch (InterruptedException ex1) {
sleepTime = 5;
throw new IOException(ex1);
}
} finally {
if (in != null) {
in.close();
}
}
sleepTime = 5;
double elapsed = System.currentTimeMillis() - start;
double speed = ((pdri.getLength() * 8.0) * 1000.0) / (elapsed * 1000.0);
try {
String msg = "File: " + pdri.getFileName() + " Destination: " + new URI(pdri.getURI()).getScheme() + "://" + pdri.getHost() + " Rx_Speed: " + speed + " Kbites/sec Rx_Size: " + (pdri.getLength()) + " bytes Elapsed_Time: " + elapsed + " ms";
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
} catch (URISyntaxException ex) {
}
return pdri;
}
use of nl.uva.vlet.io.CircularStreamBufferTransferer in project lobcder by skoulouzis.
the class GridHelper method copyVomsAndCerts.
private static void copyVomsAndCerts() throws FileNotFoundException, VlException, URISyntaxException {
File f = new File(System.getProperty("user.home") + "/.globus/vomsdir");
File vomsFile = new File(f.getAbsoluteFile() + "/voms.xml");
if (!vomsFile.exists()) {
f.mkdirs();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream in = classLoader.getResourceAsStream("/voms.xml");
FileOutputStream out = new FileOutputStream(vomsFile);
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
cBuff.startTransfer(new Long(-1));
}
f = new File(Constants.CERT_LOCATION);
if (!f.exists() || f.list().length == 0) {
f.mkdirs();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL res = classLoader.getResource("/certs/");
File sourceCerts = new File(res.toURI());
File[] certs = sourceCerts.listFiles();
for (File src : certs) {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(f.getAbsoluteFile() + "/" + src.getName());
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
cBuff.startTransfer(new Long(-1));
}
}
}
use of nl.uva.vlet.io.CircularStreamBufferTransferer in project lobcder by skoulouzis.
the class GridHelper method copyVomsAndCerts.
private static void copyVomsAndCerts() throws FileNotFoundException, VlException, URISyntaxException {
File f = new File(System.getProperty("user.home") + "/.globus/vomsdir");
File vomsFile = new File(f.getAbsoluteFile() + "/voms.xml");
if (!vomsFile.exists()) {
f.mkdirs();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream in = classLoader.getResourceAsStream("/voms.xml");
FileOutputStream out = new FileOutputStream(vomsFile);
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((BUF_SIZE), in, out);
cBuff.startTransfer(new Long(-1));
}
f = new File(Constants.CERT_LOCATION);
if (!f.exists() || f.list().length == 0) {
f.mkdirs();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL res = classLoader.getResource("/certs/");
File sourceCerts = new File(res.toURI());
File[] certs = sourceCerts.listFiles();
for (File src : certs) {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(f.getAbsoluteFile() + "/" + src.getName());
CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((BUF_SIZE), in, out);
cBuff.startTransfer(new Long(-1));
}
}
}
Aggregations