1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.tribes.io;
import java.sql.Timestamp;
import java.util.Arrays;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ChannelMessage;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.membership.MemberImpl;
import org.apache.catalina.tribes.util.UUIDGenerator;
/**
* The <code>ChannelData</code> object is used to transfer a message through the channel interceptor stack and
* eventually out on a transport to be sent to another node. While the message is being processed by the different
* interceptors, the message data can be manipulated as each interceptor seems appropriate.
*/
public class ChannelData implements ChannelMessage {
private static final long serialVersionUID = 1L;
public static final ChannelData[] EMPTY_DATA_ARRAY = new ChannelData[0];
public static volatile boolean USE_SECURE_RANDOM_FOR_UUID = false;
/**
* The options this message was sent with
*/
private int options = 0;
/**
* The message data, stored in a dynamic buffer
*/
private XByteBuffer message;
/**
* The timestamp that goes with this message
*/
private long timestamp;
/**
* A unique message id
*/
private byte[] uniqueId;
/**
* The source or reply-to address for this message
*/
private Member address;
/**
* Creates an empty channel data with a new unique Id
*
* @see #ChannelData(boolean)
*/
public ChannelData() {
this(true);
}
/**
* Create an empty channel data object
*
* @param generateUUID boolean - if true, a unique Id will be generated
*/
public ChannelData(boolean generateUUID) {
if (generateUUID) {
generateUUID();
}
}
/**
* Creates a new channel data object with data
*
* @param uniqueId - unique message id
* @param message - message data
* @param timestamp - message timestamp
*/
public ChannelData(byte[] uniqueId, XByteBuffer message, long timestamp) {
this.uniqueId = uniqueId;
this.message = message;
this.timestamp = timestamp;
}
@Override
public XByteBuffer getMessage() {
return message;
}
@Override
public void setMessage(XByteBuffer message) {
this.message = message;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public byte[] getUniqueId() {
return uniqueId;
}
public void setUniqueId(byte[] uniqueId) {
this.uniqueId = uniqueId;
}
@Override
public int getOptions() {
return options;
}
@Override
public void setOptions(int options) {
this.options = options;
}
@Override
public Member getAddress() {
return address;
}
@Override
public void setAddress(Member address) {
this.address = address;
}
/**
* Generates a UUID and invokes setUniqueId
*/
public void generateUUID() {
byte[] data = new byte[16];
UUIDGenerator.randomUUID(USE_SECURE_RANDOM_FOR_UUID, data, 0);
setUniqueId(data);
}
public int getDataPackageLength() {
return 4 + // options
8 + // timestamp off=4
4 + // unique id length off=12
uniqueId.length + // id data off=12+uniqueId.length
4 + // addr length off=12+uniqueId.length+4
address.getDataLength() + // member data off=12+uniqueId.length+4+add.length
4 + // message length off=12+uniqueId.length+4+add.length+4
message.getLength();
}
/**
* Serializes the ChannelData object into a byte[] array
*
* @return byte[]
*/
public byte[] getDataPackage() {
int length = getDataPackageLength();
byte[] data = new byte[length];
int offset = 0;
return getDataPackage(data, offset);
}
public byte[] getDataPackage(byte[] data, int offset) {
byte[] addr = address.getData(false);
XByteBuffer.toBytes(options, data, offset);
offset += 4; // options
XByteBuffer.toBytes(timestamp, data, offset);
offset += 8; // timestamp
XByteBuffer.toBytes(uniqueId.length, data, offset);
offset += 4; // uniqueId.length
System.arraycopy(uniqueId, 0, data, offset, uniqueId.length);
offset += uniqueId.length; // uniqueId data
XByteBuffer.toBytes(addr.length, data, offset);
offset += 4; // addr.length
System.arraycopy(addr, 0, data, offset, addr.length);
offset += addr.length; // addr data
XByteBuffer.toBytes(message.getLength(), data, offset);
offset += 4; // message.length
System.arraycopy(message.getBytesDirect(), 0, data, offset, message.getLength());
return data;
}
/**
* Deserializes a ChannelData object from a byte array
*
* @param xbuf byte[]
*
* @return ChannelData
*/
public static ChannelData getDataFromPackage(XByteBuffer xbuf) {
ChannelData data = new ChannelData(false);
int offset = 0;
data.setOptions(XByteBuffer.toInt(xbuf.getBytesDirect(), offset));
offset += 4; // options
data.setTimestamp(XByteBuffer.toLong(xbuf.getBytesDirect(), offset));
offset += 8; // timestamp
data.uniqueId = new byte[XByteBuffer.toInt(xbuf.getBytesDirect(), offset)];
offset += 4; // uniqueId length
System.arraycopy(xbuf.getBytesDirect(), offset, data.uniqueId, 0, data.uniqueId.length);
offset += data.uniqueId.length; // uniqueId data
// byte[] addr = new byte[XByteBuffer.toInt(xbuf.getBytesDirect(),offset)];
int addrlen = XByteBuffer.toInt(xbuf.getBytesDirect(), offset);
offset += 4; // addr length
// System.arraycopy(xbuf.getBytesDirect(),offset,addr,0,addr.length);
data.setAddress(MemberImpl.getMember(xbuf.getBytesDirect(), offset, addrlen));
// offset += addr.length; //addr data
offset += addrlen;
int xsize = XByteBuffer.toInt(xbuf.getBytesDirect(), offset);
offset += 4; // xsize length
System.arraycopy(xbuf.getBytesDirect(), offset, xbuf.getBytesDirect(), 0, xsize);
xbuf.setLength(xsize);
data.message = xbuf;
return data;
}
public static ChannelData getDataFromPackage(byte[] b) {
ChannelData data = new ChannelData(false);
int offset = 0;
data.setOptions(XByteBuffer.toInt(b, offset));
offset += 4; // options
data.setTimestamp(XByteBuffer.toLong(b, offset));
offset += 8; // timestamp
data.uniqueId = new byte[XByteBuffer.toInt(b, offset)];
offset += 4; // uniqueId length
System.arraycopy(b, offset, data.uniqueId, 0, data.uniqueId.length);
offset += data.uniqueId.length; // uniqueId data
byte[] addr = new byte[XByteBuffer.toInt(b, offset)];
offset += 4; // addr length
System.arraycopy(b, offset, addr, 0, addr.length);
data.setAddress(MemberImpl.getMember(addr));
offset += addr.length; // addr data
int xsize = XByteBuffer.toInt(b, offset);
// data.message = new XByteBuffer(new byte[xsize],false);
data.message = BufferPool.getBufferPool().getBuffer(xsize, false);
offset += 4; // message length
System.arraycopy(b, offset, data.message.getBytesDirect(), 0, xsize);
data.message.append(b, offset, xsize);
offset += xsize; // message data
return data;
}
@Override
public int hashCode() {
return XByteBuffer.toInt(getUniqueId(), 0);
}
/**
* Compares to ChannelData objects, only compares on getUniqueId().equals(o.getUniqueId())
*
* @param o Object
*
* @return boolean
*/
@Override
public boolean equals(Object o) {
if (o instanceof ChannelData) {
return Arrays.equals(getUniqueId(), ((ChannelData) o).getUniqueId());
} else {
return false;
}
}
/**
* Create a shallow clone, only the data gets recreated
*
* @return ClusterData
*/
@Override
public ChannelData clone() {
ChannelData clone;
try {
clone = (ChannelData) super.clone();
} catch (CloneNotSupportedException e) {
// Cannot happen
throw new AssertionError();
}
if (this.message != null) {
clone.message = new XByteBuffer(this.message.getBytesDirect(), false);
}
return clone;
}
@Override
public Object deepclone() {
byte[] d = this.getDataPackage();
return getDataFromPackage(d);
}
/**
* Utility method, returns true if the options flag indicates that an ack is to be sent after the message has been
* received and processed
*
* @param options int - the options for the message
*
* @return boolean
*
* @see org.apache.catalina.tribes.Channel#SEND_OPTIONS_USE_ACK
* @see org.apache.catalina.tribes.Channel#SEND_OPTIONS_SYNCHRONIZED_ACK
*/
public static boolean sendAckSync(int options) {
return ((Channel.SEND_OPTIONS_USE_ACK & options) == Channel.SEND_OPTIONS_USE_ACK) &&
((Channel.SEND_OPTIONS_SYNCHRONIZED_ACK & options) == Channel.SEND_OPTIONS_SYNCHRONIZED_ACK);
}
/**
* Utility method, returns true if the options flag indicates that an ack is to be sent after the message has been
* received but not yet processed
*
* @param options int - the options for the message
*
* @return boolean
*
* @see org.apache.catalina.tribes.Channel#SEND_OPTIONS_USE_ACK
* @see org.apache.catalina.tribes.Channel#SEND_OPTIONS_SYNCHRONIZED_ACK
*/
public static boolean sendAckAsync(int options) {
return ((Channel.SEND_OPTIONS_USE_ACK & options) == Channel.SEND_OPTIONS_USE_ACK) &&
((Channel.SEND_OPTIONS_SYNCHRONIZED_ACK & options) != Channel.SEND_OPTIONS_SYNCHRONIZED_ACK);
}
@Override
public String toString() {
return "ClusterData[src=" + getAddress() + "; id=" + bToS(getUniqueId()) + "; sent=" +
new Timestamp(this.getTimestamp()).toString() + ']';
}
public static String bToS(byte[] data) {
StringBuilder buf = new StringBuilder(4 * 16);
buf.append('{');
for (int i = 0; data != null && i < data.length; i++) {
buf.append(String.valueOf(data[i])).append(' ');
}
buf.append('}');
return buf.toString();
}
}
|