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