1 /*** 2 * 3 * Copyright 2004 Protique Ltd 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 19 package org.codehaus.activecluster; 20 import java.io.Externalizable; 21 import java.io.IOException; 22 import java.io.ObjectInput; 23 import java.io.ObjectOutput; 24 25 /*** 26 * A cluster event 27 * 28 * @version $Revision: 1.2 $ 29 */ 30 public class ClusterEvent implements Externalizable { 31 /*** 32 * A node has joined the cluster 33 */ 34 public static final int ADD_NODE = 1; 35 /*** 36 * existing node has updated it's state 37 */ 38 public static final int UPDATE_NODE = 2; 39 /*** 40 * A node has left the Cluster 41 */ 42 public static final int REMOVE_NODE = 3; 43 /*** 44 * A node has failed due to a system/network error 45 */ 46 public static final int FAILED_NODE = 4; 47 48 /*** 49 * this node has been elected Coordinator 50 */ 51 public static final int ELECTED_COORDINATOR = 5; 52 53 private transient Cluster cluster; 54 private Node node; 55 private int type; 56 57 /*** 58 * empty constructor 59 */ 60 public ClusterEvent() { 61 } 62 63 /*** 64 * @param source 65 * @param node 66 * @param type 67 */ 68 public ClusterEvent(Cluster source, Node node, int type) { 69 this.cluster = source; 70 this.node = node; 71 this.type = type; 72 } 73 74 /*** 75 * @return the Cluster 76 */ 77 public Cluster getCluster() { 78 return cluster; 79 } 80 81 /*** 82 * set the cluster 83 * @param source 84 */ 85 public void setCluster(Cluster source){ 86 this.cluster = source; 87 } 88 /*** 89 * @return the node 90 */ 91 public Node getNode() { 92 return node; 93 } 94 95 /*** 96 * @return the type of event 97 */ 98 public int getType() { 99 return type; 100 } 101 102 /*** 103 * @return pretty type 104 */ 105 public String toString() { 106 return "ClusterEvent[" + getTypeAsString() + " : " + node + "]"; 107 } 108 109 /*** 110 * dump on to a stream 111 * 112 * @param out 113 * @throws IOException 114 */ 115 public void writeExternal(ObjectOutput out) throws IOException { 116 out.writeByte(type); 117 out.writeObject(node); 118 } 119 120 /*** 121 * read from stream 122 * 123 * @param in 124 * @throws IOException 125 * @throws ClassNotFoundException 126 */ 127 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 128 type = in.readByte(); 129 node = (Node) in.readObject(); 130 } 131 132 private String getTypeAsString() { 133 String result = "unknown type"; 134 if (type == ADD_NODE) { 135 result = "ADD_NODE"; 136 } 137 else if (type == REMOVE_NODE) { 138 result = "REMOVE_NODE"; 139 } 140 else if (type == UPDATE_NODE) { 141 result = "UPDATE_NODE"; 142 } 143 else if (type == FAILED_NODE) { 144 result = "FAILED_NODE"; 145 } 146 return result; 147 } 148 }