FileDocCategorySizeDatePackage
McastService.javaAPI DocApache Tomcat 6.0.1417629Fri Jul 20 04:20:32 BST 2007org.apache.catalina.tribes.membership

McastService

public class McastService extends Object implements org.apache.catalina.tribes.MembershipService, org.apache.catalina.tribes.MembershipListener
A membership implementation using simple multicast. This is the representation of a multicast membership service. This class is responsible for maintaining a list of active cluster nodes in the cluster. If a node fails to send out a heartbeat, the node will be dismissed.
author
Filip Hanik
version
$Revision: 522651 $, $Date: 2007-03-27 00:24:32 +0200 (mar., 27 mars 2007) $

Fields Summary
private static org.apache.juli.logging.Log
log
protected org.apache.catalina.tribes.util.StringManager
sm
The string manager for this package.
private static final String
info
The descriptive information about this implementation.
protected Properties
properties
The implementation specific properties
protected McastServiceImpl
impl
A handle to the actual low level implementation
protected org.apache.catalina.tribes.MembershipListener
listener
A membership listener delegate (should be the cluster :)
protected MemberImpl
localMember
The local member
private int
mcastSoTimeout
private int
mcastTTL
protected byte[]
payload
protected byte[]
domain
protected static final org.apache.catalina.tribes.Member[]
EMPTY_MEMBERS
Return all the members
Constructors Summary
public McastService()
Create a membership service.


             
      
        //default values
        properties.setProperty("mcastPort","45564");
        properties.setProperty("mcastAddress","228.0.0.4");
        properties.setProperty("memberDropTime","3000");
        properties.setProperty("mcastFrequency","500");

    
Methods Summary
public org.apache.catalina.tribes.MemberfindMemberByName(java.lang.String name)
Return the member by name

        Member[] currentMembers = getMembers();
        for (int i = 0; i < currentMembers.length; i++) {
            if (name.equals(currentMembers[i].toString()))
                return currentMembers[i];
        }
        return null;
    
public java.lang.StringgetAddress()

        return properties.getProperty("mcastAddress");
    
public java.lang.StringgetBind()

        return properties.getProperty("mcastBindAddress");
    
public byte[]getDomain()

        return domain;
    
public longgetDropTime()

        String p = properties.getProperty("memberDropTime");
        return new Long(p).longValue();
    
public longgetFrequency()

        String p = properties.getProperty("mcastFrequency");
        return new Long(p).longValue();
    
public java.lang.StringgetInfo()
Return descriptive information about this implementation and the corresponding version number, in the format <description>/<version>.

        return (info);
    
public org.apache.catalina.tribes.MembergetLocalMember(boolean alive)
Return the local member

        if ( alive && localMember != null && impl != null) localMember.setMemberAliveTime(System.currentTimeMillis()-impl.getServiceStartTime());
        return localMember;
    
public java.lang.StringgetLocalMemberName()
Return the local member name

        return localMember.toString() ;
    
public java.lang.StringgetMcastAddr()

deprecated
use getAddress
return
String

        return getAddress();
    
public java.lang.StringgetMcastBindAddress()

deprecated
use getBind
return
String

        return getBind();
    
public longgetMcastDropTime()

deprecated
use getDropTime
return
long

        return getDropTime();
    
public longgetMcastFrequency()

deprecated
use getFrequency
return
long

        return getFrequency();
    
public intgetMcastPort()

deprecated
use getPort()
return
int

        return getPort();
    
public intgetMcastSoTimeout()

deprecated
use getSoTimeout
return
int

        return getSoTimeout();
    
public intgetMcastTTL()

deprecated
use getTtl
return
int

        return getTtl();
    
public org.apache.catalina.tribes.MembergetMember(org.apache.catalina.tribes.Member mbr)

        if ( impl == null || impl.membership == null ) return null;
        return impl.membership.getMember(mbr);
    
public org.apache.catalina.tribes.Member[]getMembers()

       
        if ( impl == null || impl.membership == null ) return EMPTY_MEMBERS;
        return impl.membership.getMembers();
    
public java.lang.String[]getMembersByName()
Return all the members by name

        Member[] currentMembers = getMembers();
        String [] membernames ;
        if(currentMembers != null) {
            membernames = new String[currentMembers.length];
            for (int i = 0; i < currentMembers.length; i++) {
                membernames[i] = currentMembers[i].toString() ;
            }
        } else
            membernames = new String[0] ;
        return membernames ;
    
public byte[]getPayload()

        return payload;
    
