Package freenet.io.comm

Examples of freenet.io.comm.Message


      return OFFER_STATUS.TRY_ANOTHER;
    } else if(reply.getSpec() == DMT.FNPSSKDataFoundHeaders) {
      byte[] headers = ((ShortBuffer) reply.getObject(DMT.BLOCK_HEADERS)).getData();
      // Wait for the data
      MessageFilter mfData = MessageFilter.create().setSource(pn).setField(DMT.UID, uid).setTimeout(getOfferedTimeout).setType(DMT.FNPSSKDataFoundData);
      Message dataMessage;
      try {
        dataMessage = node.usm.waitFor(mfData, this);
      } catch (DisconnectedException e) {
        if(logMINOR)
          Logger.minor(this, "Disconnected: "+pn+" getting data for offer for "+key);
        return OFFER_STATUS.TRY_ANOTHER;
      }
      if(dataMessage == null) {
        Logger.error(this, "Got headers but not data from "+pn+" for offer for "+key+" on "+this);
        return OFFER_STATUS.TRY_ANOTHER;
      }
      byte[] sskData = ((ShortBuffer) dataMessage.getObject(DMT.DATA)).getData();
      if(pubKey == null) {
        MessageFilter mfPK = MessageFilter.create().setSource(pn).setField(DMT.UID, uid).setTimeout(getOfferedTimeout).setType(DMT.FNPSSKPubKey);
        Message pk;
        try {
          pk = node.usm.waitFor(mfPK, this);
        } catch (DisconnectedException e) {
          if(logMINOR)
            Logger.minor(this, "Disconnected: "+pn+" getting pubkey for offer for "+key);
          return OFFER_STATUS.TRY_ANOTHER;
        }
        if(pk == null) {
          Logger.error(this, "Got data but not pubkey from "+pn+" for offer for "+key+" on "+this);
          return OFFER_STATUS.TRY_ANOTHER;
        }
        try {
          pubKey = DSAPublicKey.create(((ShortBuffer)pk.getObject(DMT.PUBKEY_AS_BYTES)).getData());
        } catch (CryptFormatException e) {
          Logger.error(this, "Bogus pubkey from "+pn+" for offer for "+key+" : "+e, e);
          return OFFER_STATUS.TRY_ANOTHER;
        }
       
View Full Code Here


      return false;
    }
  }

  protected Message createDataRequest() {
    Message req;
      if(!isSSK)
        req = DMT.createFNPCHKDataRequest(uid, htl, (NodeCHK)key);
      else// if(key instanceof NodeSSK)
        req = DMT.createFNPSSKDataRequest(uid, htl, (NodeSSK)key, pubKey == null);
      req.addSubMessage(DMT.createFNPRealTimeFlag(realTimeFlag));
      return req;
  }
