1   // ========================================================================
2   // Copyright 2007 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 dojox.cometd.demo;
16  
17  
18  import java.io.File;
19  import java.util.Set;
20  
21  import org.mortbay.cometd.AbstractBayeux;
22  import org.mortbay.cometd.ClientImpl;
23  import org.mortbay.cometd.SuspendingCometdServlet;
24  import org.mortbay.cometd.continuation.ContinuationClient;
25  import org.mortbay.cometd.continuation.ContinuationCometdServlet;
26  import org.mortbay.cometd.ext.TimesyncExtension;
27  import org.mortbay.jetty.Server;
28  import org.mortbay.jetty.bio.SocketConnector;
29  import org.mortbay.jetty.handler.ContextHandlerCollection;
30  import org.mortbay.jetty.handler.MovedContextHandler;
31  import org.mortbay.jetty.nio.SelectChannelConnector;
32  import org.mortbay.jetty.security.SslSocketConnector;
33  import org.mortbay.jetty.servlet.Context;
34  import org.mortbay.jetty.servlet.ServletHolder;
35  
36  import dojox.cometd.Message;
37  
38  
39  /* ------------------------------------------------------------ */
40  /** Main class for cometd demo.
41   * 
42   * This is of use when running demo in a terracotta cluster
43   * 
44   * @author gregw
45   *
46   */
47  public class CometdDemo
48  {
49      private static int _testHandshakeFailure;
50      
51      /* ------------------------------------------------------------ */
52      /**
53       * @param args
54       */
55      public static void main(String[] args) throws Exception
56      {
57          int port = args.length==0?8080:Integer.parseInt(args[0]);
58       
59          String base="../../../..";
60          
61          // Manually contruct context to avoid hassles with webapp classloaders for now.
62          Server server = new Server();
63          SelectChannelConnector connector=new SelectChannelConnector();
64          connector.setPort(port);
65          server.addConnector(connector);
66          SocketConnector bconnector=new SocketConnector();
67          bconnector.setPort(port+1);
68          server.addConnector(bconnector);
69          
70          
71          SslSocketConnector connector2=new SslSocketConnector();
72          connector2.setPort(port-80+443);
73          connector2.setKeystore(base+"/etc/keystore");
74          connector2.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
75          connector2.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
76          connector2.setTruststore(base+"/etc/keystore");
77          connector2.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
78          server.addConnector(connector2);  
79  
80          ContextHandlerCollection contexts = new ContextHandlerCollection();
81          server.setHandler(contexts);
82          
83          MovedContextHandler moved = new MovedContextHandler(contexts,"/","/cometd");
84          moved.setDiscardPathInfo(true);
85          
86          Context context = new Context(contexts,"/cometd",Context.NO_SECURITY|Context.SESSIONS);
87  	context.setResourceBase("./src/main/webapp");
88          
89          // Demo bayeux session manager
90          // context.getSessionHandler().setSessionManager(new BayeuxSessionManager());
91          // context.addServlet(com.acme.SessionDump.class,"/session");
92          // context.addServlet(com.acme.Dump.class,"/dump");
93          
94          
95          // Cometd servlet
96          SuspendingCometdServlet cometd_servlet=new SuspendingCometdServlet();
97          ServletHolder cometd_holder = new ServletHolder(cometd_servlet);
98          cometd_holder.setInitParameter("filters","/WEB-INF/filters.json");
99          cometd_holder.setInitParameter("timeout","180000");
100         cometd_holder.setInitParameter("interval","0");
101         cometd_holder.setInitParameter("maxInterval","10000");
102         cometd_holder.setInitParameter("multiFrameInterval","1500");
103         cometd_holder.setInitParameter("directDeliver","true");
104         cometd_holder.setInitParameter("asyncDeliver","false");
105         cometd_holder.setInitParameter("logLevel","1");
106         
107         context.addServlet(cometd_holder, "/cometd/*");
108         context.addServlet("org.mortbay.jetty.servlet.DefaultServlet", "/");
109         context.addEventListener(new BayeuxStartupListener());
110         
111         server.start();
112         
113         AbstractBayeux bayeux = cometd_servlet.getBayeux();
114         bayeux.addExtension(new TimesyncExtension());
115         
116         bayeux.setSecurityPolicy(new AbstractBayeux.DefaultPolicy(){
117             public boolean canHandshake(Message message)
118             {
119                 if (_testHandshakeFailure<0)
120                 {
121                     _testHandshakeFailure++;
122                     return false;
123                 }
124                 return true;
125             }
126             
127         });
128         
129         while (true)
130         {
131             Thread.sleep(2000);
132             Set<String> ids=bayeux.getClientIDs();
133             ClientImpl[] clients=new ClientImpl[ids.size()];
134             long[] last=new long[ids.size()];
135             int i=0;
136             long now = System.currentTimeMillis();
137             for (String id : ids)
138             {
139                 clients[i]=(ClientImpl)bayeux.getClient(id);
140                 
141                 if (clients[i] instanceof ContinuationClient)
142                 {
143                     ContinuationClient cc= (ContinuationClient)clients[i];
144 
145                     last[i]=now - cc.lastAccessed();
146                     if (cc.hasMessages() && cc.getContinuation()==null && !cc._timeout.isScheduled() && last[i]>1000)
147                         System.err.println("??? "+cc+" last="+last[i]);
148                 }
149                 
150                 i++;
151             }
152             
153             i=0;
154             
155         }
156         
157     }
158 
159 }