Package net.tomp2p.message

Examples of net.tomp2p.message.Message


     *            The channel creator where we create a UPD channel
     * @return The future that will be triggered when we receive an answer or something fails.
     */
    public FutureResponse pingTCPProbe(final PeerAddress remotePeer, final ChannelCreator channelCreator,
            final ConnectionConfiguration configuration) {
        final Message message = createMessage(remotePeer, RPC.Commands.PING.getNr(), Type.REQUEST_3);
        FutureResponse futureResponse = new FutureResponse(message);
        return new RequestHandler<FutureResponse>(futureResponse, peerBean(), connectionBean(), configuration)
                .sendTCP(channelCreator);
    }
View Full Code Here


     *            The type of the request
     * @return The request handler
     */
    private RequestHandler<FutureResponse> createHandler(final PeerAddress remotePeer, final Type type,
            final ConnectionConfiguration configuration) {
        final Message message = createMessage(remotePeer, RPC.Commands.PING.getNr(), type);
        final FutureResponse futureResponse = new FutureResponse(message);
        return new RequestHandler<FutureResponse>(futureResponse, peerBean(), connectionBean(), configuration);
    }
View Full Code Here

     * @param remotePeer
     *            The destination peer
     * @return The future of this discover handler
     */
    private FutureResponse createDiscoverHandler(final PeerAddress remotePeer) {
        final Message message = createMessage(remotePeer, RPC.Commands.PING.getNr(), Type.REQUEST_2);
        message.neighborsSet(createNeighborSet(peerBean().serverPeerAddress()));
        return new FutureResponse(message);
    }
