Package freenet.support.io

Examples of freenet.support.io.FileRandomAccessBuffer


  public void handleRequestJar(Message m, final PeerNode source, final boolean isExt) {
    final String name = isExt ? "ext" : "main";
   
    Message msg;
    final BulkTransmitter bt;
    final FileRandomAccessBuffer raf;

    if (source.isOpennet() && updateManager.dontAllowUOM()) {
      Logger.normal(this, "Peer " + source
          + " asked us for the blob file for " + name
          + "; We are a seenode, so we ignore it!");
      return;
    }
    // Do we have the data?

    int version = isExt ? NodeUpdateManager.TRANSITION_VERSION_EXT : NodeUpdateManager.TRANSITION_VERSION;
    File data = isExt ? updateManager.getTransitionExtBlob() : updateManager.getTransitionMainBlob();

    if(data == null) {
      Logger.normal(this, "Peer " + source + " asked us for the blob file for the "+name+" jar but we don't have it!");
      // Probably a race condition on reconnect, hopefully we'll be asked again
      return;
    }

    final long uid = m.getLong(DMT.UID);

    if(!source.sendingUOMJar(isExt)) {
      Logger.error(this, "Peer "+source+" asked for UOM "+(isExt?"ext":"main")+" jar twice");
      return;
    }
   
    try {
     
      try {
        raf = new FileRandomAccessBuffer(data, true);
      } catch(FileNotFoundException e) {
        Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, we have downloaded it but don't have the file even though we did have it when we checked!: " + e, e);
        return;
      } catch(IOException e) {
          Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, we have downloaded it but can't read the file on disk!: " + e, e);
          return;
      }
     
      final PartiallyReceivedBulk prb;
      long length;
      length = raf.size();
      prb = new PartiallyReceivedBulk(updateManager.node.getUSM(), length,
              Node.PACKET_SIZE, raf, true);
     
      try {
        bt = new BulkTransmitter(prb, source, uid, false, updateManager.ctr, true);
      } catch(DisconnectedException e) {
        Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, then disconnected: " + e, e);
        raf.close();
        return;
      }
     
      msg =
        isExt ? DMT.createUOMSendingExtra(uid, length, NodeUpdateManager.transitionExtJarURIAsUSK.toString(), version) :
          DMT.createUOMSendingMain(uid, length, NodeUpdateManager.transitionMainJarURIAsUSK.toString(), version);
     
    } catch (RuntimeException e) {
      source.finishedSendingUOMJar(isExt);
      throw e;
    } catch (Error e) {
      source.finishedSendingUOMJar(isExt);
      throw e;
    }
   
    final Runnable r = new Runnable() {

      @Override
      public void run() {
        try {
          if(!bt.send())
            Logger.error(this, "Failed to send "+name+" jar blob to " + source.userToString() + " : " + bt.getCancelReason());
          else
            Logger.normal(this, "Sent "+name+" jar blob to " + source.userToString());
          raf.close();
        } catch (DisconnectedException e) {
          // Not much we can do.
        } finally {
          source.finishedSendingUOMJar(isExt);
          raf.close();
        }
      }
    };

    try {
View Full Code Here


      }
    }
    File f = getBlobFile();
    if(f == null) return null;
    try {
      return new FileRandomAccessBuffer(f, true);
    } catch(FileNotFoundException e) {
      Logger.error(this, "We do not have the blob file for the revocation even though we have successfully downloaded it!", e);
      return null;
    } catch (IOException e) {
            Logger.error(this, "Error reading downloaded revocation blob file: "+e, e);
View Full Code Here

    public void testStoreTo() throws IOException, StorageFormatException, ResumeFailedException, GeneralSecurityException {
        File tempFile = File.createTempFile("test-storeto", ".tmp", base);
        byte[] buf = new byte[4096];
        Random r = new Random(1267612);
        r.nextBytes(buf);
        FileRandomAccessBuffer rafw = new FileRandomAccessBuffer(tempFile, buf.length+types[0].headerLen, false);
        EncryptedRandomAccessBuffer eraf = new EncryptedRandomAccessBuffer(types[0], rafw, secret, true);
        eraf.pwrite(0, buf, 0, buf.length);
        byte[] tmp = new byte[buf.length];
        eraf.pread(0, tmp, 0, buf.length);
        assertArrayEquals(buf, tmp);
View Full Code Here

    public void testSerialize() throws IOException, StorageFormatException, ResumeFailedException, GeneralSecurityException, ClassNotFoundException {
        File tempFile = File.createTempFile("test-storeto", ".tmp", base);
        byte[] buf = new byte[4096];
        Random r = new Random(1267612);
        r.nextBytes(buf);
        FileRandomAccessBuffer rafw = new FileRandomAccessBuffer(tempFile, buf.length+types[0].headerLen, false);
        EncryptedRandomAccessBuffer eraf = new EncryptedRandomAccessBuffer(types[0], rafw, secret, true);
        eraf.pwrite(0, buf, 0, buf.length);
        byte[] tmp = new byte[buf.length];
        eraf.pread(0, tmp, 0, buf.length);
        assertArrayEquals(buf, tmp);
View Full Code Here

  }

  public int sendFileOffer(File file, String message) throws IOException {
    String fnam = file.getName();
    String mime = DefaultMIMETypes.guessMIMEType(fnam, false);
    RandomAccessBuffer data = new FileRandomAccessBuffer(file, true);
    return sendFileOffer(fnam, mime, message, data);
  }
View Full Code Here

      acceptedOrRejected = true;
      final String baseFilename = "direct-"+FileUtil.sanitize(getName())+"-"+filename;
      final File dest = node.clientCore.downloadsDir().file(baseFilename+".part");
      destination = node.clientCore.downloadsDir().file(baseFilename);
      try {
        data = new FileRandomAccessBuffer(dest, size, false);
      } catch (IOException e) {
        // Impossible
        throw new Error("Impossible: FileNotFoundException opening with RAF with rw! "+e, e);
      }
      prb = new PartiallyReceivedBulk(node.usm, size, Node.PACKET_SIZE, data, false);
View Full Code Here

      updateManager.blow("Cannot fetch the revocation certificate from our peer because we cannot write it to disk: " + e, true);
      cancelSend(source, uid);
      return true;
    }

    FileRandomAccessBuffer raf;
    try {
      raf = new FileRandomAccessBuffer(temp, length, false);
    } catch(FileNotFoundException e) {
      Logger.error(this, "Peer " + source + " asked us for the blob file for the revocation key, we have downloaded it but don't have the file even though we did have it when we checked!: " + e, e);
      updateManager.blow("Internal error after fetching the revocation certificate from our peer, maybe out of disk space, file disappeared "+temp+" : " + e, true);
      return true;
        } catch(IOException e) {
View Full Code Here

  public void handleRequestJar(Message m, final PeerNode source) {
    final String name = "main";
   
    Message msg;
    final BulkTransmitter bt;
    final FileRandomAccessBuffer raf;

    if (source.isOpennet() && updateManager.dontAllowUOM()) {
      Logger.normal(this, "Peer " + source
          + " asked us for the blob file for " + name
          + "; We are a seenode, so we ignore it!");
      return;
    }
    // Do we have the data?

    File data = updateManager.getCurrentVersionBlobFile();
    int version = Version.buildNumber();
   
    if(version != Version.buildNumber()) {
      Logger.normal(this, "Peer " + source + " asked us for the blob file for the main jar but we are about to update...");
      // Race condition?
      return;
     
    }

    if(data == null) {
      Logger.normal(this, "Peer " + source + " asked us for the blob file for the "+name+" jar but we don't have it!");
      // Probably a race condition on reconnect, hopefully we'll be asked again
      return;
    }

    final long uid = m.getLong(DMT.UID);

    if(!source.sendingUOMJar(false)) {
      Logger.error(this, "Peer "+source+" asked for UOM main jar twice");
      return;
    }
   
    try {
     
      try {
        raf = new FileRandomAccessBuffer(data, true);
      } catch(FileNotFoundException e) {
        Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, we have downloaded it but don't have the file even though we did have it when we checked!: " + e, e);
        return;
            } catch(IOException e) {
                Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, we have downloaded it but can't read the file due to a disk I/O error: " + e, e);
                return;
      }
     
      final PartiallyReceivedBulk prb;
      long length;
      length = raf.size();
      prb = new PartiallyReceivedBulk(updateManager.node.getUSM(), length,
              Node.PACKET_SIZE, raf, true);
     
      try {
        bt = new BulkTransmitter(prb, source, uid, false, updateManager.ctr, true);
      } catch(DisconnectedException e) {
        Logger.error(this, "Peer " + source + " asked us for the blob file for the "+name+" jar, then disconnected: " + e, e);
        raf.close();
        return;
      }
     
      msg =
        DMT.createUOMSendingMainJar(uid, length, updateManager.getURI().toString(), version);
     
    } catch (RuntimeException e) {
      source.finishedSendingUOMJar(false);
      throw e;
    } catch (Error e) {
      source.finishedSendingUOMJar(false);
      throw e;
    }
   
    final Runnable r = new Runnable() {

      @Override
      public void run() {
        try {
          if(!bt.send())
            Logger.error(this, "Failed to send "+name+" jar blob to " + source.userToString() + " : " + bt.getCancelReason());
          else
            Logger.normal(this, "Sent "+name+" jar blob to " + source.userToString());
          raf.close();
        } catch (DisconnectedException e) {
          // Not much we can do.
        } finally {
          source.finishedSendingUOMJar(false);
          raf.close();
        }
      }
    };

    try {
View Full Code Here

        this.nodesAskedSendMainJar.remove(source);
      }
      return true;
    }

    FileRandomAccessBuffer raf;
    try {
      raf = new FileRandomAccessBuffer(temp, length, false);
    } catch(IOException e) {
      Logger.error(this, "Peer " + source + " sending us a main jar binary blob, but we " +
          ((e instanceof FileNotFoundException) ? "lost the temp file " : "cannot read the temp file ") + temp + " : " + e, e);
      synchronized(this) {
        this.nodesAskedSendMainJar.remove(source);
View Full Code Here

    long uid = m.getLong(DMT.UID);
    synchronized(dependencies) {
      data = dependencies.get(buf);
    }
    boolean fail = !incrementDependencies(source);
    FileRandomAccessBuffer raf;
    final BulkTransmitter bt;
   
    try {
      if(data != null)
        raf = new FileRandomAccessBuffer(data, true);
      else {
        Logger.error(this, "Dependency with hash "+HexUtil.bytesToHex(buf.getData())+" not found!");
        fail = true;
        raf = null;
      }
    } catch(IOException e) {
      Logger.error(this, "Peer " + source + " asked us for the dependency with hash "+HexUtil.bytesToHex(buf.getData())+" jar, we have downloaded it but " +
              (e instanceof FileNotFoundException ? "don't have the file" : "can't read the file")+
              " even though we did have it when we checked!: " + e, e);
      raf = null;
      fail = true;
    }
   
    PartiallyReceivedBulk prb;
    if(raf != null) {
        long thisLength = raf.size();
        prb = new PartiallyReceivedBulk(updateManager.node.getUSM(), thisLength,
                Node.PACKET_SIZE, raf, true);
        if(length != thisLength) {
            fail = true;
        }
    } else {
        prb = new PartiallyReceivedBulk(updateManager.node.getUSM(), 0,
                Node.PACKET_SIZE, new ByteArrayRandomAccessBuffer(new byte[0]), true);
        fail = true;
    }
   
    try {
      bt = new BulkTransmitter(prb, source, uid, false, updateManager.ctr, true);
    } catch(DisconnectedException e) {
      Logger.error(this, "Peer " + source + " asked us for the dependency with hash "+HexUtil.bytesToHex(buf.getData())+" jar then disconnected", e);
      raf.close();
      decrementDependencies(source);
      return;
    }
   
    if(fail) {
      cancelSend(source, uid);
      decrementDependencies(source);
    } else {
      final FileRandomAccessBuffer r = raf;
      updateManager.node.executor.execute(new Runnable() {
       
        @Override
        public void run() {
          source.incrementUOMSends();
          try {
            bt.send();
          } catch (DisconnectedException e) {
            Logger.normal(this, "Disconnected while sending dependency with hash "+HexUtil.bytesToHex(buf.getData())+" to "+source);
          } finally {
            source.decrementUOMSends();
            decrementDependencies(source);
            r.close();
          }
        }
       
      });
    }
View Full Code Here

TOP

Related Classes of freenet.support.io.FileRandomAccessBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.