KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > mcast > McastMember


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.cluster.mcast;
18
19 import org.apache.catalina.cluster.Member;
20
21 /**
22  * A <b>membership</b> implementation using simple multicast.
23  * This is the representation of a multicast member.
24  * Carries the host, and port of the this or other cluster nodes.
25  *
26  * @author Filip Hanik
27  * @version $Revision: 1.6 $, $Date: 2004/07/01 09:44:26 $
28  */

29
30 import org.apache.catalina.cluster.io.XByteBuffer;
31 public class McastMember implements Member, java.io.Serializable JavaDoc {
32
33     /**
34      * Digits, used for "superfast" de-serialization of an
35      * IP address
36      */

37     final transient static char[] digits = {
38         '0', '1', '2', '3', '4', '5',
39         '6', '7', '8', '9'};
40
41     /**
42      * Public properties specific to this implementation
43      */

44     public static final transient String JavaDoc TCP_LISTEN_PORT = "tcpListenPort";
45     public static final transient String JavaDoc TCP_LISTEN_HOST = "tcpListenHost";
46     public static final transient String JavaDoc MEMBER_NAME = "memberName";
47
48     /**
49      * The listen host for this member
50      */

51     protected String JavaDoc host;
52     /**
53      * The tcp listen port for this member
54      */

55     protected int port;
56     /**
57      * The name for this member, has be be unique within the cluster.
58      */

59     private String JavaDoc name;
60     /**
61      * Counter for how many messages have been sent from this member
62      */

63     protected int msgCount = 0;
64     /**
65      * The number of milliseconds since this members was
66      * created, is kept track of using the start time
67      */

68     protected long memberAliveTime = 0;
69
70
71     /**
72      * Construct a new member object
73      * @param name - the name of this member, cluster unique
74      * @param host - the tcp listen host
75      * @param port - the tcp listen port
76      */

77     public McastMember(String JavaDoc name,
78                        String JavaDoc 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     /**
88      *
89      * @return a Hashmap containing the following properties:<BR>
90      * 1. tcpListenPort - the port this member listens to for messages - string<BR>
91      * 2. tcpListenHost - the host address of this member - string<BR>
92      * 3. memberName - the name of this member - string<BR>
93      */

94     public java.util.HashMap JavaDoc getMemberProperties() {
95         java.util.HashMap JavaDoc map = new java.util.HashMap JavaDoc(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     /**
103      * Increment the message count.
104      */

105     protected void inc() {
106         msgCount++;
107     }
108
109     /**
110      * Create a data package to send over the wire representing this member.
111      * This is faster than serialization.
112      * @return - the bytes for this member deserialized
113      * @throws Exception
114      */

115     protected byte[] getData(long startTime) throws Exception JavaDoc {
116         //package looks like
117
//alive - 8 bytes
118
//port - 4 bytes
119
//host - 4 bytes
120
//name - remaining bytes
121
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     /**
132      * Deserializes a member from data sent over the wire
133      * @param data - the bytes received
134      * @return a member object.
135      */

136     protected static McastMember getMember(byte[] data) {
137        //package looks like
138
//alive - 8 bytes
139
//port - 4 bytes
140
//host - 4 bytes
141
//name - remaining bytes
142
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 JavaDoc(named), addressToString(addr),
151                               XByteBuffer.toInt(portd, 0),
152                               XByteBuffer.toLong(alived, 0));
153     }
154
155     /**
156      * Return the name of this object
157      * @return a unique name to the cluster
158      */

159     public String JavaDoc getName() {
160         return name;
161     }
162
163     /**
164      * Return the listen port of this member
165      * @return - tcp listen port
166      */

167     public int getPort() {
168         return this.port;
169     }
170
171     /**
172      * Return the TCP listen host for this member
173      * @return IP address or host name
174      */

175     public String JavaDoc getHost() {
176         return this.host;
177     }
178
179     /**
180      * Contains information on how long this member has been online.
181      * The result is the number of milli seconds this member has been
182      * broadcasting its membership to the cluster.
183      * @return nr of milliseconds since this member started.
184      */

185     public long getMemberAliveTime() {
186        return memberAliveTime;
187     }
188
189     public void setMemberAliveTime(long time) {
190        memberAliveTime=time;
191     }
192
193
194
195     /**
196      * String representation of this object
197      */

198     public String JavaDoc toString() {
199         return "org.apache.catalina.cluster.mcast.McastMember["+name+","+host+","+port+", alive="+memberAliveTime+"]";
200     }
201
202     /**
203      * @see java.lang.Object#hashCode()
204      * @return
205      */

206     public int hashCode() {
207         return this.name.hashCode();
208     }
209
210     /**
211      * Returns true if the param o is a McastMember with the same name
212      * @param o
213      */

214     public boolean equals(Object JavaDoc o) {
215         if ( o instanceof McastMember ) {
216             return this.name.equals(((McastMember)o).getName());
217         }
218         else
219             return false;
220     }
221
222     /**
223      * Converts for bytes (ip address) to a string representation of it<BR>
224      * Highly optimized method.
225      * @param address (4 bytes ip address)
226      * @return string representation of that ip address
227      */

228     private static final String JavaDoc 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 JavaDoc(buf, charPos, 15 - charPos);
281     }
282     public void setHost(String JavaDoc host) {
283         this.host = host;
284     }
285     public void setMsgCount(int msgCount) {
286         this.msgCount = msgCount;
287     }
288     public void setName(String JavaDoc name) {
289         this.name = name;
290     }
291     public void setPort(int port) {
292         this.port = port;
293     }
294 }
295
Popular Tags