Package net.tomp2p.message

Examples of net.tomp2p.message.Message


      public void operationComplete(FuturePeerConnection future) throws Exception {
        if (fpc.isSuccess()) {
          final PeerConnection peerConnection = fpc.peerConnection();
          if (peerConnection != null) {
            // create the necessary messages
            final Message setUpMessage = createSetupMessage(relayPeerAddress, unreachablePeerAddress);

            // send the message to the relay so it forwards it to the unreachable peer
            FutureResponse futureResponse = RelayUtils.send(peerConnection, peer.peerBean(),
                peer.connectionBean(), config, setUpMessage);

            // wait for the unreachable peer to answer
            futureResponse.addListener(new BaseFutureAdapter<FutureResponse>() {
              @Override
              public void operationComplete(FutureResponse future) throws Exception {
                // get the PeerConnection which is cached in the PeerBean object
                final PeerConnection openPeerConnection = peer.peerBean().peerConnection(unreachablePeerAddress.peerId());
                if (openPeerConnection != null && openPeerConnection.isOpen()) {
                  futureDone.done(openPeerConnection);
                } else {
                  LOG.error("The reverse connection to the unreachable peer failed.");
                  handleFail(futureDone, "No reverse connection could be established");
                }

                // can close the connection to the relay peer
                peerConnection.close();
              }
            });
          } else {
            handleFail(futureDone, "The PeerConnection was null!");
          }
        } else {
          handleFail(futureDone, "no channel could be established");
        }
      }

      private void handleFail(final FutureDone<PeerConnection> futureDone, final String failMessage) {
        LOG.error(failMessage);
        futureDone.failed(failMessage);
      }

      // this message is sent to the relay peer to initiate the rcon setup
      private Message createSetupMessage(final PeerAddress relayPeerAddress, final PeerAddress unreachablePeerAddress) {
        Message setUpMessage = new Message();
        setUpMessage.version(MESSAGE_VERSION);
        setUpMessage.sender(peer.peerAddress());
        setUpMessage.recipient(relayPeerAddress.changePeerId(unreachablePeerAddress.peerId()));
        setUpMessage.command(RPC.Commands.RCON.getNr());
        setUpMessage.type(Type.REQUEST_1);
        // setUpMessage.keepAlive(true);
        return setUpMessage;
      }
    });
    return futureDone;
View Full Code Here


  @Override
  public FutureDone<Message> forwardToUnreachable(Message message) {
    // create temporal OK message
    final FutureDone<Message> futureDone = new FutureDone<Message>();
    final Message response = createResponseMessage(message, Type.PARTIALLY_OK);
    response.recipient(message.sender());
    response.sender(unreachablePeerAddress());

    try {
      buffer.addMessage(message, connectionBean().channelServer().channelServerConfiguration().signatureFactory());
    } catch (Exception e) {
      LOG.error("Cannot encode the message", e);
View Full Code Here

        this.broadcastHandler = broadcastHandler;
    }

    public FutureResponse send(final PeerAddress remotePeer, final BroadcastBuilder broadcastBuilder,
            final ChannelCreator channelCreator, final ConnectionConfiguration configuration) {
        final Message message = createMessage(remotePeer, RPC.Commands.BROADCAST.getNr(), Type.REQUEST_FF_1);
        message.intValue(broadcastBuilder.hopCounter());
        message.key(broadcastBuilder.messageKey());
        if (broadcastBuilder.dataMap() != null) {
            message.setDataMap(new DataMap(broadcastBuilder.dataMap()));
        }
        final FutureResponse futureResponse = new FutureResponse(message);
        final RequestHandler<FutureResponse> requestHandler = new RequestHandler<FutureResponse>(
                futureResponse, peerBean(), connectionBean(), configuration);
        if (!broadcastBuilder.isUDP()) {
View Full Code Here

     * @param type
     *            The request type
     * @return The request message
     */
    public Message createMessage(final PeerAddress recipient, final byte name, final Type type) {
        return new Message().recipient(recipient).sender(peerBean().serverPeerAddress())
                .command(name).type(type).version(connectionBean().p2pId());
    }
View Full Code Here

    public Message createResponseMessage(final Message requestMessage, final Type replyType) {
        return createResponseMessage(requestMessage, replyType, peerBean().serverPeerAddress());
    }
   
    public static Message createResponseMessage(final Message requestMessage, final Type replyType, final PeerAddress peerAddress) {
        Message replyMessage = new Message();
        // this will have the ports > 40'000 that we need to know for sendig the reply
        replyMessage.senderSocket(requestMessage.senderSocket());
        replyMessage.recipientSocket(requestMessage.recipientSocket());
        replyMessage.recipient(requestMessage.sender());
        replyMessage.sender(peerAddress);
        replyMessage.command(requestMessage.command());
        replyMessage.type(replyType);
        replyMessage.version(requestMessage.version());
        replyMessage.messageId(requestMessage.messageId());
        replyMessage.udp(requestMessage.isUdp());
        return replyMessage;
    }
View Full Code Here

   *            The client side connection configuration
   * @return The future response to keep track of future events
   */
  public FutureResponse quit(final PeerAddress remotePeer, final ShutdownBuilder shutdownBuilder,
          final ChannelCreator channelCreator) {
    final Message message = createMessage(remotePeer, RPC.Commands.QUIT.getNr(), Type.REQUEST_FF_1);
    if (shutdownBuilder.isSign()) {
      message.publicKeyAndSign(shutdownBuilder.keyPair());
    }

    FutureResponse futureResponse = new FutureResponse(message);
    final RequestHandler<FutureResponse> requestHandler = new RequestHandler<FutureResponse>(futureResponse,
            peerBean(), connectionBean(), shutdownBuilder);
View Full Code Here

        Runnable runner = new Runnable() {
          @Override
          public void run() {
            semaphore.release();
           
            Message request = futureResponse.request();
            if(request.recipient().isSlow() && request.command() != Commands.PING.getNr() && request.command() != Commands.NEIGHBOR.getNr()) {
              // If the request goes to a slow peer, the channel can be closed until the response arrives
              LOG.debug("Ignoring channel close event because recipient is slow peer");
            } else {
              futureResponse.responseNow();
            }
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.