import java.util.Set;
import java.util.UUID;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.ctf.core.CTFStrings;
import org.eclipse.tracecompass.ctf.core.event.CTFClock;
import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
-import org.eclipse.tracecompass.internal.ctf.core.event.metadata.exceptions.ParseException;
+import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
+import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
import org.eclipse.tracecompass.internal.ctf.core.trace.Utils;
+import com.google.common.collect.ImmutableMap;
+
/**
* A CTF trace on the file system.
*
/**
* Collection of streams contained in the trace.
*/
- private final Map<Long, CTFStream> fStreams = new HashMap<>();
+ private final Map<Long, ICTFStream> fStreams = new HashMap<>();
/**
* Collection of environment variables set by the tracer
*/
- private final Map<String, String> fEnvironment = new HashMap<>();
+ private Map<String, String> fEnvironment = new HashMap<>();
/**
* Collection of all the clocks in a system.
}
/* Create their index */
- for (CTFStream stream : getStreams()) {
+ for (ICTFStream stream : getStreams()) {
Set<CTFStreamInput> inputs = stream.getStreamInputs();
for (CTFStreamInput s : inputs) {
addStream(s);
* @return The list of event declarations
*/
public Collection<IEventDeclaration> getEventDeclarations(Long streamId) {
- return fStreams.get(streamId).getEventDeclarations();
+ ICTFStream stream = fStreams.get(streamId);
+ if (stream == null) {
+ return null;
+ }
+ return stream.getEventDeclarations();
}
/**
* @param id
* Long the id of the stream
* @return Stream the stream that we need
+ * @since 2.0
*/
- public CTFStream getStream(Long id) {
+ public ICTFStream getStream(Long id) {
if (id == null) {
return fStreams.get(0L);
}
*
* @return Iterable<Stream> an iterable over streams.
*/
- public Iterable<CTFStream> getStreams() {
+ public Iterable<ICTFStream> getStreams() {
return fStreams.values();
}
/*
* add the stream
*/
- CTFStream stream = s.getStream();
+ ICTFStream stream = s.getStream();
fStreams.put(stream.getId(), stream);
/*
* @throws CTFException
* if there is a file error
*/
- private CTFStream openStreamInput(File streamFile) throws CTFException {
+ private ICTFStream openStreamInput(File streamFile) throws CTFException {
ByteBuffer byteBuffer;
BitBuffer streamBitBuffer;
- CTFStream stream;
+ ICTFStream stream;
if (!streamFile.canRead()) {
throw new CTFException("Unreadable file : " //$NON-NLS-1$
/* Shouldn't happen at this stage if every other check passed */
throw new CTFException(e);
}
- if (fPacketHeaderDef != null) {
- validateMagicNumber(fPacketHeaderDef);
+ final StructDefinition packetHeaderDef = getPacketHeaderDef();
+ if (packetHeaderDef != null) {
+ validateMagicNumber(packetHeaderDef);
- validateUUID(fPacketHeaderDef);
+ validateUUID(packetHeaderDef);
/* Read the stream ID */
- IDefinition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
+ IDefinition streamIDDef = packetHeaderDef.lookupDefinition(MetadataStrings.STREAM_ID);
if (streamIDDef instanceof IntegerDefinition) {
/* This doubles as a null check */
throw new CTFException("Unexpected end of stream"); //$NON-NLS-1$
}
+ if (!(stream instanceof CTFStream)) {
+ throw new CTFException("Stream is not a CTFStream, but rather a " + stream.getClass().getCanonicalName()); //$NON-NLS-1$
+ }
+ CTFStream ctfStream = (CTFStream) stream;
/*
* Create the stream input and add a reference to the streamInput in the
* stream.
*/
- stream.addInput(new CTFStreamInput(stream, streamFile));
-
- return stream;
+ ctfStream.addInput(new CTFStreamInput(ctfStream, streamFile));
+ return ctfStream;
}
private void validateUUID(StructDefinition packetHeaderDef) throws CTFException {
@Override
public Definition lookupDefinition(String lookupPath) {
if (lookupPath.equals(ILexicalScope.TRACE_PACKET_HEADER.getPath())) {
- return fPacketHeaderDef;
+ return getPacketHeaderDef();
}
return null;
}
* A stream object.
* @throws ParseException
* If there was some problem reading the metadata
+ * @since 2.0
*/
- public void addStream(CTFStream stream) throws ParseException {
+ public void addStream(ICTFStream stream) throws ParseException {
/*
* If there is already a stream without id (the null key), it must be
* the only one
/*
* If a stream with the same ID already exists, it is not valid.
*/
- CTFStream existingStream = fStreams.get(stream.getId());
+ ICTFStream existingStream = fStreams.get(stream.getId());
if (existingStream != null) {
throw new ParseException("Stream id already exists"); //$NON-NLS-1$
}
return Collections.unmodifiableMap(fEnvironment);
}
- /**
- * Add a variable to the environment variables
- *
- * @param varName
- * the name of the variable
- * @param varValue
- * the value of the variable
- */
- public void addEnvironmentVar(String varName, String varValue) {
- fEnvironment.put(varName, varValue);
- }
-
/**
* Add a clock to the clock list
*
*/
public long getCurrentStartTime() {
long currentStart = Long.MAX_VALUE;
- for (CTFStream stream : fStreams.values()) {
+ for (ICTFStream stream : fStreams.values()) {
for (CTFStreamInput si : stream.getStreamInputs()) {
currentStart = Math.min(currentStart, si.getIndex().getElement(0).getTimestampBegin());
}
*/
public long getCurrentEndTime() {
long currentEnd = Long.MIN_VALUE;
- for (CTFStream stream : fStreams.values()) {
+ for (ICTFStream stream : fStreams.values()) {
for (CTFStreamInput si : stream.getStreamInputs()) {
currentEnd = Math.max(currentEnd, si.getTimestampEnd());
}
* The file must exist
*/
public void addStream(long id, File streamFile) throws CTFException {
- CTFStream stream = null;
final File file = streamFile;
if (file == null) {
throw new CTFException("cannot create a stream with no file"); //$NON-NLS-1$
}
- if (fStreams.containsKey(id)) {
- stream = fStreams.get(id);
- } else {
+ ICTFStream stream = fStreams.get(id);
+ if (stream == null) {
stream = new CTFStream(this);
fStreams.put(id, stream);
}
- stream.addInput(new CTFStreamInput(stream, file));
+ if (stream instanceof CTFStream) {
+ CTFStream ctfStream = (CTFStream) stream;
+ ctfStream.addInput(new CTFStreamInput(stream, file));
+ } else {
+ throw new CTFException("Stream does not support adding input"); //$NON-NLS-1$
+ }
}
/**
public DeclarationScope getScope() {
return fScope;
}
+
+ /**
+ * Gets the packet header definition (UUID, magic number and such)
+ *
+ * @return the packet header definition
+ *
+ * @since 2.0
+ */
+ public StructDefinition getPacketHeaderDef() {
+ return fPacketHeaderDef;
+ }
+
+ /**
+ * Sets the environment map
+ *
+ * @param parseEnvironment
+ * The environment map
+ * @since 2.0
+ */
+ public void setEnvironment(@NonNull Map<String, String> parseEnvironment) {
+ fEnvironment = ImmutableMap.copyOf(parseEnvironment);
+ }
}
class MetadataFileFilter implements FileFilter {