FileDocCategorySizeDatePackage
TcpPingInterceptor.javaAPI DocApache Tomcat 6.0.146457Fri Jul 20 04:20:32 BST 2007org.apache.catalina.tribes.group.interceptors

TcpPingInterceptor

public class TcpPingInterceptor extends org.apache.catalina.tribes.group.ChannelInterceptorBase
Sends a ping to all members. Configure this interceptor with the TcpFailureDetector below it, and the TcpFailureDetector will act as the membership guide.
author
Filip Hanik
version
1.0

Fields Summary
protected static org.apache.juli.logging.Log
log
protected static byte[]
TCP_PING_DATA
protected long
interval
protected boolean
useThread
protected boolean
staticOnly
protected boolean
running
protected PingThread
thread
protected static AtomicInteger
cnt
WeakReference
failureDetector
WeakReference
staticMembers
Constructors Summary
Methods Summary
public longgetInterval()

        return interval;
    
public booleangetStaticOnly()

        return staticOnly;
    
public booleangetUseThread()

        return useThread;
    
public voidheartbeat()

        super.heartbeat();
        if (!getUseThread()) sendPing();
    
public voidmessageReceived(org.apache.catalina.tribes.ChannelMessage msg)

        //catch incoming 
        boolean process = true;
        if ( okToProcess(msg.getOptions()) ) {
            //check to see if it is a ping message, if so, process = false
            process = ( (msg.getMessage().getLength() != TCP_PING_DATA.length) ||
                        (!Arrays.equals(TCP_PING_DATA,msg.getMessage().getBytes()) ) );
        }//end if

        //ignore the message, it doesnt have the flag set
        if ( process ) super.messageReceived(msg);
        else if ( log.isDebugEnabled() ) log.debug("Received a TCP ping packet:"+msg);
    
protected voidsendPing()

        if (failureDetector.get()!=null) {
            //we have a reference to the failure detector
            //piggy back on that dude
            failureDetector.get().checkMembers(true);
        }else {
            if (staticOnly && staticMembers.get()!=null) {
                sendPingMessage(staticMembers.get().getMembers());
            } else {
                sendPingMessage(getMembers());
            }
        }
    
protected voidsendPingMessage(org.apache.catalina.tribes.Member[] members)

        if ( members == null || members.length == 0 ) return;
        ChannelData data = new ChannelData(true);//generates a unique Id
        data.setAddress(getLocalMember(false));
        data.setTimestamp(System.currentTimeMillis());
        data.setOptions(getOptionFlag());
        try {
            super.sendMessage(members, data, null);
        }catch (ChannelException x) {
            log.warn("Unable to send TCP ping.",x);
        }
    
public voidsetInterval(long interval)

        this.interval = interval;
    
public voidsetStaticOnly(boolean staticOnly)

        this.staticOnly = staticOnly;
    
public voidsetUseThread(boolean useThread)

        this.useThread = useThread;
    
public synchronized voidstart(int svc)

    
           
        super.start(svc);
        running = true;
        if ( thread == null ) {
            thread = new PingThread();
            thread.setDaemon(true);
            thread.setName("TcpPingInterceptor.PingThread-"+cnt.addAndGet(1));
            thread.start();
        }
        
        //acquire the interceptors to invoke on send ping events
        ChannelInterceptor next = getNext();
        while ( next != null ) {
            if ( next instanceof TcpFailureDetector ) 
                failureDetector = new WeakReference<TcpFailureDetector>((TcpFailureDetector)next);
            if ( next instanceof StaticMembershipInterceptor ) 
                staticMembers = new WeakReference<StaticMembershipInterceptor>((StaticMembershipInterceptor)next);
            next = next.getNext();
        }
        
    
public voidstop(int svc)

        running = false;
        if ( thread != null ) thread.interrupt();
        thread = null;
        super.stop(svc);