Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.membership;
 
 
A membership implementation using simple multicast. This is the representation of a multicast member. Carries the host, and port of the this or other cluster nodes.
 
 public class MemberImpl implements Memberjava.io.Externalizable {

    
Should a call to getName or getHostName try to do a DNS lookup? default is false
 
     public static final boolean DO_DNS_LOOKUPS = Boolean.parseBoolean(System.getProperty("org.apache.catalina.tribes.dns_lookups","false"));
 
     public static final transient byte[] TRIBES_MBR_BEGIN = new byte[] {84, 82, 73, 66, 69, 83, 45, 66, 1, 0};
     public static final transient byte[] TRIBES_MBR_END   = new byte[] {84, 82, 73, 66, 69, 83, 45, 69, 1, 0};

    
The listen host for this member
 
     protected byte[] host;
     protected transient String hostname;
    
The tcp listen port for this member
 
     protected int port;
    
The udp listen port for this member
 
     protected int udpPort = -1;

    
The tcp/SSL listen port for this member
 
     protected int securePort = -1;

    
Counter for how many broadcast messages have been sent from this member
 
     protected int msgCount = 0;
    
The number of milliseconds since this member was created, is kept track of using the start time
 
     protected long memberAliveTime = 0;

    
For the local member only
 
     protected transient long serviceStartTime;

    
To avoid serialization over and over again, once the local dataPkg has been set, we use that to transmit data
 
     protected transient byte[] dataPkg = null;

    
Unique session Id for this member
 
     protected byte[] uniqueId = new byte[16];

    
Custom payload that an app framework can broadcast Also used to transport stop command.
 
     protected byte[] payload = new byte[0];

    
Command, so that the custom payload doesn't have to be used This is for internal tribes use, such as SHUTDOWN_COMMAND
 
    protected byte[] command = new byte[0];

    
Domain if we want to filter based on domain.
    protected byte[] domain = new byte[0];

    
Empty constructor for serialization
    public MemberImpl() {
    }

    
Construct a new member object

Parameters:
host - the tcp listen host
port - the tcp listen port
aliveTime - the number of milliseconds since this member was created
    public MemberImpl(String host,
                      int port,
                      long aliveTimethrows IOException {
        setHostname(host);
        this. = port;
        this.=aliveTime;
    }
    public MemberImpl(String host,
                      int port,
                      long aliveTime,
                      byte[] payloadthrows IOException {
        this(host,port,aliveTime);
        setPayload(payload);
    }
    @Override
    public boolean isReady() {
        return SenderState.getSenderState(this).isReady();
    }
    @Override
    public boolean isSuspect() {
        return SenderState.getSenderState(this).isSuspect();
    }
    @Override
    public boolean isFailing() {
        return SenderState.getSenderState(this).isFailing();
    }

    
Increment the message count.
    protected void inc() {
        ++;
    }

    
Create a data package to send over the wire representing this member. This is faster than serialization.

Returns:
- the bytes for this member deserialized
    public byte[] getData()  {
        return getData(true);
    }
    @Override
    public byte[] getData(boolean getalive)  {
        return getData(getalive,false);
    }
    @Override
    public int getDataLength() {
        return .//start pkg
               4+ //data length
               8+ //alive time
               4+ //port
               4+ //secure port
               4+ //udp port
               1+ //host length
               .//host
               4+ //command length
               .//command
               4+ //domain length
               .//domain
               16+ //unique id
               4+ //payload length
               .//payload
               .//end pkg
    }
    @Override
    public byte[] getData(boolean getaliveboolean reset)  {
        if ( reset )  = null;
        //look in cache first
        if ( !=null ) {
            if ( getalive ) {
                //you'd be surprised, but System.currentTimeMillis
                //shows up on the profiler
                long alive=System.currentTimeMillis()-getServiceStartTime();
                XByteBuffer.toBytes(alive.+4);
            }
            return ;
        }
        //package looks like
        //start package TRIBES_MBR_BEGIN.length
        //package length - 4 bytes
        //alive - 8 bytes
        //port - 4 bytes
        //secure port - 4 bytes
        //udp port - 4 bytes
        //host length - 1 byte
        //host - hl bytes
        //clen - 4 bytes
        //command - clen bytes
        //dlen - 4 bytes
        //domain - dlen bytes
        //uniqueId - 16 bytes
        //payload length - 4 bytes
        //payload plen bytes
        //end package TRIBES_MBR_END.length
        byte[] addr = ;
        long alive=System.currentTimeMillis()-getServiceStartTime();
        byte hl = (byte)addr.length;
        byte[] data = new byte[getDataLength()];
        int bodylength = (getDataLength() - . - . - 4);
        int pos = 0;
        //TRIBES_MBR_BEGIN
        System.arraycopy(,0,data,pos,.);
        pos += .;
        //body length
        XByteBuffer.toBytes(bodylength,data,pos);
        pos += 4;
        //alive data
        XByteBuffer.toBytes(alive,data,pos);
        pos += 8;
        //port
        XByteBuffer.toBytes(,data,pos);
        pos += 4;
        //secure port
        XByteBuffer.toBytes(,data,pos);
        pos += 4;
        //udp port
        XByteBuffer.toBytes(,data,pos);
        pos += 4;
        //host length
        data[pos++] = hl;
        //host
        System.arraycopy(addr,0,data,pos,addr.length);
        pos+=addr.length;
        //clen - 4 bytes
        XByteBuffer.toBytes(.,data,pos);
        pos+=4;
        //command - clen bytes
        System.arraycopy(,0,data,pos,.);
        pos+=.;
        //dlen - 4 bytes
        XByteBuffer.toBytes(.,data,pos);
        pos+=4;
        //domain - dlen bytes
        System.arraycopy(,0,data,pos,.);
        pos+=.;
        //unique Id
        System.arraycopy(,0,data,pos,.);
        pos+=.;
        //payload
        XByteBuffer.toBytes(.,data,pos);
        pos+=4;
        System.arraycopy(,0,data,pos,.);
        pos+=.;
        //TRIBES_MBR_END
        System.arraycopy(,0,data,pos,.);
        pos += .;
        //create local data
         = data;
        return data;
    }
    
Deserializes a member from data sent over the wire

Parameters:
data - the bytes received
Returns:
a member object.
    public static Member getMember(byte[] dataMemberImpl member) {
        return getMember(data,0,data.length,member);
    }
    public static Member getMember(byte[] dataint offsetint lengthMemberImpl member) {
        //package looks like
        //start package TRIBES_MBR_BEGIN.length
        //package length - 4 bytes
        //alive - 8 bytes
        //port - 4 bytes
        //secure port - 4 bytes
        //udp port - 4 bytes
        //host length - 1 byte
        //host - hl bytes
        //clen - 4 bytes
        //command - clen bytes
        //dlen - 4 bytes
        //domain - dlen bytes
        //uniqueId - 16 bytes
        //payload length - 4 bytes
        //payload plen bytes
        //end package TRIBES_MBR_END.length
        int pos = offset;
        if (XByteBuffer.firstIndexOf(data,offset,)!=pos) {
            throw new IllegalArgumentException("Invalid package, should start with:"+org.apache.catalina.tribes.util.Arrays.toString());
        }
        if ( length < (.+4) ) {
            throw new ArrayIndexOutOfBoundsException("Member package to small to validate.");
        }
        pos += .;
        int bodylength = XByteBuffer.toInt(data,pos);
        pos += 4;
        if ( length < (bodylength+4+.+.) ) {
            throw new ArrayIndexOutOfBoundsException("Not enough bytes in member package.");
        }
        int endpos = pos+bodylength;
        if (XByteBuffer.firstIndexOf(data,endpos,)!=endpos) {
            throw new IllegalArgumentException("Invalid package, should end with:"+org.apache.catalina.tribes.util.Arrays.toString());
        }
        byte[] alived = new byte[8];
        System.arraycopy(dataposalived, 0, 8);
        pos += 8;
        byte[] portd = new byte[4];
        System.arraycopy(dataposportd, 0, 4);
        pos += 4;
        byte[] sportd = new byte[4];
        System.arraycopy(datapossportd, 0, 4);
        pos += 4;
        byte[] uportd = new byte[4];
        System.arraycopy(dataposuportd, 0, 4);
        pos += 4;
        byte hl = data[pos++];
        byte[] addr = new byte[hl];
        System.arraycopy(dataposaddr, 0, hl);
        pos += hl;
        int cl = XByteBuffer.toInt(datapos);
        pos += 4;
        byte[] command = new byte[cl];
        System.arraycopy(dataposcommand, 0, command.length);
        pos += command.length;
        int dl = XByteBuffer.toInt(datapos);
        pos += 4;
        byte[] domain = new byte[dl];
        System.arraycopy(dataposdomain, 0, domain.length);
        pos += domain.length;
        byte[] uniqueId = new byte[16];
        System.arraycopy(dataposuniqueId, 0, 16);
        pos += 16;
        int pl = XByteBuffer.toInt(datapos);
        pos += 4;
        byte[] payload = new byte[pl];
        System.arraycopy(datapospayload, 0, payload.length);
        pos += payload.length;
        member.setHost(addr);
        member.setPort(XByteBuffer.toInt(portd, 0));
        member.setSecurePort(XByteBuffer.toInt(sportd, 0));
        member.setUdpPort(XByteBuffer.toInt(uportd, 0));
        member.setMemberAliveTime(XByteBuffer.toLong(alived, 0));
        member.setUniqueId(uniqueId);
        member.payload = payload;
        member.domain = domain;
        member.command = command;
        member.dataPkg = new byte[length];
        System.arraycopy(dataoffsetmember.dataPkg, 0, length);
        return member;
    }
    public static Member getMember(byte[] data) {
       return getMember(data,new MemberImpl());
    }
    public static Member getMember(byte[] dataint offsetint length) {
       return getMember(data,offset,length,new MemberImpl());
    }

    
Return the name of this object

Returns:
a unique name to the cluster
    @Override
    public String getName() {
        return "tcp://"+getHostname()+":"+getPort();
    }

    
Return the listen port of this member

Returns:
- tcp listen port
    @Override
    public int getPort()  {
        return this.;
    }

    
Return the TCP listen host for this member

Returns:
IP address or host name
    @Override
    public byte[] getHost()  {
        return ;
    }
    public String getHostname() {
        if ( this. != null ) return ;
        else {
            try {
                if ()
                    this. = java.net.InetAddress.getByAddress().getHostName();
                else
                    this. = org.apache.catalina.tribes.util.Arrays.toString(,0,.,true);
                return this.;
            }catch ( IOException x ) {
                throw new RuntimeException("Unable to parse hostname.",x);
            }
        }
    }
    public int getMsgCount() {
        return this.;
    }

    
Contains information on how long this member has been online. The result is the number of milli seconds this member has been broadcasting its membership to the cluster.

Returns:
nr of milliseconds since this member started.
    @Override
    public long getMemberAliveTime() {
       return ;
    }
    public long getServiceStartTime() {
        return ;
    }
    @Override
    public byte[] getUniqueId() {
        return ;
    }
    @Override
    public byte[] getPayload() {
        return ;
    }
    @Override
    public byte[] getCommand() {
        return ;
    }
    @Override
    public byte[] getDomain() {
        return ;
    }
    @Override
    public int getSecurePort() {
        return ;
    }
    @Override
    public int getUdpPort() {
        return ;
    }
    @Override
    public void setMemberAliveTime(long time) {
       =time;
    }



    
String representation of this object
    @Override
    public String toString()  {
        StringBuilder buf = new StringBuilder(getClass().getName());
        buf.append("[");
        buf.append(getName()).append(",");
        buf.append(getHostname()).append(",");
        buf.append().append(", alive=");
        buf.append().append(", ");
        buf.append("securePort=").append().append(", ");
        buf.append("UDP Port=").append().append(", ");
        buf.append("id=").append(bToS(this.)).append(", ");
        buf.append("payload=").append(bToS(this.,8)).append(", ");
        buf.append("command=").append(bToS(this.,8)).append(", ");
        buf.append("domain=").append(bToS(this.,8)).append(", ");
        buf.append("]");
        return buf.toString();
    }
    public static String bToS(byte[] data) {
        return bToS(data,data.length);
    }
    public static String bToS(byte[] dataint max) {
        StringBuilder buf = new StringBuilder(4*16);
        buf.append("{");
        for (int i=0; data!=null && i<data.lengthi++ ) {
            buf.append(String.valueOf(data[i])).append(" ");
            if ( i==max ) {
                buf.append("...("+data.length+")");
                break;
            }
        }
        buf.append("}");
        return buf.toString();
    }

    

Returns:
The hash code
See also:
java.lang.Object.hashCode()
    @Override
    public int hashCode() {
        return getHost()[0]+getHost()[1]+getHost()[2]+getHost()[3];
    }

    
Returns true if the param o is a McastMember with the same name

Parameters:
o
    @Override
    public boolean equals(Object o) {
        if ( o instanceof MemberImpl )    {
            return Arrays.equals(this.getHost(),((MemberImpl)o).getHost()) &&
                   this.getPort() == ((MemberImpl)o).getPort() &&
                   Arrays.equals(this.getUniqueId(),((MemberImpl)o).getUniqueId());
        }
        else
            return false;
    }
    public void setHost(byte[] host) {
        this. = host;
    }
    public void setHostname(String hostthrows IOException {
         = host;
        this. = java.net.InetAddress.getByName(host).getAddress();
    }
    public void setMsgCount(int msgCount) {
        this. = msgCount;
    }
    public void setPort(int port) {
        this. = port;
        this. = null;
    }
    public void setServiceStartTime(long serviceStartTime) {
        this. = serviceStartTime;
    }
    public void setUniqueId(byte[] uniqueId) {
        this. = uniqueId!=null?uniqueId:new byte[16];
        getData(true,true);
    }
    @Override
    public void setPayload(byte[] payload) {
        byte[] oldpayload = this.;
        this. = payload!=null?payload:new byte[0];
        if ( this.getData(true,true).length > . ) {
            this. = oldpayload;
            throw new IllegalArgumentException("Payload is to large for tribes to handle.");
        }
    }
    @Override
    public void setCommand(byte[] command) {
        this. = command!=null?command:new byte[0];
        getData(true,true);
    }
    public void setDomain(byte[] domain) {
        this. = domain!=null?domain:new byte[0];
        getData(true,true);
    }
    public void setSecurePort(int securePort) {
        this. = securePort;
        this. = null;
    }
    public void setUdpPort(int port) {
        this. = port;
        this. = null;
    }
    @Override
    public void readExternal(ObjectInput inthrows IOExceptionClassNotFoundException {
        int length = in.readInt();
        byte[] message = new byte[length];
        in.readFully(message);
        getMember(message,this);
    }
    @Override
    public void writeExternal(ObjectOutput outthrows IOException {
        byte[] data = this.getData();
        out.writeInt(data.length);
        out.write(data);
    }
New to GrepCode? Check out our FAQ X