001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.msg;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.io.TCByteBufferOutput;
009: import com.tc.net.groups.NodeID;
010: import com.tc.net.protocol.tcm.MessageChannel;
011: import com.tc.net.protocol.tcm.MessageMonitor;
012: import com.tc.net.protocol.tcm.TCMessageHeader;
013: import com.tc.net.protocol.tcm.TCMessageType;
014: import com.tc.object.session.SessionID;
015:
016: import java.io.IOException;
017:
018: public class ClusterMembershipMessage extends DSOMessageBase {
019: private static final byte EVENT_TYPE = 0;
020: private static final byte NODE_ID = 1;
021:
022: private int eventType;
023: private String nodeId;
024:
025: public ClusterMembershipMessage(SessionID sessionID,
026: MessageMonitor monitor, TCByteBufferOutput out,
027: MessageChannel channel, TCMessageType type) {
028: super (sessionID, monitor, out, channel, type);
029: }
030:
031: public ClusterMembershipMessage(SessionID sessionID,
032: MessageMonitor monitor, MessageChannel channel,
033: TCMessageHeader header, TCByteBuffer[] data) {
034: super (sessionID, monitor, channel, header, data);
035: }
036:
037: public void initialize(int et, NodeID nodeID2,
038: MessageChannel[] channels) {
039: eventType = et;
040: nodeId = nodeID2.toString();
041: }
042:
043: protected void dehydrateValues() {
044: putNVPair(EVENT_TYPE, eventType);
045: putNVPair(NODE_ID, nodeId);
046: }
047:
048: protected boolean hydrateValue(byte name) throws IOException {
049: switch (name) {
050: case EVENT_TYPE:
051: eventType = getIntValue();
052: return true;
053: case NODE_ID:
054: nodeId = getStringValue();
055: return true;
056: default:
057: return false;
058: }
059: }
060:
061: public boolean isNodeConnectedEvent() {
062: return EventType.isNodeConnected(eventType);
063: }
064:
065: public boolean isNodeDisconnectedEvent() {
066: return EventType.isNodeDisconnected(eventType);
067: }
068:
069: public int getEventType() {
070: return eventType;
071: }
072:
073: public String getNodeId() {
074: return nodeId;
075: }
076:
077: protected String describePayload() {
078: return EventType.toString(eventType) + " nodeId=" + nodeId;
079: }
080:
081: public static class EventType {
082: public static final int NODE_CONNECTED = 0;
083: public static final int NODE_DISCONNECTED = 1;
084:
085: public static boolean isValidType(final int t) {
086: return t >= NODE_CONNECTED && t <= NODE_DISCONNECTED;
087: }
088:
089: public static boolean isNodeConnected(final int t) {
090: return t == NODE_CONNECTED;
091: }
092:
093: public static boolean isNodeDisconnected(final int t) {
094: return t == NODE_DISCONNECTED;
095: }
096:
097: public static String toString(int eventType) {
098: switch (eventType) {
099: case NODE_CONNECTED:
100: return "NODE_CONNECTED";
101: case NODE_DISCONNECTED:
102: return "NODE_DISCONNECTED";
103: default:
104: return "UNKNOWN";
105: }
106: }
107: }
108: }
|