1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix;
19
20 import java.nio.channels.SelectableChannel;
21 import java.nio.channels.SelectionKey;
22 import java.nio.channels.Selector;
23 import java.nio.channels.spi.AbstractSelectableChannel;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 final class AFSelectionKey extends SelectionKey {
27 private static final int OP_INVALID = 1 << 7;
28 private final AFSelector sel;
29 private final AFSocketCore core;
30 private int ops;
31 private final SelectableChannel chann;
32 private final AtomicBoolean cancelled = new AtomicBoolean();
33 private int opsReady;
34
35 AFSelectionKey(AFSelector selector, AbstractSelectableChannel ch, int ops, Object att) {
36 super();
37 this.chann = ch;
38 this.sel = selector;
39 this.ops = ops;
40
41 if (ch instanceof AFDatagramChannel<?>) {
42 this.core = ((AFDatagramChannel<?>) ch).getAFCore();
43 } else if (ch instanceof AFSocketChannel<?>) {
44 this.core = ((AFSocketChannel<?>) ch).getAFCore();
45 } else if (ch instanceof AFServerSocketChannel<?>) {
46 this.core = ((AFServerSocketChannel<?>) ch).getAFCore();
47 } else {
48 throw new UnsupportedOperationException("Unsupported channel: " + ch);
49 }
50
51 attach(att);
52 }
53
54 @Override
55 public SelectableChannel channel() {
56 return chann;
57 }
58
59 @Override
60 public Selector selector() {
61 return sel;
62 }
63
64 @Override
65 public boolean isValid() {
66 return !hasOpInvalid() && !cancelled.get() && chann.isOpen() && sel.isOpen();
67 }
68
69 boolean isCancelled() {
70 return cancelled.get();
71 }
72
73 boolean hasOpInvalid() {
74 return (opsReady & OP_INVALID) != 0;
75 }
76
77 boolean isSelected() {
78 return readyOps() != 0;
79 }
80
81 @Override
82 public void cancel() {
83 sel.remove(this);
84 cancelNoRemove();
85 }
86
87 void cancelNoRemove() {
88 if (!cancelled.compareAndSet(false, true) || !chann.isOpen()) {
89 return;
90 }
91
92 cancel1();
93 }
94
95 private void cancel1() {
96
97 }
98
99 @Override
100 public int interestOps() {
101 return ops;
102 }
103
104 @Override
105 public SelectionKey interestOps(int interestOps) {
106 this.ops = interestOps;
107 return this;
108 }
109
110 @Override
111 public int readyOps() {
112 return opsReady & ~OP_INVALID;
113 }
114
115 AFSocketCore getAFCore() {
116 return core;
117 }
118
119 void setOpsReady(int opsReady) {
120 this.opsReady = opsReady;
121 }
122
123 @Override
124 public String toString() {
125 return super.toString() + "[" + readyOps() + ";valid=" + isValid() + ";channel=" + channel()
126 + "]";
127 }
128 }