001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes.group;
018:
019: import org.apache.catalina.tribes.Member;
020: import java.util.Comparator;
021: import java.util.Arrays;
022:
023: /**
024: * <p>Title: Membership - Absolute Order</p>
025: *
026: * <p>Description: A simple, yet agreeable and efficient way of ordering members</p>
027: * <p>
028: * Ordering members can serve as a basis for electing a leader or coordinating efforts.<br>
029: * This is stinky simple, it works on the basis of the <code>Member</code> interface
030: * and orders members in the following format:
031: *
032: * <ol>
033: * <li>IP comparison - byte by byte, lower byte higher rank</li>
034: * <li>IPv4 addresses rank higher than IPv6, ie the lesser number of bytes, the higher rank</li>
035: * <li>Port comparison - lower port, higher rank</li>
036: * <li>UniqueId comparison- byte by byte, lower byte higher rank</li>
037: * </ol>
038: *
039: * </p>
040: *
041: * @author Filip Hanik
042: * @version 1.0
043: * @see org.apache.catalina.tribes.Member
044: */
045: public class AbsoluteOrder {
046: public static final AbsoluteComparator comp = new AbsoluteComparator();
047:
048: protected AbsoluteOrder() {
049: super ();
050: }
051:
052: public static void absoluteOrder(Member[] members) {
053: if (members == null || members.length == 0)
054: return;
055: Arrays.sort(members, comp);
056: }
057:
058: public static class AbsoluteComparator implements Comparator {
059: public int compare(Object o1, Object o2) {
060: if (!((o1 instanceof Member) && (o2 instanceof Member)))
061: return 0;
062: return compareMembers((Member) o1, (Member) o2);
063: }
064:
065: public int compareMembers(Member m1, Member m2) {
066: int result = compareIps(m1, m2);
067: if (result == 0)
068: result = comparePorts(m1, m2);
069: if (result == 0)
070: result = compareIds(m1, m2);
071: return result;
072: }
073:
074: public int compareIps(Member m1, Member m2) {
075: return compareBytes(m1.getHost(), m2.getHost());
076: }
077:
078: public int comparePorts(Member m1, Member m2) {
079: return compareInts(m1.getPort(), m2.getPort());
080: }
081:
082: public int compareIds(Member m1, Member m2) {
083: return compareBytes(m1.getUniqueId(), m2.getUniqueId());
084: }
085:
086: protected int compareBytes(byte[] d1, byte[] d2) {
087: int result = 0;
088: if (d1.length == d2.length) {
089: for (int i = 0; (result == 0) && (i < d1.length); i++) {
090: result = compareBytes(d1[i], d2[i]);
091: }
092: } else if (d1.length < d2.length) {
093: result = -1;
094: } else {
095: result = 1;
096: }
097: return result;
098: }
099:
100: protected int compareBytes(byte b1, byte b2) {
101: return compareInts((int) b1, (int) b2);
102: }
103:
104: protected int compareInts(int b1, int b2) {
105: int result = 0;
106: if (b1 == b2) {
107:
108: } else if (b1 < b2) {
109: result = -1;
110: } else {
111: result = 1;
112: }
113: return result;
114: }
115: }
116:
117: }
|