From bb4bd4ceb3bc78c7e7ea8f4d6b827e0715c57561 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 23 Feb 2015 06:37:22 +0100 Subject: [PATCH] Expose connection uptime --- src/main/java/net/pterodactylus/irc/Connection.java | 4 ++++ src/main/java/net/pterodactylus/irc/DefaultConnection.java | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index 580b4d7..ca6c494 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -1,6 +1,8 @@ package net.pterodactylus.irc; import java.io.IOException; +import java.time.Duration; +import java.util.Optional; /** * A connection to an IRC server. @@ -144,4 +146,6 @@ public interface Connection { */ void close() throws IOException; + Optional getUptime(); + } diff --git a/src/main/java/net/pterodactylus/irc/DefaultConnection.java b/src/main/java/net/pterodactylus/irc/DefaultConnection.java index 8fe4c4b..876aefb 100644 --- a/src/main/java/net/pterodactylus/irc/DefaultConnection.java +++ b/src/main/java/net/pterodactylus/irc/DefaultConnection.java @@ -30,11 +30,14 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.Socket; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import javax.net.SocketFactory; @@ -120,6 +123,7 @@ public class DefaultConnection extends AbstractExecutionThreadService implements /** Whether the connection has already been established. */ private final AtomicBoolean established = new AtomicBoolean(); + private final AtomicReference connectionTime = new AtomicReference<>(); /** * Creates a new connection. @@ -230,6 +234,13 @@ public class DefaultConnection extends AbstractExecutionThreadService implements } } + @Override + public java.util.Optional getUptime() { + return established.get() ? + java.util.Optional.of(Duration.between(connectionTime.get(), Instant.now())) : + java.util.Optional.empty(); + } + // // ABSTRACTEXECUTIONTHREADSERVICE METHODS // @@ -358,6 +369,7 @@ public class DefaultConnection extends AbstractExecutionThreadService implements @Subscribe public void connectionEstablished(ConnectionEstablished connectionEstablished) { if (connectionEstablished.connection() == this) { + connectionTime.set(Instant.now()); established.set(true); } } -- 2.7.4