ctf: Do not specify a maximum size for String declarations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.pcap.core / src / org / eclipse / tracecompass / internal / tmf / pcap / core / trace / PcapTrace.java
CommitLineData
b6eb4dce
VP
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.internal.tmf.pcap.core.trace;
b6eb4dce 14
5db5a3a4
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
b6eb4dce
VP
17import java.io.IOException;
18import java.nio.channels.ClosedChannelException;
333a2acb 19import java.nio.file.Path;
5db5a3a4 20import java.nio.file.Paths;
b04903a2 21import java.util.Collection;
5db5a3a4 22import java.util.Collections;
b6eb4dce
VP
23import java.util.Map;
24
25import org.eclipse.core.resources.IProject;
26import org.eclipse.core.resources.IResource;
27import org.eclipse.core.runtime.IStatus;
28import org.eclipse.core.runtime.Status;
b6eb4dce 29import org.eclipse.jdt.annotation.Nullable;
71f2817f
AM
30import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException;
31import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket;
32import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
33import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
34import org.eclipse.tracecompass.internal.pcap.core.util.LinkTypeHelper;
2bdf0193
AM
35import org.eclipse.tracecompass.internal.tmf.pcap.core.Activator;
36import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent;
b04903a2
AM
37import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapDestinationAspect;
38import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapProtocolAspect;
39import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapReferenceAspect;
40import org.eclipse.tracecompass.internal.tmf.pcap.core.event.aspect.PcapSourceAspect;
2bdf0193
AM
41import org.eclipse.tracecompass.internal.tmf.pcap.core.util.PcapEventFactory;
42import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
b04903a2 43import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
2bdf0193
AM
44import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
45import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
46import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
47import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
48import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
49import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
50import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
51import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
52import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
b6eb4dce 53
b04903a2 54import com.google.common.collect.ImmutableList;
b6eb4dce
VP
55import com.google.common.collect.ImmutableMap;
56
57/**
58 * Class that represents a TMF Pcap Trace. It is used to make the glue between
59 * the Pcap parser and TMF.
60 *
61 * TODO handle fields in TmfEventType for the filter view.
62 *
63 * @author Vincent Perot
64 */
65public class PcapTrace extends TmfTrace implements ITmfEventParser, ITmfTraceProperties, AutoCloseable {
66
5db5a3a4
AM
67 private static final Collection<ITmfEventAspect> PCAP_ASPECTS =
68 checkNotNull(ImmutableList.of(
69 ITmfEventAspect.BaseAspects.TIMESTAMP,
70 new PcapSourceAspect(),
71 new PcapDestinationAspect(),
72 new PcapReferenceAspect(),
73 new PcapProtocolAspect(),
74 ITmfEventAspect.BaseAspects.CONTENTS
75 ));
b04903a2 76
b6eb4dce
VP
77 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
78 private static final int CONFIDENCE = 50;
79 private @Nullable PcapFile fPcapFile;
5db5a3a4 80 private @Nullable Map<String, String> fTraceProperties = null;
b6eb4dce
VP
81
82 @Override
83 public synchronized ITmfLocation getCurrentLocation() {
84 PcapFile pcap = fPcapFile;
85 if (pcap == null) {
86 return new TmfLongLocation(0);
87 }
88 return new TmfLongLocation(pcap.getCurrentRank());
89 }
90
91 @Override
92 public synchronized double getLocationRatio(@Nullable ITmfLocation location) {
93 TmfLongLocation loc = (TmfLongLocation) location;
94 PcapFile pcap = fPcapFile;
95 if (loc == null || pcap == null) {
96 return 0;
97 }
98 try {
99 return (pcap.getTotalNbPackets() == 0 ? 0 : ((double) loc.getLocationInfo()) / pcap.getTotalNbPackets());
100 } catch (IOException | BadPcapFileException e) {
101 String message = e.getMessage();
102 if (message == null) {
103 message = EMPTY_STRING;
104 }
105 Activator.logError(message, e);
106 return 0;
107 }
108
109 }
110
111 @Override
112 public synchronized void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException {
113 super.initTrace(resource, path, type);
114 if (path == null) {
115 throw new TmfTraceException("No path has been specified."); //$NON-NLS-1$
116 }
5db5a3a4 117 Path filePath = checkNotNull(Paths.get(path));
b6eb4dce 118 try {
333a2acb 119 fPcapFile = new PcapFile(filePath);
b6eb4dce
VP
120 } catch (IOException | BadPcapFileException e) {
121 throw new TmfTraceException(e.getMessage(), e);
122 }
123 }
124
b04903a2
AM
125 @Override
126 public Iterable<ITmfEventAspect> getEventAspects() {
127 return PCAP_ASPECTS;
128 }
129
b6eb4dce
VP
130 @Override
131 public synchronized @Nullable PcapEvent parseEvent(@Nullable ITmfContext context) {
132 if (context == null) {
133 return null;
134 }
135
136 long rank = context.getRank();
137 PcapPacket packet = null;
138 PcapFile pcap = fPcapFile;
139 if (pcap == null) {
140 return null;
141 }
142 try {
143 pcap.seekPacket(rank);
144 packet = pcap.parseNextPacket();
145 } catch (ClosedChannelException e) {
146 /*
147 * This is handled independently and happens when the user closes
148 * the trace while it is being parsed. It simply stops the parsing.
149 * No need to log a error.
150 */
151 return null;
152 } catch (IOException | BadPcapFileException | BadPacketException e) {
153 String message = e.getMessage();
154 if (message == null) {
155 message = EMPTY_STRING;
156 }
157 Activator.logError(message, e);
158 return null;
159 }
160
161 if (packet == null) {
162 return null;
163 }
164
165 // Generate an event from this packet and return it.
166 return PcapEventFactory.createEvent(packet, pcap, this);
167
168 }
169
170 @Override
171 public synchronized ITmfContext seekEvent(double ratio) {
172 long position;
173 PcapFile pcap = fPcapFile;
174 if (pcap == null) {
175 return new TmfContext(new TmfLongLocation(0), 0);
176 }
177
178 try {
179 /*
180 * The ratio is between 0 and 1. We multiply it by the total number
181 * of packets to get the position.
182 */
183 position = (long) (ratio * pcap.getTotalNbPackets());
184 } catch (IOException | BadPcapFileException e) {
185 String message = e.getMessage();
186 if (message == null) {
187 message = EMPTY_STRING;
188 }
189 Activator.logError(message, e);
190 return new TmfContext(new TmfLongLocation(0), 0);
191 }
192 TmfLongLocation loc = new TmfLongLocation(position);
193 return new TmfContext(loc, loc.getLocationInfo());
194 }
195
196 @Override
197 public synchronized ITmfContext seekEvent(@Nullable ITmfLocation location) {
198 TmfLongLocation loc = (TmfLongLocation) location;
199 if (loc == null) {
200 return new TmfContext(new TmfLongLocation(0));
201 }
202
203 return new TmfContext(loc, loc.getLocationInfo());
204 }
205
206 @Override
207 public IStatus validate(@Nullable IProject project, @Nullable String path) {
208
209 // All validations are made when making a new pcap file.
210 if (path == null) {
211 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, EMPTY_STRING);
212 }
5db5a3a4 213 Path filePath = checkNotNull(Paths.get(path));
333a2acb 214 try (PcapFile file = new PcapFile(filePath)) {
b6eb4dce
VP
215 } catch (IOException | BadPcapFileException e) {
216 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString());
217 }
218 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
219 }
220
221 @Override
222 public synchronized void dispose() {
223 super.dispose();
224 PcapFile pcap = fPcapFile;
225 if (pcap == null) {
226 return;
227 }
228 try {
229 pcap.close();
230 fPcapFile = null;
231 } catch (IOException e) {
232 String message = e.getMessage();
233 if (message == null) {
234 message = EMPTY_STRING;
235 }
236 Activator.logError(message, e);
237 return;
238 }
239 }
240
241 @Override
242 public synchronized Map<String, String> getTraceProperties() {
243 PcapFile pcap = fPcapFile;
244 if (pcap == null) {
5db5a3a4 245 return checkNotNull(Collections.<String, String> emptyMap());
b6eb4dce
VP
246 }
247
5db5a3a4 248 Map<String, String> properties = fTraceProperties;
b6eb4dce 249 if (properties == null) {
5db5a3a4
AM
250 ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String> builder();
251 builder.put(Messages.PcapTrace_Version, String.format("%d%c%d", pcap.getMajorVersion(), '.', pcap.getMinorVersion())); //$NON-NLS-1$
252 builder.put(Messages.PcapTrace_TimeZoneCorrection, pcap.getTimeZoneCorrection() + " s"); //$NON-NLS-1$
253 builder.put(Messages.PcapTrace_TimestampAccuracy, String.valueOf(pcap.getTimeAccuracy()));
254 builder.put(Messages.PcapTrace_MaxSnapLength, pcap.getSnapLength() + " bytes"); //$NON-NLS-1$
255 builder.put(Messages.PcapTrace_LinkLayerHeaderType, LinkTypeHelper.toString((int) pcap.getDataLinkType()) + " (" + pcap.getDataLinkType() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
256 builder.put(Messages.PcapTrace_FileEndianness, pcap.getByteOrder().toString());
257
258 return checkNotNull(builder.build());
b6eb4dce
VP
259
260 }
261
262 return properties;
263 }
264
265 @Override
266 public void close() {
267 dispose();
268 }
269}
This page took 0.049838 seconds and 5 git commands to generate.