-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAMQPInnerFrame.java
More file actions
132 lines (109 loc) · 4.34 KB
/
AMQPInnerFrame.java
File metadata and controls
132 lines (109 loc) · 4.34 KB
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
/*
* This class represents an AMQP inner frame
* Class is never instantiated, but extended by other frame classes representing
* method-, header-, body- and hearthbeat frames
*/
public class AMQPInnerFrame {
AMQPInnerFrame() {}
AMQPInnerFrame(ByteArrayBuffer byteArrayBuffer) {}
//Build an AMQPInnerFrame from a packet received on the wire
//Expects one complete frame
//This method consumes data from the ByteArrayBuffer, make sure to copy data
//if you need to keep it for other purposes
public static AMQPInnerFrame build(ByteArrayBuffer byteArrayBuffer, AMQPFrame.AMQPFrameType frameType, ALongUInt length, AShortUInt channel) throws InvalidFrameException {
//Do we want to build a Method frame?
if (frameType == AMQPFrame.AMQPFrameType.METHOD) {
//System.out.println("AMQPInnerFrame building new Method frame from this buffer:");
//System.out.println(byteArrayBuffer.toHexString());
//Declare and set to null so compiler wont complain
AShortUInt amqpClass = null;
AShortUInt amqpMethod = null;
//Read class and method
try {
amqpClass = new AShortUInt(byteArrayBuffer); //Pop 2 bytes
amqpMethod = new AShortUInt(byteArrayBuffer); //Pop 2 bytes
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to read method frame class/method: " + e.toString());
}
AMQPMethodFrame amqpMethodFrame = null;
//From now on, we need to start reading the method arguments
//The structure of each argument list differs depending on which class
//and method we're dealing with
try {
amqpMethodFrame = new AMQPMethodFrame(
amqpClass,
amqpMethod,
byteArrayBuffer
);
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to build method frame, invalid encoding: " + e.toString());
}
return amqpMethodFrame;
}
//Do we want to build a Header frame?
if (frameType == AMQPFrame.AMQPFrameType.HEADER) {
//To be created
AMQPHeaderFrame amqpHeaderFrame = null;
//Class ID
AShortUInt amqpClass = null;
//Read class ID
try {
amqpClass = new AShortUInt(byteArrayBuffer); //Pop 2 bytes
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to build header frame class ID: " + e.toString());
}
//Attempt to build the frame object
try {
amqpHeaderFrame = new AMQPHeaderFrame(
amqpClass,
byteArrayBuffer
);
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to build header frame, invalid encoding: " + e.toString());
}
return amqpHeaderFrame;
}
//Do we want to build a Body frame?
if (frameType == AMQPFrame.AMQPFrameType.BODY) {
//System.err.println("Got a body frame:\n" + byteArrayBuffer.toHexString());
AMQPBodyFrame amqpBodyFrame = null;
//Attempt to build the body frame
try {
amqpBodyFrame = new AMQPBodyFrame(
length,
byteArrayBuffer
);
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to build body frame, invalid encoding: " + e.toString());
}
return amqpBodyFrame;
}
//Build a Heartbeat frame?
if (frameType == AMQPFrame.AMQPFrameType.HEARTBEAT) {
//System.err.println("Got a body frame:\n" + byteArrayBuffer.toHexString());
AMQPHeartbeatFrame amqpHeartbeatFrame = null;
//Heartbeats MUST be received on channel 0
if (channel.toInt() != 0) {
throw new InvalidFrameException("Received heartbeat on channel other than 0");
}
//Attempt to build the Heartbeat frame
try {
amqpHeartbeatFrame = new AMQPHeartbeatFrame(
length,
byteArrayBuffer
);
} catch (InvalidTypeException e) {
throw new InvalidFrameException("Failed to build Heartbeat frame, invalid encoding: " + e.toString());
}
return amqpHeartbeatFrame;
}
//Should never be reached, as all four possible frame types are created above
throw new InvalidFrameException("Unknown frame type received (probably a bug in the tester code) : " + frameType.name());
}
//Output data to wire
public ByteArrayBuffer toWire() {
System.err.println("Override me");
System.exit(1);
return null;
}
};