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.daemon;
20
21
22 import java.io.IOException;
23 import java.io.ObjectInput;
24
25 import jgroup.core.EndPoint;
26 import jgroup.relacs.mss.MssConstants;
27 import jgroup.relacs.types.EndPointImpl;
28 import jgroup.relacs.types.GroupId;
29 import jgroup.relacs.types.GroupIndex;
30 import jgroup.relacs.types.VersionId;
31 import jgroup.util.MsgFactory;
32 import jgroup.util.OutMessage;
33
34 /**
35 * The <code> MsgSynch</code> class
36 *
37 * @author Alberto Montresor
38 * @since Jgroup 0.1
39 */
40 final class MsgSynch
41 implements MssConstants, DaemonMsg
42 {
43
44 ////////////////////////////////////////////////////////////////////////////////////////////
45 // Position constants
46 ////////////////////////////////////////////////////////////////////////////////////////////
47
48 private static final int START = MSS_HEADER_SIZE;
49
50
51 ////////////////////////////////////////////////////////////////////////////////////////////
52 // Unique message instance
53 ////////////////////////////////////////////////////////////////////////////////////////////
54
55 /**
56 * As these messages are never stored by the daemon, a single message
57 * instance is sufficient. In this way, we save the costs of
58 * allocating and garbage collecting messages.
59 */
60 private static MsgSynch msg = new MsgSynch();
61
62 /** The marshalled message data (also static for same reason as above) */
63 private static OutMessage outmsg;
64
65
66 ////////////////////////////////////////////////////////////////////////////////////////////
67 // Fields
68 ////////////////////////////////////////////////////////////////////////////////////////////
69
70 int vsend; // Sender version number
71 int vdest; // Destination version number
72 EndPoint[] rset; // Reachable set (32 bit adressess)
73
74
75 ////////////////////////////////////////////////////////////////////////////////////////////
76 // Decoding methods
77 ////////////////////////////////////////////////////////////////////////////////////////////
78
79 /**
80 * Decodes a <code>MsgSynch</code> message starting from a m-received
81 * message input stream.
82 */
83 static MsgSynch unmarshal(ObjectInput stream)
84 throws IOException, ClassNotFoundException
85 {
86 // Read data
87 msg.vsend = VersionId.unmarshal(stream);
88 msg.vdest = VersionId.unmarshal(stream);
89 int len = GroupIndex.unmarshal(stream); // Length of the rset field
90 msg.rset = new EndPoint[len];
91 for (int i=0; i < len; i++) {
92 msg.rset[i] = new EndPointImpl();
93 msg.rset[i].readExternal(stream);
94 }
95 return msg;
96 }
97
98
99 ////////////////////////////////////////////////////////////////////////////////////////////
100 // Encoding methods
101 ////////////////////////////////////////////////////////////////////////////////////////////
102
103 /**
104 * Computes the encoding of a <code> MsgSynch</code> message.
105 *
106 * @param gid group identifier.
107 * @param vsend version number of the sender host
108 * @param vdest version number of the destination host
109 * @param rset reachable set
110 */
111 static MsgSynch marshal(int gid, int vsend, int vdest, EndPoint[] rset)
112 throws IOException
113 {
114 /* Allocates data buffer */
115 int size = START + GroupId.SIZE + 2*VersionId.SIZE + GroupIndex.SIZE
116 + rset.length * EndPointImpl.SIZE;
117 outmsg = MsgFactory.get(size, 1);
118 outmsg.seek(START);
119
120 /* Write group identifier and sender, destination version numbers */
121 GroupId.marshal(outmsg, gid);
122 VersionId.marshal(outmsg, vsend);
123 VersionId.marshal(outmsg, vdest);
124
125 /*
126 * Writes the identifier of the endpoints contained in rset
127 */
128 GroupIndex.marshal(outmsg, rset.length);
129 for (int i=0; i < rset.length; i++)
130 rset[i].writeExternal(outmsg);
131 return msg;
132 }
133
134
135 ////////////////////////////////////////////////////////////////////////////////////////////
136 // Methods from DaemonMsg
137 ////////////////////////////////////////////////////////////////////////////////////////////
138
139 public int size()
140 {
141 /* Note; it is ok to use the outmsg size since MsgSynch will never be
142 * forwarded or resent (it is not queued). */
143 return outmsg.size();
144 }
145
146 public OutMessage getOutMessage()
147 {
148 return outmsg;
149 }
150
151
152 ////////////////////////////////////////////////////////////////////////////////////////////
153 // Methods from Object
154 ////////////////////////////////////////////////////////////////////////////////////////////
155
156 /**
157 * Returns a string representation of this object
158 */
159 public String toString()
160 {
161 StringBuilder buffer = new StringBuilder();
162 buffer.append("[MsgSynch: vsend=");
163 buffer.append(vsend);
164 buffer.append(", vdest=");
165 buffer.append(vdest);
166 buffer.append(", rset={");
167 for (int i=0; i < rset.length; i++) {
168 buffer.append(rset[i]);
169 if (i < rset.length-1)
170 buffer.append(", ");
171 }
172 buffer.append("}]");
173 return buffer.toString();
174 }
175
176 } // END MsgSynch