View Full Code Here

    }

  /** Acknowledge the opennet path folding attempt without sending a reference. Once
   * the send completes (asynchronously), unlock everything. */
  void ackOpennet(final PeerNode next) {
    Message msg = DMT.createFNPOpennetCompletedAck(uid);
    // We probably should set opennetFinished after the send completes.
    try {
      next.sendAsync(msg, finishOpennetOnAck(next), this);
    } catch (NotConnectedException e) {
      // Ignore.
View Full Code Here

      // Wait for second timeout.
        // FIXME make this async.
        long deadline = System.currentTimeMillis() + searchTimeout;
      while(true) {
       
        Message msg;
        try {
              int timeout = (int)(Math.min(Integer.MAX_VALUE, deadline - System.currentTimeMillis()));
          msg = node.usm.waitFor(createMessageFilter(timeout, waitingFor), RequestSender.this);
        } catch (DisconnectedException e) {
          Logger.normal(this, "Disconnected from " + waitingFor
View Full Code Here

    return this.nodesRoutedTo.toArray(new PeerNode[nodesRoutedTo.size()]);
  }

  @Override
  protected Message createDataRequest() {
        Message req;
       
        req = DMT.createFNPInsertRequest(uid, htl, key);
        if(forkOnCacheable != Node.FORK_ON_CACHEABLE_DEFAULT) {
          req.addSubMessage(DMT.createFNPSubInsertForkControl(forkOnCacheable));
        }
        if(ignoreLowBackoff != Node.IGNORE_LOW_BACKOFF_DEFAULT) {
          req.addSubMessage(DMT.createFNPSubInsertIgnoreLowBackoff(ignoreLowBackoff));
        }
        if(preferInsert != Node.PREFER_INSERT_DEFAULT) {
          req.addSubMessage(DMT.createFNPSubInsertPreferInsert(preferInsert));
        }
      req.addSubMessage(DMT.createFNPRealTimeFlag(realTimeFlag));
       
      return req;
  }
View Full Code Here

  @Override
  protected void onAccepted(PeerNode next) {
        // Send them the data.
        // Which might be the new data resulting from a collision...

        Message dataInsert;
        dataInsert = DMT.createFNPDataInsert(uid, headers);
        /** What are we waiting for now??:
         * - FNPRouteNotFound - couldn't exhaust HTL, but send us the
         *   data anyway please
         * - FNPInsertReply - used up all HTL, yay
         * - FNPRejectOverload - propagating an overload error :(
         * - FNPRejectTimeout - we took too long to send the DataInsert
         * - FNPDataInsertRejected - the insert was invalid
         */
       
        int searchTimeout = calculateTimeout(htl);
        MessageFilter mfInsertReply = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPInsertReply);
        MessageFilter mfRejectedOverload = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRejectedOverload);
        MessageFilter mfRouteNotFound = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRouteNotFound);
        MessageFilter mfDataInsertRejected = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPDataInsertRejected);
        MessageFilter mfTimeout = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRejectedTimeout);
       
        MessageFilter mf = mfInsertReply.or(mfRouteNotFound.or(mfDataInsertRejected.or(mfTimeout.or(mfRejectedOverload))));

        InsertTag thisTag = forkedRequestTag;
        if(forkedRequestTag == null) thisTag = origTag;
       
        if(logMINOR) Logger.minor(this, "Sending DataInsert");
        try {
      next.sendSync(dataInsert, this, realTimeFlag);
    } catch (NotConnectedException e1) {
      if(logMINOR) Logger.minor(this, "Not connected sending DataInsert: "+next+" for "+uid);
      next.noLongerRoutingTo(thisTag, false);
      routeRequests();
      return;
    } catch (SyncSendWaitedTooLongException e) {
      Logger.error(this, "Unable to send "+dataInsert+" to "+next+" in a reasonable time");
      // Other side will fail. No need to do anything.
      next.noLongerRoutingTo(thisTag, false);
      routeRequests();
      return;
    }

    if(logMINOR) Logger.minor(this, "Sending data");
    final BackgroundTransfer transfer =
      startBackgroundTransfer(next, prb, thisTag);
   
    // Once the transfer has started, we only unlock the tag after the transfer completes (successfully or not).
   
        while (true) {

          Message msg;
         
      if(failIfReceiveFailed(thisTag, next)) {
        // The transfer has started, it will be cancelled.
        transfer.onCompleted();
        return;
      }
     
      try {
        msg = node.usm.waitFor(mf, this);
      } catch (DisconnectedException e) {
        Logger.normal(this, "Disconnected from " + next
            + " while waiting for InsertReply on " + this);
        transfer.onDisconnect(next);
        break;
      }
      if(failIfReceiveFailed(thisTag, next)) {
        // The transfer has started, it will be cancelled.
        transfer.onCompleted();
        return;
      }
     
      if (msg == null) {
       
        Logger.warning(this, "Timeout on insert "+this+" to "+next);
       
        // First timeout.
        // Could be caused by the next node, or could be caused downstream.
        next.localRejectedOverload("AfterInsertAcceptedTimeout2", realTimeFlag);
        forwardRejectedOverload();

        synchronized(this) {
          status = TIMED_OUT;
          notifyAll();
        }
       
        // Wait for the second timeout off-thread.
        // FIXME wait asynchronously.
       
        final InsertTag tag = thisTag;
        final PeerNode waitingFor = next;
        final short htl = this.htl;
       
        Runnable r = new Runnable() {

          @Override
          public void run() {
            // We do not need to unlock the tag here.
            // That will happen in the BackgroundTransfer, which has already started.
           
            // FIXME factor out
                int searchTimeout = calculateTimeout(htl);
                    MessageFilter mfInsertReply = MessageFilter.create().setSource(waitingFor).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPInsertReply);
                    MessageFilter mfRejectedOverload = MessageFilter.create().setSource(waitingFor).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRejectedOverload);
                    MessageFilter mfRouteNotFound = MessageFilter.create().setSource(waitingFor).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRouteNotFound);
                    MessageFilter mfDataInsertRejected = MessageFilter.create().setSource(waitingFor).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPDataInsertRejected);
                    MessageFilter mfTimeout = MessageFilter.create().setSource(waitingFor).setField(DMT.UID, uid).setTimeout(searchTimeout).setType(DMT.FNPRejectedTimeout);
                   
                    MessageFilter mf = mfInsertReply.or(mfRouteNotFound.or(mfDataInsertRejected.or(mfTimeout.or(mfRejectedOverload))));

                  while (true) {
                   
                    Message msg;

                if(failIfReceiveFailed(tag, waitingFor)) {
                  transfer.onCompleted();
                  return;
                }
             
              try {
                msg = node.usm.waitFor(mf, CHKInsertSender.this);
              } catch (DisconnectedException e) {
                Logger.normal(this, "Disconnected from " + waitingFor
                    + " while waiting for InsertReply on " + CHKInsertSender.this);
                transfer.onDisconnect(waitingFor);
                return;
              }
             
                if(failIfReceiveFailed(tag, waitingFor)) {
                  transfer.onCompleted();
                  return;
                }
             
              if(msg == null) {
                // Second timeout.
                // Definitely caused by the next node, fatal.
                Logger.error(this, "Got second (local) timeout on "+CHKInsertSender.this+" from "+waitingFor);
                  transfer.onCompleted();
                waitingFor.fatalTimeout();
                return;
              }
             
              if (msg.getSpec() == DMT.FNPRejectedTimeout) {
                // Next node timed out awaiting our DataInsert.
                // But we already sent it, so something is wrong. :(
                handleRejectedTimeout(msg, waitingFor);
                transfer.kill();
                return;
              }

              if (msg.getSpec() == DMT.FNPRejectedOverload) {
                if(handleRejectedOverload(msg, waitingFor, tag)) {
                  // Already set the status, and handle... will have unlocked the next node, so no need to call finished().
                  transfer.onCompleted();
                  return; // Don't try another node.
                }
                else continue;
              }

              if (msg.getSpec() == DMT.FNPRouteNotFound) {
                transfer.onCompleted();
                return; // Don't try another node.
              }
             
              if (msg.getSpec() == DMT.FNPDataInsertRejected) {
                handleDataInsertRejected(msg, waitingFor, tag);
                transfer.kill();
                return; // Don't try another node.
              }
             
              if (msg.getSpec() != DMT.FNPInsertReply) {
                Logger.error(this, "Unknown reply: " + msg);
                transfer.onCompleted();
                return;
              } else {
                // Our task is complete, one node (quite deep), has accepted the insert.
                // The request will not be routed to any other nodes, this is where the data *should* be.
                // We will removeRoutingTo() after the node has sent the transfer completion notice, which never happens before the InsertReply.
                transfer.onCompleted();
                return;
              }
                  }
          }
         
        };
       
        // Wait for the timeout off-thread.
        node.executor.execute(r);
        // Meanwhile, finish() to update allTransfersCompleted and hence allow the CHKInsertHandler to send the message downstream.
        // We have already set the status code, this is necessary in order to avoid race conditions.
        // However since it is set to TIMED_OUT, we are allowed to set it again.
        finish(TIMED_OUT, next);
        return;
      }

      if (msg.getSpec() == DMT.FNPRejectedTimeout) {
        // Next node timed out awaiting our DataInsert.
        // But we already sent it, so something is wrong. :(
        transfer.kill();
        handleRejectedTimeout(msg, next);
        return;
      }

      if (msg.getSpec() == DMT.FNPRejectedOverload) {
        if(handleRejectedOverload(msg, next, thisTag)) {
          // We have had an Accepted. This happens on a timeout downstream.
          // They will complete it (finish()), so we need to wait for a transfer completion.
          // FIXME it might be less confusing and therefore less likely to cause problems
          // if we had a different message sent post-accept???
          transfer.onCompleted();
          break;
        }
        else continue;
      }

      if (msg.getSpec() == DMT.FNPRouteNotFound) {
        //RNF means that the HTL was not exhausted, but that the data will still be stored.
        handleRNF(msg, next, thisTag);
        transfer.onCompleted();
        break;
      }

      //Can occur after reception of the entire chk block
      if (msg.getSpec() == DMT.FNPDataInsertRejected) {
        handleDataInsertRejected(msg, next, thisTag);
        transfer.kill();
        break;
      }
     
      if (msg.getSpec() != DMT.FNPInsertReply) {
        Logger.error(this, "Unknown reply: " + msg);
        transfer.onCompleted();
        finish(INTERNAL_ERROR, next);
        return;
      } else {
View Full Code Here

      }
    }
    MessageType spec = m.getSpec();
    if(spec == DMT.FNPPing) {
      // Send an FNPPong
      Message reply = DMT.createFNPPong(m.getInt(DMT.PING_SEQNO));
      try {
        source.sendAsync(reply, null, pingCounter); // nothing we can do if can't contact source
      } catch (NotConnectedException e) {
        if(logMINOR) Logger.minor(this, "Lost connection replying to "+m);
      }
View Full Code Here

    return false;
  }

  private void rejectRequest(Message m, ByteCounter ctr) {
    long uid = m.getLong(DMT.UID);
    Message msg = DMT.createFNPRejectedOverload(uid, true, false, false);
    // Send the load status anyway, hopefully this is a temporary problem.
    msg.setNeedsLoadBulk();
    msg.setNeedsLoadRT();
    try {
      m.getSource().sendAsync(msg, null, ctr);
    } catch (NotConnectedException e) {
      // Ignore
    }
View Full Code Here

        boolean realTimeFlag = DMT.getRealTimeFlag(m);
    OfferReplyTag tag = new OfferReplyTag(isSSK, source, realTimeFlag, uid, node);
   
    if(!tracker.lockUID(uid, isSSK, false, true, false, realTimeFlag, tag)) {
      if(logMINOR) Logger.minor(this, "Could not lock ID "+uid+" -> rejecting (already running)");
      Message rejected = DMT.createFNPRejectedLoop(uid);
      try {
        source.sendAsync(rejected, null, node.failureTable.senderCounter);
      } catch (NotConnectedException e) {
        Logger.normal(this, "Rejecting request from "+source.getPeer()+": "+e);
      }
      return true;
    } else {
      if(logMINOR) Logger.minor(this, "Locked "+uid);
    }
    boolean needPubKey;
    try {
    needPubKey = m.getBoolean(DMT.NEED_PUB_KEY);
    RejectReason reject =
      nodeStats.shouldRejectRequest(true, false, isSSK, false, true, source, false, false, realTimeFlag, tag);
    if(reject != null) {
      Logger.normal(this, "Rejecting FNPGetOfferedKey from "+source+" for "+key+" : "+reject);
      Message rejected = DMT.createFNPRejectedOverload(uid, true, true, realTimeFlag);
      if(reject.soft)
        rejected.addSubMessage(DMT.createFNPRejectIsSoft());
      try {
        source.sendAsync(rejected, null, node.failureTable.senderCounter);
      } catch (NotConnectedException e) {
        Logger.normal(this, "Rejecting (overload) data request from "+source.getPeer()+": "+e);
      }
View Full Code Here

        Key key = (Key) m.getObject(DMT.FREENET_ROUTING_KEY);
        boolean realTimeFlag = DMT.getRealTimeFlag(m);
        final RequestTag tag = new RequestTag(isSSK, RequestTag.START.REMOTE, source, realTimeFlag, id, node);
    if(!tracker.lockUID(id, isSSK, false, false, false, realTimeFlag, tag)) {
      if(logMINOR) Logger.minor(this, "Could not lock ID "+id+" -> rejecting (already running)");
      Message rejected = DMT.createFNPRejectedLoop(id);
      try {
        source.sendAsync(rejected, null, ctr);
      } catch (NotConnectedException e) {
        Logger.normal(this, "Rejecting request from "+source.getPeer()+": "+e);
      }
      node.failureTable.onFinalFailure(key, null, htl, htl, -1, -1, source);
      return;
    } else {
      if(logMINOR) Logger.minor(this, "Locked "+id);
    }
   
    // There are at least 2 threads that call this function.
    // DO NOT reuse the meta object, unless on a per-thread basis.
    // Object allocation is pretty cheap in modern Java anyway...
    // If we do reuse it, call reset().
    BlockMetadata meta = new BlockMetadata();
    KeyBlock block = node.fetch(key, false, false, false, false, meta);
    if(block != null)
      tag.setNotRoutedOnwards();
   
    RejectReason rejectReason = nodeStats.shouldRejectRequest(!isSSK, false, isSSK, false, false, source, block != null, false, realTimeFlag, tag);
    if(rejectReason != null) {
      // can accept 1 CHK request every so often, but not with SSKs because they aren't throttled so won't sort out bwlimitDelayTime, which was the whole reason for accepting them when overloaded...
      Logger.normal(this, "Rejecting "+(isSSK ? "SSK" : "CHK")+" request from "+source.getPeer()+" preemptively because "+rejectReason);
      Message rejected = DMT.createFNPRejectedOverload(id, true, true, realTimeFlag);
      if(rejectReason.soft)
        rejected.addSubMessage(DMT.createFNPRejectIsSoft());
      try {
        source.sendAsync(rejected, null, ctr);
      } catch (NotConnectedException e) {
        Logger.normal(this, "Rejecting (overload) data request from "+source.getPeer()+": "+e);
      }
View Full Code Here

TOP

Related Classes of freenet.io.comm.Message

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.