Package freenet.io.comm

Examples of freenet.io.comm.Message


    int dumped=v.size();
    if (dumped!=0 && logMINOR)
      Logger.minor(this, "lostOrRestartedNode dumping "+dumped+" swap requests for "+pn.getPeer());
        for(RecentlyForwardedItem item : v) {
            // Just reject it to avoid locking problems etc
            Message msg = DMT.createFNPSwapRejected(item.incomingID);
            if(logMINOR) Logger.minor(this, "Rejecting in lostOrRestartedNode: "+item.incomingID+ " from "+item.requestSender);
            try {
                item.requestSender.sendAsync(msg, null, this);
            } catch (NotConnectedException e1) {
                Logger.normal(this, "Both sender and receiver disconnected for "+item);
View Full Code Here


                myValueLong[i+2] = Double.doubleToLongBits(friendLocs[i]);
            byte[] myValue = Fields.longsToBytes(myValueLong);

            byte[] myHash = md.digest(myValue);

            Message m = DMT.createFNPSwapReply(uid, myHash);

            MessageFilter filter =
                MessageFilter.create().setType(DMT.FNPSwapCommit).setField(DMT.UID, uid).setTimeout(TIMEOUT).setSource(pn);

            node.usm.send(pn, m, LocationManager.this);

            Message commit;
            try {
                commit = node.usm.waitFor(filter, LocationManager.this);
            } catch (DisconnectedException e) {
              if(logMINOR) Logger.minor(this, "Disconnected from "+pn+" while waiting for SwapCommit");
                return;
            }

            if(commit == null) {
                // Timed out. Abort
                Logger.error(this, "Timed out waiting for SwapCommit on "+uid+" - this can happen occasionally due to connection closes, if it happens often, there may be a serious problem");
                return;
            }

            // We have a SwapCommit

            byte[] hisBuf = ((ShortBuffer)commit.getObject(DMT.DATA)).getData();

            if((hisBuf.length % 8 != 0) || (hisBuf.length < 16)) {
                Logger.error(this, "Bad content length in SwapComplete - malicious node? on "+uid);
                return;
            }

            // First does it verify?

            byte[] rehash = md.digest(hisBuf);

            if(!java.util.Arrays.equals(rehash, hisHash)) {
                Logger.error(this, "Bad hash in SwapCommit - malicious node? on "+uid);
                return;
            }

            // Now decode it

            long[] hisBufLong = Fields.bytesToLongs(hisBuf);
      if(hisBufLong.length < 2) {
        Logger.error(this, "Bad buffer length (no random, no location)- malicious node? on "+uid);
        return;
      }

            long hisRandom = hisBufLong[0];

            double hisLoc = Double.longBitsToDouble(hisBufLong[1]);
            if(!Location.isValid(hisLoc)) {
                Logger.error(this, "Bad loc: "+hisLoc+" on "+uid);
                return;
            }
            registerKnownLocation(hisLoc);

            double[] hisFriendLocs = new double[hisBufLong.length-2];
            for(int i=0;i<hisFriendLocs.length;i++) {
                hisFriendLocs[i] = Double.longBitsToDouble(hisBufLong[i+2]);
                if(!Location.isValid(hisFriendLocs[i])) {
                    Logger.error(this, "Bad friend loc: "+hisFriendLocs[i]+" on "+uid);
                    return;
                }
                registerLocationLink(hisLoc, hisFriendLocs[i]);
                registerKnownLocation(hisFriendLocs[i]);
            }

            numberOfRemotePeerLocationsSeenInSwaps += hisFriendLocs.length;

            // Send our SwapComplete

            Message confirm = DMT.createFNPSwapComplete(uid, myValue);
            //confirm.addSubMessage(DMT.createFNPSwapLocations(extractUIDs(friendLocsAndUIDs)));

            node.usm.send(pn, confirm, LocationManager.this);

            boolean shouldSwap = shouldSwap(myLoc, friendLocs, hisLoc, hisFriendLocs, random ^ hisRandom);
View Full Code Here

                    myValueLong[i+2] = Double.doubleToLongBits(friendLocs[i]);
                byte[] myValue = Fields.longsToBytes(myValueLong);

                byte[] myHash = SHA256.digest(myValue);

                Message m = DMT.createFNPSwapRequest(uid, myHash, SWAP_MAX_HTL);

                PeerNode pn = node.peers.getRandomPeer();
                if(pn == null) {
                    // Nowhere to send
                    return;
                }
                // Only 1 ID because we are sending; we won't receive
                item = addForwardedItem(uid, uid, null, pn);

                if(logMINOR) Logger.minor(this, "Sending SwapRequest "+uid+" to "+pn);

                MessageFilter filter1 =
                    MessageFilter.create().setType(DMT.FNPSwapRejected).setField(DMT.UID, uid).setSource(pn).setTimeout(TIMEOUT);
                MessageFilter filter2 =
                    MessageFilter.create().setType(DMT.FNPSwapReply).setField(DMT.UID, uid).setSource(pn).setTimeout(TIMEOUT);
                MessageFilter filter = filter1.or(filter2);

                node.usm.send(pn, m, LocationManager.this);

                if(logMINOR) Logger.minor(this, "Waiting for SwapReply/SwapRejected on "+uid);
                Message reply;
                try {
                    reply = node.usm.waitFor(filter, LocationManager.this);
                } catch (DisconnectedException e) {
                  if(logMINOR) Logger.minor(this, "Disconnected while waiting for SwapReply/SwapRejected for "+uid);
                    return;
                }

                if(reply == null) {
                    if(pn.isRoutable() && (System.currentTimeMillis() - pn.timeLastConnectionCompleted() > TIMEOUT*2)) {
                        // Timed out! Abort...
                        Logger.error(this, "Timed out waiting for SwapRejected/SwapReply on "+uid);
                    }
                    return;
                }

                if(reply.getSpec() == DMT.FNPSwapRejected) {
                    // Failed. Abort.
                  if(logMINOR) Logger.minor(this, "Swap rejected on "+uid);
                    return;
                }

                // We have an FNPSwapReply, yay
                // FNPSwapReply is exactly the same format as FNPSwapRequest
                byte[] hisHash = ((ShortBuffer)reply.getObject(DMT.HASH)).getData();

                Message confirm = DMT.createFNPSwapCommit(uid, myValue);
                //confirm.addSubMessage(DMT.createFNPSwapLocations(extractUIDs(friendLocsAndUIDs)));

                filter1.clearOr();
                MessageFilter filter3 = MessageFilter.create().setField(DMT.UID, uid).setType(DMT.FNPSwapComplete).setTimeout(TIMEOUT).setSource(pn);
                filter = filter1.or(filter3);
View Full Code Here

      if(logMINOR)
        Logger.minor(this, "Already disconnecting "+pn.shortToString());
      return;
    }
    if(sendDisconnectMessage) {
      Message msg = DMT.createFNPDisconnect(remove, purge, -1, new ShortBuffer(new byte[0]));
      try {
        pn.sendAsync(msg, new AsyncMessageCallback() {

          boolean done = false;
View Full Code Here

   * Otherwise -1.
   */
  public int routedPing(double loc2, byte[] pubKeyHash) {
    long uid = random.nextLong();
    int initialX = random.nextInt();
    Message m = DMT.createFNPRoutedPing(uid, loc2, maxHTL, initialX, pubKeyHash);
    Logger.normal(this, "Message: "+m);

    dispatcher.handleRouted(m, null);
    // FIXME: might be rejected
    MessageFilter mf1 = MessageFilter.create().setField(DMT.UID, uid).setType(DMT.FNPRoutedPong).setTimeout(5000);
    try {
      //MessageFilter mf2 = MessageFilter.create().setField(DMT.UID, uid).setType(DMT.FNPRoutedRejected).setTimeout(5000);
      // Ignore Rejected - let it be retried on other peers
      m = usm.waitFor(mf1/*.or(mf2)*/, null);
    } catch (DisconnectedException e) {
      Logger.normal(this, "Disconnected in waiting for pong");
      return -1;
    }
    if(m == null) return -1;
    if(m.getSpec() == DMT.FNPRoutedRejected) return -1;
    return m.getInt(DMT.COUNTER) - initialX;
  }
View Full Code Here

      if(isStopping) return;
      isStopping = true;
    }

    try {
      Message msg = DMT.createFNPDisconnect(false, false, -1, new ShortBuffer(new byte[0]));
      peers.localBroadcast(msg, true, false, peers.ctrDisconn);
    } catch (Throwable t) {
      try {
        // E.g. if we haven't finished startup
        Logger.error(this, "Failed to tell peers we are going down: "+t, t);
View Full Code Here

    Logger.normal(this, "Got collision on "+myKey+" ("+uid+") sending to "+next.getPeer());
   
    headers = ((ShortBuffer) msg.getObject(DMT.BLOCK_HEADERS)).getData();
    // Wait for the data
    MessageFilter mfData = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(SSKInsertHandler.DATA_INSERT_TIMEOUT).setType(DMT.FNPSSKDataFoundData);
    Message dataMessage;
    try {
      dataMessage = node.usm.waitFor(mfData, this);
    } catch (DisconnectedException e) {
      if(logMINOR)
        Logger.minor(this, "Disconnected: "+next+" getting datareply for "+this);
      next.noLongerRoutingTo(thisTag, false);
      return DO.NEXT_PEER;
    }
    if(dataMessage == null) {
      Logger.error(this, "Got headers but not data for datareply for insert from "+this);
      next.noLongerRoutingTo(thisTag, false);
      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;
View Full Code Here

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

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

        InsertTag thisTag = forkedRequestTag;
        if(forkedRequestTag == null) thisTag = origTag;
       
        // Send the headers and data
       
        Message headersMsg = DMT.createFNPSSKInsertRequestHeaders(uid, headers, realTimeFlag);
        Message dataMsg = DMT.createFNPSSKInsertRequestData(uid, data, realTimeFlag);
       
        try {
      next.sendAsync(headersMsg, null, this);
      next.sendSync(dataMsg, this, realTimeFlag);
      sentPayload(data.length);
    } catch (NotConnectedException e1) {
      if(logMINOR) Logger.minor(this, "Not connected to "+next);
      next.noLongerRoutingTo(thisTag, false);
      routeRequests();
      return;
    } catch (SyncSendWaitedTooLongException e) {
      Logger.error(this, "Waited too long to send "+dataMsg+" to "+next+" on "+this);
      next.noLongerRoutingTo(thisTag, false);
      routeRequests();
      return;
    }
       
        // Do we need to send them the pubkey?
       
    if(needPubKey) {
          Message pkMsg = DMT.createFNPSSKPubKey(uid, pubKey, realTimeFlag);
          try {
            next.sendSync(pkMsg, this, realTimeFlag);
          } catch (NotConnectedException e) {
            if(logMINOR) Logger.minor(this, "Node disconnected while sending pubkey: "+next);
        next.noLongerRoutingTo(thisTag, false);
        routeRequests();
        return;
          } catch (SyncSendWaitedTooLongException e) {
            Logger.warning(this, "Took too long to send pubkey to "+next+" on "+this);
        next.noLongerRoutingTo(thisTag, false);
        routeRequests();
        return;
      }
         
          // Wait for the SSKPubKeyAccepted
         
          // FIXME doubled the timeout because handling it properly would involve forking.
          MessageFilter mf1 = MessageFilter.create().setSource(next).setField(DMT.UID, uid).setTimeout(ACCEPTED_TIMEOUT*2).setType(DMT.FNPSSKPubKeyAccepted);
         
          Message newAck;
      try {
        newAck = node.usm.waitFor(mf1, this);
      } catch (DisconnectedException e) {
        if(logMINOR) Logger.minor(this, "Disconnected from "+next);
        next.noLongerRoutingTo(thisTag, false);
        routeRequests();
        return;
      }
         
          if(newAck == null) {
            handleNoPubkeyAccepted(next, thisTag);
        // Try another peer
          routeRequests();
          return;
          }
        }
       
        // We have sent them the pubkey, and the data.
        // Wait for the response.
       
    MessageFilter mf = makeSearchFilter(next, calculateTimeout(htl));
       
        while (true) {
          Message msg;
      try {
        msg = node.usm.waitFor(mf, this);
      } catch (DisconnectedException e) {
        Logger.normal(this, "Disconnected from " + next
            + " while waiting for InsertReply on " + this);
View Full Code Here

    private OFFER_STATUS tryOffer(final BlockOffer offer, final PeerNode pn, final OfferList offers) {
      if(pn == null) return OFFER_STATUS.TRY_ANOTHER;
      if(pn.getBootID() != offer.bootID) return OFFER_STATUS.TRY_ANOTHER;
      origTag.addRoutedTo(pn, true);
      Message msg = DMT.createFNPGetOfferedKey(key, offer.authenticator, pubKey == null, uid);
      msg.addSubMessage(DMT.createFNPRealTimeFlag(realTimeFlag));
      try {
        pn.sendSync(msg, this, realTimeFlag);
    } catch (NotConnectedException e2) {
      if(logMINOR)
        Logger.minor(this, "Disconnected: "+pn+" getting offer for "+key);
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.