| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AsyncQueueWriter |
|
| 1.0;1 |
| 1 | /* | |
| 2 | * | |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 4 | * | |
| 5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
| 6 | * | |
| 7 | * The contents of this file are subject to the terms of either the GNU | |
| 8 | * General Public License Version 2 only ("GPL") or the Common Development | |
| 9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
| 10 | * may not use this file except in compliance with the License. You can obtain | |
| 11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
| 12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
| 13 | * language governing permissions and limitations under the License. | |
| 14 | * | |
| 15 | * When distributing the software, include this License Header Notice in each | |
| 16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
| 17 | * Sun designates this particular file as subject to the "Classpath" exception | |
| 18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
| 19 | * accompanied this code. If applicable, add the following below the License | |
| 20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
| 21 | * identifying information: "Portions Copyrighted [year] | |
| 22 | * [name of copyright owner]" | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * | |
| 26 | * If you wish your version of this file to be governed by only the CDDL or | |
| 27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
| 28 | * elects to include this software in this distribution under the [CDDL or GPL | |
| 29 | * Version 2] license." If you don't indicate a single choice of license, a | |
| 30 | * recipient has the option to distribute your version of this file under | |
| 31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
| 32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
| 33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
| 34 | * only if the new code is made subject to such option by the copyright | |
| 35 | * holder. | |
| 36 | * | |
| 37 | */ | |
| 38 | ||
| 39 | package com.sun.grizzly.async; | |
| 40 | ||
| 41 | import java.io.IOException; | |
| 42 | import java.net.SocketAddress; | |
| 43 | import java.nio.ByteBuffer; | |
| 44 | import java.nio.channels.SelectableChannel; | |
| 45 | import java.nio.channels.SelectionKey; | |
| 46 | ||
| 47 | /** | |
| 48 | * Common inteface to be implemented by protocol dependant asynchronous queue | |
| 49 | * writers implementations | |
| 50 | * | |
| 51 | * @author Alexey Stashok | |
| 52 | */ | |
| 53 | public interface AsyncQueueWriter { | |
| 54 | /** | |
| 55 | * Method writes {@link ByteBuffer} to the {@link SelectableChannel} | |
| 56 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 57 | * it tries to write {@link ByteBuffer} to the given | |
| 58 | * {@link SelectableChannel} directly (without putting to the queue). | |
| 59 | * If associated write queue is not empty or after direct writing | |
| 60 | * {@link ByteBuffer} still has ready data to be written - | |
| 61 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 62 | * and {@link SelectableChannel} will be registered on | |
| 63 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 64 | * If an exception occurs, during direct writing - it will be propagated | |
| 65 | * to the caller directly, otherwise it will be just logged by | |
| 66 | * Grizzly framework. | |
| 67 | * | |
| 68 | * @param key {@link SelectionKey} associated with | |
| 69 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 70 | * should be written to | |
| 71 | * @param buffer {@link ByteBuffer} | |
| 72 | * @throws java.io.IOException | |
| 73 | */ | |
| 74 | public void write(SelectionKey key, ByteBuffer buffer) throws IOException; | |
| 75 | ||
| 76 | /** | |
| 77 | * Method writes {@link ByteBuffer} to the {@link SelectableChannel} | |
| 78 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 79 | * it tries to write {@link ByteBuffer} to the given | |
| 80 | * {@link SelectableChannel} directly (without putting to the queue). | |
| 81 | * If associated write queue is not empty or after direct writing | |
| 82 | * {@link ByteBuffer} still has ready data to be written - | |
| 83 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 84 | * and {@link SelectableChannel} will be registered on | |
| 85 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 86 | * If an exception occurs, during direct writing - it will be propagated | |
| 87 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 88 | * added to a writing queue - exception notification will come via | |
| 89 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 90 | * | |
| 91 | * @param key {@link SelectionKey} associated with | |
| 92 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 93 | * should be written to | |
| 94 | * @param buffer {@link ByteBuffer} | |
| 95 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 96 | * which will get notified, when | |
| 97 | * {@link ByteBuffer} will be completely written | |
| 98 | * @throws java.io.IOException | |
| 99 | */ | |
| 100 | public void write(SelectionKey key, ByteBuffer buffer, | |
| 101 | AsyncWriteCallbackHandler callbackHandler) throws IOException; | |
| 102 | ||
| 103 | /** | |
| 104 | * Method writes {@link ByteBuffer} to the {@link SelectableChannel} | |
| 105 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 106 | * it tries to write {@link ByteBuffer} to the given | |
| 107 | * {@link SelectableChannel} directly (without putting to the queue). | |
| 108 | * If associated write queue is not empty or after direct writing | |
| 109 | * {@link ByteBuffer} still has ready data to be written - | |
| 110 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 111 | * and {@link SelectableChannel} will be registered on | |
| 112 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 113 | * If an exception occurs, during direct writing - it will be propagated | |
| 114 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 115 | * added to a writing queue - exception notification will come via | |
| 116 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 117 | * Before data will be written on {@link SelectableChannel}, first it | |
| 118 | * will be passed for preprocessing to <code>AsyncQueueDataProcessor</code>, | |
| 119 | * and then preprocessor result data | |
| 120 | * (<code>AsyncQueueDataProcessor.getResultByteBuffer()</code>) will be | |
| 121 | * written on the {@link SelectableChannel}. | |
| 122 | * | |
| 123 | * @param key {@link SelectionKey} associated with | |
| 124 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 125 | * should be written to | |
| 126 | * @param buffer {@link ByteBuffer} | |
| 127 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 128 | * which will get notified, when | |
| 129 | * {@link ByteBuffer} will be completely written | |
| 130 | * @param writePreProcessor <code>AsyncQueueDataProcessor</code>, which | |
| 131 | * will perform data processing, before it will be | |
| 132 | * written on {@link SelectableChannel} | |
| 133 | * @throws java.io.IOException | |
| 134 | */ | |
| 135 | public void write(SelectionKey key, ByteBuffer buffer, | |
| 136 | AsyncWriteCallbackHandler callbackHandler, | |
| 137 | AsyncQueueDataProcessor writePreProcessor) throws IOException; | |
| 138 | ||
| 139 | /** | |
| 140 | * Method writes {@link ByteBuffer} to the {@link SelectableChannel} | |
| 141 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 142 | * it tries to write {@link ByteBuffer} to the given | |
| 143 | * {@link SelectableChannel} directly (without putting to the queue). | |
| 144 | * If associated write queue is not empty or after direct writing | |
| 145 | * {@link ByteBuffer} still has ready data to be written - | |
| 146 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 147 | * and {@link SelectableChannel} will be registered on | |
| 148 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 149 | * If an exception occurs, during direct writing - it will be propagated | |
| 150 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 151 | * added to a writing queue - exception notification will come via | |
| 152 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 153 | * Before data will be written on {@link SelectableChannel}, first it | |
| 154 | * will be passed for preprocessing to <code>AsyncQueueDataProcessor</code>, | |
| 155 | * and then preprocessor result data | |
| 156 | * (<code>AsyncQueueDataProcessor.getResultByteBuffer()</code>) will be | |
| 157 | * written on the {@link SelectableChannel}. | |
| 158 | * | |
| 159 | * @param key {@link SelectionKey} associated with | |
| 160 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 161 | * should be written to | |
| 162 | * @param buffer {@link ByteBuffer} | |
| 163 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 164 | * which will get notified, when | |
| 165 | * {@link ByteBuffer} will be completely written | |
| 166 | * @param writePreProcessor <code>AsyncQueueDataProcessor</code>, which | |
| 167 | * will perform data processing, before it will be | |
| 168 | * written on {@link SelectableChannel} | |
| 169 | * @param isCloneByteBuffer if true - {@link AsyncQueueWriter} will | |
| 170 | * clone given | |
| 171 | * {@link ByteBuffer} before puting it to the | |
| 172 | * {@link AsyncQueue} | |
| 173 | * @throws java.io.IOException | |
| 174 | */ | |
| 175 | public void write(SelectionKey key, ByteBuffer buffer, | |
| 176 | AsyncWriteCallbackHandler callbackHandler, | |
| 177 | AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) | |
| 178 | throws IOException; | |
| 179 | ||
| 180 | /** | |
| 181 | * Method sends {@link ByteBuffer} to the {@link SocketAddress} | |
| 182 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 183 | * it tries to write {@link ByteBuffer} to the given | |
| 184 | * {@link SocketAddress} directly (without putting to the queue). | |
| 185 | * If associated write queue is not empty or after direct writing | |
| 186 | * {@link ByteBuffer} still has ready data to be written - | |
| 187 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 188 | * and {@link SelectableChannel} will be registered on | |
| 189 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 190 | * If an exception occurs, during direct writing - it will be propagated | |
| 191 | * to the caller directly, otherwise it will be just logged by | |
| 192 | * Grizzly framework. | |
| 193 | * | |
| 194 | * @param key {@link SelectionKey} associated with | |
| 195 | * {@link SelectableChannel}, which will be used to | |
| 196 | * send{@link ByteBuffer} to | |
| 197 | * @param dstAddress destination address {@link ByteBuffer} will be sent to | |
| 198 | * @param buffer {@link ByteBuffer} | |
| 199 | * @throws java.io.IOException | |
| 200 | */ | |
| 201 | public void write(SelectionKey key, SocketAddress dstAddress, | |
| 202 | ByteBuffer buffer) throws IOException; | |
| 203 | ||
| 204 | /** | |
| 205 | * Method sends {@link ByteBuffer} to the {@link SocketAddress} | |
| 206 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 207 | * it tries to write {@link ByteBuffer} to the given | |
| 208 | * {@link SocketAddress} directly (without putting to the queue). | |
| 209 | * If associated write queue is not empty or after direct writing | |
| 210 | * {@link ByteBuffer} still has ready data to be written - | |
| 211 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 212 | * and {@link SelectableChannel} will be registered on | |
| 213 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 214 | * If an exception occurs, during direct writing - it will be propagated | |
| 215 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 216 | * added to a writing queue - exception notification will come via | |
| 217 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 218 | * | |
| 219 | * @param key {@link SelectionKey} associated with | |
| 220 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 221 | * should be written to | |
| 222 | * @param dstAddress destination address {@link ByteBuffer} will be sent to | |
| 223 | * @param buffer {@link ByteBuffer} | |
| 224 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 225 | * which will get notified, when | |
| 226 | * {@link ByteBuffer} will be completely written | |
| 227 | * @throws java.io.IOException | |
| 228 | */ | |
| 229 | public void write(SelectionKey key, SocketAddress dstAddress, | |
| 230 | ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler) | |
| 231 | throws IOException; | |
| 232 | ||
| 233 | /** | |
| 234 | * Method sends {@link ByteBuffer} to the {@link SocketAddress} | |
| 235 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 236 | * it tries to write {@link ByteBuffer} to the given | |
| 237 | * {@link SocketAddress} directly (without putting to the queue). | |
| 238 | * If associated write queue is not empty or after direct writing | |
| 239 | * {@link ByteBuffer} still has ready data to be written - | |
| 240 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 241 | * and {@link SelectableChannel} will be registered on | |
| 242 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 243 | * If an exception occurs, during direct writing - it will be propagated | |
| 244 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 245 | * added to a writing queue - exception notification will come via | |
| 246 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 247 | * Before data will be written on {@link SelectableChannel}, first it | |
| 248 | * will be passed for preprocessing to <code>AsyncQueueDataProcessor</code>, | |
| 249 | * and then preprocessor result data | |
| 250 | * (<code>AsyncQueueDataProcessor.getResultByteBuffer()</code>) will be | |
| 251 | * written on the {@link SelectableChannel}. | |
| 252 | * | |
| 253 | * @param key {@link SelectionKey} associated with | |
| 254 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 255 | * should be written to | |
| 256 | * @param dstAddress destination address {@link ByteBuffer} will be sent to | |
| 257 | * @param buffer {@link ByteBuffer} | |
| 258 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 259 | * which will get notified, when | |
| 260 | * {@link ByteBuffer} will be completely written | |
| 261 | * @param writePreProcessor <code>AsyncQueueDataProcessor</code>, which | |
| 262 | * will perform data processing, before it will be | |
| 263 | * written on {@link SelectableChannel} | |
| 264 | * @throws java.io.IOException | |
| 265 | */ | |
| 266 | public void write(SelectionKey key, SocketAddress dstAddress, | |
| 267 | ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, | |
| 268 | AsyncQueueDataProcessor writePreProcessor) throws IOException; | |
| 269 | ||
| 270 | /** | |
| 271 | * Method sends {@link ByteBuffer} to the {@link SocketAddress} | |
| 272 | * First, if {@link SelectableChannel} associated write queue is empty - | |
| 273 | * it tries to write {@link ByteBuffer} to the given | |
| 274 | * {@link SocketAddress} directly (without putting to the queue). | |
| 275 | * If associated write queue is not empty or after direct writing | |
| 276 | * {@link ByteBuffer} still has ready data to be written - | |
| 277 | * {@link ByteBuffer} will be added to {@link AsyncQueue} | |
| 278 | * and {@link SelectableChannel} will be registered on | |
| 279 | * {@link SelectorHandler}, waiting for OP_WRITE event. | |
| 280 | * If an exception occurs, during direct writing - it will be propagated | |
| 281 | * to the caller directly, otherwise, if the {@link ByteBuffer} is | |
| 282 | * added to a writing queue - exception notification will come via | |
| 283 | * <code>AsyncWriteCallbackHandler.onIOException()</code> | |
| 284 | * Before data will be written on {@link SelectableChannel}, first it | |
| 285 | * will be passed for preprocessing to <code>AsyncQueueDataProcessor</code>, | |
| 286 | * and then preprocessor result data | |
| 287 | * (<code>AsyncQueueDataProcessor.getResultByteBuffer()</code>) will be | |
| 288 | * written on the {@link SelectableChannel}. | |
| 289 | * | |
| 290 | * @param key {@link SelectionKey} associated with | |
| 291 | * {@link SelectableChannel} {@link ByteBuffer} | |
| 292 | * should be written to | |
| 293 | * @param dstAddress destination address {@link ByteBuffer} will be sent to | |
| 294 | * @param buffer {@link ByteBuffer} | |
| 295 | * @param callbackHandler {@link AsyncWriteCallbackHandler}, | |
| 296 | * which will get notified, when | |
| 297 | * {@link ByteBuffer} will be completely written | |
| 298 | * @param writePreProcessor <code>AsyncQueueDataProcessor</code>, which | |
| 299 | * will perform data processing, before it will be | |
| 300 | * written on {@link SelectableChannel} | |
| 301 | * @param isCloneByteBuffer if true - {@link AsyncQueueWriter} will | |
| 302 | * clone given | |
| 303 | * {@link ByteBuffer} before puting it to the | |
| 304 | * {@link AsyncQueue} | |
| 305 | * @throws java.io.IOException | |
| 306 | */ | |
| 307 | public void write(SelectionKey key, SocketAddress dstAddress, | |
| 308 | ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, | |
| 309 | AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) | |
| 310 | throws IOException; | |
| 311 | ||
| 312 | /** | |
| 313 | * Checks whether there is any data in {@link AsyncQueue} ready | |
| 314 | * to be written to the {@link SelectableChannel}, associated with the | |
| 315 | * given {@link SelectionKey} | |
| 316 | * | |
| 317 | * @param key {@link SelectionKey} associated with {@link SelectableChannel} | |
| 318 | * @return true, if there is ready data. False otherwise. | |
| 319 | */ | |
| 320 | public boolean hasReadyAsyncWriteData(SelectionKey key); | |
| 321 | ||
| 322 | /** | |
| 323 | * Callback method, which should be called by {@link SelectorHandler} to | |
| 324 | * notify, that {@link SelectableChannel}, associated with the given | |
| 325 | * {@link SelectionKey} is ready to transmit data. | |
| 326 | * | |
| 327 | * @param key {@link SelectionKey} associated with {@link SelectableChannel} | |
| 328 | * @throws java.io.IOException | |
| 329 | */ | |
| 330 | public void onWrite(SelectionKey key) throws IOException; | |
| 331 | ||
| 332 | /** | |
| 333 | * Callback method, which should be called by {@link SelectorHandler} to | |
| 334 | * notify, that given {@link SelectableChannel} is going to be closed, so | |
| 335 | * related {@link SelectableChannel} data could be released from | |
| 336 | * {@link AsyncQueue} | |
| 337 | * | |
| 338 | * @param {@link SelectableChannel} | |
| 339 | * @throws java.io.IOException | |
| 340 | */ | |
| 341 | public void onClose(SelectableChannel channel); | |
| 342 | ||
| 343 | /** | |
| 344 | * Close {@link AsyncQueueWriter} and release its resources | |
| 345 | */ | |
| 346 | public void close(); | |
| 347 | } |