public intgetPort()

        String p = properties.getProperty("mcastPort");
        return new Integer(p).intValue();
    
public java.util.PropertiesgetProperties()
Return the properties, see setProperties

        return properties;
    
public intgetSoTimeout()

        return mcastSoTimeout;
    
public intgetTtl()

        return mcastTTL;
    
public booleanhasMembers()
has members?

       if ( impl == null || impl.membership == null ) return false;
       return impl.membership.hasMembers();
    
protected voidhasProperty(java.util.Properties properties, java.lang.String name)
Check if a required property is available.

param
properties The set of properties
param
name The property to check for

        if ( properties.getProperty(name)==null) throw new IllegalArgumentException("McastService:Required property \""+name+"\" is missing.");
    
public static voidmain(java.lang.String[] args)
Simple test program

param
args Command-line arguments
throws
Exception If an error occurs

		if(log.isInfoEnabled())
            log.info("Usage McastService hostname tcpport");
        McastService service = new McastService();
        java.util.Properties p = new java.util.Properties();
        p.setProperty("mcastPort","5555");
        p.setProperty("mcastAddress","224.10.10.10");
        p.setProperty("mcastClusterDomain","catalina");
        p.setProperty("bindAddress","localhost");
        p.setProperty("memberDropTime","3000");
        p.setProperty("mcastFrequency","500");
        p.setProperty("tcpListenPort","4000");
        p.setProperty("tcpListenHost","127.0.0.1");
        service.setProperties(p);
        service.start();
        Thread.sleep(60*1000*60);
    
public voidmemberAdded(org.apache.catalina.tribes.Member member)

        if ( listener!=null ) listener.memberAdded(member);
    
public voidmemberDisappeared(org.apache.catalina.tribes.Member member)
Callback from the impl when a new member has been received

param
member The member

        if ( listener!=null ) listener.memberDisappeared(member);
    
public voidremoveMembershipListener()
Remove the membership listener

        listener = null;
    
public voidsetAddress(java.lang.String addr)

        properties.setProperty("mcastAddress", addr);
    
public voidsetBind(java.lang.String bindaddr)

        properties.setProperty("mcastBindAddress", bindaddr);
    
public voidsetDomain(byte[] domain)

        this.domain = domain;
        if ( localMember != null ) {
            localMember.setDomain(domain);
            localMember.getData(true,true);
            try {
                if (impl != null) impl.send(false);
            }catch ( Exception x ) {
                log.error("Unable to send domain update.",x);
            }
        }
    
public voidsetDropTime(long time)

        properties.setProperty("memberDropTime", String.valueOf(time));
    
public voidsetFrequency(long time)

        properties.setProperty("mcastFrequency", String.valueOf(time));
    
public voidsetLocalMemberProperties(java.lang.String listenHost, int listenPort)
Sets the local member properties for broadcasting

        properties.setProperty("tcpListenHost",listenHost);
        properties.setProperty("tcpListenPort",String.valueOf(listenPort));
        try {
            if (localMember != null) {
                localMember.setHostname(listenHost);
                localMember.setPort(listenPort);
            } else {
                localMember = new MemberImpl(listenHost, listenPort, 0);
                localMember.setUniqueId(UUIDGenerator.randomUUID(true));
                localMember.setPayload(getPayload());
                localMember.setDomain(getDomain());
            }
            localMember.getData(true, true);
        }catch ( IOException x ) {
            throw new IllegalArgumentException(x);
        }
    
public voidsetMcastAddr(java.lang.String addr)

deprecated
use setAddress
param
addr String

        setAddress(addr);
    
public voidsetMcastBindAddress(java.lang.String bindaddr)

        setBind(bindaddr);
    
public voidsetMcastDropTime(long time)

        setDropTime(time);
    
public voidsetMcastFrequency(long time)

deprecated
use setFrequency
param
time long

        setFrequency(time);
    
public voidsetMcastPort(int port)

deprecated
use setPort
param
port int

        setPort(port);
    
public voidsetMcastSoTimeout(int mcastSoTimeout)

deprecated
use setSoTimeout
param
mcastSoTimeout int

        setSoTimeout(mcastSoTimeout);
    
public voidsetMcastTTL(int mcastTTL)

deprecated
use setTtl
param
mcastTTL int

        setTtl(mcastTTL);
    
public voidsetMembershipListener(org.apache.catalina.tribes.MembershipListener listener)
Add a membership listener, this version only supports one listener per service, so calling this method twice will result in only the second listener being active.

