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