You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/05/30 21:56:36 UTC

svn commit: r1487991 - /tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java

Author: markt
Date: Thu May 30 19:56:35 2013
New Revision: 1487991

URL: http://svn.apache.org/r1487991
Log:
Add a cut down, simple to debug, multicast tester than can be used to determine if multicast cluster membership is going to work in a given environment.

Added:
    tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java   (with props)

Added: tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java?rev=1487991&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java (added)
+++ tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java Thu May 30 19:56:35 2013
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.tribes;
+
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+import java.net.UnknownHostException;
+
+/**
+ * A simple multicast test that replicates the core elements of Tomcat's
+ * multicast membership. If this works then multicast membership should work.
+ */
+public class TesterMulticast {
+
+    private static final String ADDRESS = "228.0.0.4";
+    private static final int PORT = 56565;
+    private static final InetAddress INET_ADDRESS;
+
+    static {
+        InetAddress result = null;
+        try {
+             result = InetAddress.getByName(ADDRESS);
+        } catch (UnknownHostException e) {
+            // deal with later
+        }
+        INET_ADDRESS = result;
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        // Start Rx Thread
+        Rx rx = new Rx();
+        Thread rxThread = new Thread(rx);
+        rxThread.setDaemon(true);
+        rxThread.start();
+
+        // Start Tx Thread
+        Tx tx = new Tx();
+        Thread txThread = new Thread(tx);
+        txThread.setDaemon(true);
+        txThread.start();
+
+
+        Thread.sleep(10000);
+
+        tx.stop();
+        rx.stop();
+    }
+
+    private static class Rx implements Runnable {
+
+        private volatile boolean run = true;
+
+        @Override
+        public void run() {
+            try (MulticastSocket s = new MulticastSocket(PORT)) {
+                s.setLoopbackMode(false);
+                s.joinGroup(INET_ADDRESS);
+                DatagramPacket p = new DatagramPacket(new byte[4], 4);
+                p.setAddress(INET_ADDRESS);
+                p.setPort(PORT);
+                while (run) {
+                    s.receive(p);
+                    String d = new String (p.getData());
+                    System.out.println("Rx: " + d);
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        public void stop() {
+            run = false;
+        }
+    }
+
+    private static class Tx implements Runnable {
+
+        private volatile boolean run = true;
+
+        @Override
+        public void run() {
+            try (MulticastSocket s = new MulticastSocket(PORT)) {
+                s.setLoopbackMode(false);
+                s.joinGroup(INET_ADDRESS);
+                DatagramPacket p = new DatagramPacket(new byte[4], 4);
+                p.setAddress(INET_ADDRESS);
+                p.setPort(PORT);
+                long counter = 0;
+                String msg;
+                while (run) {
+                    msg = String.format("%04d", Long.valueOf(counter));
+                    p.setData(msg.getBytes());
+                    System.out.println("Tx: " + msg);
+                    s.send(p);
+                    counter++;
+                    Thread.sleep(500);
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        public void stop() {
+            run = false;
+        }
+    }
+}

Propchange: tomcat/trunk/test/org/apache/catalina/tribes/TesterMulticast.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org