Package net.tomp2p.message

Examples of net.tomp2p.message.Message


   * @return FutureDone with a peer connection to the newly set up relay peer
   */
  private FutureDone<PeerConnection> sendMessage(final PeerAddress candidate) {
    final FutureDone<PeerConnection> futureDone = new FutureDone<PeerConnection>();

    final Message message = relayRPC.createMessage(candidate, RPC.Commands.RELAY.getNr(), Type.REQUEST_1);

    // depend on the relay type whether to keep the connection open or close it after the setup.
    message.keepAlive(relayType.keepConnectionOpen());

    // encode the relay type in the message such that the relay node knows how to handle
    message.intValue(relayType.ordinal());

    // server credentials only used by Android peers
    if (relayType == RelayType.ANDROID) {
      if (gcmServerCredentials == null) {
        LOG.error("No available GCM server found. Seems that they are all occupied. Configure more during peer setup!");
        return futureDone.failed("No GCM server available");
      } else if(!gcmServerCredentials.valid()) {
        LOG.error("GCM Server Configuration is not valid. Please provide a valid configuration");
        return futureDone.failed("Invalid GCM configuration");
      } else {
        // add the registration ID, the GCM authentication key and the map update interval
        message.buffer(RelayUtils.encodeString(gcmServerCredentials.registrationId()));
        message.buffer(RelayUtils.encodeString(gcmServerCredentials.senderAuthenticationKey()));
        message.intValue(mapUpdateInterval);
      }
    }

    LOG.debug("Setting up relay connection to peer {}, message {}", candidate, message);
    final FuturePeerConnection fpc = peer.createPeerConnection(candidate);
View Full Code Here


        || message.command() == RPC.Commands.PUT_CONFIRM.getNr()
        || message.command() == RPC.Commands.GET_LATEST.getNr()
        || message.command() == RPC.Commands.GET_LATEST_WITH_DIGEST.getNr())) {
      throw new IllegalArgumentException("Message content is wrong " + message.command());
    }
        final Message responseMessage = createResponseMessage(message, Type.OK);

        //switch/case does not work here out of the box, need to convert byte back to enum, not sure if thats worth it.
        if (message.command() == RPC.Commands.ADD.getNr()) {
          handleAdd(message, responseMessage, isDomainProtected(message));
        } else if(message.command() == RPC.Commands.PUT.getNr()) {
            handlePut(message, responseMessage, isStoreIfAbsent(message), isDomainProtected(message));
        } else if (message.command() == RPC.Commands.PUT_CONFIRM.getNr()) {
          handlePutConfirm(message, responseMessage);
        } else if (message.command() == RPC.Commands.GET.getNr()) {
            handleGet(message, responseMessage);
    } else if (message.command() == RPC.Commands.GET_LATEST.getNr()) {
      handleGetLatest(message, responseMessage, false);
    } else if (message.command() == RPC.Commands.GET_LATEST_WITH_DIGEST.getNr()) {
      handleGetLatest(message, responseMessage, true);
        } else if (message.command() == RPC.Commands.DIGEST.getNr()
            || message.command() == RPC.Commands.DIGEST_BLOOMFILTER.getNr()
            || message.command() == RPC.Commands.DIGEST_META_VALUES.getNr()) {
            handleDigest(message, responseMessage);
        } else if (message.command() == RPC.Commands.REMOVE.getNr()) {
            handleRemove(message, responseMessage, message.type() == Type.REQUEST_2);
        } else if (message.command() == RPC.Commands.PUT_META.getNr()) {
            handlePutMeta(message, responseMessage, message.type() == Type.REQUEST_2);
        }else {
            throw new IllegalArgumentException("Message content is wrong");
        }
        if (sign) {
            responseMessage.publicKeyAndSign(peerBean().getKeyPair());
        }
        LOG.debug("response for storage request: {}", responseMessage);
        responder.response(responseMessage);
    }
