KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > msg > ClusterMembershipMessage


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.object.msg;
6
7 import com.tc.bytes.TCByteBuffer;
8 import com.tc.io.TCByteBufferOutput;
9 import com.tc.net.protocol.tcm.ChannelID;
10 import com.tc.net.protocol.tcm.MessageChannel;
11 import com.tc.net.protocol.tcm.MessageMonitor;
12 import com.tc.net.protocol.tcm.TCMessageHeader;
13 import com.tc.net.protocol.tcm.TCMessageType;
14 import com.tc.object.session.SessionID;
15
16 import java.io.IOException JavaDoc;
17
18 public class ClusterMembershipMessage extends DSOMessageBase {
19   private static final byte EVENT_TYPE = 0;
20   private static final byte NODE_ID = 1;
21
22   private int eventType;
23   private String JavaDoc nodeId;
24
25   public ClusterMembershipMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
26                                   TCMessageType type) {
27     super(monitor, out, channel, type);
28   }
29
30   public ClusterMembershipMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel,
31                                   TCMessageHeader header, TCByteBuffer[] data) {
32     super(sessionID, monitor, channel, header, data);
33   }
34
35   public void initialize(int et, ChannelID cid, MessageChannel[] channels) {
36     eventType = et;
37     nodeId = toString(cid);
38   }
39
40   private String JavaDoc toString(ChannelID cid) {
41     return String.valueOf(cid.toLong());
42   }
43
44   protected void dehydrateValues() {
45     putNVPair(EVENT_TYPE, eventType);
46     putNVPair(NODE_ID, nodeId);
47   }
48
49   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
50     switch (name) {
51       case EVENT_TYPE:
52         eventType = getIntValue();
53         return true;
54       case NODE_ID:
55         nodeId = getStringValue();
56         return true;
57       default:
58         return false;
59     }
60   }
61
62   public boolean isNodeConnectedEvent() {
63     return EventType.isNodeConnected(eventType);
64   }
65
66   public boolean isNodeDisconnectedEvent() {
67     return EventType.isNodeDisconnected(eventType);
68   }
69
70   public int getEventType() {
71     return eventType;
72   }
73
74   public String JavaDoc getNodeId() {
75     return nodeId;
76   }
77
78   protected String JavaDoc describePayload() {
79     return EventType.toString(eventType) + " nodeId=" + nodeId;
80   }
81
82   public static class EventType {
83     public static final int NODE_CONNECTED = 0;
84     public static final int NODE_DISCONNECTED = 1;
85
86     public static boolean isValidType(final int t) {
87       return t >= NODE_CONNECTED && t <= NODE_DISCONNECTED;
88     }
89
90     public static boolean isNodeConnected(final int t) {
91       return t == NODE_CONNECTED;
92     }
93
94     public static boolean isNodeDisconnected(final int t) {
95       return t == NODE_DISCONNECTED;
96     }
97
98     public static String JavaDoc toString(int eventType) {
99       switch (eventType) {
100         case NODE_CONNECTED:
101           return "NODE_CONNECTED";
102         case NODE_DISCONNECTED:
103           return "NODE_DISCONNECTED";
104         default:
105           return "UNKNOWN";
106       }
107     }
108   }
109 }
110
Popular Tags