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