|
13 | 13 | import io.netty.buffer.ByteBuf; |
14 | 14 |
|
15 | 15 | public class NativeMessageTreeEncoder implements MessageTreeEncoder { |
16 | | - public static final String ID = "NT1"; // native message tree version 1 |
17 | | - |
18 | | - @Override |
19 | | - public void encode(MessageTree tree, ByteBuf buf) { |
20 | | - Context ctx = new Context(tree); |
21 | | - |
22 | | - Encoder.HEADER.encode(ctx, buf, null); |
23 | | - |
24 | | - Message root = tree.getMessage(); |
25 | | - |
26 | | - if (root != null) { |
27 | | - encodeMessage(ctx, buf, root); |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - private void encodeMessage(Context ctx, ByteBuf buf, Message msg) { |
32 | | - if (msg instanceof Transaction) { |
33 | | - Transaction transaction = (Transaction) msg; |
34 | | - List<Message> children = transaction.getChildren(); |
35 | | - |
36 | | - Encoder.TRANSACTION_START.encode(ctx, buf, msg); |
37 | | - |
38 | | - for (Message child : children) { |
39 | | - if (child != null) { |
40 | | - encodeMessage(ctx, buf, child); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - Encoder.TRANSACTION_END.encode(ctx, buf, msg); |
45 | | - } else if (msg instanceof Event) { |
46 | | - Encoder.EVENT.encode(ctx, buf, msg); |
47 | | - } else if (msg instanceof Heartbeat) { |
48 | | - Encoder.HEARTBEAT.encode(ctx, buf, msg); |
49 | | - } else if (msg instanceof Trace) { |
50 | | - Encoder.TRACE.encode(ctx, buf, msg); |
51 | | - } else { |
52 | | - throw new RuntimeException(String.format("Unsupported message(%s).", msg)); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - private static class Context { |
57 | | - private static Charset UTF8 = Charset.forName("UTF-8");; |
58 | | - |
59 | | - private MessageTree m_tree; |
60 | | - |
61 | | - public Context(MessageTree tree) { |
62 | | - m_tree = tree; |
63 | | - } |
64 | | - |
65 | | - public MessageTree getMessageTree() { |
66 | | - return m_tree; |
67 | | - } |
68 | | - |
69 | | - public void writeDuration(ByteBuf buf, long duration) { |
70 | | - writeVarint(buf, duration); |
71 | | - } |
72 | | - |
73 | | - public void writeId(ByteBuf buf, char id) { |
74 | | - buf.writeByte(id); |
75 | | - } |
76 | | - |
77 | | - public void writeString(ByteBuf buf, String str) { |
78 | | - if (str == null) { |
79 | | - str = "null"; |
80 | | - } |
81 | | - |
82 | | - if (str.length() == 0) { |
83 | | - writeVarint(buf, 0); |
84 | | - } else { |
85 | | - byte[] data = str.getBytes(UTF8); |
86 | | - |
87 | | - writeVarint(buf, data.length); |
88 | | - buf.writeBytes(data); |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - public void writeTimestamp(ByteBuf buf, long timestamp) { |
93 | | - writeVarint(buf, timestamp); |
94 | | - } |
95 | | - |
96 | | - private void writeVarint(ByteBuf buf, long value) { |
97 | | - while (true) { |
98 | | - if ((value & ~0x7FL) == 0) { |
99 | | - buf.writeByte((byte) value); |
100 | | - return; |
101 | | - } else { |
102 | | - buf.writeByte(((byte) value & 0x7F) | 0x80); |
103 | | - value >>>= 7; |
104 | | - } |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - public void writeVersion(ByteBuf buf, String version) { |
109 | | - buf.writeBytes(version.getBytes()); |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - private static enum Encoder { |
114 | | - HEADER { |
115 | | - @Override |
116 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
117 | | - MessageTree tree = ctx.getMessageTree(); |
118 | | - |
119 | | - ctx.writeVersion(buf, ID); |
120 | | - ctx.writeString(buf, tree.getDomain()); |
121 | | - ctx.writeString(buf, tree.getHostName()); |
122 | | - ctx.writeString(buf, tree.getIpAddress()); |
123 | | - ctx.writeString(buf, tree.getThreadGroupName()); |
124 | | - ctx.writeString(buf, tree.getThreadId()); |
125 | | - ctx.writeString(buf, tree.getThreadName()); |
126 | | - ctx.writeString(buf, tree.getMessageId()); |
127 | | - ctx.writeString(buf, tree.getParentMessageId()); |
128 | | - ctx.writeString(buf, tree.getRootMessageId()); |
129 | | - ctx.writeString(buf, tree.getSessionToken()); |
130 | | - } |
131 | | - }, |
132 | | - |
133 | | - TRANSACTION_START { |
134 | | - @Override |
135 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
136 | | - ctx.writeId(buf, 't'); |
137 | | - ctx.writeTimestamp(buf, msg.getTimestamp()); |
138 | | - ctx.writeString(buf, msg.getType()); |
139 | | - ctx.writeString(buf, msg.getName()); |
140 | | - } |
141 | | - }, |
142 | | - |
143 | | - TRANSACTION_END { |
144 | | - @Override |
145 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
146 | | - Transaction t = (Transaction) msg; |
147 | | - |
148 | | - ctx.writeId(buf, 'T'); |
149 | | - ctx.writeString(buf, msg.getStatus()); |
150 | | - ctx.writeString(buf, msg.getData().toString()); |
151 | | - ctx.writeDuration(buf, t.getDurationInMicros()); |
152 | | - } |
153 | | - }, |
154 | | - |
155 | | - EVENT { |
156 | | - @Override |
157 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
158 | | - ctx.writeId(buf, 'E'); |
159 | | - ctx.writeTimestamp(buf, msg.getTimestamp()); |
160 | | - ctx.writeString(buf, msg.getType()); |
161 | | - ctx.writeString(buf, msg.getName()); |
162 | | - ctx.writeString(buf, msg.getStatus()); |
163 | | - ctx.writeString(buf, msg.getData().toString()); |
164 | | - } |
165 | | - }, |
166 | | - |
167 | | - HEARTBEAT { |
168 | | - @Override |
169 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
170 | | - ctx.writeId(buf, 'H'); |
171 | | - ctx.writeTimestamp(buf, msg.getTimestamp()); |
172 | | - ctx.writeString(buf, msg.getType()); |
173 | | - ctx.writeString(buf, msg.getName()); |
174 | | - ctx.writeString(buf, msg.getStatus()); |
175 | | - ctx.writeString(buf, msg.getData().toString()); |
176 | | - } |
177 | | - }, |
178 | | - |
179 | | - TRACE { |
180 | | - @Override |
181 | | - protected void encode(Context ctx, ByteBuf buf, Message msg) { |
182 | | - ctx.writeId(buf, 'L'); |
183 | | - ctx.writeTimestamp(buf, msg.getTimestamp()); |
184 | | - ctx.writeString(buf, msg.getType()); |
185 | | - ctx.writeString(buf, msg.getName()); |
186 | | - ctx.writeString(buf, msg.getStatus()); |
187 | | - ctx.writeString(buf, msg.getData().toString()); |
188 | | - } |
189 | | - }; |
190 | | - |
191 | | - protected abstract void encode(Context ctx, ByteBuf buf, Message msg); |
192 | | - } |
| 16 | + public static final String ID = "NT1"; // native message tree version 1 |
| 17 | + |
| 18 | + @Override |
| 19 | + public void encode(MessageTree tree, ByteBuf buf) { |
| 20 | + Context ctx = new Context(tree); |
| 21 | + |
| 22 | + Encoder.HEADER.encode(ctx, buf, null); |
| 23 | + |
| 24 | + Message root = tree.getMessage(); |
| 25 | + |
| 26 | + if (root != null) { |
| 27 | + encodeMessage(ctx, buf, root); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void encodeMessage(Context ctx, ByteBuf buf, Message msg) { |
| 32 | + if (msg instanceof Transaction) { |
| 33 | + Transaction transaction = (Transaction) msg; |
| 34 | + List<Message> children = transaction.getChildren(); |
| 35 | + |
| 36 | + Encoder.TRANSACTION_START.encode(ctx, buf, msg); |
| 37 | + |
| 38 | + for (Message child : children) { |
| 39 | + if (child != null) { |
| 40 | + encodeMessage(ctx, buf, child); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Encoder.TRANSACTION_END.encode(ctx, buf, msg); |
| 45 | + } else if (msg instanceof Event) { |
| 46 | + Encoder.EVENT.encode(ctx, buf, msg); |
| 47 | + } else if (msg instanceof Heartbeat) { |
| 48 | + Encoder.HEARTBEAT.encode(ctx, buf, msg); |
| 49 | + } else if (msg instanceof Trace) { |
| 50 | + Encoder.TRACE.encode(ctx, buf, msg); |
| 51 | + } else { |
| 52 | + throw new RuntimeException(String.format("Unsupported message(%s).", msg)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static class Context { |
| 57 | + private static Charset UTF8 = Charset.forName("UTF-8");; |
| 58 | + |
| 59 | + private MessageTree m_tree; |
| 60 | + |
| 61 | + public Context(MessageTree tree) { |
| 62 | + m_tree = tree; |
| 63 | + } |
| 64 | + |
| 65 | + public MessageTree getMessageTree() { |
| 66 | + return m_tree; |
| 67 | + } |
| 68 | + |
| 69 | + public void writeDuration(ByteBuf buf, long duration) { |
| 70 | + writeVarint(buf, duration); |
| 71 | + } |
| 72 | + |
| 73 | + public void writeId(ByteBuf buf, char id) { |
| 74 | + buf.writeByte(id); |
| 75 | + } |
| 76 | + |
| 77 | + public void writeString(ByteBuf buf, String str) { |
| 78 | + if (str == null) { |
| 79 | + buf.writeByte(-1); |
| 80 | + } else if (str.length() == 0) { |
| 81 | + writeVarint(buf, 0); |
| 82 | + } else { |
| 83 | + byte[] data = str.getBytes(UTF8); |
| 84 | + |
| 85 | + writeVarint(buf, data.length); |
| 86 | + buf.writeBytes(data); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void writeTimestamp(ByteBuf buf, long timestamp) { |
| 91 | + writeVarint(buf, timestamp); |
| 92 | + } |
| 93 | + |
| 94 | + private void writeVarint(ByteBuf buf, long value) { |
| 95 | + while (true) { |
| 96 | + if ((value & ~0x7FL) == 0) { |
| 97 | + buf.writeByte((byte) value); |
| 98 | + return; |
| 99 | + } else { |
| 100 | + buf.writeByte(((byte) value & 0x7F) | 0x80); |
| 101 | + value >>>= 7; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void writeVersion(ByteBuf buf, String version) { |
| 107 | + buf.writeBytes(version.getBytes()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static enum Encoder { |
| 112 | + HEADER { |
| 113 | + @Override |
| 114 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 115 | + MessageTree tree = ctx.getMessageTree(); |
| 116 | + |
| 117 | + ctx.writeVersion(buf, ID); |
| 118 | + ctx.writeString(buf, tree.getDomain()); |
| 119 | + ctx.writeString(buf, tree.getHostName()); |
| 120 | + ctx.writeString(buf, tree.getIpAddress()); |
| 121 | + ctx.writeString(buf, tree.getThreadGroupName()); |
| 122 | + ctx.writeString(buf, tree.getThreadId()); |
| 123 | + ctx.writeString(buf, tree.getThreadName()); |
| 124 | + ctx.writeString(buf, tree.getMessageId()); |
| 125 | + ctx.writeString(buf, tree.getParentMessageId()); |
| 126 | + ctx.writeString(buf, tree.getRootMessageId()); |
| 127 | + ctx.writeString(buf, tree.getSessionToken()); |
| 128 | + } |
| 129 | + }, |
| 130 | + |
| 131 | + TRANSACTION_START { |
| 132 | + @Override |
| 133 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 134 | + ctx.writeId(buf, 't'); |
| 135 | + ctx.writeTimestamp(buf, msg.getTimestamp()); |
| 136 | + ctx.writeString(buf, msg.getType()); |
| 137 | + ctx.writeString(buf, msg.getName()); |
| 138 | + } |
| 139 | + }, |
| 140 | + |
| 141 | + TRANSACTION_END { |
| 142 | + @Override |
| 143 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 144 | + Transaction t = (Transaction) msg; |
| 145 | + |
| 146 | + ctx.writeId(buf, 'T'); |
| 147 | + ctx.writeString(buf, msg.getStatus()); |
| 148 | + ctx.writeString(buf, msg.getData().toString()); |
| 149 | + ctx.writeDuration(buf, t.getDurationInMicros()); |
| 150 | + } |
| 151 | + }, |
| 152 | + |
| 153 | + EVENT { |
| 154 | + @Override |
| 155 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 156 | + ctx.writeId(buf, 'E'); |
| 157 | + ctx.writeTimestamp(buf, msg.getTimestamp()); |
| 158 | + ctx.writeString(buf, msg.getType()); |
| 159 | + ctx.writeString(buf, msg.getName()); |
| 160 | + ctx.writeString(buf, msg.getStatus()); |
| 161 | + ctx.writeString(buf, msg.getData().toString()); |
| 162 | + } |
| 163 | + }, |
| 164 | + |
| 165 | + HEARTBEAT { |
| 166 | + @Override |
| 167 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 168 | + ctx.writeId(buf, 'H'); |
| 169 | + ctx.writeTimestamp(buf, msg.getTimestamp()); |
| 170 | + ctx.writeString(buf, msg.getType()); |
| 171 | + ctx.writeString(buf, msg.getName()); |
| 172 | + ctx.writeString(buf, msg.getStatus()); |
| 173 | + ctx.writeString(buf, msg.getData().toString()); |
| 174 | + } |
| 175 | + }, |
| 176 | + |
| 177 | + TRACE { |
| 178 | + @Override |
| 179 | + protected void encode(Context ctx, ByteBuf buf, Message msg) { |
| 180 | + ctx.writeId(buf, 'L'); |
| 181 | + ctx.writeTimestamp(buf, msg.getTimestamp()); |
| 182 | + ctx.writeString(buf, msg.getType()); |
| 183 | + ctx.writeString(buf, msg.getName()); |
| 184 | + ctx.writeString(buf, msg.getStatus()); |
| 185 | + ctx.writeString(buf, msg.getData().toString()); |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + protected abstract void encode(Context ctx, ByteBuf buf, Message msg); |
| 190 | + } |
193 | 191 | } |
0 commit comments