1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.protocol.specification;
17
18 import fulmine.Addressable;
19 import fulmine.Domain;
20 import fulmine.IAddressable;
21 import fulmine.Type;
22 import fulmine.context.IFrameworkContext;
23 import fulmine.event.EventFrameExecution;
24 import fulmine.model.container.IContainer;
25 import fulmine.model.container.IContainer.DataState;
26 import fulmine.protocol.wire.operation.BasicOperation;
27 import fulmine.protocol.wire.operation.IOperationScope;
28
29
30
31
32
33
34
35
36
37
38
39 public final class FrameReader implements IFrameReader
40 {
41
42
43
44
45
46 public final static ThreadLocal<Boolean> inContext =
47 new ThreadLocal<Boolean>()
48 {
49 @Override
50 protected Boolean initialValue()
51 {
52 return Boolean.FALSE;
53 }
54 };
55
56
57
58
59
60
61
62
63
64 public static boolean inContext()
65 {
66 return (inContext.get() != null && inContext.get().booleanValue());
67 }
68
69
70 public FrameReader()
71 {
72 super();
73 }
74
75
76
77
78
79
80
81
82 public static void debug(byte[] frame)
83 {
84
85 int identityLength =
86 ByteReader.readInteger(frame, 0, IFrameConstants.ID_SIZE_LENGTH);
87 String identity =
88 ByteReader.readString(frame, IFrameConstants.ID_SIZE_LENGTH,
89 identityLength);
90 final int recordTypeStart =
91 IFrameConstants.ID_SIZE_LENGTH + identityLength;
92 byte recordType =
93 (byte) ByteReader.readInteger(frame, recordTypeStart,
94 IFrameConstants.RECORD_TYPE_LENGTH);
95 int recordState =
96 ByteReader.readInteger(frame, recordTypeStart
97 + IFrameConstants.RECORD_TYPE_LENGTH,
98 IFrameConstants.RECORD_STATE_LENGTH);
99 byte recordDomain =
100 (byte) ByteReader.readInteger(frame, recordTypeStart
101 + IFrameConstants.RECORD_TYPE_LENGTH
102 + IFrameConstants.RECORD_STATE_LENGTH,
103 IFrameConstants.RECORD_DOMAIN_LENGTH);
104
105 int headerSizeStart =
106 identityLength + IFrameConstants.ID_SIZE_LENGTH
107 + IFrameConstants.HEADER_SIZE_START;
108
109 int headerLength =
110 ByteReader.readInteger(frame, headerSizeStart,
111 IFrameConstants.HEADER_SIZE_LENGTH);
112
113 int dataSizeStart =
114 identityLength + IFrameConstants.ID_SIZE_LENGTH
115 + IFrameConstants.DATA_SIZE_START;
116 int dataLength =
117 ByteReader.readInteger(frame, dataSizeStart,
118 IFrameConstants.DATA_SIZE_LENGTH);
119
120 System.out.println("Record ID size=" + identityLength);
121 System.out.println("Record ID=" + identity);
122 System.out.println("Record type=" + recordType);
123 System.out.println("Record state=" + recordState);
124 System.out.println("Record domain=" + recordDomain);
125 System.out.println("Header size=" + headerLength);
126 System.out.println("Data size=" + dataLength);
127
128 }
129
130 public IAddressable getRemoteContainerDetailsFromFrame(final byte[] frame,
131 String remoteContextIdentity)
132 {
133
134 final int identityLength =
135 ByteReader.readInteger(frame, 0, IFrameConstants.ID_SIZE_LENGTH);
136 final String identity =
137 ByteReader.readString(frame, IFrameConstants.ID_SIZE_LENGTH,
138 identityLength);
139 final int recordTypeStart =
140 IFrameConstants.ID_SIZE_LENGTH + identityLength;
141 final byte recordType =
142 (byte) ByteReader.readInteger(frame, recordTypeStart,
143 IFrameConstants.RECORD_TYPE_LENGTH);
144 final byte domain =
145 (byte) ByteReader.readInteger(frame, recordTypeStart
146 + IFrameConstants.RECORD_TYPE_LENGTH
147 + IFrameConstants.RECORD_STATE_LENGTH,
148 IFrameConstants.RECORD_DOMAIN_LENGTH);
149
150 return new Addressable(identity, Type.get(recordType),
151 Domain.get(domain));
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public IContainer getRemoteContainerForFrame(final byte[] frame,
167 String remoteContextIdentity, final IFrameworkContext context)
168 {
169 try
170 {
171 FrameReader.inContext.set(Boolean.TRUE);
172 IAddressable identity =
173 getRemoteContainerDetailsFromFrame(frame, remoteContextIdentity);
174 return context.getRemoteContainer(remoteContextIdentity,
175 identity.getIdentity(), identity.getType(),
176 identity.getDomain());
177 }
178 finally
179 {
180 FrameReader.inContext.remove();
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public IContainer read(byte[] frame, String remoteContextIdentity,
198 IFrameworkContext context)
199 {
200 try
201 {
202 FrameReader.inContext.set(Boolean.TRUE);
203
204 final int identityLength =
205 ByteReader.readInteger(frame, 0, IFrameConstants.ID_SIZE_LENGTH);
206 final String identity =
207 ByteReader.readString(frame, IFrameConstants.ID_SIZE_LENGTH,
208 identityLength);
209 final int recordTypeStart =
210 IFrameConstants.ID_SIZE_LENGTH + identityLength;
211 final byte recordType =
212 (byte) ByteReader.readInteger(frame, recordTypeStart,
213 IFrameConstants.RECORD_TYPE_LENGTH);
214 final int recordState =
215 ByteReader.readInteger(frame, recordTypeStart
216 + IFrameConstants.RECORD_TYPE_LENGTH,
217 IFrameConstants.RECORD_STATE_LENGTH);
218 final byte domain =
219 (byte) ByteReader.readInteger(frame, recordTypeStart
220 + IFrameConstants.RECORD_TYPE_LENGTH
221 + IFrameConstants.RECORD_STATE_LENGTH,
222 IFrameConstants.RECORD_DOMAIN_LENGTH);
223
224
225 final IContainer container =
226 context.getRemoteContainer(remoteContextIdentity, identity,
227 Type.get(recordType), Domain.get(domain));
228 final BasicOperation scope =
229 new BasicOperation(context.getPermissionProfile());
230
231
232
233
234
235
236 container.beginFrame(new EventFrameExecution());
237 try
238 {
239 container.readState(scope, frame, 0, frame.length);
240 container.setState(DataState.fromOrdinal(recordState));
241 }
242 finally
243 {
244
245 container.endFrame();
246 }
247 scope.validate();
248 return container;
249 }
250 finally
251 {
252 FrameReader.inContext.remove();
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 public static void readNestedSWF(final IOperationScope scope,
272 final byte[] buffer, final int start, final int numberOfBytes,
273 final FieldReader.IFieldReaderTask readerTask)
274 {
275 final int[] headerStart = new int[1];
276 final int[] headerLen = new int[1];
277 final int[] dataStart = new int[1];
278 final int[] dataLen = new int[1];
279 findNestedHeaderAndDataBufferPositions(buffer, start, headerStart,
280 headerLen, dataStart, dataLen);
281 if (headerLen[0] != 0)
282 {
283 FieldReader.readSWFFieldSpecs(scope, buffer, headerStart[0],
284 headerLen[0], dataStart[0], dataLen[0], readerTask);
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 public static void readNestedIWF(final IOperationScope scope,
304 final byte[] buffer, final int start, final int numberOfBytes,
305 final FieldReader.IFieldReaderTask readerTask)
306 {
307 final int[] headerStart = new int[1];
308 final int[] headerLen = new int[1];
309 final int[] dataStart = new int[1];
310 final int[] dataLen = new int[1];
311 findNestedHeaderAndDataBufferPositions(buffer, start, headerStart,
312 headerLen, dataStart, dataLen);
313 FieldReader.readIWFFieldSpecs(scope, buffer, headerStart[0],
314 headerLen[0], dataStart[0], dataLen[0], readerTask);
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 public static void findHeaderAndDataBufferPositions(final byte[] frame,
336 final int start, final int[] headerStart, final int[] headerLength,
337 final int[] dataStart, final int[] dataLength)
338 {
339 final int dynamicSectionSize =
340 IFrameConstants.ID_SIZE_LENGTH
341 + ByteReader.readInteger(frame, start,
342 IFrameConstants.ID_SIZE_LENGTH);
343 headerStart[0] =
344 start + dynamicSectionSize
345 + IFrameConstants.PREAMBLE_STATIC_SECTION_LENGTH;
346 final int headerSizeStart =
347 start + dynamicSectionSize + IFrameConstants.HEADER_SIZE_START;
348
349 headerLength[0] =
350 ByteReader.readInteger(frame, headerSizeStart,
351 IFrameConstants.HEADER_SIZE_LENGTH);
352
353 dataStart[0] = headerLength[0] + headerStart[0];
354 final int dataSizeStart =
355 start + dynamicSectionSize + IFrameConstants.DATA_SIZE_START;
356 dataLength[0] =
357 ByteReader.readInteger(frame, dataSizeStart,
358 IFrameConstants.DATA_SIZE_LENGTH);
359 }
360
361
362
363
364
365
366
367
368
369
370
371 private static void findNestedHeaderAndDataBufferPositions(byte[] buffer,
372 int start, int[] headerStart, int[] headerLen, int[] dataStart,
373 int[] dataLen)
374 {
375 headerStart[0] = IFrameConstants.NESTED_FRAME_PREAMBLE_LENGTH + start;
376 headerLen[0] =
377 ByteReader.readInteger(buffer, start,
378 IFrameConstants.HEADER_SIZE_LENGTH);
379 dataStart[0] = headerStart[0] + headerLen[0];
380 dataLen[0] =
381 ByteReader.readInteger(buffer, IFrameConstants.HEADER_SIZE_LENGTH
382 + start, IFrameConstants.DATA_SIZE_LENGTH);
383 }
384 }