17899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath/*
27899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * Copyright (C) 2012 Square, Inc.
37899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath *
47899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * Licensed under the Apache License, Version 2.0 (the "License");
57899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * you may not use this file except in compliance with the License.
67899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * You may obtain a copy of the License at
77899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath *
87899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath *      http://www.apache.org/licenses/LICENSE-2.0
97899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath *
107899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * Unless required by applicable law or agreed to in writing, software
117899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * distributed under the License is distributed on an "AS IS" BASIS,
127899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * See the License for the specific language governing permissions and
147899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * limitations under the License.
157899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath */
162231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpackage com.squareup.okhttp.internal.spdy;
177899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
187899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamathimport java.util.concurrent.CountDownLatch;
197899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamathimport java.util.concurrent.TimeUnit;
207899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
217899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath/**
227899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath * A locally-originated ping.
237899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath */
247899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamathpublic final class Ping {
2554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  private final CountDownLatch latch = new CountDownLatch(1);
2654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  private long sent = -1;
2754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  private long received = -1;
287899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
2954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  Ping() {
3054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
317899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
3254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  void send() {
3354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (sent != -1) throw new IllegalStateException();
3454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    sent = System.nanoTime();
3554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
367899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
3754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  void receive() {
3854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (received != -1 || sent == -1) throw new IllegalStateException();
3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    received = System.nanoTime();
4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    latch.countDown();
4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
427899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
4354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  void cancel() {
4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (received != -1 || sent == -1) throw new IllegalStateException();
4554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    received = sent - 1;
4654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    latch.countDown();
4754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
482231db3e6bb54447a9b14cf004a6cb03c373651cjwilson
4954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  /**
5054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * Returns the round trip time for this ping in nanoseconds, waiting for the
5154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * response to arrive if necessary. Returns -1 if the response was
5254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * cancelled.
5354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   */
5454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  public long roundTripTime() throws InterruptedException {
5554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    latch.await();
5654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    return received - sent;
5754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
587899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath
5954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  /**
6054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * Returns the round trip time for this ping in nanoseconds, or -1 if the
6154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * response was cancelled, or -2 if the timeout elapsed before the round
6254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   * trip completed.
6354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson   */
6454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  public long roundTripTime(long timeout, TimeUnit unit) throws InterruptedException {
6554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (latch.await(timeout, unit)) {
6654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      return received - sent;
6754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    } else {
6854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      return -2;
697899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath    }
7054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
717899c5ab935cf542069835ec7d3e457db596dbf7Narayan Kamath}
72