Skip to content

Commit d3de9fd

Browse files
committed
slim down Frame. Access Frame elements directly from flyweight and ByteBuffer. Naming & cleanup.
1 parent 7e241e0 commit d3de9fd

File tree

7 files changed

+232
-229
lines changed

7 files changed

+232
-229
lines changed

src/main/java/io/reactivesocket/Frame.java

Lines changed: 85 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package io.reactivesocket;
1717

18-
import uk.co.real_logic.agrona.DirectBuffer;
19-
2018
import java.nio.ByteBuffer;
2119

2220
/**
@@ -26,180 +24,154 @@
2624
*/
2725
public class Frame
2826
{
29-
private static final int INITIAL_MESSAGE_ARRAY_SIZE = 256;
30-
// TODO: make thread local to demonstrate idea
27+
// thread local as it needs to be single-threaded access
3128
private static final ThreadLocal<FrameFlyweight> FRAME_HANDLER = ThreadLocal.withInitial(FrameFlyweight::new);
3229

33-
private Frame() {
34-
}
35-
3630
// not final so we can reuse this object
3731
private ByteBuffer byteBuffer;
38-
private byte[] messageArray = new byte[INITIAL_MESSAGE_ARRAY_SIZE];
39-
private FrameType frameType;
4032

41-
private long streamId;
42-
private int version;
43-
private int flags;
44-
private int messageLength = 0;
33+
private Frame() {
34+
}
4535

36+
/**
37+
* Return underlying {@link ByteBuffer} for frame
38+
*
39+
* @return underlying {@link ByteBuffer} for frame
40+
*/
4641
public ByteBuffer getByteBuffer() {
4742
return byteBuffer;
4843
}
4944

50-
public String getMessage() {
51-
if (frameType == null) {
52-
decode();
53-
}
54-
return new String(messageArray, 0, messageLength);
45+
/**
46+
* Return frame data as a String
47+
*
48+
* @return frame data as {@link System}
49+
*/
50+
public String getData() {
51+
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
52+
53+
return frameFlyweight.framePayload(byteBuffer, 0);
5554
}
5655

56+
/**
57+
* Return frame stream identifier
58+
*
59+
* @return frame stream identifier
60+
*/
5761
public long getStreamId() {
58-
if (frameType == null) {
59-
decode();
60-
}
61-
return streamId;
62+
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
63+
64+
return frameFlyweight.streamId(byteBuffer);
6265
}
6366

64-
public FrameType getMessageType() {
65-
if (frameType == null) {
66-
decode();
67-
}
68-
return frameType;
67+
/**
68+
* Return frame {@link FrameType}
69+
*
70+
* @return frame type
71+
*/
72+
public FrameType getType() {
73+
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
74+
75+
return frameFlyweight.frameType(byteBuffer);
6976
}
7077

78+
/**
79+
* Return frame version
80+
*
81+
* @return frame version
82+
*/
7183
public int getVersion()
7284
{
73-
if (frameType == null)
74-
{
75-
decode();
76-
}
77-
return version;
85+
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
86+
87+
return frameFlyweight.version(byteBuffer);
7888
}
7989

90+
/**
91+
* Return frame flags
92+
*
93+
* @return frame flags
94+
*/
8095
public int getFlags()
8196
{
82-
if (frameType == null)
83-
{
84-
decode();
85-
}
86-
return flags;
97+
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
98+
99+
return frameFlyweight.flags(byteBuffer);
87100
}
88101

89102
/**
90103
* Mutates this Frame to contain the given ByteBuffer
91104
*
92-
* @param b
105+
* @param byteBuffer to wrap
93106
*/
94-
public void wrap(final ByteBuffer b) {
95-
this.streamId = -1;
96-
this.frameType = null;
97-
this.byteBuffer = b;
107+
public void wrap(final ByteBuffer byteBuffer) {
108+
this.byteBuffer = byteBuffer;
98109
}
99110

100111
/**
101112
* Construct a new Frame from the given ByteBuffer
102113
*
103-
* @param b
104-
* @return
114+
* @param byteBuffer to wrap
115+
* @return new {@link Frame}
105116
*/
106-
public static Frame from(final ByteBuffer b) {
117+
public static Frame from(final ByteBuffer byteBuffer) {
107118
Frame f = new Frame();
108-
f.byteBuffer = b;
119+
f.byteBuffer = byteBuffer;
109120
return f;
110121
}
111122

112123
/**
113-
* Mutates this Frame to contain the given message.
114-
*
115-
* @param streamId
116-
* @param type
117-
* @param message
124+
* Mutates this Frame to contain the given parameters.
125+
*
126+
* NOTE: allocates a new {@link ByteBuffer}
127+
*
128+
* @param streamId to include in frame
129+
* @param type to include in frame
130+
* @param message to include in frame
118131
*/
119132
public void wrap(final long streamId, final FrameType type, final String message) {
120-
this.streamId = streamId;
121-
this.frameType = type;
122-
123-
final byte[] messageBytes = message.getBytes();
124-
ensureMessageArrayCapacity(messageBytes.length);
125-
126-
System.arraycopy(messageBytes, 0, this.messageArray, 0, messageBytes.length);
127-
this.messageLength = messageBytes.length;
128-
129-
this.byteBuffer = createByteBufferAndEncode(streamId, type, messageBytes);
130-
}
131-
132-
public void setFromDecode(final int version, final long streamId, final FrameType type, final int flags)
133-
{
134-
this.version = version;
135-
this.streamId = streamId;
136-
this.frameType = type;
137-
this.flags = flags;
138-
}
139-
140-
public void setFromDecode(final DirectBuffer buffer, final int offset, final int messageLength)
141-
{
142-
ensureMessageArrayCapacity(messageLength);
143-
this.messageLength = messageLength;
144-
buffer.getBytes(offset, this.messageArray, 0, messageLength);
133+
this.byteBuffer = createByteBufferAndEncode(streamId, type, message);
145134
}
146135

147136
/**
148-
* Construct a new Frame with the given message.
137+
* Construct a new Frame with the given parameters.
149138
*
150-
* @param streamId
151-
* @param type
152-
* @param message
153-
* @return
139+
* @param streamId to include in frame
140+
* @param type to include in frame
141+
* @param message to include in frame
142+
* @return new {@link Frame}
154143
*/
155144
public static Frame from(long streamId, FrameType type, String message) {
156145
Frame f = new Frame();
157-
f.streamId = streamId;
158-
f.frameType = type;
159-
160-
final byte[] messageBytes = message.getBytes();
161-
f.ensureMessageArrayCapacity(messageBytes.length);
162-
163-
f.byteBuffer = createByteBufferAndEncode(streamId, type, messageBytes);
164-
165-
System.arraycopy(messageBytes, 0, f.messageArray, 0, messageBytes.length);
166-
f.messageLength = messageBytes.length;
146+
f.byteBuffer = createByteBufferAndEncode(streamId, type, message);
167147
return f;
168148
}
169149

170-
private static ByteBuffer createByteBufferAndEncode(long streamId, FrameType type, final byte[] message) {
150+
private static ByteBuffer createByteBufferAndEncode(long streamId, FrameType type, final String message) {
171151
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
172152

173153
// TODO: allocation side effect of how this works currently with the rest of the machinery.
174-
final ByteBuffer buffer = ByteBuffer.allocate(FrameFlyweight.computeFrameLength(message.length));
154+
final ByteBuffer buffer = ByteBuffer.allocate(FrameFlyweight.computeFrameLength(message.length()));
175155

176156
frameFlyweight.encode(buffer, streamId, type, message);
177157
return buffer;
178158
}
179159

180-
private void decode() {
160+
@Override
161+
public String toString() {
181162
final FrameFlyweight frameFlyweight = FRAME_HANDLER.get();
163+
FrameType type = FrameType.SETUP;
164+
String payload = "";
165+
long streamId = -1;
182166

183-
frameFlyweight.decode(this, byteBuffer, 0);
184-
}
185-
186-
private void ensureMessageArrayCapacity(final int length)
187-
{
188-
if (messageArray.length < length)
167+
try
189168
{
190-
messageArray = new byte[length];
191-
}
192-
}
193-
194-
@Override
195-
public String toString() {
196-
if (frameType == null) {
197-
try {
198-
decode();
199-
} catch (Exception e) {
200-
e.printStackTrace();
201-
}
169+
type = frameFlyweight.frameType(byteBuffer);
170+
payload = frameFlyweight.framePayload(byteBuffer, 0);
171+
streamId = frameFlyweight.streamId(byteBuffer);
172+
} catch (Exception e) {
173+
e.printStackTrace();
202174
}
203-
return "Frame => ID: " + streamId + " Type: " + frameType + " Data: " + getMessage();
175+
return "Frame => ID: " + streamId + " Type: " + type + " Data: " + payload;
204176
}
205177
}

0 commit comments

Comments
 (0)