Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | ||
486efb2e | 13 | package org.eclipse.linuxtools.ctf.core.trace; |
866e5b51 FC |
14 | |
15 | import java.util.HashMap; | |
16 | import java.util.HashSet; | |
0594c61c | 17 | import java.util.Map; |
866e5b51 FC |
18 | import java.util.Set; |
19 | ||
8e964be1 | 20 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
866e5b51 | 21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
b73145e2 | 22 | import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration; |
ce2388e0 | 23 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; |
866e5b51 FC |
24 | |
25 | /** | |
26 | * <b><u>Stream</u></b> | |
27 | * <p> | |
28 | * Represents a stream in a trace. | |
486efb2e | 29 | * @since 2.0 |
866e5b51 FC |
30 | */ |
31 | public class Stream { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
37 | ||
38 | /** | |
39 | * The numerical ID of the stream | |
40 | */ | |
2ab0ccec | 41 | private Long fId = null; |
866e5b51 FC |
42 | |
43 | /** | |
44 | * Declarations of the stream-specific structures | |
45 | */ | |
2ab0ccec MK |
46 | private StructDeclaration fPacketContextDecl = null; |
47 | private StructDeclaration fEventHeaderDecl = null; | |
48 | private StructDeclaration fEventContextDecl = null; | |
866e5b51 FC |
49 | |
50 | /** | |
51 | * The trace to which the stream belongs | |
52 | */ | |
2ab0ccec | 53 | private CTFTrace fTrace = null; |
866e5b51 FC |
54 | |
55 | /** | |
56 | * Maps event ids to events | |
57 | */ | |
2ab0ccec | 58 | private Map<Long, IEventDeclaration> fEvents = new HashMap<>(); |
866e5b51 FC |
59 | |
60 | /** | |
61 | * The inputs associated to this stream | |
62 | */ | |
2ab0ccec | 63 | private final Set<StreamInput> fInputs = new HashSet<>(); |
866e5b51 FC |
64 | |
65 | // ------------------------------------------------------------------------ | |
66 | // Constructors | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | /** | |
70 | * Constructs a Stream that belongs to a Trace | |
71 | * | |
72 | * @param trace | |
73 | * The trace to which belongs this stream. | |
74 | */ | |
75 | public Stream(CTFTrace trace) { | |
2ab0ccec | 76 | fTrace = trace; |
866e5b51 FC |
77 | } |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // Getters/Setters/Predicates | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
9ac2eb62 MK |
83 | /** |
84 | * Sets the id of a stream | |
85 | * @param id the id of a stream | |
86 | */ | |
866e5b51 | 87 | public void setId(long id) { |
2ab0ccec | 88 | fId = id; |
866e5b51 FC |
89 | } |
90 | ||
9ac2eb62 MK |
91 | /** |
92 | * Gets the id of a stream | |
93 | * @return id the id of a stream | |
94 | */ | |
866e5b51 | 95 | public Long getId() { |
2ab0ccec | 96 | return fId; |
866e5b51 FC |
97 | } |
98 | ||
9ac2eb62 MK |
99 | /** |
100 | * Is the id of a stream set | |
be6df2d8 AM |
101 | * |
102 | * @return If the ID is set or not | |
9ac2eb62 MK |
103 | */ |
104 | public boolean isIdSet() { | |
2ab0ccec | 105 | return fId != null; |
866e5b51 FC |
106 | } |
107 | ||
9ac2eb62 MK |
108 | /** |
109 | * | |
110 | * @return is the event header set (timestamp and stuff) (see Ctf Spec) | |
111 | */ | |
112 | public boolean isEventHeaderSet() { | |
2ab0ccec | 113 | return fEventHeaderDecl != null; |
866e5b51 FC |
114 | } |
115 | ||
9ac2eb62 MK |
116 | /** |
117 | * | |
118 | * @return is the event context set (pid and stuff) (see Ctf Spec) | |
119 | */ | |
120 | public boolean isEventContextSet() { | |
2ab0ccec | 121 | return fEventContextDecl != null; |
866e5b51 FC |
122 | } |
123 | ||
9ac2eb62 MK |
124 | /** |
125 | * | |
126 | * @return Is the packet context set (see Ctf Spec) | |
127 | */ | |
128 | public boolean isPacketContextSet() { | |
2ab0ccec | 129 | return fPacketContextDecl != null; |
866e5b51 FC |
130 | } |
131 | ||
9ac2eb62 MK |
132 | /** |
133 | * | |
134 | * @param eventHeader the current event header for all events in this stream | |
135 | */ | |
866e5b51 | 136 | public void setEventHeader(StructDeclaration eventHeader) { |
2ab0ccec | 137 | fEventHeaderDecl = eventHeader; |
866e5b51 FC |
138 | } |
139 | ||
9ac2eb62 MK |
140 | /** |
141 | * | |
142 | * @param eventContext the context for all events in this stream | |
143 | */ | |
866e5b51 | 144 | public void setEventContext(StructDeclaration eventContext) { |
2ab0ccec | 145 | fEventContextDecl = eventContext; |
866e5b51 FC |
146 | } |
147 | ||
9ac2eb62 MK |
148 | /** |
149 | * | |
150 | * @param packetContext the packet context for all packets in this stream | |
151 | */ | |
866e5b51 | 152 | public void setPacketContext(StructDeclaration packetContext) { |
2ab0ccec | 153 | fPacketContextDecl = packetContext; |
866e5b51 FC |
154 | } |
155 | ||
9ac2eb62 MK |
156 | /** |
157 | * | |
158 | * @return the event header declaration in structdeclaration form | |
159 | */ | |
866e5b51 | 160 | public StructDeclaration getEventHeaderDecl() { |
2ab0ccec | 161 | return fEventHeaderDecl; |
866e5b51 FC |
162 | } |
163 | ||
9ac2eb62 MK |
164 | /** |
165 | * | |
166 | * @return the event context declaration in structdeclaration form | |
167 | */ | |
866e5b51 | 168 | public StructDeclaration getEventContextDecl() { |
2ab0ccec | 169 | return fEventContextDecl; |
866e5b51 FC |
170 | } |
171 | ||
9ac2eb62 MK |
172 | /** |
173 | * | |
174 | * @return the packet context declaration in structdeclaration form | |
175 | */ | |
866e5b51 | 176 | public StructDeclaration getPacketContextDecl() { |
2ab0ccec | 177 | return fPacketContextDecl; |
866e5b51 FC |
178 | } |
179 | ||
9ac2eb62 MK |
180 | /** |
181 | * | |
182 | * @return the set of all stream inputs for this stream | |
183 | */ | |
866e5b51 | 184 | public Set<StreamInput> getStreamInputs() { |
2ab0ccec | 185 | return fInputs; |
866e5b51 FC |
186 | } |
187 | ||
9ac2eb62 MK |
188 | /** |
189 | * | |
190 | * @return the parent trace | |
191 | */ | |
866e5b51 | 192 | public CTFTrace getTrace() { |
2ab0ccec | 193 | return fTrace; |
866e5b51 FC |
194 | } |
195 | ||
9ac2eb62 MK |
196 | /** |
197 | * | |
198 | * @return all the event declarations for this stream, using the id as a key for the hashmap. | |
199 | */ | |
0594c61c | 200 | public Map<Long, IEventDeclaration> getEvents() { |
2ab0ccec | 201 | return fEvents; |
866e5b51 FC |
202 | } |
203 | ||
204 | // ------------------------------------------------------------------------ | |
205 | // Operations | |
206 | // ------------------------------------------------------------------------ | |
207 | ||
208 | /** | |
209 | * Adds an event to the event map. | |
210 | * | |
211 | * An event in a stream can omit its id if it is the only event in this | |
212 | * stream. An event for which no id has been specified has a null id. It is | |
213 | * thus not possible to add an event with the null key if the map is not | |
214 | * empty. It is also not possible to add an event to the map if the null key | |
215 | * is present in the map. | |
216 | * | |
217 | * @param event | |
be6df2d8 | 218 | * The event to add |
866e5b51 | 219 | * @throws ParseException |
be6df2d8 AM |
220 | * If there was a problem reading the event or adding it to the |
221 | * stream | |
866e5b51 | 222 | */ |
8e964be1 | 223 | public void addEvent(IEventDeclaration event) throws ParseException { |
866e5b51 FC |
224 | /* |
225 | * If there is an event without id (the null key), it must be the only | |
226 | * one | |
227 | */ | |
2ab0ccec | 228 | if (fEvents.get(null) != null) { |
866e5b51 FC |
229 | throw new ParseException( |
230 | "Event without id with multiple events in a stream"); //$NON-NLS-1$ | |
231 | } | |
232 | ||
233 | /* | |
234 | * If there is an event without id (the null key), it must be the only | |
235 | * one | |
236 | */ | |
2ab0ccec | 237 | if ((event.getId() == null) && (fEvents.size() != 0)) { |
866e5b51 FC |
238 | throw new ParseException( |
239 | "Event without id with multiple events in a stream"); //$NON-NLS-1$ | |
240 | } | |
241 | ||
242 | /* Check if an event with the same ID already exists */ | |
2ab0ccec | 243 | if (fEvents.get(event.getId()) != null) { |
866e5b51 FC |
244 | throw new ParseException("Event id already exists"); //$NON-NLS-1$ |
245 | } | |
b73145e2 | 246 | if (event.getId() == null) { |
2ab0ccec | 247 | fEvents.put(EventDeclaration.UNSET_EVENT_ID, event); |
b73145e2 JCK |
248 | } else { |
249 | /* Put the event in the map */ | |
2ab0ccec | 250 | fEvents.put(event.getId(), event); |
b73145e2 | 251 | } |
866e5b51 FC |
252 | } |
253 | ||
254 | /** | |
255 | * Add an input to this Stream | |
256 | * | |
257 | * @param input | |
258 | * The StreamInput to add. | |
259 | */ | |
260 | public void addInput(StreamInput input) { | |
2ab0ccec | 261 | fInputs.add(input); |
866e5b51 FC |
262 | } |
263 | ||
866e5b51 FC |
264 | @Override |
265 | public String toString() { | |
2ab0ccec MK |
266 | return "Stream [id=" + fId + ", packetContextDecl=" + fPacketContextDecl //$NON-NLS-1$ //$NON-NLS-2$ |
267 | + ", eventHeaderDecl=" + fEventHeaderDecl //$NON-NLS-1$ | |
268 | + ", eventContextDecl=" + fEventContextDecl + ", trace=" + fTrace //$NON-NLS-1$ //$NON-NLS-2$ | |
269 | + ", events=" + fEvents + ", inputs=" + fInputs + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
866e5b51 FC |
270 | } |
271 | } |