Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 package org.jboss.remoting.detection.multicast;
 
 
MulticastDetector is a remoting detector that broadcasts detection messages using muliticast. The default multicast ip is 224.1.9.1 and port 2410.

Author(s):
Jeff Haynie
Adrian Brock
Tom Elrod
Version:
$Revision: 5957 $
 
 public class MulticastDetector extends AbstractDetector implements MulticastDetectorMBean
 {
    private static int threadCounter = 0;
 
    private String defaultIP = "224.1.9.1";
 
    private InetAddress addr;
    private InetAddress bindAddr;
    private int port = 2410;
    private MulticastSocket socket;
    private Listener listener = new Listener("Remoting Multicast Detector - Listener Thread: " + ++);
    private int bufferSize = 10000;


   

Returns:
The IP that is used to broadcast detection messages on via multicast.
 
    public String getDefaultIP()
    {
       return ;
    }

   

Parameters:
defaultIP The IP that is used to broadcast detection messages on via multicast.
 
    public void setDefaultIP(String defaultIP)
    {
       this. = defaultIP;
    }

   
return the multicast address of the detector

Returns:
 
    public InetAddress getAddress()
    {
       return ;
    }

   
set the interface address of the multicast

Parameters:
ip
 
    public void setAddress(InetAddress ip)
    {
       this. = ip;
    }

   
return the bind address of the detector

Returns:
   {
      return ;
   }

   
set the bind address of the multicast

Parameters:
ip
   public void setBindAddress(InetAddress ip)
   {
      this. = ip;
   }

   
get the port that the detector is multicasting to

Returns:
   public int getPort()
   {
      return ;
   }

   
set the port for detections to be multicast to

Parameters:
port
   public void setPort(int port)
   {
      this. = port;
   }
   public int getBufferSize()
   {
      return ;
   }
   public void setBufferSize(int bufferSize)
   {
      this. = bufferSize;
   }
   
   
called by MBeanServer to start the mbean lifecycle

   public void start() throws Exception
   {
      if( == null)
      {
         this. = InetAddress.getByName();
      }
      // check to see if we're running on a machine with loopback and no NIC
      InetAddress localHost = LocalHostUtil.getLocalHost();
      if( == null && localHost.getHostAddress().equals("127.0.0.1"))
      {
         // use this to bind so multicast will work w/o network
         this. = localHost;
      }
      SocketAddress saddr = new InetSocketAddress();
       = new MulticastSocket(saddr);
      .joinGroup();
      super.start();
      if( == null)
      {
          = new Listener("Remoting Multicast Detector - Listener Thread: " + ++);
      }
      .start();
   }

   
called by the MBeanServer to stop the mbean lifecycle

   public void stop() throws Exception
   {
      super.stop();
      if( != null)
      {
         try
         {
            . = false;
            .interrupt();
         }
         catch (Exception e)
         {
            {
               .warn("Error stopping multicast detector.  " + e.getMessage());
            }         }
          = null;
      }
      if( != null)
      {
         try
         {
            .leaveGroup();
            .close();
         }
         catch (IOException e)
         {
            .warn("Error stopping multicast detector.  " + e.getMessage());
         }
          = null;
      }
   }

   
subclasses must implement to provide the specific heartbeat protocol for this server to send out to other servers on the network
   protected void heartbeat()
   {
      if( != null)
      {
         Detection msg = createDetection();
         if (msg == null)
            return;
         
         try
         {
            if(.isTraceEnabled())
            {
               .trace("sending heartbeat: " + msg);
            }
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
            objectOut.writeObject(msg);
            objectOut.flush();
            byteOut.flush();
            byte buf[] = byteOut.toByteArray();
            DatagramPacket p = new DatagramPacket(bufbuf.length);
            .send(p);
         }
         catch(Throwable ex)
         {
            // its failed
            .debug("heartbeat failed"ex);
         }
      }
   }
   protected void forceHeartbeat()
   {
      if( != null)
      {
         String msg = "Send heartbeat";
         try
         {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
            objectOut.writeObject(msg);
            objectOut.flush();
            byteOut.flush();
            byte buf[] = byteOut.toByteArray();
            DatagramPacket p = new DatagramPacket(bufbuf.length);
            .send(p);

            
This is a bit of a hack, but am going to wait a few seconds to allow for any group members (other multicast detectors) to receive the msg and then respond themselves with their detection messages. Since don't know number of servers that are members of the group, can't really wait until all detection messages, just hope they come in before end of waiting.
            Thread.currentThread().sleep(2000);
         }
         catch(Throwable ex)
         {
            // its failed
            .debug("forced heartbeat failed"ex);
         }
      }
   }
   private void listen(DatagramPacket pbyte[] buf)
   {
      if( != null)
      {
         try
         {
            // should block until we get a multicast
            .receive(p);
            // take the multicast, and deserialize into the detection event
            ByteArrayInputStream byteInput = new ByteArrayInputStream(buf);
            ObjectInputStream objectInput = new ObjectInputStream(byteInput);
            Object obj = objectInput.readObject();
            if(obj instanceof Detection)
            {
               Detection msg = (Detection)obj;
               if(.isTraceEnabled())
               {
                  .trace("received detection: " + msg);
               }
               // let the subclass do the hard work off handling detection
               detect(msg);
            }
            else
            {
               // for now, assume anything *not* of type Detection
               // is a prompt to send out detection msg
               heartbeat();
            }
         }
         catch(Throwable e)
         {
            if(e instanceof java.io.InvalidClassException)
            {
               return;
            }
            if( != null)
            {
               .debug("Error receiving detection"e);
            }
         }
      }
   }
   private final class Listener extends Thread
   {
      boolean running = true;
      public Listener(String name)
      {
         super(name);
      }
      public void run()
      {
         .debug("Using bufferSize: " + );
         byte[] buf = new byte[];
         DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
         //p.setAddress(addr);
         //p.setPort(port);
         while()
         {
            listen(pbuf);
         }
      }
   }
New to GrepCode? Check out our FAQ X