ctf: add Event header data types
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFStream.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.trace;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
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;
26
27 /**
28 * <b><u>Stream</u></b>
29 * <p>
30 * Represents a stream in a trace.
31 *
32 * @since 3.0
33 */
34 public class CTFStream {
35
36 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39
40 /**
41 * The numerical ID of the stream
42 */
43 private Long fId = null;
44
45 /**
46 * Declarations of the stream-specific structures
47 */
48 private StructDeclaration fPacketContextDecl = null;
49 private IDeclaration fEventHeaderDecl = null;
50 private StructDeclaration fEventContextDecl = null;
51
52 /**
53 * The trace to which the stream belongs
54 */
55 private CTFTrace fTrace = null;
56
57 /**
58 * Maps event ids to events
59 */
60 private Map<Long, IEventDeclaration> fEvents = new HashMap<>();
61
62 /**
63 * The inputs associated to this stream
64 */
65 private final Set<CTFStreamInput> fInputs = new HashSet<>();
66
67 // ------------------------------------------------------------------------
68 // Constructors
69 // ------------------------------------------------------------------------
70
71 /**
72 * Constructs a Stream that belongs to a Trace
73 *
74 * @param trace
75 * The trace to which belongs this stream.
76 */
77 public CTFStream(CTFTrace trace) {
78 fTrace = trace;
79 }
80
81 // ------------------------------------------------------------------------
82 // Getters/Setters/Predicates
83 // ------------------------------------------------------------------------
84
85 /**
86 * Sets the id of a stream
87 *
88 * @param id
89 * the id of a stream
90 */
91 public void setId(long id) {
92 fId = id;
93 }
94
95 /**
96 * Gets the id of a stream
97 *
98 * @return id the id of a stream
99 */
100 public Long getId() {
101 return fId;
102 }
103
104 /**
105 * Is the id of a stream set
106 *
107 * @return If the ID is set or not
108 */
109 public boolean isIdSet() {
110 return fId != null;
111 }
112
113 /**
114 *
115 * @return is the event header set (timestamp and stuff) (see Ctf Spec)
116 */
117 public boolean isEventHeaderSet() {
118 return fEventHeaderDecl != null;
119 }
120
121 /**
122 *
123 * @return is the event context set (pid and stuff) (see Ctf Spec)
124 */
125 public boolean isEventContextSet() {
126 return fEventContextDecl != null;
127 }
128
129 /**
130 *
131 * @return Is the packet context set (see Ctf Spec)
132 */
133 public boolean isPacketContextSet() {
134 return fPacketContextDecl != null;
135 }
136
137 /**
138 * Sets the event header
139 *
140 * @param eventHeader
141 * the current event header for all events in this stream
142 */
143 public void setEventHeader(StructDeclaration eventHeader) {
144 fEventHeaderDecl = eventHeader;
145 }
146
147 /**
148 * Sets the event header, this typically has the id and the timestamp
149 *
150 * @param eventHeader
151 * the current event header for all events in this stream
152 * @since 3.1
153 */
154 public void setEventHeader(IEventHeaderDeclaration eventHeader) {
155 fEventHeaderDecl = eventHeader;
156 }
157
158 /**
159 *
160 * @param eventContext
161 * the context for all events in this stream
162 */
163 public void setEventContext(StructDeclaration eventContext) {
164 fEventContextDecl = eventContext;
165 }
166
167 /**
168 *
169 * @param packetContext
170 * the packet context for all packets in this stream
171 */
172 public void setPacketContext(StructDeclaration packetContext) {
173 fPacketContextDecl = packetContext;
174 }
175
176 /**
177 *
178 * @return the event header declaration in structdeclaration form
179 * @deprecated use {@link CTFStream#getEventHeaderDeclaration()}
180 */
181 @Deprecated
182 public StructDeclaration getEventHeaderDecl() {
183 return (StructDeclaration) ((fEventHeaderDecl instanceof StructDeclaration) ? fEventHeaderDecl : null);
184 }
185
186 /**
187 * Gets the event header declaration
188 *
189 * @return the event header declaration in declaration form
190 * @since 3.1
191 */
192 public IDeclaration getEventHeaderDeclaration() {
193 return fEventHeaderDecl;
194 }
195
196 /**
197 *
198 * @return the event context declaration in structdeclaration form
199 */
200 public StructDeclaration getEventContextDecl() {
201 return fEventContextDecl;
202 }
203
204 /**
205 *
206 * @return the packet context declaration in structdeclaration form
207 */
208 public StructDeclaration getPacketContextDecl() {
209 return fPacketContextDecl;
210 }
211
212 /**
213 *
214 * @return the set of all stream inputs for this stream
215 */
216 public Set<CTFStreamInput> getStreamInputs() {
217 return fInputs;
218 }
219
220 /**
221 *
222 * @return the parent trace
223 */
224 public CTFTrace getTrace() {
225 return fTrace;
226 }
227
228 /**
229 *
230 * @return all the event declarations for this stream, using the id as a key
231 * for the hashmap.
232 */
233 public Map<Long, IEventDeclaration> getEvents() {
234 return fEvents;
235 }
236
237 // ------------------------------------------------------------------------
238 // Operations
239 // ------------------------------------------------------------------------
240
241 /**
242 * Adds an event to the event map.
243 *
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.
249 *
250 * @param event
251 * The event to add
252 * @throws ParseException
253 * If there was a problem reading the event or adding it to the
254 * stream
255 */
256 public void addEvent(IEventDeclaration event) throws ParseException {
257 /*
258 * If there is an event without id (the null key), it must be the only
259 * one
260 */
261 if (fEvents.get(null) != null) {
262 throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$
263 }
264
265 /*
266 * If there is an event without id (the null key), it must be the only
267 * one
268 */
269 if ((event.getId() == null) && (fEvents.size() != 0)) {
270 throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$
271 }
272
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$
276 }
277 if (event.getId() == null) {
278 fEvents.put(EventDeclaration.UNSET_EVENT_ID, event);
279 } else {
280 /* Put the event in the map */
281 fEvents.put(event.getId(), event);
282 }
283 }
284
285 /**
286 * Add an input to this Stream
287 *
288 * @param input
289 * The StreamInput to add.
290 */
291 public void addInput(CTFStreamInput input) {
292 fInputs.add(input);
293 }
294
295 @Override
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$
301 }
302 }
This page took 0.071506 seconds and 5 git commands to generate.