Search in sources :

Example 1 with Pong

use of io.bitsquare.p2p.peers.keepalive.messages.Pong in project bitsquare by bitsquare.

the class KeepAliveHandler method onMessage.

///////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onMessage(Message message, Connection connection) {
    if (message instanceof Pong) {
        Log.traceCall(message.toString() + "\n\tconnection=" + connection);
        if (!stopped) {
            Pong pong = (Pong) message;
            if (pong.requestNonce == nonce) {
                int roundTripTime = (int) (System.currentTimeMillis() - sendTs);
                log.trace("roundTripTime=" + roundTripTime + "\n\tconnection=" + connection);
                connection.getStatistic().setRoundTripTime(roundTripTime);
                cleanup();
                listener.onComplete();
            } else {
                log.warn("Nonce not matching. That should never happen.\n\t" + "We drop that message. nonce={} / requestNonce={}", nonce, pong.requestNonce);
            }
        } else {
            log.trace("We have stopped already. We ignore that onMessage call.");
        }
    }
}
Also used : Pong(io.bitsquare.p2p.peers.keepalive.messages.Pong)

Example 2 with Pong

use of io.bitsquare.p2p.peers.keepalive.messages.Pong in project bitsquare by bitsquare.

the class KeepAliveManager method onMessage.

///////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onMessage(Message message, Connection connection) {
    if (message instanceof Ping) {
        Log.traceCall(message.toString() + "\n\tconnection=" + connection);
        if (!stopped) {
            Ping ping = (Ping) message;
            // We get from peer last measured rrt
            connection.getStatistic().setRoundTripTime(ping.lastRoundTripTime);
            Pong pong = new Pong(ping.nonce);
            SettableFuture<Connection> future = networkNode.sendMessage(connection, pong);
            Futures.addCallback(future, new FutureCallback<Connection>() {

                @Override
                public void onSuccess(Connection connection) {
                    log.trace("Pong sent successfully");
                }

                @Override
                public void onFailure(@NotNull Throwable throwable) {
                    if (!stopped) {
                        String errorMessage = "Sending pong to " + connection + " failed. That is expected if the peer is offline. pong=" + pong + "." + "Exception: " + throwable.getMessage();
                        log.debug(errorMessage);
                        peerManager.handleConnectionFault(connection);
                    } else {
                        log.warn("We have stopped already. We ignore that  networkNode.sendMessage.onFailure call.");
                    }
                }
            });
        } else {
            log.warn("We have stopped already. We ignore that onMessage call.");
        }
    }
}
Also used : Ping(io.bitsquare.p2p.peers.keepalive.messages.Ping) Pong(io.bitsquare.p2p.peers.keepalive.messages.Pong)

Aggregations

Pong (io.bitsquare.p2p.peers.keepalive.messages.Pong)2 Ping (io.bitsquare.p2p.peers.keepalive.messages.Ping)1