KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > l2 > state > Enrollment


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.l2.state;
6
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9 import com.tc.net.groups.NodeID;
10 import com.tc.util.Assert;
11
12 import java.io.Externalizable JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInput JavaDoc;
15 import java.io.ObjectOutput JavaDoc;
16
17 public class Enrollment implements Externalizable JavaDoc {
18
19   private static final TCLogger logger = TCLogging.getLogger(Enrollment.class);
20   private NodeID nodeID;
21   private int[] weights;
22   private boolean isNew;
23
24   public Enrollment() {
25     // To make serialization happy
26
}
27
28   public Enrollment(NodeID nodeID, boolean isNew, int[] weights) {
29     this.nodeID = nodeID;
30     this.isNew = isNew;
31     Assert.assertNotNull(weights);
32     this.weights = weights;
33   }
34
35   public NodeID getNodeID() {
36     return nodeID;
37   }
38
39   public boolean isANewCandidate() {
40     return isNew;
41   }
42
43   public boolean wins(Enrollment other) {
44     if (isNew != other.isNew) {
45       // an old candidate always wins over a new candidate
46
return !isNew;
47     }
48     int myLength = weights.length;
49     int otherLength = other.weights.length;
50     if (myLength > otherLength) {
51       return true;
52     } else if (myLength < otherLength) {
53       return false;
54     } else {
55       for (int i = 0; i < myLength; i++) {
56         if (weights[i] > other.weights[i]) {
57           // wins
58
return true;
59         } else if (weights[i] < other.weights[i]) {
60           // loses
61
return false;
62         }
63       }
64
65       // XXX:: Both are the same weight. This should happen once we fix the weights to
66
// be unique (based on hardware,ip,process id etc.) But now it is possible and we
67
// handle it. If two nodes dont agree because of this there will be a re-election
68
logger.warn("Two Enrollments with same weights : " + this + " == " + other);
69       return false;
70     }
71   }
72
73   public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
74     this.nodeID = (NodeID) in.readObject();
75     this.isNew = in.readBoolean();
76     this.weights = new int[in.readInt()];
77     for (int i = 0; i < weights.length; i++) {
78       weights[i] = in.readInt();
79     }
80   }
81
82   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
83     out.writeObject(this.nodeID);
84     out.writeBoolean(this.isNew);
85     out.writeInt(weights.length);
86     for (int i = 0; i < weights.length; i++) {
87       out.writeInt(weights[i]);
88     }
89   }
90
91   public int hashCode() {
92     return nodeID.hashCode();
93   }
94
95   public boolean equals(Object JavaDoc o) {
96     if (o instanceof Enrollment) {
97       Enrollment oe = (Enrollment) o;
98       return nodeID.equals(oe.nodeID);
99     }
100     return false;
101   }
102
103   public String JavaDoc toString() {
104     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Enrollment [ ");
105     sb.append(nodeID).append(", isNew = ").append(isNew);
106     sb.append(", weights = ");
107     int length = weights.length;
108     for (int i = 0; i < length; i++) {
109       sb.append(weights[i]);
110       if (i < length - 1) {
111         sb.append(",");
112       }
113     }
114     sb.append(" ]");
115     return sb.toString();
116   }
117
118 }
119
Popular Tags