KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > l2 > msg > ClusterStateMessage


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.msg;
6
7 import com.tc.l2.ha.ClusterState;
8 import com.tc.net.groups.AbstractGroupMessage;
9 import com.tc.net.groups.MessageID;
10 import com.tc.net.protocol.transport.ConnectionID;
11
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInput JavaDoc;
14 import java.io.ObjectOutput JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class ClusterStateMessage extends AbstractGroupMessage {
20
21   public static final int OBJECT_ID = 0x00;
22   public static final int NEW_CONNECTION_CREATED = 0x01;
23   public static final int CONNECTION_DESTROYED = 0x02;
24   public static final int COMPLETE_STATE = 0xF0;
25   public static final int OPERATION_SUCCESS = 0xFF;
26
27   private long nextAvailableObjectID;
28   private String JavaDoc clusterID;
29   private ConnectionID connectionID;
30   private long nextAvailableChannelID;
31   private Set JavaDoc connectionIDs;
32
33   // To make serialization happy
34
public ClusterStateMessage() {
35     super(-1);
36   }
37
38   public ClusterStateMessage(int type) {
39     super(type);
40   }
41
42   public ClusterStateMessage(int type, MessageID requestID) {
43     super(type, requestID);
44   }
45
46   public ClusterStateMessage(int type, ConnectionID connID) {
47     super(type);
48     this.connectionID = connID;
49   }
50
51   protected void basicReadExternal(int msgType, ObjectInput JavaDoc in) throws IOException JavaDoc {
52     switch (msgType) {
53       case OBJECT_ID:
54         nextAvailableObjectID = in.readLong();
55         break;
56       case NEW_CONNECTION_CREATED:
57       case CONNECTION_DESTROYED:
58         connectionID = readConnectionID(in);
59         break;
60       case COMPLETE_STATE:
61         nextAvailableObjectID = in.readLong();
62         nextAvailableChannelID = in.readLong();
63         clusterID = in.readUTF();
64         int size = in.readInt();
65         connectionIDs = new HashSet JavaDoc(size);
66         for (int i = 0; i < size; i++) {
67           connectionIDs.add(readConnectionID(in));
68         }
69         break;
70       case OPERATION_SUCCESS:
71         break;
72       default:
73         throw new AssertionError JavaDoc("Unknown type : " + msgType);
74     }
75   }
76
77   protected void basicWriteExternal(int msgType, ObjectOutput JavaDoc out) throws IOException JavaDoc {
78     switch (msgType) {
79       case OBJECT_ID:
80         out.writeLong(nextAvailableObjectID);
81         break;
82       case NEW_CONNECTION_CREATED:
83       case CONNECTION_DESTROYED:
84         writeConnectionID(connectionID, out);
85         break;
86       case COMPLETE_STATE:
87         out.writeLong(nextAvailableObjectID);
88         out.writeLong(nextAvailableChannelID);
89         out.writeUTF(clusterID);
90         out.writeInt(connectionIDs.size());
91         for (Iterator JavaDoc i = connectionIDs.iterator(); i.hasNext();) {
92           ConnectionID conn = (ConnectionID) i.next();
93           writeConnectionID(conn, out);
94         }
95         break;
96       case OPERATION_SUCCESS:
97         break;
98       default:
99         throw new AssertionError JavaDoc("Unknown type : " + msgType);
100     }
101   }
102
103   private void writeConnectionID(ConnectionID conn, ObjectOutput JavaDoc out) throws IOException JavaDoc {
104     out.writeLong(conn.getChannelID());
105     out.writeUTF(conn.getServerID());
106   }
107
108   private ConnectionID readConnectionID(ObjectInput JavaDoc in) throws IOException JavaDoc {
109     return new ConnectionID(in.readLong(), in.readUTF());
110   }
111
112   public long getNextAvailableObjectID() {
113     return nextAvailableObjectID;
114   }
115
116   public String JavaDoc getClusterID() {
117     return clusterID;
118   }
119
120   public ConnectionID getConnectionID() {
121     return connectionID;
122   }
123
124   public void initMessage(ClusterState state) {
125     switch (getType()) {
126       case OBJECT_ID:
127         nextAvailableObjectID = state.getNextAvailableObjectID();
128         break;
129       case COMPLETE_STATE:
130         nextAvailableObjectID = state.getNextAvailableObjectID();
131         nextAvailableChannelID = state.getNextAvailableChannelID();
132         clusterID = state.getClusterID();
133         connectionIDs = state.getAllConnections();
134         break;
135       default:
136         throw new AssertionError JavaDoc("Wrong Type : " + getType());
137     }
138   }
139
140   public void initState(ClusterState state) {
141     switch (getType()) {
142       case OBJECT_ID:
143         state.setNextAvailableObjectID(nextAvailableObjectID);
144         break;
145       case COMPLETE_STATE:
146         state.setNextAvailableObjectID(nextAvailableObjectID);
147         state.setNextAvailableChannelID(nextAvailableChannelID);
148         state.setClusterID(clusterID);
149         for (Iterator JavaDoc i = connectionIDs.iterator(); i.hasNext();) {
150           ConnectionID conn = (ConnectionID) i.next();
151           state.addNewConnection(conn);
152         }
153         break;
154       case NEW_CONNECTION_CREATED:
155         state.addNewConnection(connectionID);
156         break;
157       case CONNECTION_DESTROYED:
158         state.removeConnection(connectionID);
159         break;
160       default:
161         throw new AssertionError JavaDoc("Wrong Type : " + getType());
162     }
163   }
164
165 }
166
Popular Tags