View Full Code Here

  @Override
  public FutureDone<Message> forwardToUnreachable(final Message message) {
    // Send message via direct message through the open connection to the unreachable peer
    LOG.debug("Sending {} to unreachable peer {}", message, peerConnection.remotePeer());
    final Message envelope = createMessage(peerConnection.remotePeer(), RPC.Commands.RELAY.getNr(), Type.REQUEST_2);
    try {
      message.restoreContentReferences();
      // add the message into the payload
      envelope.buffer(RelayUtils.encodeMessage(message, connectionBean().channelServer().channelServerConfiguration().signatureFactory()));
    } catch (Exception e) {
      LOG.error("Cannot encode the message", e);
      return new FutureDone<Message>().failed(e);
    }

    // always keep the connection open
    envelope.keepAlive(true);

    // this will be read RelayRPC.handlePiggyBackMessage
    Collection<PeerSocketAddress> peerSocketAddresses = new ArrayList<PeerSocketAddress>(1);
    peerSocketAddresses.add(new PeerSocketAddress(message.sender().inetAddress(), 0, 0));
    envelope.peerSocketAddresses(peerSocketAddresses);
   
    // holds the message that will be returned to he requester
    final FutureDone<Message> futureDone = new FutureDone<Message>();

    // Forward a message through the open peer connection to the unreachable peer.
    FutureResponse fr = RelayUtils.send(peerConnection, peerBean(), connectionBean(), config, envelope);
    fr.addListener(new BaseFutureAdapter<FutureResponse>() {
      public void operationComplete(FutureResponse future) throws Exception {
        if (future.isSuccess()) {
          InetSocketAddress senderSocket = message.recipientSocket();
          if (senderSocket == null) {
            senderSocket = unreachablePeerAddress().createSocketTCP();
          }
          InetSocketAddress recipientSocket = message.senderSocket();
          if (recipientSocket == null) {
            recipientSocket = message.sender().createSocketTCP();
          }

          Buffer buffer = future.responseMessage().buffer(0);
          Message responseFromUnreachablePeer = RelayUtils.decodeMessage(buffer, recipientSocket, senderSocket, connectionBean().channelServer().channelServerConfiguration().signatureFactory());
          responseFromUnreachablePeer.restoreContentReferences();
          futureDone.done(responseFromUnreachablePeer);
        } else {
          futureDone.failed("Could not forward message over TCP channel");
        }
      }
View Full Code Here

    }
   
    @Override
    public void response(final Message responseMessage) {
      // piggyback the late response. It will be unwrapped by the dispatcher
      Message envelope = dispatchHandler.createMessage(responseMessage.recipient(), Commands.RELAY.getNr(), Type.REQUEST_5);
      try {
        envelope.buffer(RelayUtils.encodeMessage(responseMessage, peer.connectionBean().channelServer().channelServerConfiguration().signatureFactory()));
      } catch (Exception e) {
        LOG.error("Cannot wrap the late response into an envelope", e);
        return;
      }
     
View Full Code Here

   * @return when the buffer request is done
   */
  public FutureDone<Void> sendBufferRequest() {
    final FutureDone<Void> futureDone = new FutureDone<Void>();

    Message message = dispatchHandler.createMessage(relayAddress(), Commands.RELAY.getNr(), Type.REQUEST_4);
    // close the connection after this message
    message.keepAlive(false);

    FutureResponse response = sendToRelay(message);
    response.addListener(new BaseFutureAdapter<FutureResponse>() {
      @Override
      public void operationComplete(FutureResponse futureResponse) throws Exception {
View Full Code Here

    public static Message createDummyMessage(Number160 idSender, String inetSender, int tcpPortSendor,
            int udpPortSender, Number160 idRecipien, String inetRecipient, int tcpPortRecipient,
            int udpPortRecipient, byte command, Type type, boolean firewallUDP, boolean firewallTCP)
            throws UnknownHostException {
        Message message = new Message();
        PeerAddress n1 = createAddress(idSender, inetSender, tcpPortSendor, udpPortSender, firewallUDP,
                firewallTCP);
        message.sender(n1);
        //
        PeerAddress n2 = createAddress(idRecipien, inetRecipient, tcpPortRecipient, udpPortRecipient,
                firewallUDP, firewallTCP);
        message.recipient(n2);
        message.type(type);
        message.command(command);
        return message;
    }
View Full Code Here

   * in addition checks that the relay peers of the decoded message are set correctly
   */
  public static Message decodeRelayedMessage(Buffer buf, InetSocketAddress recipient, InetSocketAddress sender,
      SignatureFactory signatureFactory) throws InvalidKeyException, NoSuchAlgorithmException,
      InvalidKeySpecException, SignatureException, IOException {
    Message decodedMessage = decodeMessage(buf, recipient, sender, signatureFactory);
    boolean isRelay = decodedMessage.sender().isRelayed();
    if (isRelay && !decodedMessage.peerSocketAddresses().isEmpty()) {
      PeerAddress tmpSender = decodedMessage.sender().changePeerSocketAddresses(decodedMessage.peerSocketAddresses());
      decodedMessage.sender(tmpSender);
    }
    return decodedMessage;
  }
View Full Code Here

    while (messageBuffer.readableBytes() > 0) {
      int size = messageBuffer.readInt();
      ByteBuf message = messageBuffer.readBytes(size);
     
      try {
        Message decodedMessage = decodeRelayedMessage(new Buffer(message), recipient, sender, signatureFactory);
        messages.add(decodedMessage);
      } catch (Exception e) {
        LOG.error("Cannot decode buffered message. Skip it.", e);
      }
    }
View Full Code Here

  public FutureResponse addToTracker(final PeerAddress remotePeer, AddTrackerBuilder builder,
          ChannelCreator channelCreator) {

    Utils.nullCheck(remotePeer, builder.locationKey(), builder.domainKey());
    final Message message = createMessage(remotePeer, RPC.Commands.TRACKER_ADD.getNr(), Type.REQUEST_3);
    if (builder.isSign()) {
      message.publicKeyAndSign(builder.keyPair());
    }
    message.key(builder.locationKey());
    message.key(builder.domainKey());
    if (builder.getBloomFilter() != null) {
      message.bloomFilter(builder.getBloomFilter());
    }
    final FutureResponse futureResponse = new FutureResponse(message);

    addTrackerDataListener(futureResponse, new Number320(builder.locationKey(), builder.domainKey()));
    RequestHandler<FutureResponse> requestHandler = new RequestHandler<FutureResponse>(futureResponse, peerBean(),
            connectionBean(), builder);

    TrackerData trackerData = new TrackerData(new HashMap<PeerStatistic, Data>());
    PeerAddress peerAddressToAnnounce = builder.peerAddressToAnnounce();
    if (peerAddressToAnnounce == null) {
      peerAddressToAnnounce = peerBean().serverPeerAddress();
    }
    trackerData.put(new PeerStatistic(peerAddressToAnnounce), builder.attachement());
    trackerData = UtilsTracker.limit(trackerData, TrackerRPC.MAX_MSG_SIZE_UDP);
    message.trackerData(trackerData);

    if (builder.isForceTCP()) {
      return requestHandler.sendTCP(channelCreator);
    } else {
      return requestHandler.sendUDP(channelCreator);
View Full Code Here

                            futureSync.failed(future);
                            LOG.error("checkDirect failed {}", future.failedReason());
                            return;
                        }

                        Message responseMessage = future.responseMessage();
                        DataMap dataMap = responseMessage.dataMap(0);

                        if (dataMap == null) {
                          LOG.error("nothing received, something is wrong");
                            futureSync.failed("nothing received, something is wrong");
                            return;
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.