1 16 17 package org.apache.catalina.cluster.mcast; 18 19 import org.apache.catalina.cluster.Member; 20 21 29 30 import org.apache.catalina.cluster.io.XByteBuffer; 31 public class McastMember implements Member, java.io.Serializable { 32 33 37 final transient static char[] digits = { 38 '0', '1', '2', '3', '4', '5', 39 '6', '7', '8', '9'}; 40 41 44 public static final transient String TCP_LISTEN_PORT = "tcpListenPort"; 45 public static final transient String TCP_LISTEN_HOST = "tcpListenHost"; 46 public static final transient String MEMBER_NAME = "memberName"; 47 48 51 protected String host; 52 55 protected int port; 56 59 private String name; 60 63 protected int msgCount = 0; 64 68 protected long memberAliveTime = 0; 69 70 71 77 public McastMember(String name, 78 String host, 79 int port, 80 long aliveTime) { 81 this.host = host; 82 this.port = port; 83 this.name = name; 84 this.memberAliveTime=aliveTime; 85 } 86 87 94 public java.util.HashMap getMemberProperties() { 95 java.util.HashMap map = new java.util.HashMap (2); 96 map.put(McastMember.TCP_LISTEN_HOST,this.host); 97 map.put(McastMember.TCP_LISTEN_PORT,String.valueOf(this.port)); 98 map.put(McastMember.MEMBER_NAME,name); 99 return map; 100 } 101 102 105 protected void inc() { 106 msgCount++; 107 } 108 109 115 protected byte[] getData(long startTime) throws Exception { 116 byte[] named = getName().getBytes(); 122 byte[] addr = java.net.InetAddress.getByName(host).getAddress(); 123 byte[] data = new byte[8+4+addr.length+named.length]; 124 long alive=System.currentTimeMillis()-startTime; 125 System.arraycopy(XByteBuffer.toBytes((long)alive),0,data,0,8); 126 System.arraycopy(XByteBuffer.toBytes(port),0,data,8,4); 127 System.arraycopy(addr,0,data,12,addr.length); 128 System.arraycopy(named,0,data,8+4+addr.length,named.length); 129 return data; 130 } 131 136 protected static McastMember getMember(byte[] data) { 137 byte[] alived = new byte[8]; 143 System.arraycopy(data, 0, alived, 0, 8); 144 byte[] portd = new byte[4]; 145 System.arraycopy(data, 8, portd, 0, 4); 146 byte[] addr = new byte[4]; 147 System.arraycopy(data, 12, addr, 0, 4); 148 byte[] named = new byte[data.length - 16]; 149 System.arraycopy(data, 16, named, 0, named.length); 150 return new McastMember(new String (named), addressToString(addr), 151 XByteBuffer.toInt(portd, 0), 152 XByteBuffer.toLong(alived, 0)); 153 } 154 155 159 public String getName() { 160 return name; 161 } 162 163 167 public int getPort() { 168 return this.port; 169 } 170 171 175 public String getHost() { 176 return this.host; 177 } 178 179 185 public long getMemberAliveTime() { 186 return memberAliveTime; 187 } 188 189 public void setMemberAliveTime(long time) { 190 memberAliveTime=time; 191 } 192 193 194 195 198 public String toString() { 199 return "org.apache.catalina.cluster.mcast.McastMember["+name+","+host+","+port+", alive="+memberAliveTime+"]"; 200 } 201 202 206 public int hashCode() { 207 return this.name.hashCode(); 208 } 209 210 214 public boolean equals(Object o) { 215 if ( o instanceof McastMember ) { 216 return this.name.equals(((McastMember)o).getName()); 217 } 218 else 219 return false; 220 } 221 222 228 private static final String addressToString(byte[] address) { 229 int q, r = 0; 230 int charPos = 15; 231 char[] buf = new char[15]; 232 char dot = '.'; 233 234 int i = address[3] & 0xFF; 235 for (; ; ) 236 { 237 q = (i * 52429) >>> (19); 238 r = i - ( (q << 3) + (q << 1)); 239 buf[--charPos] = digits[r]; 240 i = q; 241 if (i == 0) 242 break; 243 } 244 buf[--charPos] = dot; 245 i = address[2] & 0xFF; 246 for (; ; ) 247 { 248 q = (i * 52429) >>> (19); 249 r = i - ( (q << 3) + (q << 1)); 250 buf[--charPos] = digits[r]; 251 i = q; 252 if (i == 0) 253 break; 254 } 255 buf[--charPos] = dot; 256 257 i = address[1] & 0xFF; 258 for (; ; ) 259 { 260 q = (i * 52429) >>> (19); 261 r = i - ( (q << 3) + (q << 1)); 262 buf[--charPos] = digits[r]; 263 i = q; 264 if (i == 0) 265 break; 266 } 267 268 buf[--charPos] = dot; 269 i = address[0] & 0xFF; 270 271 for (; ; ) 272 { 273 q = (i * 52429) >>> (19); 274 r = i - ( (q << 3) + (q << 1)); 275 buf[--charPos] = digits[r]; 276 i = q; 277 if (i == 0) 278 break; 279 } 280 return new String (buf, charPos, 15 - charPos); 281 } 282 public void setHost(String host) { 283 this.host = host; 284 } 285 public void setMsgCount(int msgCount) { 286 this.msgCount = msgCount; 287 } 288 public void setName(String name) { 289 this.name = name; 290 } 291 public void setPort(int port) { 292 this.port = port; 293 } 294 } 295 | Popular Tags |