KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.groups.AbstractGroupMessage;
8 import com.tc.net.groups.MessageID;
9 import com.tc.object.ObjectID;
10 import com.tc.util.Assert;
11 import com.tc.util.ObjectIDSet2;
12
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInput JavaDoc;
15 import java.io.ObjectOutput JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class ObjectListSyncMessage extends AbstractGroupMessage {
20
21   public static final int REQUEST = 0;
22   public static final int RESPONSE = 1;
23
24   private Set JavaDoc oids;
25
26   // To make serialization happy
27
public ObjectListSyncMessage() {
28     super(-1);
29   }
30
31   public ObjectListSyncMessage(int type) {
32     super(type);
33   }
34
35   public ObjectListSyncMessage(MessageID messageID, int type, Set JavaDoc oids) {
36     super(type, messageID);
37     this.oids = oids;
38   }
39
40   protected void basicReadExternal(int msgType, ObjectInput JavaDoc in) throws IOException JavaDoc {
41     switch (msgType) {
42       case REQUEST:
43         // Nothing to read
44
break;
45       case RESPONSE:
46         oids = new ObjectIDSet2();
47         int size = in.readInt();
48         for (int i = 0; i < size; i++) {
49           oids.add(new ObjectID(in.readLong()));
50         }
51         break;
52       default:
53         throw new AssertionError JavaDoc("Unknown Message Type : " + msgType);
54     }
55   }
56
57   protected void basicWriteExternal(int msgType, ObjectOutput JavaDoc out) throws IOException JavaDoc {
58     switch (msgType) {
59       case REQUEST:
60         // Nothing to write
61
break;
62       case RESPONSE:
63         Assert.assertNotNull(oids);
64         out.writeInt(oids.size());
65         for (Iterator JavaDoc i = oids.iterator(); i.hasNext();) {
66           ObjectID oid = (ObjectID) i.next();
67           out.writeLong(oid.toLong());
68         }
69         break;
70       default:
71         throw new AssertionError JavaDoc("Unknown Message Type : " + msgType);
72     }
73   }
74
75   public Set JavaDoc getObjectIDs() {
76     Assert.assertNotNull(oids);
77     return oids;
78   }
79
80   public String JavaDoc toString() {
81     return "ObjectListSyncMessage [ " + messageFrom() + ", type = " + getTypeString() + ", " + oids + "]";
82   }
83
84   private String JavaDoc getTypeString() {
85     switch (getType()) {
86       case REQUEST:
87         return "REQUEST";
88       case RESPONSE:
89         return "RESPONSE";
90       default:
91         throw new AssertionError JavaDoc("Unknow Type ! : " + getType());
92     }
93   }
94
95 }
96
Popular Tags