View Full Code Here

                || message.type() == Type.REQUEST_2 || message.type() == Type.REQUEST_3
                || message.type() == Type.REQUEST_4) && message
                .command() == RPC.Commands.PING.getNr())) {
            throw new IllegalArgumentException("Message content is wrong");
        }
        final Message responseMessage;
        // probe
        if (message.type() == Type.REQUEST_3) {
            LOG.debug("reply to probing, fire message to {}", message.sender());

            responseMessage = createResponseMessage(message, Type.OK);

            if (message.isUdp()) {
                connectionBean().reservation().create(1, 0)
                        .addListener(new BaseFutureAdapter<FutureChannelCreator>() {
                            @Override
                            public void operationComplete(final FutureChannelCreator future) throws Exception {
                                if (future.isSuccess()) {
                                  LOG.debug("fire UDP to {}", message.sender());
                                    FutureResponse futureResponse = fireUDP(message.sender(), future
                                            .channelCreator(), connectionBean().channelServer()
                                            .channelServerConfiguration());
                                    Utils.addReleaseListener(future.channelCreator(), futureResponse);
                                } else {
                                  Utils.addReleaseListener(future.channelCreator());
                                    LOG.warn("handleResponse for REQUEST_3 failed (UDP) {}",
                                            future.failedReason());
                                }
                            }
                        });
            } else {
                connectionBean().reservation().create(0, 1)
                        .addListener(new BaseFutureAdapter<FutureChannelCreator>() {
                            @Override
                            public void operationComplete(final FutureChannelCreator future) throws Exception {
                                if (future.isSuccess()) {
                                  LOG.debug("fire TCP to {}", message.sender());
                                    FutureResponse futureResponse = fireTCP(message.sender(), future
                                            .channelCreator(), connectionBean().channelServer()
                                            .channelServerConfiguration());
                                    Utils.addReleaseListener(future.channelCreator(), futureResponse);
                                } else {
                                  Utils.addReleaseListener(future.channelCreator());
                                    LOG.warn("handleResponse for REQUEST_3 failed (TCP) {}",
                                            future.failedReason());
                                }
                            }
                        });
            }
        } else if (message.type() == Type.REQUEST_2) { // discover
            LOG.debug("reply to discover, found {}", message.sender());
            responseMessage = createResponseMessage(message, Type.OK);
            responseMessage.neighborsSet(createNeighborSet(message.sender()));
        } else if (message.type() == Type.REQUEST_1 || message.type() == Type.REQUEST_4) { // regular ping
            LOG.debug("reply to regular ping {}", message.sender());
            // test if this is a broadcast message to ourselves. If it is, do not
            // reply.
            if (message.isUdp() && message.sender().peerId().equals(peerBean().serverPeerAddress().peerId())
View Full Code Here

     *            The client side connection configuration
     * @return The future response to keep track of future events
     */
    public FutureResponse closeNeighbors(final PeerAddress remotePeer, final SearchValues searchValues,
            final Type type, final ChannelCreator channelCreator, final ConnectionConfiguration configuration) {
        Message message = createMessage(remotePeer, RPC.Commands.NEIGHBOR.getNr(), type);
        if (!message.isRequest()) {
            throw new IllegalArgumentException("The type must be a request");
        }
       
        message.key(searchValues.locationKey());
        message.key(searchValues.domainKey() == null ? Number160.ZERO : searchValues.domainKey());
       
        if(searchValues.from() !=null && searchValues.to()!=null) {
          Collection<Number640> collection = new ArrayList<Number640>(2);
          collection.add(searchValues.from());
          collection.add(searchValues.to());
          KeyCollection keyCollection = new KeyCollection(collection);
          message.keyCollection(keyCollection);
        } else {
          if (searchValues.contentKey() != null) {
            message.key(searchValues.contentKey());
          }
       
          if (searchValues.keyBloomFilter() != null) {
            message.bloomFilter(searchValues.keyBloomFilter());
          }
          if (searchValues.contentBloomFilter() != null) {
            message.bloomFilter(searchValues.contentBloomFilter());
          }
        }
        return send(message, configuration, channelCreator);
    }
View Full Code Here

      FutureResponse futureResponse = new FutureResponse(message);
      futureResponse.addListener(new BaseFutureAdapter<FutureResponse>() {
      @Override
            public void operationComplete(FutureResponse future) throws Exception {
              if(future.isSuccess()) {
                Message response = future.responseMessage();
                if(response != null) {
                  NeighborSet ns = response.neighborsSet(0);
                  if(ns!=null) {
                    for(PeerAddress neighbors:ns.neighbors()) {
                      synchronized (peerBean().peerStatusListeners()) {
                        for (PeerStatusListener peerStatusListener : peerBean().peerStatusListeners()) {
                          peerStatusListener.peerFound(neighbors, response.sender(), null);
                        }
                      }
                    }
                  }
                }
View Full Code Here

        Number160 domainKey = message.key(1);
       
        Collection<PeerAddress> neighbors = getNeighbors(locationKey, NEIGHBOR_SIZE);
        if(neighbors == null) {
            //return empty neighbor set
            Message response = createResponseMessage(message, Type.NOT_FOUND);
            response.neighborsSet(new NeighborSet(-1, Collections.<PeerAddress>emptyList()));
            responder.response(response);
            return;
        }
       
        // Create response message and set neighbors
        final Message responseMessage = createResponseMessage(message, Type.OK);
       
        LOG.debug("found the following neighbors {}", neighbors);
        NeighborSet neighborSet = new NeighborSet(NEIGHBOR_LIMIT, neighbors);
        responseMessage.neighborsSet(neighborSet);
        // check for fastget, -1 if, no domain provided, so we cannot
        // check content length, 0 for content not here , > 0 content here
        // int contentLength = -1;
        Number160 contentKey = message.key(2);
        SimpleBloomFilter<Number160> keyBloomFilter = message.bloomFilter(0);
        SimpleBloomFilter<Number160> contentBloomFilter = message.bloomFilter(1);
        KeyCollection keyCollection = message.keyCollection(0);
        // it is important to set an integer if a value is present
        boolean isDigest = message.type() != Type.REQUEST_1;
        if (isDigest) {
            if (message.type() == Type.REQUEST_2) {
                final DigestInfo digestInfo;
                if (peerBean().digestStorage() == null) {
                  //no storage to search
                  digestInfo = new DigestInfo();
                }
                else if (contentKey != null && locationKey!=null && domainKey!=null) {
                  Number320 locationAndDomainKey = new Number320(locationKey, domainKey);
                    Number640 from = new Number640(locationAndDomainKey, contentKey, Number160.ZERO);
                    Number640 to = new Number640(locationAndDomainKey, contentKey, Number160.MAX_VALUE);
                    digestInfo = peerBean().digestStorage().digest(from, to, -1, true);
                } else if ((keyBloomFilter != null || contentBloomFilter != null&& locationKey!=null && domainKey!=null) {
                  Number320 locationAndDomainKey = new Number320(locationKey, domainKey);
                    digestInfo = peerBean().digestStorage().digest(locationAndDomainKey, keyBloomFilter,
                            contentBloomFilter, -1, true, true);
                } else if (keyCollection!=null && keyCollection.keys().size() == 2) {
                  Iterator<Number640> iterator = keyCollection.keys().iterator();
                  Number640 from = iterator.next();
                  Number640 to = iterator.next();
                  digestInfo = peerBean().digestStorage().digest(from, to, -1, true);
                } else if (locationKey!=null && domainKey!=null){
                  Number320 locationAndDomainKey = new Number320(locationKey, domainKey);
                    Number640 from = new Number640(locationAndDomainKey, Number160.ZERO, Number160.ZERO);
                    Number640 to = new Number640(locationAndDomainKey, Number160.MAX_VALUE, Number160.MAX_VALUE);
                    digestInfo = peerBean().digestStorage().digest(from, to, -1, true);
                } else {
                  LOG.warn("did not search for anything");
                  digestInfo = new DigestInfo();
                }
                responseMessage.intValue(digestInfo.size());
                responseMessage.key(digestInfo.keyDigest());
                responseMessage.key(digestInfo.contentDigest());
            } else if (message.type() == Type.REQUEST_3) {
              final DigestInfo digestInfo;
              if(peerBean().digestTracker() == null) {
                //no tracker to search
                digestInfo = new DigestInfo();
              } else {
                digestInfo = peerBean().digestTracker().digest(locationKey, domainKey, contentKey);
                if (digestInfo.size() == 0) { 
                  LOG.debug("No entry found on peer {}", message.recipient());
                }
              }
                responseMessage.intValue(digestInfo.size());
            }
            else if (message.type() == Type.REQUEST_4) {
              synchronized (peerBean().peerStatusListeners()) {
                for (PeerStatusListener peerStatusListener : peerBean().peerStatusListeners()) {
              peerStatusListener.peerFailed(message.sender(), new PeerException(AbortCause.SHUTDOWN, "shutdown"));
View Full Code Here

     * @return The future response to keep track of future events
     */
    public FutureResponse peerExchange(final PeerAddress remotePeer, final Number320 key,
        final ChannelCreator channelCreator, TrackerData peers,
            final ConnectionConfiguration connectionConfiguration) {
        final Message message = createMessage(remotePeer, RPC.Commands.PEX.getNr(), Type.REQUEST_FF_1);

        //TrackerData peers;
        //peers = trackerStorage.peers(key);
        LOG.debug("we got stored meshPeers size: {}", peers);
       
        if (peers == null || peers.size() == 0) {
            //future is success as we did not do PEX, since its not necessary
            return new FutureResponse(null).response(null);
        }

        peers = UtilsTracker.limit(peers, TrackerRPC.MAX_MSG_SIZE_UDP);

        message.key(key.locationKey());
        message.key(key.domainKey());
       
    LOG.debug("sent ({}) to {} / {}", message.sender().peerId(), remotePeer.peerId(), peers.size());
    message.trackerData(peers);
    FutureResponse futureResponse = new FutureResponse(message);
    final RequestHandler<FutureResponse> requestHandler = new RequestHandler<FutureResponse>(futureResponse,
            peerBean(), connectionBean(), connectionConfiguration);
    if (!connectionConfiguration.isForceTCP()) {
      return requestHandler.fireAndForgetUDP(channelCreator);
View Full Code Here

     * @return FutureResponse that stores which content keys have been stored.
     */

    public RequestHandler<FutureResponse> sendInternal(final PeerAddress remotePeer,
            final SendDirectBuilderI sendDirectBuilder) {
        final Message message = createMessage(remotePeer, RPC.Commands.DIRECT_DATA.getNr(),
                sendDirectBuilder.isRaw() ? Type.REQUEST_1 : Type.REQUEST_2);
        final FutureResponse futureResponse = new FutureResponse(message,
                sendDirectBuilder.progressListener());

        if (sendDirectBuilder.isSign()) {
            message.publicKeyAndSign(sendDirectBuilder.keyPair());
        }
        message.streaming(sendDirectBuilder.isStreaming());

        if (sendDirectBuilder.isRaw()) {
            message.buffer(sendDirectBuilder.buffer());
        } else {
            byte[] me;
            try {
                me = Utils.encodeJavaObject(sendDirectBuilder.object());
                message.buffer(new Buffer(Unpooled.wrappedBuffer(me)));
            } catch (IOException e) {
                futureResponse.failed("cannot convert object", e);
            }      
        }

View Full Code Here

    public void handleResponse(final Message message, PeerConnection peerConnection, final boolean sign, Responder responder) throws Exception {
        if (!((message.type() == Type.REQUEST_1 || message.type() == Type.REQUEST_2) && message
                .command() == RPC.Commands.DIRECT_DATA.getNr())) {
            throw new IllegalArgumentException("Message content is wrong");
        }
        final Message responseMessage = createResponseMessage(message, Type.OK);

        if (sign) {
            responseMessage.publicKeyAndSign(peerBean().getKeyPair());
        }
        final RawDataReply rawDataReply2 = rawDataReply;
        final ObjectDataReply objectDataReply2 = objectDataReply;
        if (message.type() == Type.REQUEST_1 && rawDataReply2 == null) {
            responseMessage.type(Type.NOT_FOUND);
        } else if (message.type() == Type.REQUEST_2 && objectDataReply2 == null) {
            responseMessage.type(Type.NOT_FOUND);
        } else {
            final Buffer requestBuffer = message.buffer(0);
            // the user can reply with null, indicating not found. Or
            // returning the request buffer, which means nothing is
            // returned. Or an exception can be thrown
            if (message.type() == Type.REQUEST_1) {
                LOG.debug("handling request1");
                final Buffer replyBuffer = rawDataReply2.reply(message.sender(), requestBuffer,
                        message.isDone());
                if (replyBuffer == null && message.isDone()) {
                    responseMessage.type(Type.NOT_FOUND);
                } else if (replyBuffer != requestBuffer) {
                    // can be partial as well
                    if (!replyBuffer.isComplete()) {
                        responseMessage.streaming();
                    }
                    responseMessage.buffer(replyBuffer);
                }
            } else { // no streaming here when we deal with objects
                Object obj = Utils.decodeJavaObject(requestBuffer.buffer());
                LOG.debug("handling {}", obj);

                Object reply = objectDataReply2.reply(message.sender(), obj);
                if (reply == null) {
                    responseMessage.type(Type.NOT_FOUND);
                } else if (reply == obj) {
                    responseMessage.type(Type.OK);
                } else {
                    byte[] me = Utils.encodeJavaObject(reply);
                    responseMessage.buffer(new Buffer(Unpooled.wrappedBuffer(me)));
                }
            }
        }
        responder.response(responseMessage);
    }
View Full Code Here

TOP

Related Classes of net.tomp2p.message.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.