1   /*
2    * Copyright (C) The Spice Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.spice.netserve.connection.handlers;
9   
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
18      extends AbstractRequestHandler
19  {
20      private final long m_delay;
21      private final boolean m_wakeupOnInterrupt;
22  
23      private boolean m_exited;
24      private boolean m_exitDueToInterrupt;
25  
26      DelayingRequestHandler( final long delay,
27                              final boolean wakeupOnInterrupt )
28      {
29          m_delay = delay;
30          m_wakeupOnInterrupt = wakeupOnInterrupt;
31      }
32  
33      protected void doPerformRequest( Socket socket )
34          throws Exception
35      {
36          final int code = System.identityHashCode( this );
37          final String prefix = "Handler(" + code + ") ";
38          System.out.println( prefix + "Started" );
39          final long then = System.currentTimeMillis() + m_delay;
40          while( System.currentTimeMillis() < then )
41          {
42              final long rest = then - System.currentTimeMillis();
43              System.out.println( prefix + "Sleeping for " + rest );
44              try
45              {
46                  Thread.sleep( rest );
47              }
48              catch( InterruptedException e )
49              {
50                  System.out.println( prefix + "Woken up" );
51                  if( m_wakeupOnInterrupt )
52                  {
53                      m_exitDueToInterrupt = true;
54                      break;
55                  }
56              }
57          }
58          m_exited = true;
59          System.out.println( prefix + "Returning" );
60      }
61  
62      boolean isExitDueToInterrupt()
63      {
64          return m_exitDueToInterrupt;
65      }
66  
67      boolean isExited()
68      {
69          return m_exited;
70      }
71  }