Package freenet.keys

Examples of freenet.keys.SSKBlock


      }
    }
   
    try {
      key.setPubKey(pubKey);
      block = new SSKBlock(data, headers, key, false);
    } catch (SSKVerifyException e1) {
      Logger.error(this, "Invalid SSK from "+source, e1);
      Message msg = DMT.createFNPDataInsertRejected(uid, DMT.DATA_INSERT_REJECTED_SSK_ERROR);
      try {
        source.sendSync(msg, this, realTimeFlag);
      } catch (NotConnectedException e) {
        // Ignore
      } catch (SyncSendWaitedTooLongException e) {
        // Ignore
      }
      return;
    }
   
    SSKBlock storedBlock = node.fetch(key, false, false, false, canWriteDatastore, false, null);
   
    if((storedBlock != null) && !storedBlock.equals(block)) {
      try {
        RequestHandler.sendSSK(storedBlock.getRawHeaders(), storedBlock.getRawData(), false, pubKey, source, uid, this, realTimeFlag);
      } catch (NotConnectedException e1) {
        if(logMINOR) Logger.minor(this, "Lost connection to source on "+uid);
        return;
      } catch (WaitedTooLongException e1) {
        Logger.error(this, "Took too long to send ssk datareply to "+uid+" (because of throttling)");
        return;
      } catch (PeerRestartedException e) {
        if(logMINOR) Logger.minor(this, "Source restarted on "+uid);
        return;
      } catch (SyncSendWaitedTooLongException e) {
        Logger.error(this, "Took too long to send ssk datareply to "+uid);
        return;
      }
      block = storedBlock;
    }
   
    if(logMINOR) Logger.minor(this, "Got block for "+key+" for "+uid);
   
        if(htl > 0)
            sender = node.makeInsertSender(block, htl, uid, tag, source, false, false, canWriteDatastore, forkOnCacheable, preferInsert, ignoreLowBackoff, realTimeFlag);
       
        boolean receivedRejectedOverload = false;
       
        while(true) {
            synchronized(sender) {
                try {
                  if(sender.getStatus() == SSKInsertSender.NOT_FINISHED)
                    sender.wait(5000);
                } catch (InterruptedException e) {
                  // Ignore
                }
            }

            if((!receivedRejectedOverload) && sender.receivedRejectedOverload()) {
              receivedRejectedOverload = true;
              // Forward it
              // Does not need to be sent synchronously since is non-terminal.
              Message m = DMT.createFNPRejectedOverload(uid, false, true, realTimeFlag);
              try {
          source.sendAsync(m, null, this);
        } catch (NotConnectedException e) {
          if(logMINOR) Logger.minor(this, "Lost connection to source");
          return;
        }
            }
           
            if(sender.hasRecentlyCollided()) {
              // Forward collision
              data = sender.getData();
              headers = sender.getHeaders();
              collided = true;
            try {
          block = new SSKBlock(data, headers, key, true);
        } catch (SSKVerifyException e1) {
          // Is verified elsewhere...
          throw new Error("Impossible: " + e1, e1);
        }
        try {
View Full Code Here


   * on a separate thread. However, blocking disk I/O *should happen on this thread*. We deliberately
   * serialise it, as high latencies can otherwise result.
   */
  protected void innerSendOfferedKey(Key key, final boolean isSSK, boolean needPubKey, final long uid, final PeerNode source, final OfferReplyTag tag, final boolean realTimeFlag) throws NotConnectedException {
    if(isSSK) {
      SSKBlock block = node.fetch((NodeSSK)key, false, false, false, false, true, null);
      if(block == null) {
        // Don't have the key
        source.sendAsync(DMT.createFNPGetOfferedKeyInvalid(uid, DMT.GET_OFFERED_KEY_REJECTED_NO_KEY), null, senderCounter);
        tag.unlockHandler();
        return;
      }
     
      final Message data = DMT.createFNPSSKDataFoundData(uid, block.getRawData(), realTimeFlag);
      Message headers = DMT.createFNPSSKDataFoundHeaders(uid, block.getRawHeaders(), realTimeFlag);
      final int dataLength = block.getRawData().length;
     
      source.sendAsync(headers, null, senderCounter);
     
      node.executor.execute(new PrioRunnable() {

        @Override
        public int getPriority() {
          return NativeThread.HIGH_PRIORITY;
        }

        @Override
        public void run() {
          try {
            source.sendSync(data, senderCounter, realTimeFlag);
            senderCounter.sentPayload(dataLength);
          } catch (NotConnectedException e) {
            // :(
          } catch (SyncSendWaitedTooLongException e) {
            // Impossible
          } finally {
            tag.unlockHandler();
          }
        }
       
      }, "Send offered SSK");
     
      if(needPubKey) {
        Message pk = DMT.createFNPSSKPubKey(uid, block.getPubKey(), realTimeFlag);
        source.sendAsync(pk, null, senderCounter);
      }
    } else {
      CHKBlock block = node.fetch((NodeCHK)key, false, false, false, false, true, null);
      if(block == null) {
        // Don't have the key
        source.sendAsync(DMT.createFNPGetOfferedKeyInvalid(uid, DMT.GET_OFFERED_KEY_REJECTED_NO_KEY), null, senderCounter);
        tag.unlockHandler();
        return;
      }
      Message df = DMT.createFNPCHKDataFound(uid, block.getRawHeaders());
      source.sendAsync(df, null, senderCounter);
          PartiallyReceivedBlock prb =
            new PartiallyReceivedBlock(Node.PACKETS_IN_BLOCK, Node.PACKET_SIZE, block.getRawData());
          final BlockTransmitter bt =
            new BlockTransmitter(node.usm, node.getTicker(), source, uid, prb, senderCounter, BlockTransmitter.NEVER_CASCADE,
                new BlockTransmitterCompletion() {

          @Override
View Full Code Here

    try {
      Object o = node.makeRequestSender(key.getNodeKey(true), node.maxHTL(), uid, tag, null, localOnly, ignoreStore, false, true, canWriteClientCache, realTimeFlag);
      if(o instanceof SSKBlock)
        try {
          tag.setServedFromDatastore();
          SSKBlock block = (SSKBlock) o;
          key.setPublicKey(block.getPubKey());
          return ClientSSKBlock.construct(block, key);
        } catch(SSKVerifyException e) {
          Logger.error(this, "Does not verify: " + e, e);
          throw new LowLevelGetException(LowLevelGetException.DECODE_FAILED);
        }
      if(o == null)
        throw new LowLevelGetException(LowLevelGetException.DATA_NOT_FOUND_IN_STORE);
      rs = (RequestSender) o;
      boolean rejectedOverload = false;
      short waitStatus = 0;
      while(true) {
        waitStatus = rs.waitUntilStatusChange(waitStatus);
        if((!rejectedOverload) && (waitStatus & RequestSender.WAIT_REJECTED_OVERLOAD) != 0) {
          requestStarters.rejectedOverload(true, false, realTimeFlag);
          rejectedOverload = true;
        }

        int status = rs.getStatus();

        if(status == RequestSender.NOT_FINISHED)
          continue;

        if(status != RequestSender.TIMED_OUT && status != RequestSender.GENERATED_REJECTED_OVERLOAD && status != RequestSender.INTERNAL_ERROR) {
          if(logMINOR)
            Logger.minor(this, "SSK fetch cost " + rs.getTotalSentBytes() + '/' + rs.getTotalReceivedBytes() + " bytes (" + status + ')');
          nodeStats.localSskFetchBytesSentAverage.report(rs.getTotalSentBytes());
          nodeStats.localSskFetchBytesReceivedAverage.report(rs.getTotalReceivedBytes());
          if(status == RequestSender.SUCCESS)
            // See comments above successfulSskFetchBytesSentAverage : we don't relay the data, so
            // reporting the sent bytes would be inaccurate.
            //nodeStats.successfulSskFetchBytesSentAverage.report(rs.getTotalSentBytes());
            nodeStats.successfulSskFetchBytesReceivedAverage.report(rs.getTotalReceivedBytes());
        }

        long rtt = System.currentTimeMillis() - startTime;
        if((status == RequestSender.TIMED_OUT) ||
          (status == RequestSender.GENERATED_REJECTED_OVERLOAD)) {
          if(!rejectedOverload) {
            requestStarters.rejectedOverload(true, false, realTimeFlag);
            rejectedOverload = true;
          }
          node.nodeStats.reportSSKOutcome(rtt, false, realTimeFlag);
        } else
          if(rs.hasForwarded() &&
            ((status == RequestSender.DATA_NOT_FOUND) ||
            (status == RequestSender.RECENTLY_FAILED) ||
            (status == RequestSender.SUCCESS) ||
            (status == RequestSender.ROUTE_NOT_FOUND) ||
            (status == RequestSender.VERIFY_FAILURE) ||
            (status == RequestSender.GET_OFFER_VERIFY_FAILURE))) {

            if(!rejectedOverload)
              requestStarters.requestCompleted(true, false, key.getNodeKey(true), realTimeFlag);
            // Count towards RTT even if got a RejectedOverload - but not if timed out.
            requestStarters.getThrottle(true, false, realTimeFlag).successfulCompletion(rtt);
            node.nodeStats.reportSSKOutcome(rtt, status == RequestSender.SUCCESS, realTimeFlag);
          }

        if(rs.getStatus() == RequestSender.SUCCESS)
          try {
            SSKBlock block = rs.getSSKBlock();
            key.setPublicKey(block.getPubKey());
            return ClientSSKBlock.construct(block, key);
          } catch(SSKVerifyException e) {
            Logger.error(this, "Does not verify: " + e, e);
            throw new LowLevelGetException(LowLevelGetException.DECODE_FAILED);
          }
View Full Code Here

    }
    tag.setAccepted();
    try {
      long startTime = System.currentTimeMillis();
      // Be consistent: use the client cache to check for collisions as this is a local insert.
      SSKBlock altBlock = node.fetch(block.getKey(), false, true, canWriteClientCache, false, false, null);
      if(altBlock != null && !altBlock.equals(block))
        throw new LowLevelPutException(altBlock);
      is = node.makeInsertSender(block,
        node.maxHTL(), uid, tag, null, false, canWriteClientCache, false, forkOnCacheable, preferInsert, ignoreLowBackoff, realTimeFlag);
      boolean hasReceivedRejectedOverload = false;
      // Wait for status
      while(true) {
        synchronized(is) {
          if(is.getStatus() == SSKInsertSender.NOT_FINISHED)
            try {
              is.wait(SECONDS.toMillis(5));
            } catch(InterruptedException e) {
              // Ignore
            }
          if(is.getStatus() != SSKInsertSender.NOT_FINISHED)
            break;
        }
        if((!hasReceivedRejectedOverload) && is.receivedRejectedOverload()) {
          hasReceivedRejectedOverload = true;
          requestStarters.rejectedOverload(true, true, realTimeFlag);
        }
      }

      // Wait for completion
      while(true) {
        synchronized(is) {
          if(is.getStatus() != SSKInsertSender.NOT_FINISHED)
            break;
          try {
            is.wait(SECONDS.toMillis(10));
          } catch(InterruptedException e) {
            // Go around again
          }
        }
      }

      if(logMINOR)
        Logger.minor(this, "Completed " + uid + " overload=" + hasReceivedRejectedOverload + ' ' + is.getStatusString());

      // Finished?
      if(!hasReceivedRejectedOverload)
        // Is it ours? Did we send a request?
        if(is.sentRequest() && (is.uid == uid) && ((is.getStatus() == SSKInsertSender.ROUTE_NOT_FOUND) || (is.getStatus() == SSKInsertSender.SUCCESS))) {
          // It worked!
          long endTime = System.currentTimeMillis();
          long rtt = endTime - startTime;
          requestStarters.requestCompleted(true, true, block.getKey(), realTimeFlag);
          requestStarters.getThrottle(true, true, realTimeFlag).successfulCompletion(rtt);
        }

      int status = is.getStatus();

      if(status != CHKInsertSender.TIMED_OUT && status != CHKInsertSender.GENERATED_REJECTED_OVERLOAD && status != CHKInsertSender.INTERNAL_ERROR && status != CHKInsertSender.ROUTE_REALLY_NOT_FOUND) {
        int sent = is.getTotalSentBytes();
        int received = is.getTotalReceivedBytes();
        if(logMINOR)
          Logger.minor(this, "Local SSK insert cost " + sent + '/' + received + " bytes (" + status + ')');
        nodeStats.localSskInsertBytesSentAverage.report(sent);
        nodeStats.localSskInsertBytesReceivedAverage.report(received);
        if(status == SSKInsertSender.SUCCESS)
          // Only report Sent bytes as we haven't received anything.
          nodeStats.successfulSskInsertBytesSentAverage.report(sent);
      }

      boolean deep = node.shouldStoreDeep(block.getKey(), null, is == null ? new PeerNode[0] : is.getRoutedTo());

      if(is.hasCollided()) {
        SSKBlock collided = is.getBlock();
        // Store it locally so it can be fetched immediately, and overwrites any locally inserted.
        try {
          // Has collided *on the network*, not locally.
          node.storeInsert(collided, deep, true, canWriteClientCache, false);
        } catch(KeyCollisionException e) {
View Full Code Here

  public SSKBlock fetch(NodeSSK key, boolean dontPromote, boolean canReadClientCache, boolean canWriteClientCache, boolean canWriteDatastore, boolean forULPR, BlockMetadata meta) {
    double loc=key.toNormalizedDouble();
    double dist=Location.distance(lm.getLocation(), loc);
    if(canReadClientCache) {
      try {
        SSKBlock block = sskClientcache.fetch(key, dontPromote || !canWriteClientCache, canReadClientCache, forULPR, false, meta);
        if(block != null) {
          nodeStats.avgClientCacheSSKSuccess.report(loc);
          if (dist > nodeStats.furthestClientCacheSSKSuccess)
          nodeStats.furthestClientCacheSSKSuccess=dist;
          if(logDEBUG) Logger.debug(this, "Found key "+key+" in client-cache");
          return block;
        }
      } catch (IOException e) {
        Logger.error(this, "Could not read from client cache: "+e, e);
      }
    }
    if(forULPR || useSlashdotCache || canReadClientCache) {
      try {
        SSKBlock block = sskSlashdotcache.fetch(key, dontPromote, canReadClientCache, forULPR, false, meta);
        if(block != null) {
          nodeStats.avgSlashdotCacheSSKSuccess.report(loc);
          if (dist > nodeStats.furthestSlashdotCacheSSKSuccess)
          nodeStats.furthestSlashdotCacheSSKSuccess=dist;
          if(logDEBUG) Logger.debug(this, "Found key "+key+" in slashdot-cache");
          return block;
        }
      } catch (IOException e) {
        Logger.error(this, "Could not read from slashdot/ULPR cache: "+e, e);
      }
    }
    boolean ignoreOldBlocks = !writeLocalToDatastore;
    if(canReadClientCache) ignoreOldBlocks = false;
    if(logMINOR) dumpStoreHits();
    try {

      nodeStats.avgRequestLocation.report(loc);
      SSKBlock block = sskDatastore.fetch(key, dontPromote || !canWriteDatastore, canReadClientCache, forULPR, ignoreOldBlocks, meta);
      if(block == null) {
        SSKStore store = oldSSK;
        if(store != null)
          block = store.fetch(key, dontPromote || !canWriteDatastore, canReadClientCache, forULPR, ignoreOldBlocks, meta);
      }
View Full Code Here

    if(key == null) {
      key = getPubKey.getKey(clientSSK.pubKeyHash, canReadClientCache, false, null);
    }
    if(key == null) return null;
    clientSSK.setPublicKey(key);
    SSKBlock block = fetch((NodeSSK)clientSSK.getNodeKey(true), false, canReadClientCache, canWriteClientCache, canWriteDatastore, false, null);
    if(block == null) {
      if(logMINOR)
        Logger.minor(this, "Could not find key for "+clientSSK);
      return null;
    }
View Full Code Here

      return DO.NEXT_PEER;
    }
    // collided, overwrite data with remote data
    try {
      data = ((ShortBuffer) dataMessage.getObject(DMT.DATA)).getData();
      block = new SSKBlock(data, headers, block.getKey(), false);
     
      synchronized(this) {
        hasRecentlyCollided = true;
        hasCollided = true;
        notifyAll();
View Full Code Here

     * @param next The node we received the data from.
   * @param wasFork
     */
  private void finishSSK(PeerNode next, boolean wasFork, byte[] headers, byte[] sskData) {
      try {
      block = new SSKBlock(sskData, headers, (NodeSSK)key, false);
      node.storeShallow(block, canWriteClientCache, canWriteDatastore, false);
      if(node.random.nextInt(RANDOM_REINSERT_INTERVAL) == 0)
        node.queueRandomReinsert(block);
      synchronized(this) {
        finalHeaders = headers;
View Full Code Here

     * @param next The node we received the data from.
     * @return True if the request has completed. False if we need to look elsewhere.
     */
  private boolean finishSSKFromGetOffer(PeerNode next, byte[] headers, byte[] sskData) {
      try {
      block = new SSKBlock(sskData, headers, (NodeSSK)key, false);
      synchronized(this) {
        finalHeaders = headers;
        finalSskData = sskData;
      }
      node.storeShallow(block, canWriteClientCache, canWriteDatastore, tryOffersOnly);
View Full Code Here

        synchronized(this) {
          finalHeaders = headers;
          finalSskData = data;
        }
        try {
          SSKBlock block = new SSKBlock(data, headers, (NodeSSK)key, false);
            if(logMINOR) Logger.minor(this, "Verified SSK");
        node.storeShallow(block, canWriteClientCache, canWriteDatastore, tryOffersOnly);
      } catch (KeyCollisionException e) {
        Logger.normal(this, "Collision on "+this);
      }
View Full Code Here

TOP

Related Classes of freenet.keys.SSKBlock

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.