Search in sources :

Example 1 with CircularStreamBufferTransferer

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;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) PDRI(nl.uva.cs.lobcder.resources.PDRI) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 2 with CircularStreamBufferTransferer

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();
        }
    }
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) VRandomReadable(nl.uva.vlet.vrs.io.VRandomReadable)

Example 3 with CircularStreamBufferTransferer

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;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PDRI(nl.uva.cs.lobcder.resources.PDRI) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 4 with CircularStreamBufferTransferer

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));
        }
    }
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Example 5 with CircularStreamBufferTransferer

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));
        }
    }
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

CircularStreamBufferTransferer (nl.uva.vlet.io.CircularStreamBufferTransferer)6 InputStream (java.io.InputStream)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 DesEncrypter (nl.uva.cs.lobcder.util.DesEncrypter)3 BadRequestException (io.milton.http.exceptions.BadRequestException)2 ConflictException (io.milton.http.exceptions.ConflictException)2 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)2 NotFoundException (io.milton.http.exceptions.NotFoundException)2 FileInputStream (java.io.FileInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 SQLException (java.sql.SQLException)2 PDRI (nl.uva.cs.lobcder.resources.PDRI)2 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)2 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1