1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IEventHeaderDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
24 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.EventDeclaration
;
25 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
28 * <b><u>Stream</u></b>
30 * Represents a stream in a trace.
34 public class CTFStream
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 * The numerical ID of the stream
43 private Long fId
= null;
46 * Declarations of the stream-specific structures
48 private StructDeclaration fPacketContextDecl
= null;
49 private IDeclaration fEventHeaderDecl
= null;
50 private StructDeclaration fEventContextDecl
= null;
53 * The trace to which the stream belongs
55 private CTFTrace fTrace
= null;
58 * Maps event ids to events
60 private Map
<Long
, IEventDeclaration
> fEvents
= new HashMap
<>();
63 * The inputs associated to this stream
65 private final Set
<CTFStreamInput
> fInputs
= new HashSet
<>();
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * Constructs a Stream that belongs to a Trace
75 * The trace to which belongs this stream.
77 public CTFStream(CTFTrace trace
) {
81 // ------------------------------------------------------------------------
82 // Getters/Setters/Predicates
83 // ------------------------------------------------------------------------
86 * Sets the id of a stream
91 public void setId(long id
) {
96 * Gets the id of a stream
98 * @return id the id of a stream
100 public Long
getId() {
105 * Is the id of a stream set
107 * @return If the ID is set or not
109 public boolean isIdSet() {
115 * @return is the event header set (timestamp and stuff) (see Ctf Spec)
117 public boolean isEventHeaderSet() {
118 return fEventHeaderDecl
!= null;
123 * @return is the event context set (pid and stuff) (see Ctf Spec)
125 public boolean isEventContextSet() {
126 return fEventContextDecl
!= null;
131 * @return Is the packet context set (see Ctf Spec)
133 public boolean isPacketContextSet() {
134 return fPacketContextDecl
!= null;
138 * Sets the event header
141 * the current event header for all events in this stream
143 public void setEventHeader(StructDeclaration eventHeader
) {
144 fEventHeaderDecl
= eventHeader
;
148 * Sets the event header, this typically has the id and the timestamp
151 * the current event header for all events in this stream
154 public void setEventHeader(IEventHeaderDeclaration eventHeader
) {
155 fEventHeaderDecl
= eventHeader
;
160 * @param eventContext
161 * the context for all events in this stream
163 public void setEventContext(StructDeclaration eventContext
) {
164 fEventContextDecl
= eventContext
;
169 * @param packetContext
170 * the packet context for all packets in this stream
172 public void setPacketContext(StructDeclaration packetContext
) {
173 fPacketContextDecl
= packetContext
;
178 * @return the event header declaration in structdeclaration form
179 * @deprecated use {@link CTFStream#getEventHeaderDeclaration()}
182 public StructDeclaration
getEventHeaderDecl() {
183 return (StructDeclaration
) ((fEventHeaderDecl
instanceof StructDeclaration
) ? fEventHeaderDecl
: null);
187 * Gets the event header declaration
189 * @return the event header declaration in declaration form
192 public IDeclaration
getEventHeaderDeclaration() {
193 return fEventHeaderDecl
;
198 * @return the event context declaration in structdeclaration form
200 public StructDeclaration
getEventContextDecl() {
201 return fEventContextDecl
;
206 * @return the packet context declaration in structdeclaration form
208 public StructDeclaration
getPacketContextDecl() {
209 return fPacketContextDecl
;
214 * @return the set of all stream inputs for this stream
216 public Set
<CTFStreamInput
> getStreamInputs() {
222 * @return the parent trace
224 public CTFTrace
getTrace() {
230 * @return all the event declarations for this stream, using the id as a key
233 public Map
<Long
, IEventDeclaration
> getEvents() {
237 // ------------------------------------------------------------------------
239 // ------------------------------------------------------------------------
242 * Adds an event to the event map.
244 * An event in a stream can omit its id if it is the only event in this
245 * stream. An event for which no id has been specified has a null id. It is
246 * thus not possible to add an event with the null key if the map is not
247 * empty. It is also not possible to add an event to the map if the null key
248 * is present in the map.
252 * @throws ParseException
253 * If there was a problem reading the event or adding it to the
256 public void addEvent(IEventDeclaration event
) throws ParseException
{
258 * If there is an event without id (the null key), it must be the only
261 if (fEvents
.get(null) != null) {
262 throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$
266 * If there is an event without id (the null key), it must be the only
269 if ((event
.getId() == null) && (fEvents
.size() != 0)) {
270 throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$
273 /* Check if an event with the same ID already exists */
274 if (fEvents
.get(event
.getId()) != null) {
275 throw new ParseException("Event id already exists"); //$NON-NLS-1$
277 if (event
.getId() == null) {
278 fEvents
.put(EventDeclaration
.UNSET_EVENT_ID
, event
);
280 /* Put the event in the map */
281 fEvents
.put(event
.getId(), event
);
286 * Add an input to this Stream
289 * The StreamInput to add.
291 public void addInput(CTFStreamInput input
) {
296 public String
toString() {
297 return "Stream [id=" + fId
+ ", packetContextDecl=" + fPacketContextDecl
//$NON-NLS-1$ //$NON-NLS-2$
298 + ", eventHeaderDecl=" + fEventHeaderDecl
//$NON-NLS-1$
299 + ", eventContextDecl=" + fEventContextDecl
+ ", trace=" + fTrace
//$NON-NLS-1$ //$NON-NLS-2$
300 + ", events=" + fEvents
+ ", inputs=" + fInputs
+ "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$