package io.grpc.internal; import com.google.common.base.Stopwatch; import io.grpc.internal.ClientTransport; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public class Http2Ping { private static final Logger log = Logger.getLogger(Http2Ping.class.getName()); private Map callbacks = new LinkedHashMap(); private boolean completed; private final long data; private Throwable failureCause; private long roundTripTimeNanos; private final Stopwatch stopwatch; public Http2Ping(long j, Stopwatch stopwatch) { this.data = j; this.stopwatch = stopwatch; } public void addCallback(ClientTransport.PingCallback pingCallback, Executor executor) { Runnable asRunnable; synchronized (this) { if (!this.completed) { this.callbacks.put(pingCallback, executor); return; } Throwable th = this.failureCause; if (th != null) { asRunnable = asRunnable(pingCallback, th); } else { asRunnable = asRunnable(pingCallback, this.roundTripTimeNanos); } doExecute(executor, asRunnable); } } public boolean complete() { synchronized (this) { if (this.completed) { return false; } this.completed = true; long elapsed = this.stopwatch.elapsed(TimeUnit.NANOSECONDS); this.roundTripTimeNanos = elapsed; Map map = this.callbacks; this.callbacks = null; for (Map.Entry entry : map.entrySet()) { doExecute(entry.getValue(), asRunnable(entry.getKey(), elapsed)); } return true; } } public void failed(Throwable th) { synchronized (this) { if (this.completed) { return; } this.completed = true; this.failureCause = th; Map map = this.callbacks; this.callbacks = null; for (Map.Entry entry : map.entrySet()) { notifyFailed(entry.getKey(), entry.getValue(), th); } } } public static void notifyFailed(ClientTransport.PingCallback pingCallback, Executor executor, Throwable th) { doExecute(executor, asRunnable(pingCallback, th)); } private static void doExecute(Executor executor, Runnable runnable) { try { executor.execute(runnable); } catch (Throwable th) { log.log(Level.SEVERE, "Failed to execute PingCallback", th); } } private static Runnable asRunnable(ClientTransport.PingCallback pingCallback, long j) { return new Runnable(pingCallback, j) { // from class: io.grpc.internal.Http2Ping.1 final ClientTransport.PingCallback val$callback; final long val$roundTripTimeNanos; { this.val$callback = pingCallback; this.val$roundTripTimeNanos = j; } @Override // java.lang.Runnable public void run() { this.val$callback.onSuccess(this.val$roundTripTimeNanos); } }; } private static Runnable asRunnable(ClientTransport.PingCallback pingCallback, Throwable th) { return new Runnable(pingCallback, th) { // from class: io.grpc.internal.Http2Ping.2 final ClientTransport.PingCallback val$callback; final Throwable val$failureCause; { this.val$callback = pingCallback; this.val$failureCause = th; } @Override // java.lang.Runnable public void run() { this.val$callback.onFailure(this.val$failureCause); } }; } public long payload() { return this.data; } }