param
listener The listener

        this.listener = listener;
    
public voidsetPayload(byte[] payload)

        this.payload = payload;
        if ( localMember != null ) {
            localMember.setPayload(payload);
            localMember.getData(true,true);
            try {
                if (impl != null) impl.send(false);
            }catch ( Exception x ) {
                log.error("Unable to send payload update.",x);
            }
        }
    
public voidsetPort(int port)

        properties.setProperty("mcastPort", String.valueOf(port));
    
public voidsetProperties(java.util.Properties properties)

param
properties
All are required
1. mcastPort - the port to listen to
2. mcastAddress - the mcast group address
4. bindAddress - the bind address if any - only one that can be null
5. memberDropTime - the time a member is gone before it is considered gone.
6. mcastFrequency - the frequency of sending messages
7. tcpListenPort - the port this member listens to
8. tcpListenHost - the bind address of this member
exception
java.lang.IllegalArgumentException if a property is missing.

        hasProperty(properties,"mcastPort");
        hasProperty(properties,"mcastAddress");
        hasProperty(properties,"memberDropTime");
        hasProperty(properties,"mcastFrequency");
        hasProperty(properties,"tcpListenPort");
        hasProperty(properties,"tcpListenHost");
        this.properties = properties;
    
public voidsetSoTimeout(int mcastSoTimeout)

        this.mcastSoTimeout = mcastSoTimeout;
        properties.setProperty("mcastSoTimeout", String.valueOf(mcastSoTimeout));
    
public voidsetTtl(int mcastTTL)

        this.mcastTTL = mcastTTL;
        properties.setProperty("mcastTTL", String.valueOf(mcastTTL));
    
public voidstart()
Start broadcasting and listening to membership pings

throws
java.lang.Exception if a IO error occurs

        start(MembershipService.MBR_RX);
        start(MembershipService.MBR_TX);
    
public voidstart(int level)

        hasProperty(properties,"mcastPort");
        hasProperty(properties,"mcastAddress");
        hasProperty(properties,"memberDropTime");
        hasProperty(properties,"mcastFrequency");
        hasProperty(properties,"tcpListenPort");
        hasProperty(properties,"tcpListenHost");

        if ( impl != null ) {
            impl.start(level);
            return;
        }
        String host = getProperties().getProperty("tcpListenHost");
        int port = Integer.parseInt(getProperties().getProperty("tcpListenPort"));
        
        if ( localMember == null ) {
            localMember = new MemberImpl(host, port, 100);
            localMember.setUniqueId(UUIDGenerator.randomUUID(true));
        } else {
            localMember.setHostname(host);
            localMember.setPort(port);
            localMember.setMemberAliveTime(100);
        }
        if ( this.payload != null ) localMember.setPayload(payload);
        if ( this.domain != null ) localMember.setDomain(domain);
        localMember.setServiceStartTime(System.currentTimeMillis());
        java.net.InetAddress bind = null;
        if ( properties.getProperty("mcastBindAddress")!= null ) {
            bind = java.net.InetAddress.getByName(properties.getProperty("mcastBindAddress"));
        }
        int ttl = -1;
        int soTimeout = -1;
        if ( properties.getProperty("mcastTTL") != null ) {
            try {
                ttl = Integer.parseInt(properties.getProperty("mcastTTL"));
            } catch ( Exception x ) {
                log.error("Unable to parse mcastTTL="+properties.getProperty("mcastTTL"),x);
            }
        }
        if ( properties.getProperty("mcastSoTimeout") != null ) {
            try {
                soTimeout = Integer.parseInt(properties.getProperty("mcastSoTimeout"));
            } catch ( Exception x ) {
                log.error("Unable to parse mcastSoTimeout="+properties.getProperty("mcastSoTimeout"),x);
            }
        }

        impl = new McastServiceImpl((MemberImpl)localMember,Long.parseLong(properties.getProperty("mcastFrequency")),
                                    Long.parseLong(properties.getProperty("memberDropTime")),
                                    Integer.parseInt(properties.getProperty("mcastPort")),
                                    bind,
                                    java.net.InetAddress.getByName(properties.getProperty("mcastAddress")),
                                    ttl,
                                    soTimeout,
                                    this);
        
        impl.start(level);
		

    
public voidstop(int svc)
Stop broadcasting and listening to membership pings

        try  {
            if ( impl != null && impl.stop(svc) ) impl = null;
        } catch ( Exception x)  {
            log.error("Unable to stop the mcast service, level:"+svc+".",x);
        }