01: /*
02: * Copyright (C) The Spice Group. All rights reserved.
03: *
04: * This software is published under the terms of the Spice
05: * Software License version 1.1, a copy of which has been included
06: * with this distribution in the LICENSE.txt file.
07: */
08: package org.codehaus.spice.netserve.connection.handlers;
09:
10: import java.net.Socket;
11:
12: /**
13: *
14: * @author Peter Donald
15: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
16: */
17: class DelayingRequestHandler extends AbstractRequestHandler {
18: private final long m_delay;
19: private final boolean m_wakeupOnInterrupt;
20:
21: private boolean m_exited;
22: private boolean m_exitDueToInterrupt;
23:
24: DelayingRequestHandler(final long delay,
25: final boolean wakeupOnInterrupt) {
26: m_delay = delay;
27: m_wakeupOnInterrupt = wakeupOnInterrupt;
28: }
29:
30: protected void doPerformRequest(Socket socket) throws Exception {
31: final int code = System.identityHashCode(this );
32: final String prefix = "Handler(" + code + ") ";
33: System.out.println(prefix + "Started");
34: final long then = System.currentTimeMillis() + m_delay;
35: while (System.currentTimeMillis() < then) {
36: final long rest = then - System.currentTimeMillis();
37: System.out.println(prefix + "Sleeping for " + rest);
38: try {
39: Thread.sleep(rest);
40: } catch (InterruptedException e) {
41: System.out.println(prefix + "Woken up");
42: if (m_wakeupOnInterrupt) {
43: m_exitDueToInterrupt = true;
44: break;
45: }
46: }
47: }
48: m_exited = true;
49: System.out.println(prefix + "Returning");
50: }
51:
52: boolean isExitDueToInterrupt() {
53: return m_exitDueToInterrupt;
54: }
55:
56: boolean isExited() {
57: return m_exited;
58: }
59: }
|