Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
4918b8f2 | 13 | package org.eclipse.linuxtools.tmf.tests.stubs.trace; |
d18dd09b ASL |
14 | |
15 | import java.io.EOFException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | import java.util.Vector; | |
19 | ||
4c564a2d | 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
6c13869b | 21 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
4c564a2d | 22 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6c13869b FC |
23 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
a1440d1f | 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
6c13869b | 28 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; |
d18dd09b ASL |
29 | |
30 | /** | |
31 | * <b><u>TmfEventParserStub</u></b> | |
32 | * <p> | |
33 | * TODO: Implement me. Please. | |
34 | */ | |
3b38ea61 | 35 | @SuppressWarnings("nls") |
2352aed9 | 36 | public class TmfEventParserStub implements ITmfEventParser<TmfEvent> { |
d18dd09b | 37 | |
e31e01e8 | 38 | // ------------------------------------------------------------------------ |
d18dd09b | 39 | // Attributes |
e31e01e8 | 40 | // ------------------------------------------------------------------------ |
d18dd09b | 41 | |
085d898f | 42 | private static final int NB_TYPES = 10; |
28b94d61 | 43 | private final TmfEventType[] fTypes; |
7e6347b0 | 44 | private ITmfTrace<TmfEvent> fEventStream; |
d18dd09b | 45 | |
e31e01e8 | 46 | // ------------------------------------------------------------------------ |
d18dd09b | 47 | // Constructors |
e31e01e8 | 48 | // ------------------------------------------------------------------------ |
d18dd09b | 49 | |
7e6347b0 FC |
50 | public TmfEventParserStub(final ITmfTrace<TmfEvent> eventStream) { |
51 | fEventStream = eventStream; | |
085d898f FC |
52 | fTypes = new TmfEventType[NB_TYPES]; |
53 | for (int i = 0; i < NB_TYPES; i++) { | |
54 | final Vector<String> fields = new Vector<String>(); | |
55 | for (int j = 1; j <= i; j++) { | |
56 | final String field = "Fmt-" + i + "-Fld-" + j; | |
57 | fields.add(field); | |
58 | } | |
59 | final String[] fieldArray = new String[i]; | |
60 | final ITmfEventField rootField = TmfEventField.makeRoot(fields.toArray(fieldArray)); | |
61 | fTypes[i] = new TmfEventType("UnitTest", "Type-" + i, rootField); | |
62 | } | |
d18dd09b ASL |
63 | } |
64 | ||
e31e01e8 | 65 | // ------------------------------------------------------------------------ |
d18dd09b | 66 | // Operators |
e31e01e8 | 67 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
68 | |
69 | static final String typePrefix = "Type-"; | |
d4011df2 | 70 | @Override |
085d898f | 71 | @SuppressWarnings("unchecked") |
2352aed9 | 72 | public TmfEvent parseEvent(final ITmfContext context) { |
d18dd09b | 73 | |
7e6347b0 | 74 | if (! (fEventStream instanceof TmfTraceStub)) |
d18dd09b | 75 | return null; |
d18dd09b | 76 | |
085d898f | 77 | // Highly inefficient... |
7e6347b0 | 78 | final RandomAccessFile stream = ((TmfTraceStub) fEventStream).getStream(); |
085d898f FC |
79 | // String name = eventStream.getName(); |
80 | // name = name.substring(name.lastIndexOf('/') + 1); | |
81 | ||
82 | // no need to use synchronized since it's already cover by the calling method | |
83 | ||
84 | long location = 0; | |
0316808c | 85 | if (context != null && context.getLocation() != null) { |
085d898f | 86 | location = ((TmfLocation<Long>) (context.getLocation())).getLocation(); |
0316808c FC |
87 | try { |
88 | stream.seek(location); | |
89 | ||
90 | final long ts = stream.readLong(); | |
91 | final String source = stream.readUTF(); | |
92 | final String type = stream.readUTF(); | |
93 | final Integer reference = stream.readInt(); | |
94 | final int typeIndex = Integer.parseInt(type.substring(typePrefix.length())); | |
95 | final String[] fields = new String[typeIndex]; | |
96 | for (int i = 0; i < typeIndex; i++) | |
97 | fields[i] = stream.readUTF(); | |
98 | ||
99 | final StringBuffer content = new StringBuffer("["); | |
100 | if (typeIndex > 0) | |
101 | content.append(fields[0]); | |
102 | for (int i = 1; i < typeIndex; i++) | |
103 | content.append(", ").append(fields[i]); | |
104 | content.append("]"); | |
105 | ||
106 | final TmfEventField root = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content.toString()); | |
107 | final TmfEvent event = new TmfEvent(fEventStream, | |
108 | new TmfTimestamp(ts, -3, 0), // millisecs | |
109 | source, fTypes[typeIndex], root, reference.toString()); | |
110 | return event; | |
111 | } catch (final EOFException e) { | |
112 | } catch (final IOException e) { | |
113 | } | |
085d898f | 114 | } |
d18dd09b ASL |
115 | return null; |
116 | } | |
117 | ||
e31e01e8 | 118 | } |