| 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.suspendable; |
| 40 | |
|
| 41 | |
import com.sun.grizzly.Context; |
| 42 | |
import com.sun.grizzly.PipelineFullException; |
| 43 | |
import com.sun.grizzly.ProtocolFilter; |
| 44 | |
import com.sun.grizzly.Context.KeyRegistrationState; |
| 45 | |
import com.sun.grizzly.Controller; |
| 46 | |
import com.sun.grizzly.DefaultProtocolChain; |
| 47 | |
import com.sun.grizzly.SelectorHandler; |
| 48 | |
import com.sun.grizzly.util.ThreadAttachment; |
| 49 | |
import com.sun.grizzly.util.ThreadAttachment.Mode; |
| 50 | |
import com.sun.grizzly.util.Utils; |
| 51 | |
import com.sun.grizzly.util.WorkerThread; |
| 52 | |
import java.io.IOException; |
| 53 | |
import java.nio.ByteBuffer; |
| 54 | |
import java.nio.channels.SelectionKey; |
| 55 | |
import java.util.Iterator; |
| 56 | |
import java.util.concurrent.ConcurrentHashMap; |
| 57 | |
import java.util.logging.Level; |
| 58 | |
import java.util.logging.Logger; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public class SuspendableFilter<T> implements ProtocolFilter { |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | 3 | public enum Suspend {BEFORE,AFTER} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | 6 | private ConcurrentHashMap<byte[],SuspendableHandlerWrapper<? extends T>> suspendCandidates |
| 148 | |
= new ConcurrentHashMap<byte[],SuspendableHandlerWrapper<? extends T>>(); |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | 6 | protected ConcurrentHashMap<SelectionKey,KeyHandler> suspendedKeys |
| 155 | |
= new ConcurrentHashMap<SelectionKey,KeyHandler>(); |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | 1 | private static SuspendableMonitor suspendableMonitor = new SuspendableMonitor(); |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | 1 | private static Logger logger = Controller.logger(); |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
private Controller controller; |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
private DefaultProtocolChain protocolChain; |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | 6 | private int nextFilterPosition = 2; |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | 6 | public SuspendableFilter(){ |
| 190 | 6 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | 0 | public SuspendableFilter(int nextFilterPosition){ |
| 199 | 0 | this.nextFilterPosition = nextFilterPosition; |
| 200 | 0 | } |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public Suspendable suspend(String match){ |
| 214 | 0 | return suspend(match, 60000, null, null); |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
public Suspendable suspend(String match,long expireTime, |
| 231 | |
T attachement,SuspendableHandler<? extends T> sh){ |
| 232 | 0 | return suspend(match,expireTime,attachement,sh,Suspend.AFTER); |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
public Suspendable suspend(String match,long expireTime, |
| 250 | |
T attachement,SuspendableHandler<? extends T> sh, Suspend suspendWhen){ |
| 251 | 6 | Suspendable s = new Suspendable(this); |
| 252 | 6 | suspendCandidates.put(match.getBytes(), new SuspendableHandlerWrapper( |
| 253 | |
sh,attachement, expireTime,s,suspendWhen)); |
| 254 | 6 | return s; |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
public boolean execute(Context ctx) throws IOException { |
| 271 | 9 | WorkerThread wt = (WorkerThread)Thread.currentThread(); |
| 272 | 9 | ByteBuffer bb = wt.getByteBuffer(); |
| 273 | 9 | controller = ctx.getController(); |
| 274 | |
|
| 275 | 9 | if (ctx.getProtocol() == Controller.Protocol.TCP){ |
| 276 | 9 | ctx.getSelectionKey().attach(null); |
| 277 | |
} else { |
| 278 | 0 | wt.getAttachment().setTimeout(null); |
| 279 | |
} |
| 280 | |
|
| 281 | 9 | if (protocolChain == null){ |
| 282 | 6 | if (ctx.getProtocolChain() instanceof DefaultProtocolChain){ |
| 283 | 6 | protocolChain = (DefaultProtocolChain)ctx.getProtocolChain(); |
| 284 | |
} else { |
| 285 | 0 | throw new IllegalStateException("SuspendableFilter cannot be " + |
| 286 | |
"used without the DefaultProtocolChain"); |
| 287 | |
} |
| 288 | |
} |
| 289 | |
|
| 290 | 9 | log("Trying to match " + ctx.getSelectionKey()); |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | 9 | SuspendableHandlerWrapper<? extends T> sh = null; |
| 295 | 9 | Iterator<byte[]> iterator = suspendCandidates.keySet().iterator(); |
| 296 | 9 | byte[] matchBytes = null; |
| 297 | 9 | while(iterator.hasNext()){ |
| 298 | 9 | matchBytes = iterator.next(); |
| 299 | 9 | if (Utils.findBytes(bb,matchBytes) > -1){ |
| 300 | 9 | log("Find match: " + (new String(matchBytes)) |
| 301 | |
+ " Suspending: " + ctx.getSelectionKey()); |
| 302 | 9 | sh = suspendCandidates.get(matchBytes); |
| 303 | 9 | break; |
| 304 | |
} |
| 305 | |
} |
| 306 | |
|
| 307 | 9 | if (sh != null){ |
| 308 | 9 | KeyHandler kh = new KeyHandler(); |
| 309 | 9 | kh.setSuspendableHandler(sh); |
| 310 | 9 | suspendedKeys.put(ctx.getSelectionKey(),kh); |
| 311 | 9 | if (sh.getSuspendWhen() == Suspend.BEFORE){ |
| 312 | 4 | suspend(ctx,true); |
| 313 | 4 | log("-----> " + ctx.getKeyRegistrationState()); |
| 314 | 4 | return false; |
| 315 | |
} |
| 316 | |
} |
| 317 | |
|
| 318 | 5 | return true; |
| 319 | |
} |
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
public boolean postExecute(Context ctx) throws IOException { |
| 332 | 12 | log("<----- " + ctx.getKeyRegistrationState()); |
| 333 | |
|
| 334 | 12 | if (!suspendedKeys.isEmpty() |
| 335 | |
&& ctx.getKeyRegistrationState() == KeyRegistrationState.REGISTER){ |
| 336 | 5 | suspend(ctx,false); |
| 337 | 5 | return false; |
| 338 | |
} |
| 339 | 7 | return true; |
| 340 | |
} |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
private void suspend(Context ctx, boolean incomingRequest){ |
| 352 | |
try { |
| 353 | 9 | SelectionKey key = ctx.getSelectionKey(); |
| 354 | 9 | KeyHandler kh = suspendedKeys.get(key); |
| 355 | |
|
| 356 | 9 | SuspendableHandlerWrapper<? extends T> sh = null; |
| 357 | 9 | if (kh != null){ |
| 358 | 9 | sh = kh.getSuspendableHandler(); |
| 359 | |
} else { |
| 360 | 0 | kh = new KeyHandler(); |
| 361 | |
} |
| 362 | |
|
| 363 | 9 | if (kh != null && !incomingRequest){ |
| 364 | 5 | if (sh.getSuspendWhen() == Suspend.BEFORE){ |
| 365 | 0 | return; |
| 366 | |
} |
| 367 | |
} |
| 368 | |
|
| 369 | |
|
| 370 | 9 | if (sh == null) { |
| 371 | |
|
| 372 | 0 | sh = new SuspendableHandlerWrapper(new SuspendableHandler() { |
| 373 | |
|
| 374 | |
public void interupted(Object attachment) { |
| 375 | 0 | } |
| 376 | |
|
| 377 | |
public void expired(Object attachment) { |
| 378 | 0 | } |
| 379 | |
|
| 380 | |
public void resumed(Object attachment) { |
| 381 | 0 | } |
| 382 | |
}, null, 30000, new Suspendable(this), Suspend.AFTER); |
| 383 | |
} |
| 384 | 9 | sh.setSuspendableFilter(this); |
| 385 | 9 | sh.suspendable.setKey(key); |
| 386 | 9 | sh.setSelectorHandler(ctx.getSelectorHandler()); |
| 387 | |
|
| 388 | 9 | kh.setSuspendableHandler(sh); |
| 389 | 9 | kh.setKey(key); |
| 390 | 9 | WorkerThread workerThread = (WorkerThread) Thread.currentThread(); |
| 391 | 9 | ThreadAttachment attachment = workerThread.getAttachment(); |
| 392 | 9 | attachment.setMode(Mode.STORE_ALL); |
| 393 | 9 | kh.setThreadAttachment(workerThread.detach()); |
| 394 | 9 | ctx.setKeyRegistrationState(KeyRegistrationState.NONE); |
| 395 | |
|
| 396 | 9 | suspendableMonitor.suspend(kh); |
| 397 | 0 | } catch (Throwable ex) { |
| 398 | 0 | if (logger.isLoggable(Level.FINE)){ |
| 399 | 0 | logger.log(Level.FINE,"suspend",ex); |
| 400 | |
} |
| 401 | 9 | } |
| 402 | 9 | } |
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
protected boolean resume(SelectionKey key){ |
| 411 | 5 | KeyHandler kh = suspendedKeys.remove(key); |
| 412 | 5 | if (kh.getSuspendableHandler() == null){ |
| 413 | 0 | return false; |
| 414 | |
} |
| 415 | 5 | log("Resuming: " + kh.getSuspendableHandler()); |
| 416 | |
|
| 417 | 5 | kh.getSuspendableHandler().getSuspendableHandler(). |
| 418 | |
resumed(kh.getSuspendableHandler().getAttachment()); |
| 419 | |
|
| 420 | 5 | if (kh.getSuspendableHandler().getSuspendWhen() == Suspend.AFTER){ |
| 421 | 2 | kh.getSuspendableHandler().getSelectorHandler() |
| 422 | |
.register(key.channel(), SelectionKey.OP_READ); |
| 423 | |
} else { |
| 424 | |
try { |
| 425 | 3 | Context ctx = controller.pollContext(key); |
| 426 | 3 | controller.configureContext(ctx, |
| 427 | |
kh.getSuspendableHandler().getSelectorHandler()); |
| 428 | 3 | ctx.execute(SuspendableContextTask.poll( |
| 429 | |
protocolChain,kh.getThreadAttachment(),nextFilterPosition)); |
| 430 | 0 | } catch (PipelineFullException ex) { |
| 431 | 0 | logger.log(Level.SEVERE,"resume exception", ex); |
| 432 | 3 | } |
| 433 | |
} |
| 434 | |
|
| 435 | 5 | return true; |
| 436 | |
} |
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
protected void cancel(SelectionKey key){ |
| 445 | 2 | log("Cancelling: " + key); |
| 446 | 2 | KeyHandler kh = suspendedKeys.remove(key); |
| 447 | 2 | if (kh == null){ |
| 448 | 0 | return; |
| 449 | |
} |
| 450 | |
|
| 451 | 2 | if (kh.getSuspendableHandler() == null){ |
| 452 | 0 | return; |
| 453 | |
} |
| 454 | 2 | kh.getSuspendableHandler().getSuspendableHandler() |
| 455 | |
.interupted(kh.getSuspendableHandler().getAttachment()); |
| 456 | 2 | kh.getSuspendableHandler().getSelectorHandler() |
| 457 | |
.getSelectionKeyHandler().cancel(key); |
| 458 | 2 | kh.setThreadAttachment(null); |
| 459 | 2 | } |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | 9 | protected class SuspendableHandlerWrapper<A>{ |
| 468 | |
public SuspendableHandlerWrapper(SuspendableHandler<A> sh, A attachment, |
| 469 | 6 | long expireTime, Suspendable suspendable, Suspend suspendWhen){ |
| 470 | 6 | this.suspendableHandler = sh; |
| 471 | 6 | this.attachment = attachment; |
| 472 | 6 | this.expireTime = expireTime; |
| 473 | 6 | this.suspendable = suspendable; |
| 474 | 6 | this.suspendWhen = suspendWhen; |
| 475 | 6 | } |
| 476 | |
|
| 477 | |
private SuspendableHandler<A> suspendableHandler; |
| 478 | |
private A attachment; |
| 479 | 6 | private long expireTime = 0L; |
| 480 | |
private SuspendableFilter suspendableFilter; |
| 481 | |
private Suspendable suspendable; |
| 482 | |
private SelectorHandler selectorHandler; |
| 483 | |
private Suspend suspendWhen; |
| 484 | |
|
| 485 | |
protected |
| 486 | |
|
| 487 | |
SuspendableHandler<A> getSuspendableHandler() { |
| 488 | 10 | return suspendableHandler; |
| 489 | |
} |
| 490 | |
|
| 491 | |
protected void setSuspendableHandler(SuspendableHandler<A> suspendableHandler) { |
| 492 | 0 | this.suspendableHandler = suspendableHandler; |
| 493 | 0 | } |
| 494 | |
|
| 495 | |
protected A getAttachment() { |
| 496 | 10 | return attachment; |
| 497 | |
} |
| 498 | |
|
| 499 | |
protected void setAttachment(A attachment) { |
| 500 | 0 | this.attachment = attachment; |
| 501 | 0 | } |
| 502 | |
|
| 503 | |
protected long getExpireTime() { |
| 504 | 341743 | return expireTime; |
| 505 | |
} |
| 506 | |
|
| 507 | |
protected void setExpireTime(long expireTime) { |
| 508 | 0 | this.expireTime = expireTime; |
| 509 | 0 | } |
| 510 | |
|
| 511 | |
protected SuspendableFilter getSuspendableFilter() { |
| 512 | 3 | return suspendableFilter; |
| 513 | |
} |
| 514 | |
|
| 515 | |
protected void setSuspendableFilter(SuspendableFilter suspendableFilter) { |
| 516 | 9 | this.suspendableFilter = suspendableFilter; |
| 517 | 9 | } |
| 518 | |
|
| 519 | |
protected Suspendable getSuspendable() { |
| 520 | 0 | return suspendable; |
| 521 | |
} |
| 522 | |
|
| 523 | |
protected void setSuspendable(Suspendable suspendable) { |
| 524 | 0 | this.suspendable = suspendable; |
| 525 | 0 | } |
| 526 | |
|
| 527 | |
protected SelectorHandler getSelectorHandler() { |
| 528 | 7 | return selectorHandler; |
| 529 | |
} |
| 530 | |
|
| 531 | |
protected void setSelectorHandler(SelectorHandler selectorHandler) { |
| 532 | 9 | this.selectorHandler = selectorHandler; |
| 533 | 9 | } |
| 534 | |
|
| 535 | |
protected Suspend getSuspendWhen() { |
| 536 | 19 | return suspendWhen; |
| 537 | |
} |
| 538 | |
|
| 539 | |
protected void setSuspendWhen(Suspend suspendWhen) { |
| 540 | 0 | this.suspendWhen = suspendWhen; |
| 541 | 0 | } |
| 542 | |
} |
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | 9 | protected class KeyHandler{ |
| 549 | |
private SelectionKey key; |
| 550 | |
private SelectionKey foreignKey; |
| 551 | |
private SuspendableHandlerWrapper handler; |
| 552 | |
private ThreadAttachment threadAttachment; |
| 553 | 9 | private long registrationTime = 0L; |
| 554 | |
|
| 555 | |
protected |
| 556 | |
|
| 557 | |
SelectionKey getKey() { |
| 558 | 21 | return key; |
| 559 | |
} |
| 560 | |
|
| 561 | |
protected void setKey(SelectionKey key) { |
| 562 | 9 | this.key = key; |
| 563 | 9 | } |
| 564 | |
|
| 565 | |
protected SelectionKey getForeignKey() { |
| 566 | 9 | return foreignKey; |
| 567 | |
} |
| 568 | |
|
| 569 | |
protected void setForeignKey(SelectionKey foreignKey) { |
| 570 | 9 | this.foreignKey = foreignKey; |
| 571 | 9 | } |
| 572 | |
|
| 573 | |
protected SuspendableHandlerWrapper getSuspendableHandler() { |
| 574 | 341799 | return handler; |
| 575 | |
} |
| 576 | |
|
| 577 | |
protected void setSuspendableHandler(SuspendableHandlerWrapper sh) { |
| 578 | 18 | this.handler = sh; |
| 579 | 18 | } |
| 580 | |
|
| 581 | |
protected ThreadAttachment getThreadAttachment() { |
| 582 | 3 | return threadAttachment; |
| 583 | |
} |
| 584 | |
|
| 585 | |
protected void setThreadAttachment(ThreadAttachment threadAttachment) { |
| 586 | 11 | this.threadAttachment = threadAttachment; |
| 587 | 11 | } |
| 588 | |
|
| 589 | |
protected long getRegistrationTime() { |
| 590 | 341766 | return registrationTime; |
| 591 | |
} |
| 592 | |
|
| 593 | |
protected void setRegistrationTime(long registrationTime) { |
| 594 | 12 | this.registrationTime = registrationTime; |
| 595 | 12 | } |
| 596 | |
} |
| 597 | |
|
| 598 | |
|
| 599 | |
private void log(String message){ |
| 600 | 41 | if (logger.isLoggable(Level.FINE)) { |
| 601 | 0 | logger.log(Level.FINE, message); |
| 602 | |
} |
| 603 | 41 | } |
| 604 | |
} |