Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
146a887c | 2 | * Copyright (c) 2009 Ericsson |
d18dd09b ASL |
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.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | ||
19 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
d18dd09b ASL |
20 | |
21 | /** | |
22 | * <b><u>TmfTraceStub</u></b> | |
23 | * <p> | |
8d2e2848 | 24 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 25 | */ |
146a887c | 26 | public class TmfTraceStub extends TmfTrace { |
d18dd09b | 27 | |
146a887c | 28 | // ======================================================================== |
d18dd09b | 29 | // Attributes |
146a887c | 30 | // ======================================================================== |
d18dd09b ASL |
31 | |
32 | // The actual stream | |
146a887c | 33 | private final RandomAccessFile fTrace; |
d18dd09b ASL |
34 | |
35 | // The associated event parser | |
146a887c | 36 | private final ITmfEventParser fParser; |
d18dd09b | 37 | |
146a887c | 38 | // ======================================================================== |
d18dd09b | 39 | // Constructors |
146a887c | 40 | // ======================================================================== |
d18dd09b ASL |
41 | |
42 | /** | |
43 | * @param filename | |
44 | * @throws FileNotFoundException | |
45 | */ | |
46 | public TmfTraceStub(String filename) throws FileNotFoundException { | |
8d2e2848 FC |
47 | this(filename, DEFAULT_CACHE_SIZE, false); |
48 | } | |
49 | ||
50 | /** | |
51 | * @param filename | |
52 | * @throws FileNotFoundException | |
53 | */ | |
54 | public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException { | |
55 | this(filename, DEFAULT_CACHE_SIZE, waitForCompletion); | |
d18dd09b ASL |
56 | } |
57 | ||
58 | /** | |
59 | * @param filename | |
60 | * @param cacheSize | |
61 | * @throws FileNotFoundException | |
62 | */ | |
63 | public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException { | |
8d2e2848 FC |
64 | this(filename, cacheSize, false); |
65 | } | |
66 | ||
67 | /** | |
68 | * @param filename | |
69 | * @param cacheSize | |
70 | * @throws FileNotFoundException | |
71 | */ | |
72 | public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException { | |
73 | super(filename, cacheSize, waitForCompletion); | |
d18dd09b ASL |
74 | fTrace = new RandomAccessFile(filename, "r"); |
75 | fParser = new TmfEventParserStub(); | |
146a887c | 76 | indexStream(); |
d18dd09b ASL |
77 | } |
78 | ||
146a887c | 79 | // ======================================================================== |
d18dd09b | 80 | // Accessors |
146a887c | 81 | // ======================================================================== |
d18dd09b ASL |
82 | |
83 | public RandomAccessFile getStream() { | |
84 | return fTrace; | |
85 | } | |
86 | ||
146a887c | 87 | // ======================================================================== |
d18dd09b | 88 | // Operators |
146a887c | 89 | // ======================================================================== |
d18dd09b | 90 | |
146a887c FC |
91 | /* (non-Javadoc) |
92 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#seekLocation(java.lang.Object) | |
93 | */ | |
4e3aa37d | 94 | public TmfTraceContext seekLocation(Object location) { |
146a887c | 95 | TmfTraceContext context = null; |
d18dd09b | 96 | try { |
4e3aa37d FC |
97 | synchronized(fTrace) { |
98 | fTrace.seek((location != null) ? (Long) location : 0); | |
99 | context = new TmfTraceContext(getCurrentLocation(), null, 0); | |
100 | } | |
d18dd09b | 101 | } catch (IOException e) { |
146a887c | 102 | // TODO Auto-generated catch block |
d18dd09b ASL |
103 | e.printStackTrace(); |
104 | } | |
146a887c | 105 | return context; |
d18dd09b ASL |
106 | } |
107 | ||
146a887c FC |
108 | /* (non-Javadoc) |
109 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#getCurrentLocation() | |
110 | */ | |
4e3aa37d FC |
111 | @Override |
112 | public Object getCurrentLocation() { | |
d18dd09b | 113 | try { |
146a887c | 114 | return new Long(fTrace.getFilePointer()); |
d18dd09b | 115 | } catch (IOException e) { |
146a887c | 116 | // TODO Auto-generated catch block |
d18dd09b ASL |
117 | e.printStackTrace(); |
118 | } | |
119 | return null; | |
120 | } | |
121 | ||
146a887c FC |
122 | /* (non-Javadoc) |
123 | * @see org.eclipse.linuxtools.tmf.trace.TmfTrace#parseEvent() | |
124 | */ | |
4e3aa37d | 125 | @Override |
8d2e2848 | 126 | public TmfEvent parseEvent(TmfTraceContext context) { |
cc6eec3e | 127 | try { |
8d2e2848 FC |
128 | // paserNextEvent updates the context |
129 | TmfEvent event = fParser.parseNextEvent(this, context); | |
130 | if (event != null) { | |
131 | context.setTimestamp(event.getTimestamp()); | |
132 | } | |
cc6eec3e FC |
133 | return event; |
134 | } | |
135 | catch (IOException e) { | |
136 | e.printStackTrace(); | |
137 | } | |
138 | return null; | |
d18dd09b ASL |
139 | } |
140 | ||
cc6eec3e | 141 | // ======================================================================== |
146a887c FC |
142 | // Helper functions |
143 | // ======================================================================== | |
d18dd09b | 144 | |
146a887c | 145 | } |