1:
37:
38:
39: package ;
40:
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
52: public class KqueueSelectionKeyImpl extends AbstractSelectionKey
53: {
54: int interestOps;
55: int readyOps;
56: int activeOps = 0;
57: int key;
58: int fd;
59:
60:
61: private final KqueueSelectorImpl selector;
62:
63:
64: private final SelectableChannel channel;
65:
66: private final VMChannelOwner natChannel;
67:
68: public KqueueSelectionKeyImpl(KqueueSelectorImpl selector,
69: SelectableChannel channel)
70: {
71: this.selector = selector;
72: this.channel = channel;
73: natChannel = (VMChannelOwner) channel;
74: interestOps = 0;
75: readyOps = 0;
76: }
77:
78:
81:
82: public SelectableChannel channel()
83: {
84: return channel;
85: }
86:
87:
90:
91: public int interestOps()
92: {
93: return interestOps;
94: }
95:
96:
99:
100: public SelectionKey interestOps(int ops)
101: {
102: if (!isValid())
103: throw new IllegalStateException("key is invalid");
104: if ((ops & ~channel.validOps()) != 0)
105: throw new IllegalArgumentException("channel does not support all operations");
106:
107: selector.setInterestOps(this, ops);
108: return this;
109: }
110:
111:
114:
115: public int readyOps()
116: {
117: return readyOps;
118: }
119:
120:
123:
124: public Selector selector()
125: {
126: return selector;
127: }
128:
129: public String toString()
130: {
131: if (!isValid())
132: return super.toString() + " [ fd: " + fd + " <<invalid>> ]";
133: return super.toString() + " [ fd: " + fd + " interest ops: {"
134: + ((interestOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
135: + ((interestOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
136: + ((interestOps & OP_READ) != 0 ? " OP_READ" : "")
137: + ((interestOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
138: + " }; ready ops: {"
139: + ((readyOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "")
140: + ((readyOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "")
141: + ((readyOps & OP_READ) != 0 ? " OP_READ" : "")
142: + ((readyOps & OP_WRITE) != 0 ? " OP_WRITE" : "")
143: + " } ]";
144: }
145:
146: public int hashCode()
147: {
148: return fd;
149: }
150:
151: public boolean equals(Object o)
152: {
153: if (!(o instanceof KqueueSelectionKeyImpl))
154: return false;
155: KqueueSelectionKeyImpl that = (KqueueSelectionKeyImpl) o;
156: return that.fd == this.fd && that.channel.equals(this.channel);
157: }
158:
159:
160: boolean isReadActive()
161: {
162: return (activeOps & (OP_READ | OP_ACCEPT)) != 0;
163: }
164:
165: boolean isReadInterested()
166: {
167: return (interestOps & (OP_READ | OP_ACCEPT)) != 0;
168: }
169:
170: boolean isWriteActive()
171: {
172: return (activeOps & (OP_WRITE | OP_CONNECT)) != 0;
173: }
174:
175: boolean isWriteInterested()
176: {
177: return (interestOps & (OP_WRITE | OP_CONNECT)) != 0;
178: }
179:
180: boolean needCommitRead()
181: {
182: return isReadActive() == (!isReadInterested());
183: }
184:
185: boolean needCommitWrite()
186: {
187: return isWriteActive() == (!isWriteInterested());
188: }
189: }