Package net.tomp2p.peers

Examples of net.tomp2p.peers.PeerAddress


     * @param isUDP
     *            Set to true if UDP should be used, false for TCP.
     * @return The future response
     */
    private FuturePing ping(final InetSocketAddress address, final Number160 peerId, final boolean isUDP) {
        return ping(new PeerAddress(peerId, address), isUDP);
    }
View Full Code Here


        //
        if (broadcast) {
            return broadcast0();
        }
        if (peerAddress == null && inetAddress != null && bootstrapTo == null) {
            peerAddress = new PeerAddress(Number160.ZERO, inetAddress, portTCP, portUDP);
            return bootstrapPing(peerAddress);

        } else if (peerAddress != null && bootstrapTo == null) {
            bootstrapTo = new ArrayList<PeerAddress>(1);
            bootstrapTo.add(peerAddress);
View Full Code Here

      decodeSignature(buf, readerBefore, donePayload);
     
      if(donePayload) {
        boolean isRelay = message.sender().isRelayed();
        if(isRelay && !message.peerSocketAddresses().isEmpty()) {
          PeerAddress tmpSender = message.sender().changePeerSocketAddresses(message.peerSocketAddresses());
          message.sender(tmpSender);
            }
      }
     
      // see https://github.com/netty/netty/issues/1976
View Full Code Here

          int header = buf.getUnsignedShort(buf.readerIndex());
          size = PeerAddress.size(header);
          if (buf.readableBytes() < size) {
            return false;
          }
          PeerAddress pa = new PeerAddress(buf);
          neighborSet.add(pa);
        }
        message.neighborsSet(neighborSet);
        lastContent = contentTypes.poll();
        neighborSize = -1;
        neighborSet = null;
        break;
      case SET_PEER_SOCKET:
        if (peerSocketAddressSize == -1 && buf.readableBytes() < Utils.BYTE_BYTE_SIZE) {
          return false;
        }
        if (peerSocketAddressSize == -1) {
          peerSocketAddressSize = buf.readUnsignedByte();
        }
        if (peerSocketAddresses == null) {
          peerSocketAddresses = new ArrayList<PeerSocketAddress>(peerSocketAddressSize);
        }
        for (int i = peerSocketAddresses.size(); i < peerSocketAddressSize; i++) {
          if (buf.readableBytes() < Utils.BYTE_BYTE_SIZE) {
            return false;
          }
          int header = buf.getUnsignedByte(buf.readerIndex());
          boolean isIPv4 = header == 0;
          size = PeerSocketAddress.size(isIPv4);
          if (buf.readableBytes() < size + Utils.BYTE_BYTE_SIZE) {
            return false;
          }
          //skip the ipv4/ipv6 header
          buf.skipBytes(1);
          peerSocketAddresses.add(PeerSocketAddress.create(buf, isIPv4));
        }
        message.peerSocketAddresses(peerSocketAddresses);
        lastContent = contentTypes.poll();
        peerSocketAddressSize = -1;
        peerSocketAddresses = null;
        break;
      case SET_KEY640:
        if (keyCollectionSize == -1 && buf.readableBytes() < Utils.INTEGER_BYTE_SIZE) {
          return false;
        }
        if (keyCollectionSize == -1) {
          keyCollectionSize = buf.readInt();
        }
        if (keyCollection == null) {
          keyCollection = new KeyCollection(new ArrayList<Number640>(keyCollectionSize));
        }
        for (int i = keyCollection.size(); i < keyCollectionSize; i++) {
          if (buf.readableBytes() < Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE
              + Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE) {
            return false;
          }
          byte[] me2 = new byte[Number160.BYTE_ARRAY_SIZE];
          buf.readBytes(me2);
          Number160 locationKey = new Number160(me2);
          buf.readBytes(me2);
          Number160 domainKey = new Number160(me2);
          buf.readBytes(me2);
          Number160 contentKey = new Number160(me2);
          buf.readBytes(me2);
          Number160 versionKey = new Number160(me2);
          keyCollection.add(new Number640(locationKey, domainKey, contentKey, versionKey));
        }
        message.keyCollection(keyCollection);
        lastContent = contentTypes.poll();
        keyCollectionSize = -1;
        keyCollection = null;
        break;
      case MAP_KEY640_DATA:
        if (mapSize == -1 && buf.readableBytes() < Utils.INTEGER_BYTE_SIZE) {
          return false;
        }
        if (mapSize == -1) {
          mapSize = buf.readInt();
        }
        if (dataMap == null) {
          dataMap = new DataMap(new HashMap<Number640, Data>(2 * mapSize));
        }
        if (data != null) {
          if (!data.decodeBuffer(buf)) {
            return false;
          }
          if (!data.decodeDone(buf, message.publicKey(0), signatureFactory)) {
            return false;
          }
          data = null;
          key = null;
        }
        for (int i = dataMap.size(); i < mapSize; i++) {
          if (key == null) {
            if (buf.readableBytes() < Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE
                + Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE) {
              return false;
            }
            byte[] me3 = new byte[Number160.BYTE_ARRAY_SIZE];
            buf.readBytes(me3);
            Number160 locationKey = new Number160(me3);
            buf.readBytes(me3);
            Number160 domainKey = new Number160(me3);
            buf.readBytes(me3);
            Number160 contentKey = new Number160(me3);
            buf.readBytes(me3);
            Number160 versionKey = new Number160(me3);
            key = new Number640(locationKey, domainKey, contentKey, versionKey);
          }
          data = Data.decodeHeader(buf, signatureFactory);
          if (data == null) {
            return false;
          }
          dataMap.dataMap().put(key, data);

          if (!data.decodeBuffer(buf)) {
            return false;
          }
          if (!data.decodeDone(buf, message.publicKey(0), signatureFactory)) {
            return false;
          }
          // if we have signed the message, set the public key anyway, but only if we indicated so
          if (message.isSign() && message.publicKey(0) != null && data.hasPublicKey()
              && (data.publicKey() == null || data.publicKey() == PeerBuilder.EMPTY_PUBLIC_KEY)) {
            data.publicKey(message.publicKey(0));
          }
          data = null;
          key = null;
        }

        message.setDataMap(dataMap);
        lastContent = contentTypes.poll();
        mapSize = -1;
        dataMap = null;
        break;
      case MAP_KEY640_KEYS:
        if (keyMap640KeysSize == -1 && buf.readableBytes() < Utils.INTEGER_BYTE_SIZE) {
          return false;
        }
        if (keyMap640KeysSize == -1) {
          keyMap640KeysSize = buf.readInt();
        }
        if (keyMap640Keys == null) {
          keyMap640Keys = new KeyMap640Keys(new TreeMap<Number640, Collection<Number160>>());
        }

        final int meta = Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE
            + Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE;

        for (int i = keyMap640Keys.size(); i < keyMap640KeysSize; i++) {
          if (buf.readableBytes() < meta + Utils.BYTE_BYTE_SIZE) {
            return false;
          }
          size = buf.getUnsignedByte(buf.readerIndex() + meta);
         
          if (buf.readableBytes() < meta + Utils.BYTE_BYTE_SIZE + (size * Number160.BYTE_ARRAY_SIZE )) {
            return false;
          }
          byte[] me3 = new byte[Number160.BYTE_ARRAY_SIZE];
          buf.readBytes(me3);
          Number160 locationKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 domainKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 contentKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 versionKey = new Number160(me3);

          int numBasedOn = buf.readByte();
          Set<Number160> value = new HashSet<Number160>(numBasedOn);
          for (int j = 0; j < numBasedOn; j++) {
            buf.readBytes(me3);
            Number160 basedOnKey = new Number160(me3);
            value.add(basedOnKey);
          }

          keyMap640Keys.put(new Number640(locationKey, domainKey, contentKey, versionKey), value);
        }

        message.keyMap640Keys(keyMap640Keys);
        lastContent = contentTypes.poll();
        keyMap640KeysSize = -1;
        keyMap640Keys = null;
        break;
      case MAP_KEY640_BYTE:
        if (keyMapByteSize == -1 && buf.readableBytes() < Utils.INTEGER_BYTE_SIZE) {
          return false;
        }
        if (keyMapByteSize == -1) {
          keyMapByteSize = buf.readInt();
        }
        if (keyMapByte == null) {
          keyMapByte = new KeyMapByte(new HashMap<Number640, Byte>(2 * keyMapByteSize));
        }

        for (int i = keyMapByte.size(); i < keyMapByteSize; i++) {
          if (buf.readableBytes() < Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE
              + Number160.BYTE_ARRAY_SIZE + Number160.BYTE_ARRAY_SIZE + 1) {
            return false;
          }
          byte[] me3 = new byte[Number160.BYTE_ARRAY_SIZE];
          buf.readBytes(me3);
          Number160 locationKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 domainKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 contentKey = new Number160(me3);
          buf.readBytes(me3);
          Number160 versionKey = new Number160(me3);
          byte value = buf.readByte();
          keyMapByte.put(new Number640(locationKey, domainKey, contentKey, versionKey), value);
        }

        message.keyMapByte(keyMapByte);
        lastContent = contentTypes.poll();
        keyMapByteSize = -1;
        keyMapByte = null;
        break;
      case BYTE_BUFFER:
        if (bufferSize == -1 && buf.readableBytes() < Utils.INTEGER_BYTE_SIZE) {
          return false;
        }
        if (bufferSize == -1) {
          bufferSize = buf.readInt();
        }
        if (buffer == null) {
          buffer = new DataBuffer();
        }
       
        final int already = buffer.alreadyTransferred();
        final int remaining = bufferSize - already;
        // already finished
        if (remaining != 0) {
          int read = buffer.transferFrom(buf, remaining);
          if(read != remaining) {
            LOG.debug("Still looking for data. Indicating that its not finished yet. Read = {}, Size = {}.", buffer.alreadyTransferred(), bufferSize);
            return false;
          }
        }
       
        ByteBuf buf2 = AlternativeCompositeByteBuf.compBuffer(buffer.toByteBufs());
        message.buffer(new Buffer(buf2, bufferSize));
        lastContent = contentTypes.poll();
        bufferSize = -1;
        buffer = null;
        break;
      case SET_TRACKER_DATA:
        if (trackerDataSize == -1 && buf.readableBytes() < Utils.BYTE_BYTE_SIZE) {
          return false;
        }
        if (trackerDataSize == -1) {
          trackerDataSize = buf.readUnsignedByte();
        }
        if (trackerData == null) {
          trackerData = new TrackerData(new HashMap<PeerStatistic, Data>(2 * trackerDataSize));
        }
        if (currentTrackerData != null) {
          if (!currentTrackerData.decodeBuffer(buf)) {
            return false;
          }
          if (!currentTrackerData.decodeDone(buf, message.publicKey(0), signatureFactory)) {
            return false;
          }
          currentTrackerData = null;
        }
        for (int i = trackerData.size(); i < trackerDataSize; i++) {

          if (buf.readableBytes() < Utils.SHORT_BYTE_SIZE) {
            return false;
          }

          int header = buf.getUnsignedShort(buf.readerIndex());

          size = PeerAddress.size(header);

          if (buf.readableBytes() < size) {
            return false;
          }
          PeerAddress pa = new PeerAddress(buf);
          PeerStatistic ps = new PeerStatistic(pa);

          currentTrackerData = Data.decodeHeader(buf, signatureFactory);
          if (currentTrackerData == null) {
            return false;
View Full Code Here

        // remove neighbors that are over the limit
        int serializedSize = 1;
        // no need to cut if we don't provide a limit
        if (neighborLimit >= 0) {
      for (Iterator<PeerAddress> iterator = neighbors.iterator(); iterator.hasNext();) {
        PeerAddress neighbor = iterator.next();
        serializedSize += neighbor.size();
        if (serializedSize > neighborLimit) {
          iterator.remove();
        }
      }
        }
View Full Code Here

    if (peer.isShutdown()) {
      return FUTURE_REQUEST_SHUTDOWN;
    }

    final boolean keepAlive;
    final PeerAddress remotePeer;
    if (recipientAddress != null && recipientConnection == null) {
      keepAlive = false;
      remotePeer = recipientAddress;
    } else if (recipientAddress == null && recipientConnection != null) {
      keepAlive = true;
View Full Code Here

        if (future.isFailed() && future.isNat() && !isManualPorts()) {
          Ports externalPorts = setupPortforwarding(future.internalAddress().getHostAddress(), peer
              .connectionBean().channelServer().channelServerConfiguration().portsForwarding());
          if (externalPorts != null) {
            final PeerAddress serverAddressOrig = peer.peerBean().serverPeerAddress();
            final PeerAddress serverAddress = serverAddressOrig
                .changePorts(externalPorts.tcpPort(), externalPorts.udpPort())
                .changeAddress(future.externalAddress());
           
            //set the new address regardless wheter it will succeed or not.
            // The discover will eventually check wheter the announced ip matches the one that it sees.
            peer.peerBean().serverPeerAddress(serverAddress);
           
            // test with discover again
            DiscoverBuilder builder = new DiscoverBuilder(peer).peerAddress(futureNAT.reporter());
            builder.start().addListener(new BaseFutureAdapter<FutureDiscover>() {
              @Override
              public void operationComplete(FutureDiscover future) throws Exception {
                if (future.isSuccess()) {
                  //UPNP or NAT-PMP was successful, set flag
                  peer.peerBean().serverPeerAddress(serverAddress.changePortForwarding(true));
                  peer.peerBean().serverPeerAddress().internalPeerSocketAddress(serverAddressOrig.peerSocketAddress());
                  futureNAT.done(future.peerAddress(), future.reporter());
                } else {
                  // indicate relay
                  PeerAddress pa = peer.peerBean().serverPeerAddress().changeFirewalledTCP(true)
                      .changeFirewalledUDP(true);
                  peer.peerBean().serverPeerAddress(pa);
                  futureNAT.failed(future);
                }
              }
            });
          } else {
            // indicate relay
            PeerAddress pa = peer.peerBean().serverPeerAddress().changeFirewalledTCP(true)
                .changeFirewalledUDP(true);
            peer.peerBean().serverPeerAddress(pa);
            futureNAT.failed("could not setup NAT");
          }
        } else if (future.isSuccess()) {
View Full Code Here

      }
    });
  }

  private FutureRelayNAT startRelay(final FutureRelayNAT futureBootstrapNAT, final BootstrapBuilder bootstrapBuilder) {
    PeerAddress upa = peer.peerBean().serverPeerAddress();
    upa = upa.changeFirewalledTCP(true).changeFirewalledUDP(true).changeSlow(relayType.isSlow());
    peer.peerBean().serverPeerAddress(upa);
    // find neighbors

    FutureBootstrap futureBootstrap = bootstrapBuilder.start();
    futureBootstrapNAT.futureBootstrap0(futureBootstrap);
View Full Code Here

    }
    Collection<PeerAddress> all = new ArrayList<PeerAddress>();
    all.addAll(alreadyAsked);
    all.addAll(queueToAsk);
    for (Iterator<PeerAddress> iterator = newNeighbors.iterator(); iterator.hasNext();) {
      PeerAddress newNeighbor = iterator.next();
      for (PeerFilter peerFilter : peerFilters) {
        if (peerFilter.reject(newNeighbor, all, locationkey)) {
          iterator.remove();
        }
      }
View Full Code Here

        final SortedSet<PeerAddress> result = new TreeSet<PeerAddress>(queueToAsk.comparator());
        Utils.difference(newPeers, result, alreadyAsked);
        if (result.size() == 0) {
            return false;
        }
        PeerAddress first = result.first();
        boolean newInfo = isNew(queueToAsk, first);
        queueToAsk.addAll(result);
        return newInfo;
    }
View Full Code Here

TOP

Related Classes of net.tomp2p.peers.PeerAddress

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.