1 // ========================================================================
2 // Copyright 2006 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //========================================================================
14
15 package org.mortbay.cometd.continuation;
16
17 import java.util.Timer;
18 import java.util.TimerTask;
19
20 import javax.servlet.ServletContext;
21
22 import org.mortbay.cometd.AbstractBayeux;
23 import org.mortbay.cometd.ClientImpl;
24 import org.mortbay.thread.Timeout;
25 import org.mortbay.thread.Timeout.Task;
26
27 /* ------------------------------------------------------------ */
28 /**
29 * Extension of Bayeux that uses {@link ContinuationClient}s.
30 * @author gregw
31 * @deprecated use {@link org.mortbay.cometd.SuspendingBayeux}
32 *
33 */
34 public class ContinuationBayeux extends AbstractBayeux
35 {
36 private static int __id;
37 private transient Timer _tick;
38 private transient Timeout _timeout;
39 private long _now;
40
41
42 /* ------------------------------------------------------------ */
43 /* (non-Javadoc)
44 * @see org.mortbay.cometd.AbstractBayeux#newClient(java.lang.String, dojox.io.cometd.Destination)
45 */
46 public ClientImpl newRemoteClient()
47 {
48 return new ContinuationClient(this);
49 }
50
51 /* ------------------------------------------------------------ */
52 /* (non-Javadoc)
53 * @see org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext)
54 */
55 protected void initialize(ServletContext context)
56 {
57 super.initialize(context);
58
59 _tick=new Timer("ContinuationBayeux-"+__id++, true);
60 _timeout=new Timeout();
61 _timeout.setDuration(getMaxInterval());
62
63 _tick.schedule(new TimerTask()
64 {
65 public void run()
66 {
67 _timeout.setNow();
68 _now=_timeout.getNow();
69 _timeout.tick();
70 }
71 },500L,500L);
72 }
73
74 /* ------------------------------------------------------------ */
75 long getNow()
76 {
77 return _now;
78 }
79
80 /* ------------------------------------------------------------ */
81 public void setMaxInterval(long ms)
82 {
83 _timeout.setDuration(ms);
84 super.setMaxInterval(ms);
85 }
86
87 /* ------------------------------------------------------------ */
88 /* (non-Javadoc)
89 * @see org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext)
90 */
91 public void destroy()
92 {
93 _tick.cancel();
94 }
95
96 /* ------------------------------------------------------------ */
97 void startTimeout(Task timeout,long delay)
98 {
99 if (delay==0)
100 _timeout.schedule(timeout);
101 else
102 _timeout.schedule(timeout,delay);
103 }
104
105 /* ------------------------------------------------------------ */
106 public void cancelTimeout(Task timeout)
107 {
108 if (timeout!=null)
109 timeout.cancel();
110 }
111
112 }