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