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 | ||
13 | package org.eclipse.linuxtools.tmf.trace; | |
14 | ||
15 | import java.io.EOFException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | import java.util.Vector; | |
19 | ||
20 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
21 | import org.eclipse.linuxtools.tmf.event.TmfEventContent; | |
22 | import org.eclipse.linuxtools.tmf.event.TmfEventReference; | |
23 | import org.eclipse.linuxtools.tmf.event.TmfEventSource; | |
24 | import org.eclipse.linuxtools.tmf.event.TmfEventType; | |
25 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
26 | import org.eclipse.linuxtools.tmf.parser.ITmfEventParser; | |
27 | ||
28 | /** | |
29 | * <b><u>TmfEventParserStub</u></b> | |
30 | * <p> | |
31 | * TODO: Implement me. Please. | |
32 | */ | |
33 | public class TmfEventParserStub implements ITmfEventParser { | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Attributes | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | private final int NB_TYPES = 10; | |
40 | private final TmfEventType[] fTypes; | |
41 | ||
42 | // ------------------------------------------------------------------------ | |
43 | // Constructors | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
46 | public TmfEventParserStub() { | |
47 | fTypes = new TmfEventType[NB_TYPES]; | |
48 | for (int i = 0; i < NB_TYPES; i++) { | |
49 | Vector<String> format = new Vector<String>(); | |
50 | for (int j = 1; j <= i; j++) { | |
51 | format.add(new String("Fmt-" + i + "-Fld-" + j)); | |
52 | } | |
53 | String[] fields = new String[i]; | |
54 | fTypes[i] = new TmfEventType("Type-" + i, format.toArray(fields)); | |
55 | } | |
56 | } | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Operators | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | static final String typePrefix = "Type-"; | |
63 | @SuppressWarnings("unchecked") | |
64 | public TmfEvent parseNextEvent(ITmfTrace eventStream, TmfContext context) throws IOException { | |
65 | ||
66 | if (! (eventStream instanceof TmfTraceStub)) { | |
67 | return null; | |
68 | } | |
69 | ||
70 | // Highly inefficient... | |
71 | RandomAccessFile stream = ((TmfTraceStub) eventStream).getStream(); | |
72 | String name = eventStream.getName(); | |
73 | name = name.substring(name.lastIndexOf('/') + 1); | |
74 | ||
75 | synchronized(stream) { | |
76 | long location = 0; | |
77 | if (context != null) | |
78 | location = ((TmfLocation<Long>) (context.getLocation())).getLocation(); | |
79 | stream.seek(location); | |
80 | ||
81 | try { | |
82 | long ts = stream.readLong(); | |
83 | String source = stream.readUTF(); | |
84 | String type = stream.readUTF(); | |
85 | @SuppressWarnings("unused") | |
86 | int reference = stream.readInt(); | |
87 | int typeIndex = Integer.parseInt(type.substring(typePrefix.length())); | |
88 | String[] fields = new String[typeIndex]; | |
89 | for (int i = 0; i < typeIndex; i++) { | |
90 | fields[i] = stream.readUTF(); | |
91 | } | |
92 | ||
93 | String content = "["; | |
94 | if (typeIndex > 0) { | |
95 | content += fields[0]; | |
96 | } | |
97 | for (int i = 1; i < typeIndex; i++) { | |
98 | content += ", " + fields[i]; | |
99 | } | |
100 | content += "]"; | |
101 | ||
102 | TmfEvent event = new TmfEvent( | |
103 | new TmfTimestamp(ts, (byte) -3, 0), // millisecs | |
104 | new TmfEventSource(source), | |
105 | fTypes[typeIndex], | |
106 | new TmfEventReference(name)); | |
107 | TmfEventContent cnt = new TmfEventContent(event, content); | |
108 | event.setContent(cnt); | |
109 | return event; | |
110 | } catch (EOFException e) { | |
111 | } | |
112 | } | |
113 | return null; | |
114 | } | |
115 | ||
116 | } |