1 /* 2 * Copyright (c) 1998-2002 The Jgroup Team. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package jgroup.relacs.events; 20 21 import java.io.Externalizable; 22 import java.io.IOException; 23 import java.io.ObjectInput; 24 import java.io.ObjectOutput; 25 26 /** 27 * The <code>Event</code> class is the common superclass for all events 28 * that may be exchanged between the daemon and members. Different 29 * types of events are distinguished using an integer tag, that may be 30 * obtained using method <code>getTag()</code>. Each event is 31 * associated to a group identifier, that may be obtained using method 32 * <code>getGid()</code>. The group identifier is needed because an 33 * application may be member of most than one group at a time. 34 * 35 * @author Alberto Montresor 36 * @since Jgroup 1.2 37 */ 38 public class Event 39 implements EventTags, Externalizable 40 { 41 42 private static final long serialVersionUID = 7265112762984225812L; 43 44 //////////////////////////////////////////////////////////////////////////////////////////// 45 // Fields 46 //////////////////////////////////////////////////////////////////////////////////////////// 47 48 /** Tag identifying the event */ 49 protected int tag; 50 51 /** Group identifier */ 52 protected int gid; 53 54 55 //////////////////////////////////////////////////////////////////////////////////////////// 56 // Constructors 57 //////////////////////////////////////////////////////////////////////////////////////////// 58 59 /** 60 * Default constructor for externalization. 61 */ 62 public Event() 63 { 64 } 65 66 /** 67 * Constructs a new generic <CODE>Event</CODE> object. 68 * 69 * @param tag tag identifying the event 70 * @param gid group identifier 71 */ 72 public Event(int tag, int gid) 73 { 74 this.tag = tag; 75 this.gid = gid; 76 } 77 78 79 //////////////////////////////////////////////////////////////////////////////////////////// 80 // Methods 81 //////////////////////////////////////////////////////////////////////////////////////////// 82 83 /** 84 * Returns the tag identifying this event. 85 */ 86 public int getTag() 87 { 88 return tag; 89 } 90 91 92 /** 93 * Returns the group identifier associated with this event. 94 */ 95 public int getGid() 96 { 97 return gid; 98 } 99 100 101 //////////////////////////////////////////////////////////////////////////////////////////// 102 // Methods for externalization 103 //////////////////////////////////////////////////////////////////////////////////////////// 104 105 /** 106 * Restores the content of this object from the marshalled data 107 * contained in the specified input stream. 108 * 109 * @param in the stream to be read 110 */ 111 public void readExternal(ObjectInput in) 112 throws IOException, ClassNotFoundException 113 { 114 tag = in.readInt(); 115 gid = in.readInt(); 116 } 117 118 119 /** 120 * Marshals the content of this object to the specified output 121 * stream. 122 * 123 * @param out the stream to be written 124 */ 125 public void writeExternal(ObjectOutput out) 126 throws IOException 127 { 128 out.writeInt(tag); 129 out.writeInt(gid); 130 } 131 132 133 //////////////////////////////////////////////////////////////////////////////////////////// 134 // Methods from Object 135 //////////////////////////////////////////////////////////////////////////////////////////// 136 137 public String toString() 138 { 139 StringBuilder buf = new StringBuilder(); 140 buf.append("[Event: tag="); 141 switch (tag) { 142 case DELIVERY_EVENT: 143 buf.append("DELIVERY_EVENT"); 144 break; 145 case DELIVERY_ACK: 146 buf.append("DELIVERY_ACK"); 147 break; 148 case INSTALL_EVENT: 149 buf.append("INSTALL_EVENT"); 150 break; 151 case INSTALL_ACK: 152 buf.append("INSTALL_ACK"); 153 break; 154 case PREPARE_EVENT: 155 buf.append("PREPARE_EVENT"); 156 break; 157 case PREPARE_ACK: 158 buf.append("PREPARE_ACK"); 159 break; 160 case MULTICAST_REQUEST: 161 buf.append("MULTICAST_REQUEST"); 162 break; 163 case JOIN_REQUEST: 164 buf.append("JOIN_REQUEST"); 165 break; 166 case LEAVE_REQUEST: 167 buf.append("LEAVE_REQUEST"); 168 break; 169 case MEMBER_LEFT_EVENT: 170 buf.append("MEMBER_LEFT_EVENT"); 171 break; 172 case DAEMON_FAILURE_EVENT: 173 buf.append("DAEMON_FAILURE_EVENT"); 174 break; 175 default: 176 buf.append("UNKNOWN EVENT TAG: " + tag); 177 break; 178 } 179 buf.append(", gid="); 180 buf.append(gid); 181 buf.append("]"); 182 return buf.toString(); 183 } 184 185 } // END Event