1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
package com.sun.grizzly.util; |
40 | |
|
41 | |
import com.sun.grizzly.CallbackHandler; |
42 | |
import java.net.SocketAddress; |
43 | |
import java.nio.channels.SelectableChannel; |
44 | |
import java.nio.channels.SelectionKey; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 1013 | public class SelectionKeyOP { |
51 | |
private int op; |
52 | |
private SelectionKey key; |
53 | |
private SelectableChannel channel; |
54 | |
|
55 | 1 | private static ConcurrentLinkedQueuePool<SelectionKeyOP> readWritePool = |
56 | 1013 | new ConcurrentLinkedQueuePool<SelectionKeyOP>() { |
57 | |
@Override |
58 | |
public SelectionKeyOP newInstance() { |
59 | 1012 | return new SelectionKeyOP(); |
60 | |
} |
61 | |
}; |
62 | |
|
63 | 1 | private static ConcurrentLinkedQueuePool<SelectionKeyOP> connectPool = |
64 | 2 | new ConcurrentLinkedQueuePool<SelectionKeyOP>() { |
65 | |
@Override |
66 | |
public SelectionKeyOP newInstance() { |
67 | 1 | return new ConnectSelectionKeyOP(); |
68 | |
} |
69 | |
}; |
70 | |
|
71 | |
public static SelectionKeyOP aquireSelectionKeyOP(int op) { |
72 | 426524 | if (op == SelectionKey.OP_READ || op == SelectionKey.OP_WRITE || |
73 | |
op == (SelectionKey.OP_WRITE | SelectionKey.OP_READ)) { |
74 | 426324 | SelectionKeyOP operation = readWritePool.poll(); |
75 | 426317 | return operation; |
76 | 197 | } else if (op == SelectionKey.OP_CONNECT) { |
77 | 197 | SelectionKeyOP operation = connectPool.poll(); |
78 | 197 | return operation; |
79 | |
} |
80 | |
|
81 | 0 | throw new IllegalStateException("Unknown operation or operation is not supported"); |
82 | |
} |
83 | |
|
84 | |
public static void releaseSelectionKeyOP(SelectionKeyOP operation) { |
85 | 426511 | int op = operation.op; |
86 | 426511 | operation.recycle(); |
87 | |
|
88 | 426511 | if (op == SelectionKey.OP_READ || op == SelectionKey.OP_WRITE || |
89 | |
op == (SelectionKey.OP_WRITE | SelectionKey.OP_READ)) { |
90 | 426314 | readWritePool.offer(operation); |
91 | 426314 | return; |
92 | 197 | } else if (op == SelectionKey.OP_CONNECT) { |
93 | 197 | connectPool.offer(operation); |
94 | 197 | return; |
95 | |
} |
96 | |
|
97 | 0 | throw new IllegalStateException("Unknown operation or operation is not supported"); |
98 | |
} |
99 | |
|
100 | 1013 | private SelectionKeyOP() { |
101 | 1012 | } |
102 | |
|
103 | |
public int getOp() { |
104 | 1279139 | return op; |
105 | |
} |
106 | |
|
107 | |
public void setOp(int op) { |
108 | 426523 | this.op = op; |
109 | 426423 | } |
110 | |
|
111 | |
public SelectionKey getSelectionKey() { |
112 | 426314 | return key; |
113 | |
} |
114 | |
|
115 | |
public void setSelectionKey(SelectionKey key) { |
116 | 426292 | this.key = key; |
117 | 426293 | } |
118 | |
|
119 | |
public SelectableChannel getChannel() { |
120 | 32 | return channel; |
121 | |
} |
122 | |
|
123 | |
public void setChannel(SelectableChannel channel) { |
124 | 32 | this.channel = channel; |
125 | 32 | } |
126 | |
|
127 | |
protected void recycle() { |
128 | 426314 | op = 0; |
129 | 426314 | key = null; |
130 | 426314 | channel = null; |
131 | 426314 | } |
132 | |
|
133 | 1 | public static class ConnectSelectionKeyOP extends SelectionKeyOP { |
134 | |
private SocketAddress localAddress; |
135 | |
private SocketAddress remoteAddress; |
136 | |
private CallbackHandler callbackHandler; |
137 | |
|
138 | |
public SocketAddress getLocalAddress() { |
139 | 197 | return localAddress; |
140 | |
} |
141 | |
|
142 | |
public void setLocalAddress(SocketAddress localAddress) { |
143 | 197 | this.localAddress = localAddress; |
144 | 197 | } |
145 | |
|
146 | |
public SocketAddress getRemoteAddress() { |
147 | 197 | return remoteAddress; |
148 | |
} |
149 | |
|
150 | |
public void setRemoteAddress(SocketAddress remoteAddress) { |
151 | 197 | this.remoteAddress = remoteAddress; |
152 | 197 | } |
153 | |
|
154 | |
public CallbackHandler getCallbackHandler() { |
155 | 197 | return callbackHandler; |
156 | |
} |
157 | |
|
158 | |
public void setCallbackHandler(CallbackHandler callbackHandler) { |
159 | 197 | this.callbackHandler = callbackHandler; |
160 | 197 | } |
161 | |
|
162 | |
@Override |
163 | |
protected void recycle() { |
164 | 197 | localAddress = null; |
165 | 197 | remoteAddress = null; |
166 | 197 | callbackHandler = null; |
167 | 197 | } |
168 | |
} |
169 | |
} |