Package freenet.io.comm

Examples of freenet.io.comm.Message


        }
   
        if(logMINOR) Logger.minor(this, "Routing request to "+next);
        nodesRoutedTo.add(next);
       
        Message req = createDataRequest();
       
        // Not possible to get an accurate time for sending, guaranteed to be not later than the time of receipt.
        // Why? Because by the time the sent() callback gets called, it may already have been acked, under heavy load.
        // So take it from when we first started to try to send the request.
        // See comments below when handling FNPRecentlyFailed for why we need this.
View Full Code Here


        }
       
        if(logMINOR) Logger.minor(this, "Routing request to "+next+" realtime="+realTimeFlag);
        nodesRoutedTo.add(next);
       
        Message req = createDataRequest();
       
        // Not possible to get an accurate time for sending, guaranteed to be not later than the time of receipt.
        // Why? Because by the time the sent() callback gets called, it may already have been acked, under heavy load.
        // So take it from when we first started to try to send the request.
        // See comments below when handling FNPRecentlyFailed for why we need this.
View Full Code Here

  /** Here FINISHED means accepted, WAIT means try again (soft reject). */
    private DO waitForAccepted(RequestLikelyAcceptedState expectedAcceptState, PeerNode next, UIDTag origTag) {
      while(true) {
       
        Message msg;
       
        MessageFilter mf = makeAcceptedRejectedFilter(next, getAcceptedTimeout(), origTag);
       
        try {
          msg = node.usm.waitFor(mf, this);
          if(logMINOR) Logger.minor(this, "first part got "+msg);
        } catch (DisconnectedException e) {
          Logger.normal(this, "Disconnected from "+next+" while waiting for Accepted on "+uid);
          next.noLongerRoutingTo(origTag, false);
          return DO.NEXT_PEER;
        }
       
        if(msg == null) {
          if(logMINOR) Logger.minor(this, "Timeout waiting for Accepted for "+this);
          // Timeout waiting for Accepted
          next.localRejectedOverload("AcceptedTimeout", realTimeFlag);
          forwardRejectedOverload();
          int t = timeSinceSent();
          node.failureTable.onFailed(key, next, htl, t, t);
          synchronized(this) {
            rejectedLoops++;
          }
          // Try next node
          handleAcceptedRejectedTimeout(next, origTag);
          return DO.NEXT_PEER;
        }
       
        if(msg.getSpec() == DMT.FNPRejectedLoop) {
          if(logMINOR) Logger.minor(this, "Rejected loop");
          next.successNotOverload(realTimeFlag);
          int t = timeSinceSent();
          node.failureTable.onFailed(key, next, htl, t, t);
          // Find another node to route to
          next.noLongerRoutingTo(origTag, false);
          return DO.NEXT_PEER;
        }
       
        if(msg.getSpec() == DMT.FNPRejectedOverload) {
          if(logMINOR) Logger.minor(this, "Rejected: overload");
          // Non-fatal - probably still have time left
          if (msg.getBoolean(DMT.IS_LOCAL)) {
           
            if(logMINOR) Logger.minor(this, "Is local");
 
          // FIXME soft rejects, only check then, but don't backoff if sane
          // FIXME recalculate with broader check, allow a few percent etc.
           
            if(msg.getSubMessage(DMT.FNPRejectIsSoft) != null && expectedAcceptState != null) {
              if(logMINOR) Logger.minor(this, "Soft rejection, waiting to resend");
              if(expectedAcceptState == RequestLikelyAcceptedState.GUARANTEED)
                // Need to recalculate to be sure this is an error.
                Logger.normal(this, "Rejected overload yet expected state was "+expectedAcceptState);
              nodesRoutedTo.remove(next);
View Full Code Here

        } catch (NotConnectedException e) {
          // Ignore, we have the data.
        }
        return true;
      }
      Message m;
      try {
        m = prb.usm.waitFor(mfSendKilled.or(mfPacket), ctr);
      } catch (DisconnectedException e) {
        prb.abort(RetrievalException.SENDER_DISCONNECTED, "Sender disconnected");
        return false;
      }
      if(peer.getBootID() != peerBootID) {
        prb.abort(RetrievalException.SENDER_DIED, "Sender restarted");
        return false;
      }
      if(m == null) {
        prb.abort(RetrievalException.TIMED_OUT, "Sender timeout");
        return false;
      }
      if(m.getSpec() == DMT.FNPBulkSendAborted) {
        prb.abort(RetrievalException.SENDER_DIED, "Sender cancelled send");
        return false;
      }
      if(m.getSpec() == DMT.FNPBulkPacketSend) {
        int packetNo = m.getInt(DMT.PACKET_NO);
        byte[] data = ((ShortBuffer) m.getObject(DMT.DATA)).getData();
        prb.received(packetNo, data, 0, data.length);
      }
    }
  }
View Full Code Here

    }

    /** @return True . */
    private boolean innerRun(int packetNo, BitArray copied) {
      try {
        Message msg = DMT.createPacketTransmit(_uid, packetNo, copied, _prb.getPacket(packetNo), realTime);
        MyAsyncMessageCallback cb = new MyAsyncMessageCallback();
        MessageItem item;
        // Everything is throttled.
        item = _destination.sendAsync(msg, cb, _ctr);
        synchronized(itemsPending) {
View Full Code Here

      }
     
      // Congestion control and bandwidth limiting
      try {
        if(logMINOR) Logger.minor(this, "Sending packet "+blockNo);
        Message msg = DMT.createFNPBulkPacketSend(uid, blockNo, buf, realTime);
        UnsentPacketTag tag = new UnsentPacketTag();
        peer.sendAsync(msg, tag, ctr);
        synchronized(this) {
          while(inFlightPackets >= max && !failedPacket)
            try {
View Full Code Here

  }
 
  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
View Full Code Here

        }
    }

    private void realRun() {
        // Send Accepted
        Message accepted = DMT.createFNPSSKAccepted(uid, pubKey == null);
       
        try {
      source.sendAsync(accepted, null, this);
    } catch (NotConnectedException e1) {
      if(logMINOR) Logger.minor(this, "Lost connection to source");
      return;
    }
   
    if(tag.shouldSlowDown()) {
      try {
        source.sendAsync(DMT.createFNPRejectedOverload(uid, false, false, realTimeFlag), null, this);
      } catch (NotConnectedException e) {
        // Ignore.
      }
    }
   
    while(headers == null || data == null || pubKey == null) {
      MessageFilter mfDataInsertRejected = MessageFilter.create().setType(DMT.FNPDataInsertRejected).setField(DMT.UID, uid).setSource(source).setTimeout(DATA_INSERT_TIMEOUT);
      MessageFilter mf = mfDataInsertRejected;
      if(headers == null) {
        MessageFilter m = MessageFilter.create().setType(DMT.FNPSSKInsertRequestHeaders).setField(DMT.UID, uid).setSource(source).setTimeout(DATA_INSERT_TIMEOUT);
        mf = m.or(mf);
      }
      if(data == null) {
        MessageFilter m = MessageFilter.create().setType(DMT.FNPSSKInsertRequestData).setField(DMT.UID, uid).setSource(source).setTimeout(DATA_INSERT_TIMEOUT);
        mf = m.or(mf);
      }
      if(pubKey == null) {
        MessageFilter m = MessageFilter.create().setType(DMT.FNPSSKPubKey).setField(DMT.UID, uid).setSource(source).setTimeout(DATA_INSERT_TIMEOUT);
        mf = m.or(mf);
      }
      Message msg;
      try {
        msg = node.usm.waitFor(mf, this);
      } catch (DisconnectedException e) {
        if(logMINOR) Logger.minor(this, "Lost connection to source on "+uid);
        return;
      }
      if(msg == null) {
        Logger.normal(this, "Failed to receive all parts (data="+(data==null?"null":"ok")+" headers="+(headers==null?"null":"ok")+" pk="+pubKey+") for "+uid);
        Message failed = DMT.createFNPDataInsertRejected(uid, DMT.DATA_INSERT_REJECTED_RECEIVE_FAILED);
        try {
          source.sendSync(failed, this, realTimeFlag);
        } catch (NotConnectedException e) {
          // Ignore
        } catch (SyncSendWaitedTooLongException e) {
          // Ignore
        }
        return;
      } else if(msg.getSpec() == DMT.FNPSSKInsertRequestHeaders) {
        headers = ((ShortBuffer)msg.getObject(DMT.BLOCK_HEADERS)).getData();
      } else if(msg.getSpec() == DMT.FNPSSKInsertRequestData) {
        data = ((ShortBuffer)msg.getObject(DMT.DATA)).getData();
      } else if(msg.getSpec() == DMT.FNPSSKPubKey) {
        byte[] pubkeyAsBytes = ((ShortBuffer)msg.getObject(DMT.PUBKEY_AS_BYTES)).getData();
        try {
          pubKey = DSAPublicKey.create(pubkeyAsBytes);
          if(logMINOR) Logger.minor(this, "Got pubkey on "+uid+" : "+pubKey);
          Message confirm = DMT.createFNPSSKPubKeyAccepted(uid);
          try {
            source.sendAsync(confirm, null, this);
          } catch (NotConnectedException e) {
            if(logMINOR) Logger.minor(this, "Lost connection to source on "+uid);
            return;
          }
        } catch (CryptFormatException e) {
          Logger.error(this, "Invalid pubkey from "+source+" on "+uid);
          msg = DMT.createFNPDataInsertRejected(uid, DMT.DATA_INSERT_REJECTED_SSK_ERROR);
          try {
            source.sendSync(msg, this, realTimeFlag);
          } catch (NotConnectedException ee) {
            // Ignore
          } catch (SyncSendWaitedTooLongException ee) {
            // Ignore
          }
          return;
        }
      } else if(msg.getSpec() == DMT.FNPDataInsertRejected) {
            try {
          source.sendAsync(DMT.createFNPDataInsertRejected(uid, msg.getShort(DMT.DATA_INSERT_REJECTED_REASON)), null, this);
        } catch (NotConnectedException e) {
          // Ignore.
        }
        return;
      } else {
        Logger.error(this, "Unexpected message? "+msg+" on "+this);
      }
    }
   
    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 {
          RequestHandler.sendSSK(headers, data, 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 bwlimiting");
          return;
        } catch (PeerRestartedException e) {
          Logger.error(this, "Peer restarted on "+uid);
          return;
        } catch (SyncSendWaitedTooLongException e) {
          Logger.error(this, "Took too long to send ssk datareply to "+uid);
          return;
        }
            }
           
            int status = sender.getStatus();
           
            if(status == SSKInsertSender.NOT_FINISHED) {
               continue;
            }
           
            // Local RejectedOverload's (fatal).
            // Internal error counts as overload. It'd only create a timeout otherwise, which is the same thing anyway.
            // We *really* need a good way to deal with nodes that constantly R_O!
            if((status == SSKInsertSender.TIMED_OUT) ||
                (status == SSKInsertSender.GENERATED_REJECTED_OVERLOAD) ||
                (status == SSKInsertSender.INTERNAL_ERROR)) {
              // Unlock early for originator, late for target; see UIDTag comments.
              tag.unlockHandler();
                Message msg = DMT.createFNPRejectedOverload(uid, true, true, realTimeFlag);
                try {
          source.sendSync(msg, this, realTimeFlag);
        } catch (NotConnectedException e) {
          if(logMINOR) Logger.minor(this, "Lost connection to source");
          return;
        } catch (SyncSendWaitedTooLongException e) {
          Logger.error(this, "Took too long to send "+msg+" to "+source);
          return;
        }
                // Might as well store it anyway.
                if((status == SSKInsertSender.TIMED_OUT) ||
                    (status == SSKInsertSender.GENERATED_REJECTED_OVERLOAD))
                  canCommit = true;
                finish(status);
                return;
            }
           
            if((status == SSKInsertSender.ROUTE_NOT_FOUND) || (status == SSKInsertSender.ROUTE_REALLY_NOT_FOUND)) {
              // Unlock early for originator, late for target; see UIDTag comments.
              tag.unlockHandler();
                Message msg = DMT.createFNPRouteNotFound(uid, sender.getHTL());
                try {
          source.sendSync(msg, this, realTimeFlag);
        } catch (NotConnectedException e) {
          if(logMINOR) Logger.minor(this, "Lost connection to source");
          return;
        } catch (SyncSendWaitedTooLongException e) {
          Logger.error(this, "Took too long to send "+msg+" to source");
        }
                canCommit = true;
                finish(status);
                return;
            }
           
            if(status == SSKInsertSender.SUCCESS) {
              // Unlock early for originator, late for target; see UIDTag comments.
              tag.unlockHandler();
              Message msg = DMT.createFNPInsertReply(uid);
              try {
          source.sendSync(msg, this, realTimeFlag);
        } catch (NotConnectedException e) {
          if(logMINOR) Logger.minor(this, "Lost connection to source");
          return;
        } catch (SyncSendWaitedTooLongException e) {
          Logger.error(this, "Took too long to send "+msg+" to "+source);
        }
                canCommit = true;
                finish(status);
                return;
            }
           
            // Otherwise...?
            Logger.error(this, "Unknown status code: "+sender.getStatusString());
          // Unlock early for originator, late for target; see UIDTag comments.
          tag.unlockHandler();
            Message msg = DMT.createFNPRejectedOverload(uid, true, true, realTimeFlag);
            try {
        source.sendSync(msg, this, realTimeFlag);
      } catch (NotConnectedException e) {
        // Ignore
      } catch (SyncSendWaitedTooLongException e) {
View Full Code Here

    {
        ArrayList<Message> lossyMessages = new ArrayList<Message>(l.size());
      for(byte[] buf : l) {
        // FIXME factor out parsing once we are sure these are not bogus.
        // For now we have to be careful.
        Message msg = Message.decodeMessageLax(buf, pn, 0);
        if(msg == null) {
          lossyMessages.clear();
          break;
        }
        if(!msg.getSpec().isLossyPacketMessage()) {
          lossyMessages.clear();
          break;
        }
        lossyMessages.add(msg);
      }
View Full Code Here

              item = messageQueue.grabQueuedMessageItem(i);
              if(item == null) {
                if(mustSendKeepalive && packet.noFragments()) {
                  // Create a ping for keepalive purposes.
                  // It will be acked, this ensures both sides don't timeout.
                  Message msg;
                  synchronized(this) {
                    msg = DMT.createFNPPing(pingCounter++);
                  }
                  item = new MessageItem(msg, null, null);
                  item.setDeadline(now + PacketSender.MAX_COALESCING_DELAY);
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.