forked from apache/activemq-artemis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PacketImpl.java
477 lines (310 loc) · 13.3 KB
/
PacketImpl.java
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* 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.activemq.artemis.core.protocol.core.impl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper;
import org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection;
import org.apache.activemq.artemis.core.protocol.core.Packet;
import org.apache.activemq.artemis.utils.DataConstants;
public class PacketImpl implements Packet {
// Constants -------------------------------------------------------------------------
// 2.0.0
public static final int ADDRESSING_CHANGE_VERSION = 129;
// 2.7.0
public static final int ARTEMIS_2_7_0_VERSION = 130;
public static final int ASYNC_RESPONSE_CHANGE_VERSION = ARTEMIS_2_7_0_VERSION;
public static final int CONSUMER_PRIORITY_CHANGE_VERSION = ARTEMIS_2_7_0_VERSION;
public static final int FQQN_CHANGE_VERSION = ARTEMIS_2_7_0_VERSION;
public static final SimpleString OLD_QUEUE_PREFIX = new SimpleString("jms.queue.");
public static final SimpleString OLD_TEMP_QUEUE_PREFIX = new SimpleString("jms.tempqueue.");
public static final SimpleString OLD_TOPIC_PREFIX = new SimpleString("jms.topic.");
public static final SimpleString OLD_TEMP_TOPIC_PREFIX = new SimpleString("jms.temptopic.");
// The minimal size for all the packets, Common data for all the packets (look at
// PacketImpl.encode)
public static final int PACKET_HEADERS_SIZE = DataConstants.SIZE_INT + DataConstants.SIZE_BYTE +
DataConstants.SIZE_LONG;
protected long channelID;
private final byte type;
protected int size = -1;
// The packet types
// -----------------------------------------------------------------------------------
public static final byte PING = 10;
public static final byte DISCONNECT = 11;
public static final byte DISCONNECT_CONSUMER = 12;
public static final byte DISCONNECT_CONSUMER_KILL = 13;
// Miscellaneous
public static final byte EXCEPTION = 20;
public static final byte NULL_RESPONSE = 21;
public static final byte PACKETS_CONFIRMED = 22;
// Server
public static final byte CREATESESSION = 30;
public static final byte CREATESESSION_RESP = 31;
public static final byte REATTACH_SESSION = 32;
public static final byte REATTACH_SESSION_RESP = 33;
public static final byte CREATE_QUEUE = 34;
public static final byte DELETE_QUEUE = 35;
public static final byte CREATE_SHARED_QUEUE = 36;
// Session
public static final byte SESS_XA_FAILED = 39;
public static final byte SESS_CREATECONSUMER = 40;
public static final byte SESS_ACKNOWLEDGE = 41;
public static final byte SESS_EXPIRED = 42;
public static final byte SESS_COMMIT = 43;
public static final byte SESS_ROLLBACK = 44;
public static final byte SESS_QUEUEQUERY = 45;
public static final byte SESS_QUEUEQUERY_RESP = 46;
public static final byte SESS_BINDINGQUERY = 49;
public static final byte SESS_BINDINGQUERY_RESP = 50;
public static final byte SESS_XA_START = 51;
public static final byte SESS_XA_END = 52;
public static final byte SESS_XA_COMMIT = 53;
public static final byte SESS_XA_PREPARE = 54;
public static final byte SESS_XA_RESP = 55;
public static final byte SESS_XA_ROLLBACK = 56;
public static final byte SESS_XA_JOIN = 57;
public static final byte SESS_XA_SUSPEND = 58;
public static final byte SESS_XA_RESUME = 59;
public static final byte SESS_XA_FORGET = 60;
public static final byte SESS_XA_INDOUBT_XIDS = 61;
public static final byte SESS_XA_INDOUBT_XIDS_RESP = 62;
public static final byte SESS_XA_SET_TIMEOUT = 63;
public static final byte SESS_XA_SET_TIMEOUT_RESP = 64;
public static final byte SESS_XA_GET_TIMEOUT = 65;
public static final byte SESS_XA_GET_TIMEOUT_RESP = 66;
public static final byte SESS_START = 67;
public static final byte SESS_STOP = 68;
public static final byte SESS_CLOSE = 69;
public static final byte SESS_FLOWTOKEN = 70;
public static final byte SESS_SEND = 71;
public static final byte SESS_SEND_LARGE = 72;
public static final byte SESS_SEND_CONTINUATION = 73;
public static final byte SESS_CONSUMER_CLOSE = 74;
public static final byte SESS_RECEIVE_MSG = 75;
public static final byte SESS_RECEIVE_LARGE_MSG = 76;
public static final byte SESS_RECEIVE_CONTINUATION = 77;
public static final byte SESS_FORCE_CONSUMER_DELIVERY = 78;
public static final byte SESS_PRODUCER_REQUEST_CREDITS = 79;
public static final byte SESS_PRODUCER_CREDITS = 80;
public static final byte SESS_INDIVIDUAL_ACKNOWLEDGE = 81;
public static final byte SESS_PRODUCER_FAIL_CREDITS = 82;
// Replication
public static final byte REPLICATION_RESPONSE = 90;
public static final byte REPLICATION_APPEND = 91;
public static final byte REPLICATION_APPEND_TX = 92;
public static final byte REPLICATION_DELETE = 93;
public static final byte REPLICATION_DELETE_TX = 94;
public static final byte REPLICATION_PREPARE = 95;
public static final byte REPLICATION_COMMIT_ROLLBACK = 96;
public static final byte REPLICATION_PAGE_WRITE = 97;
public static final byte REPLICATION_PAGE_EVENT = 98;
public static final byte REPLICATION_LARGE_MESSAGE_BEGIN = 99;
public static final byte REPLICATION_LARGE_MESSAGE_END = 100;
public static final byte REPLICATION_LARGE_MESSAGE_WRITE = 101;
/*
* code 102 was REPLICATION_COMPARE_DATA, released into production as a message, but as part of
* the (then) non-function replication system.
*/
public static final byte REPLICATION_SYNC_FILE = 103;
public static final byte SESS_ADD_METADATA = 104;
public static final byte SESS_ADD_METADATA2 = 105;
public static final byte SESS_UNIQUE_ADD_METADATA = 106;
// HA
public static final byte CLUSTER_TOPOLOGY = 110;
public static final byte NODE_ANNOUNCE = 111;
public static final byte SUBSCRIBE_TOPOLOGY = 112;
// For newer versions
public static final byte SUBSCRIBE_TOPOLOGY_V2 = 113;
public static final byte CLUSTER_TOPOLOGY_V2 = 114;
public static final byte BACKUP_REGISTRATION = 115;
public static final byte BACKUP_REGISTRATION_FAILED = 116;
public static final byte REPLICATION_START_FINISH_SYNC = 120;
public static final byte REPLICATION_SCHEDULED_FAILOVER = 121;
public static final byte CLUSTER_TOPOLOGY_V3 = 122;
//do not reuse
//public static final byte NODE_ANNOUNCE_V2 = 123;
public static final byte DISCONNECT_V2 = 124;
public static final byte CLUSTER_CONNECT = 125;
public static final byte CLUSTER_CONNECT_REPLY = 126;
public static final byte BACKUP_REQUEST = 127;
//oops ran out of positive bytes
public static final byte BACKUP_REQUEST_RESPONSE = -1;
public static final byte QUORUM_VOTE = -2;
public static final byte QUORUM_VOTE_REPLY = -3;
public static final byte CHECK_FOR_FAILOVER = -4;
public static final byte CHECK_FOR_FAILOVER_REPLY = -5;
public static final byte SCALEDOWN_ANNOUNCEMENT = -6;
public static final byte SESS_QUEUEQUERY_RESP_V2 = -7;
public static final byte SESS_BINDINGQUERY_RESP_V2 = -8;
public static final byte REPLICATION_RESPONSE_V2 = -9;
public static final byte SESS_BINDINGQUERY_RESP_V3 = -10;
public static final byte CREATE_ADDRESS = -11;
public static final byte CREATE_QUEUE_V2 = -12;
public static final byte CREATE_SHARED_QUEUE_V2 = -13;
public static final byte SESS_QUEUEQUERY_RESP_V3 = -14;
public static final byte SESS_BINDINGQUERY_RESP_V4 = -15;
// Static --------------------------------------------------------
public PacketImpl(final byte type) {
this.type = type;
}
// Public --------------------------------------------------------
public SimpleString convertName(SimpleString name) {
if (name == null) {
return null;
}
if (name.startsWith(OLD_QUEUE_PREFIX)) {
return name.subSeq(OLD_QUEUE_PREFIX.length(), name.length());
} else if (name.startsWith(OLD_TOPIC_PREFIX)) {
return name.subSeq(OLD_TOPIC_PREFIX.length(), name.length());
} else {
return name;
}
}
@Override
public byte getType() {
return type;
}
@Override
public long getChannelID() {
return channelID;
}
@Override
public void setChannelID(final long channelID) {
this.channelID = channelID;
}
@Override
public ActiveMQBuffer encode(final CoreRemotingConnection connection) {
ActiveMQBuffer buffer = createPacket(connection);
encodeHeader(buffer);
encodeRest(buffer, connection);
encodeSize(buffer);
return buffer;
}
protected void encodeHeader(ActiveMQBuffer buffer) {
// The standard header fields
buffer.writeInt(0); // The length gets filled in at the end
buffer.writeByte(type);
buffer.writeLong(channelID);
}
protected void encodeSize(ActiveMQBuffer buffer) {
encodeSize(buffer, buffer.writerIndex());
}
protected void encodeSize(ActiveMQBuffer buffer, int size) {
this.size = size;
// The length doesn't include the actual length byte
int len = size - DataConstants.SIZE_INT;
buffer.setInt(0, len);
}
protected ActiveMQBuffer createPacket(CoreRemotingConnection connection) {
return createPacket(connection, expectedEncodeSize());
}
protected ActiveMQBuffer createPacket(CoreRemotingConnection connection, int size) {
if (connection == null) {
return new ChannelBufferWrapper(Unpooled.buffer(size));
} else {
return connection.createTransportBuffer(size);
}
}
@Override
public void decode(final ActiveMQBuffer buffer) {
channelID = buffer.readLong();
decodeRest(buffer);
size = buffer.readerIndex();
}
protected static ByteBuf copyMessageBuffer(ByteBuf buffer, int skipBytes) {
ByteBuf newNettyBuffer = Unpooled.buffer(buffer.capacity() - PACKET_HEADERS_SIZE - skipBytes);
int read = buffer.readerIndex();
int writ = buffer.writerIndex();
buffer.readerIndex(PACKET_HEADERS_SIZE);
newNettyBuffer.writeBytes(buffer, buffer.readableBytes() - skipBytes);
buffer.setIndex( read, writ );
newNettyBuffer.setIndex( 0, writ - PACKET_HEADERS_SIZE - skipBytes);
return newNettyBuffer;
}
@Override
public int getPacketSize() {
if (size == -1) {
throw new IllegalStateException("Packet hasn't been encoded/decoded yet");
}
return size;
}
@Override
public boolean isResponse() {
return false;
}
public void encodeRest(final ActiveMQBuffer buffer) {
}
public void encodeRest(final ActiveMQBuffer buffer, final CoreRemotingConnection coreRemotingConnection) {
encodeRest(buffer);
}
public void decodeRest(final ActiveMQBuffer buffer) {
}
@Override
public boolean isRequiresConfirmations() {
return true;
}
@Override
public String toString() {
return getParentString() + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (channelID ^ (channelID >>> 32));
result = prime * result + size;
result = prime * result + type;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof PacketImpl)) {
return false;
}
PacketImpl other = (PacketImpl) obj;
return (channelID == other.channelID) && (size == other.size) && (type != other.type);
}
protected String getParentString() {
return "PACKET(" + this.getClass().getSimpleName() + ")[type=" + type + ", channelID=" + channelID + ", responseAsync=" + isResponseAsync() + ", requiresResponse=" + isRequiresResponse() + ", correlationID=" + getCorrelationID() + ", packetObject=" + this.getClass().getSimpleName();
}
private int stringEncodeSize(final String str) {
return DataConstants.SIZE_INT + str.length() * 2;
}
protected int nullableStringEncodeSize(final String str) {
return DataConstants.SIZE_BOOLEAN + (str != null ? stringEncodeSize(str) : 0);
}
@Override
public boolean isRequiresResponse() {
return false;
}
@Override
public boolean isResponseAsync() {
return false;
}
@Override
public long getCorrelationID() {
return -1;
}
@Override
public void setCorrelationID(long correlationID) {
}
}