Author: fhanik
Date: Tue Oct 21 11:07:04 2008
New Revision: 706696

URL: http://svn.apache.org/viewvc?rev=706696&view=rev
Log:
work on the bayeux samples

Added:
    tomcat/trunk/test/org/apache/cometd/bayeux/samples/BayeuxStockTicker.java
    tomcat/trunk/webapps/cometd/examples/simplechat/ticker.html
Modified:
    tomcat/trunk/java/org/apache/tomcat/bayeux/BayeuxServlet.java
    tomcat/trunk/java/org/apache/tomcat/bayeux/ClientImpl.java
    tomcat/trunk/java/org/apache/tomcat/bayeux/RequestBase.java
    tomcat/trunk/test/org/apache/cometd/bayeux/samples/EchoChatClient.java
    tomcat/trunk/webapps/cometd/WEB-INF/web.xml
    tomcat/trunk/webapps/cometd/index.html

Modified: tomcat/trunk/java/org/apache/tomcat/bayeux/BayeuxServlet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/bayeux/BayeuxServlet.java?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/bayeux/BayeuxServlet.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/bayeux/BayeuxServlet.java Tue Oct 21 
11:07:04 2008
@@ -92,7 +92,7 @@
     
     protected int getReconnectInterval() {
         String rs = servletConfig.getInitParameter("reconnectInterval");
-        int rct = 5000; //5 seconds
+        int rct = 1000; //1 seconds
         try {
             rct = Integer.parseInt(rs);
         }catch (NumberFormatException nfe) {

Modified: tomcat/trunk/java/org/apache/tomcat/bayeux/ClientImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/bayeux/ClientImpl.java?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/bayeux/ClientImpl.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/bayeux/ClientImpl.java Tue Oct 21 
11:07:04 2008
@@ -115,9 +115,10 @@
             //local clients must have a listener
             ArrayList<Message> list = new ArrayList<Message>();
             for (int i=0; msgs!=null && i<msgs.length; i++) {
+                //dont deliver to ourselves
                 if (this!=msgs[i].getClient()) list.add(msgs[i]);
             }
-            if (getListener() != null) {
+            if (getListener() != null && list.size()>0) {
                 getListener().deliver(list.toArray(new Message[0]));
             }
         } else {

Modified: tomcat/trunk/java/org/apache/tomcat/bayeux/RequestBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/bayeux/RequestBase.java?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/bayeux/RequestBase.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/bayeux/RequestBase.java Tue Oct 21 
11:07:04 2008
@@ -76,7 +76,7 @@
     
     protected static Log log = LogFactory.getLog(RequestBase.class);
     
-    protected int reconnectInterval;
+    protected int reconnectInterval = 1000;
     
     protected RequestBase(TomcatBayeux tb, CometEvent event, JSONObject jsReq) 
throws JSONException {
         this.tomcatBayeux = tb;

Added: tomcat/trunk/test/org/apache/cometd/bayeux/samples/BayeuxStockTicker.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/cometd/bayeux/samples/BayeuxStockTicker.java?rev=706696&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/cometd/bayeux/samples/BayeuxStockTicker.java 
(added)
+++ tomcat/trunk/test/org/apache/cometd/bayeux/samples/BayeuxStockTicker.java 
Tue Oct 21 11:07:04 2008
@@ -0,0 +1,215 @@
+package org.apache.cometd.bayeux.samples;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletContextAttributeListener;
+import javax.servlet.ServletContextAttributeEvent;
+import org.apache.cometd.bayeux.Bayeux;
+
+import java.text.DecimalFormat;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.cometd.bayeux.Client;
+import org.apache.cometd.bayeux.Listener;
+import org.apache.cometd.bayeux.Message;
+import org.apache.cometd.bayeux.Channel;
+
+public class BayeuxStockTicker implements ServletContextListener,
+        ServletContextAttributeListener, Listener {
+
+    static AtomicInteger counter = new AtomicInteger(0);
+    protected int id;
+    protected Bayeux b;
+    protected Client c;
+    protected boolean alive = true;
+    protected boolean initialized = false;
+    protected TickerThread tt = new TickerThread();
+
+    public BayeuxStockTicker() {
+        id = counter.incrementAndGet();
+        System.out.println("new listener created with id:" + id);
+    }
+
+    public void contextDestroyed(ServletContextEvent servletContextEvent) {
+        alive = false;
+        tt.run = false;
+        tt.interrupt();
+    }
+
+    public void contextInitialized(ServletContextEvent servletContextEvent) {
+    }
+
+    public void attributeAdded(ServletContextAttributeEvent scae) {
+        if (scae.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX)) {
+            if (initialized) return;
+            initialized = true;
+            System.out.println("Starting stock ticker server client!");
+            b = (Bayeux) scae.getValue();
+            c = b.newClient("stock-ticker-", this);
+            tt.start();
+        }
+    }
+
+    public void attributeRemoved(ServletContextAttributeEvent scae) {
+        if (scae.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX)) {
+            initialized = false;
+            b = (Bayeux) scae.getValue();
+            List<Channel> chs = b.getChannels();
+            for (Channel ch : chs) {
+                ch.unsubscribe(c);
+            }
+        }
+    }
+
+    public void attributeReplaced(
+            ServletContextAttributeEvent servletContextAttributeEvent) {
+    }
+
+    public void removed(boolean timeout) {
+        System.out.println("Client removed.");
+    }
+
+    public void deliver(Message[] msgs) {
+        for (int i = 0; msgs != null && i < msgs.length; i++) {
+            Message msg = msgs[i];
+            System.out.println("[stock ticker server client ]received 
message:" + msg);
+        }
+    }
+
+    public class TickerThread extends Thread {
+        public boolean run = true;
+
+        public TickerThread() {
+            setName("Ticker Thread");
+        }
+
+        public void run() {
+            try {
+                
+                Stock[] stocks = new Stock[] { 
+                        new Stock("GOOG", 435.43),
+                        new Stock("YHOO", 27.88), 
+                        new Stock("SPRG", 1015.55), };
+                for (Stock s : stocks) {
+                    Channel ch = b.getChannel("/stock/"+s.getSymbol(), true);
+                    ch.subscribe(c);
+                    
+                }
+                Random r = new Random(System.currentTimeMillis());
+                while (run) {
+                    for (int j = 0; j < 1; j++) {
+                        int i = r.nextInt() % 3;
+                        if (i < 0)
+                            i = i * (-1);
+                        Stock stock = stocks[i];
+                        double change = r.nextDouble();
+                        boolean plus = r.nextBoolean();
+                        if (plus) {
+                            stock.setValue(stock.getValue() + change);
+                        } else {
+                            stock.setValue(stock.getValue() - change);
+                        }
+                        Channel ch = b.getChannel("/stock/"+stock.getSymbol(), 
true);
+                        Message m = b.newMessage(c);
+                        m.put("stock", stock.toString());
+                        m.put("symbol", stock.getSymbol());
+                        m.put("price", stock.getValueAsString());
+                        m.put("change", stock.getLastChangeAsString());
+                        ch.publish(m);
+                        System.out.println("Stock: "+stock.getSymbol()+" 
Price: "+stock.getValueAsString()+" Change: "+stock.getLastChangeAsString());
+                    }
+                    Thread.sleep(850);
+                }
+            } catch (InterruptedException ix) {
+
+            } catch (Exception x) {
+                x.printStackTrace();
+            }
+        }
+    }
+
+    public static class Stock {
+        protected static DecimalFormat df = new DecimalFormat("0.00");
+        protected String symbol = "";
+        protected double value = 0.0d;
+        protected double lastchange = 0.0d;
+        protected int cnt = 0;
+
+        public Stock(String symbol, double initvalue) {
+            this.symbol = symbol;
+            this.value = initvalue;
+        }
+
+        public void setCnt(int c) {
+            this.cnt = c;
+        }
+
+        public int getCnt() {
+            return cnt;
+        }
+
+        public String getSymbol() {
+            return symbol;
+        }
+
+        public double getValue() {
+            return value;
+        }
+
+        public void setValue(double value) {
+            double old = this.value;
+            this.value = value;
+            this.lastchange = value - old;
+        }
+
+        public String getValueAsString() {
+            return df.format(value);
+        }
+
+        public double getLastChange() {
+            return this.lastchange;
+        }
+
+        public void setLastChange(double lastchange) {
+            this.lastchange = lastchange;
+        }
+
+        public String getLastChangeAsString() {
+            return df.format(lastchange);
+        }
+
+        public int hashCode() {
+            return symbol.hashCode();
+        }
+
+        public boolean equals(Object other) {
+            if (other instanceof Stock) {
+                return this.symbol.equals(((Stock) other).symbol);
+            } else {
+                return false;
+            }
+        }
+        
+        public String toString(){
+            StringBuffer buf = new StringBuffer("STOCK#");
+            buf.append(getSymbol());
+            buf.append("#");
+            buf.append(getValueAsString());
+            buf.append("#");
+            buf.append(getLastChangeAsString());
+            buf.append("#");
+            buf.append(String.valueOf(getCnt()));
+            return buf.toString();
+         
+        }
+
+        public Object clone() {
+            Stock s = new Stock(this.getSymbol(), this.getValue());
+            s.setLastChange(this.getLastChange());
+            s.setCnt(this.cnt);
+            return s;
+        }
+    }
+
+}
\ No newline at end of file

Modified: tomcat/trunk/test/org/apache/cometd/bayeux/samples/EchoChatClient.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/cometd/bayeux/samples/EchoChatClient.java?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/cometd/bayeux/samples/EchoChatClient.java 
(original)
+++ tomcat/trunk/test/org/apache/cometd/bayeux/samples/EchoChatClient.java Tue 
Oct 21 11:07:04 2008
@@ -19,33 +19,23 @@
     protected Client c;
     protected boolean alive = true;
     protected TimestampThread tt = new TimestampThread();
+
     public EchoChatClient() {
         id = counter.incrementAndGet();
         System.out.println("new listener created with id:"+id);
     }
 
-    /**
-     * contextDestroyed
-     *
-     * @param servletContextEvent ServletContextEvent
-     * @todo Implement this javax.servlet.ServletContextListener method
-     */
     public void contextDestroyed(ServletContextEvent servletContextEvent) {
         alive = false;
         tt.interrupt();
     }
 
-    /**
-     * contextInitialized
-     *
-     * @param servletContextEvent ServletContextEvent
-     * @todo Implement this javax.servlet.ServletContextListener method
-     */
     public void contextInitialized(ServletContextEvent servletContextEvent) {
     }
 
     public void attributeAdded(ServletContextAttributeEvent scae) {
         if (scae.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX)) {
+            System.out.println("Starting echo chat client!");
             b = (Bayeux)scae.getValue();
             c = b.newClient("echochat-",this);
             Channel ch = b.getChannel("/chat/demo",true);
@@ -101,7 +91,7 @@
                     m.put("join",false);
                     ch.publish(m);
                 }catch (InterruptedException ignore) {
-                    
+                    Thread.currentThread().interrupted();
                 }catch (Exception x) {
                     x.printStackTrace();
                 }

Modified: tomcat/trunk/webapps/cometd/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/cometd/WEB-INF/web.xml?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/webapps/cometd/WEB-INF/web.xml (original)
+++ tomcat/trunk/webapps/cometd/WEB-INF/web.xml Tue Oct 21 11:07:04 2008
@@ -13,6 +13,10 @@
       <param-name>timeout</param-name>
       <param-value>120000000</param-value>
     </init-param>
+    <init-param>
+      <param-name>reconnectInterval</param-name>
+      <param-value>250</param-value>
+    </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
 
@@ -21,6 +25,13 @@
     <url-pattern>/cometd/*</url-pattern>
   </servlet-mapping>
   
+  <listener>
+    
<listener-class>org.apache.cometd.bayeux.samples.EchoChatClient</listener-class>
+  </listener>
+  <listener>
+    
<listener-class>org.apache.cometd.bayeux.samples.BayeuxStockTicker</listener-class>
+  </listener>
+  
 </web-app>
 
 

Added: tomcat/trunk/webapps/cometd/examples/simplechat/ticker.html
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/cometd/examples/simplechat/ticker.html?rev=706696&view=auto
==============================================================================
--- tomcat/trunk/webapps/cometd/examples/simplechat/ticker.html (added)
+++ tomcat/trunk/webapps/cometd/examples/simplechat/ticker.html Tue Oct 21 
11:07:04 2008
@@ -0,0 +1,129 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" >
+<title>Bayeux Stock Ticker</title>
+<script type="text/javascript" 
src="../../dojo/dojo.js.uncompressed.js"></script>
+<script type="text/javascript" src="../../dojox/cometd.js"></script>
+<script type="text/javascript" src="../../dojox/cometd/_base.js"></script>
+<script type="text/javascript">
+
+dojo.require("dojox.cometd");
+
+dojo.addOnUnload(function() {
+         dojox.cometd.init("/cometd/cometd");
+         dojox.cometd.startBatch();
+         dojox.cometd.unsubscribe("/stock/GOOG", this,"");
+         dojox.cometd.unsubscribe("/stock/YHOO", this,"");
+         dojox.cometd.unsubscribe("/stock/SPRG", this,"");
+         dojox.cometd.endBatch();
+       });
+
+
+dojo.addOnLoad(function() {
+  dojox.cometd.init("/cometd/cometd");
+  dojox.cometd.startBatch();
+  dojox.cometd.subscribe("/stock/GOOG", onMsgEvent);
+  dojox.cometd.subscribe("/stock/YHOO", onMsgEvent);
+  dojox.cometd.subscribe("/stock/SPRG", onMsgEvent);
+  dojox.cometd.endBatch();
+});
+
+
+function trim(str) {
+    return str.replace(/(^\s+|\s+$)/g,'');
+}
+
+
+function clear() {
+  dojo.byId("msgtext").value = "";
+  dojo.byId("msgtext").focus();
+}
+
+
+function enterKeyHandler(e) {
+if (!e) e = window.event;
+   if (e.keyCode == 13) {
+      send(trim(dojo.byId("msgtext").value));
+      clear();
+   }
+
+}
+
+function subscribe(box, symbol) {
+       if (box.checked) {
+               dojox.cometd.subscribe("/stock/"+symbol, onMsgEvent);
+       } else {
+               dojox.cometd.unsubscribe("/stock/"+symbol, onMsgEvent);
+       }
+}
+
+function removeChildrenFromNode(node)
+{
+   if(node == undefined || node == null)
+   {
+      return;
+   }
+
+   var len = node.childNodes.length;
+
+       while (node.hasChildNodes())
+       {
+         node.removeChild(node.firstChild);
+       }
+}
+
+function onMsgEvent(event) {
+   // Break apart the text string into screen name and message parts.
+   var symbol = event.data.symbol;
+   var price = event.data.price;
+   var pricechange = event.data.change;
+   //alert("symbol: "+symbol+" price: "+price+" change: "+pricechange);
+
+   var pricenode = dojo.byId("price."+symbol);
+   var changenode = dojo.byId("change."+symbol);
+   removeChildrenFromNode(pricenode);
+   removeChildrenFromNode(changenode);
+   var pricelabel = document.createTextNode(price);
+   pricelabel.value = price;
+   var changelabel = document.createTextNode(pricechange);
+   changelabel.value = pricechange;
+   pricenode.appendChild(pricelabel);
+   changenode.appendChild(changelabel);
+}
+
+
+</script>
+</head>
+<body bgcolor="#ffffff">
+<h1 align="center">Bayeux Stock Ticker</h1>
+<h2 align="left"> &nbsp;</h2>
+<p>
+<table cellspacing="0" cellpadding="3" width="100%" align="center" border="0">
+  <tr>
+    <td>SYMBOL</td>
+    <td>PRICE</td>
+    <td>LAST CHANGE</td>
+    <td>SUBSCRIBE</td></tr>
+  <tr>
+    <td>SPRG</td>
+    <td id="price.SPRG"></td>
+    <td id="change.SPRG"></td>
+    <td id="check.SPRG"><input type="checkbox" id="check.SPRG" checked></td>
+  </tr>
+  <tr>
+    <td>GOOG</td>
+    <td id="price.GOOG"></td>
+    <td id="change.GOOG"></td>
+    <td id="check.GOOG"><input type="checkbox" id="check.GOOG" checked></td>
+  </tr>
+  <tr>
+    <td>YHOO</td>
+    <td id="price.YHOO"></td>
+    <td id="change.YHOO"></td>
+    <td id="check.YHOO"><input type="checkbox" id="check.GOOG" checked></td>
+  </tr>
+</table>
+</p>
+</body>
+</html>
\ No newline at end of file

Modified: tomcat/trunk/webapps/cometd/index.html
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/cometd/index.html?rev=706696&r1=706695&r2=706696&view=diff
==============================================================================
--- tomcat/trunk/webapps/cometd/index.html (original)
+++ tomcat/trunk/webapps/cometd/index.html Tue Oct 21 11:07:04 2008
@@ -3,4 +3,5 @@
 
 <p>
 Try the <a href="examples/simplechat/cometdchat.htm">Simple Chat Demo</a>.</br>
+Try the <a href="examples/simplechat/ticker.html">Stock Ticker Demo</a>.</br>
 </p>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to