|
1 /* |
|
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.incubator.http.internal; |
|
27 |
|
28 import java.time.Duration; |
|
29 import java.time.Instant; |
|
30 import java.util.concurrent.atomic.AtomicLong; |
|
31 |
|
32 /** |
|
33 * Timeout event notified by selector thread. Executes the given handler if |
|
34 * the timer not canceled first. |
|
35 * |
|
36 * Register with {@link HttpClientImpl#registerTimer(TimeoutEvent)}. |
|
37 * |
|
38 * Cancel with {@link HttpClientImpl#cancelTimer(TimeoutEvent)}. |
|
39 */ |
|
40 abstract class TimeoutEvent implements Comparable<TimeoutEvent> { |
|
41 |
|
42 private static final AtomicLong COUNTER = new AtomicLong(); |
|
43 // we use id in compareTo to make compareTo consistent with equals |
|
44 // see TimeoutEvent::compareTo below; |
|
45 private final long id = COUNTER.incrementAndGet(); |
|
46 private final Instant deadline; |
|
47 |
|
48 TimeoutEvent(Duration duration) { |
|
49 deadline = Instant.now().plus(duration); |
|
50 } |
|
51 |
|
52 public abstract void handle(); |
|
53 |
|
54 public Instant deadline() { |
|
55 return deadline; |
|
56 } |
|
57 |
|
58 @Override |
|
59 public int compareTo(TimeoutEvent other) { |
|
60 if (other == this) return 0; |
|
61 // if two events have the same deadline, but are not equals, then the |
|
62 // smaller is the one that was created before (has the smaller id). |
|
63 // This is arbitrary and we don't really care which is smaller or |
|
64 // greater, but we need a total order, so two events with the |
|
65 // same deadline cannot compare == 0 if they are not equals. |
|
66 final int compareDeadline = this.deadline.compareTo(other.deadline); |
|
67 if (compareDeadline == 0 && !this.equals(other)) { |
|
68 long diff = this.id - other.id; // should take care of wrap around |
|
69 if (diff < 0) return -1; |
|
70 else if (diff > 0) return 1; |
|
71 else assert false : "Different events with same id and deadline"; |
|
72 } |
|
73 return compareDeadline; |
|
74 } |
|
75 |
|
76 @Override |
|
77 public String toString() { |
|
78 return "TimeoutEvent[id=" + id + ", deadline=" + deadline + "]"; |
|
79 } |
|
80 } |