Move the multi logic to MultiCastSender with
authorjfclere <jfclere@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 9 Apr 2009 13:00:21 +0000 (13:00 +0000)
committerjfclere <jfclere@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 9 Apr 2009 13:00:21 +0000 (13:00 +0000)
the idea to TCP sockets and a list of proxy too.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@763635 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/ha/backend/HeartbeatListener.java
java/org/apache/catalina/ha/backend/MultiCastSender.java [new file with mode: 0644]
java/org/apache/catalina/ha/backend/Sender.java [new file with mode: 0644]

index e196cd7..453068b 100644 (file)
@@ -55,8 +55,6 @@ public class HeartbeatListener
     public void setPort(int port) { this.port = port; }
 
     /* for multicasting stuff */
-    MulticastSocket s = null;
-    InetAddress group = null;
     String ip = "224.0.1.105"; /* Multicast IP */
     int multiport = 23364;     /* Multicast Port */
     int ttl = 16;
@@ -70,23 +68,17 @@ public class HeartbeatListener
 
     private CollectedInfo coll = null;
 
+    private Sender sender = null;
+
     public void containerEvent(ContainerEvent event) {
     }
 
     public void lifecycleEvent(LifecycleEvent event) {
         Object source = event.getLifecycle();
         if (Lifecycle.PERIODIC_EVENT.equals(event.getType())) {
-            if (s == null) {
-                try {
-                    group = InetAddress.getByName(ip);
-                    s = new MulticastSocket(port);
-                    s.setTimeToLive(16);
-                    s.joinGroup(group);
-                } catch (Exception ex) {
-                    log.error("Unable to use multicast: " + ex);
-                    s = null;
-                    return;
-                } 
+            if (sender == null) {
+                sender = new MultiCastSender();
+                sender.init(this);
             }
 
             /* Read busy and ready */
@@ -108,19 +100,10 @@ public class HeartbeatListener
             }
             String output = new String();
             output = "v=1&ready=" + coll.ready + "&busy=" + coll.busy;
-            byte[] buf;
-            try {
-                buf = output.getBytes("US-ASCII");
-            } catch (UnsupportedEncodingException ex) {
-                buf = output.getBytes();
-            }
-            DatagramPacket data = new DatagramPacket(buf, buf.length, group, multiport);
             try {
-                s.send(data);
+                sender.send(output);
             } catch (Exception ex) {
                 log.error("Unable to send colllected load information: " + ex);
-                s.close();
-                s = null;
             }
         }
     }
diff --git a/java/org/apache/catalina/ha/backend/MultiCastSender.java b/java/org/apache/catalina/ha/backend/MultiCastSender.java
new file mode 100644 (file)
index 0000000..23c32bb
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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.ha.backend;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+import java.net.MulticastSocket;
+import java.net.InetAddress;
+import java.net.DatagramPacket;
+import java.io.UnsupportedEncodingException;
+
+/*
+ * Sender to proxies using multicast socket.
+ */
+public class MultiCastSender
+    implements Sender {
+
+    private static Log log = LogFactory.getLog(HeartbeatListener.class);
+
+    HeartbeatListener config = null;
+
+    /* for multicasting stuff */
+    MulticastSocket s = null;
+    InetAddress group = null;
+
+    public void init(HeartbeatListener config) {
+        this.config = config;
+    }
+
+    public int send(String mess) throws Exception {
+        if (s == null) {
+            try {
+                group = InetAddress.getByName(config.getGroup());
+                s = new MulticastSocket(config.getMultiport());
+                s.setTimeToLive(config.getTtl());
+                s.joinGroup(group);
+            } catch (Exception ex) {
+                log.error("Unable to use multicast: " + ex);
+                s = null;
+                return -1;
+            } 
+        }
+
+        byte[] buf;
+        try {
+            buf = mess.getBytes("US-ASCII");
+        } catch (UnsupportedEncodingException ex) {
+            buf = mess.getBytes();
+        }
+        DatagramPacket data = new DatagramPacket(buf, buf.length, group, config.getMultiport());
+        try {
+            s.send(data);
+        } catch (Exception ex) {
+            log.error("Unable to send colllected load information: " + ex);
+            s.close();
+            s = null;
+            return -1;
+        }
+        return 0;
+    }
+
+}
diff --git a/java/org/apache/catalina/ha/backend/Sender.java b/java/org/apache/catalina/ha/backend/Sender.java
new file mode 100644 (file)
index 0000000..b969208
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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.ha.backend;
+
+/*
+ * Interface to send data to proxies
+ *
+ */
+public interface Sender {
+
+  /**
+   * Set the configuration parameters
+   */
+  public void init(HeartbeatListener config);
+
+  /**
+   * Send the message to the proxies
+   */
+  public int send(String mess) throws Exception;
+}