package net.pterodactylus.irc;
import java.io.IOException;
+import java.time.Duration;
+import java.util.Optional;
/**
* A connection to an IRC server.
*/
void close() throws IOException;
+ Optional<Duration> getUptime();
+
}
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;
/** Whether the connection has already been established. */
private final AtomicBoolean established = new AtomicBoolean();
+ private final AtomicReference<Instant> connectionTime = new AtomicReference<>();
/**
* Creates a new connection.
}
}
+ @Override
+ public java.util.Optional<Duration> getUptime() {
+ return established.get() ?
+ java.util.Optional.of(Duration.between(connectionTime.get(), Instant.now())) :
+ java.util.Optional.<Duration>empty();
+ }
+
//
// ABSTRACTEXECUTIONTHREADSERVICE METHODS
//
@Subscribe
public void connectionEstablished(ConnectionEstablished connectionEstablished) {
if (connectionEstablished.connection() == this) {
+ connectionTime.set(Instant.now());
established.set(true);
}
}