Add null-checks for Map.get()
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.tmf.pcap.core / src / org / eclipse / tracecompass / internal / tmf / pcap / core / analysis / StreamListAnalysis.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.analysis;
b6eb4dce
VP
14
15import java.util.HashMap;
fa62dc1d 16import java.util.List;
b6eb4dce
VP
17import java.util.Map;
18
19import org.eclipse.core.runtime.IProgressMonitor;
20import org.eclipse.core.runtime.NullProgressMonitor;
21import org.eclipse.jdt.annotation.Nullable;
2bdf0193
AM
22import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent;
23import org.eclipse.tracecompass.internal.tmf.pcap.core.event.TmfPacketStreamBuilder;
24import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
25import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace;
26import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
27import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
28import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
29import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
30import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
31import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
32import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
5c5fa260 33import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
b6eb4dce
VP
34
35/**
36 * A pcap-specific analysis that parse an entire trace to find all the streams.
37 *
38 * @author Vincent Perot
39 */
40public class StreamListAnalysis extends TmfAbstractAnalysisModule {
41
42 /**
43 * The Stream List analysis ID.
44 */
45 public static final String ID = "org.eclipse.linuxtools.tmf.pcap.core.analysis.stream"; //$NON-NLS-1$
46
47 private @Nullable ITmfEventRequest fRequest;
c88feda9 48 private final Map<TmfPcapProtocol, TmfPacketStreamBuilder> fBuilders;
b6eb4dce
VP
49
50 /**
51 * The default constructor. It initializes all variables.
52 */
53 public StreamListAnalysis() {
54 super();
55 fBuilders = new HashMap<>();
e20e3d49 56 for (TmfPcapProtocol protocol : TmfPcapProtocol.values()) {
b6eb4dce
VP
57 if (protocol.supportsStream()) {
58 fBuilders.put(protocol, new TmfPacketStreamBuilder(protocol));
59 }
60 }
61 }
62
63 @Override
64 public boolean canExecute(ITmfTrace trace) {
65
66 // Trace is Pcap
67 if (trace instanceof PcapTrace) {
68 return true;
69 }
70
71 // Trace is not a TmfExperiment
72 if (!(trace instanceof TmfExperiment)) {
73 return false;
74 }
75
76 // Trace is TmfExperiment. Check if it has a PcapTrace.
77 TmfExperiment experiment = (TmfExperiment) trace;
fa62dc1d
BH
78 List<ITmfTrace> traces = experiment.getTraces();
79 for (ITmfTrace expTrace : traces) {
80 if (expTrace instanceof PcapTrace) {
b6eb4dce
VP
81 return true;
82 }
83 }
84
85 // No Pcap :(
86 return false;
87 }
88
89 @Override
90 protected boolean executeAnalysis(@Nullable IProgressMonitor monitor) throws TmfAnalysisException {
91 IProgressMonitor mon = (monitor == null ? new NullProgressMonitor() : monitor);
e1c415b3
BH
92 ITmfTrace trace = getTrace();
93 if (trace == null) {
94 /* This analysis was cancelled in the meantime */
b6eb4dce
VP
95 return false;
96 }
97
98 ITmfEventRequest request = fRequest;
99 if ((request != null) && (!request.isCompleted())) {
100 request.cancel();
101 }
102
103 request = new TmfEventRequest(PcapEvent.class,
104 TmfTimeRange.ETERNITY, 0L, ITmfEventRequest.ALL_DATA,
105 ITmfEventRequest.ExecutionType.BACKGROUND) {
106
107 @Override
108 public void handleData(ITmfEvent data) {
109 // Called for each event
110 super.handleData(data);
111 if (!(data instanceof PcapEvent)) {
112 return;
113 }
114 PcapEvent event = (PcapEvent) data;
202956f1
AM
115 for (Map.Entry<TmfPcapProtocol, TmfPacketStreamBuilder> entry : fBuilders.entrySet()) {
116 entry.getValue().addEventToStream(event);
b6eb4dce
VP
117 }
118
119 }
120 };
e1c415b3 121 trace.sendRequest(request);
b6eb4dce
VP
122 fRequest = request;
123 try {
124 request.waitForCompletion();
125 } catch (InterruptedException e) {
126 // Request was canceled.
127 return false;
128 }
129
130 return !mon.isCanceled() && !request.isCancelled() && !request.isFailed();
131
132 }
133
134 @Override
135 protected void canceling() {
136 ITmfEventRequest req = fRequest;
137 if ((req != null) && (!req.isCompleted())) {
138 req.cancel();
139 }
140 }
141
142 /**
143 * Getter method that returns the packet builder associated to a particular
144 * protocol.
145 *
146 * @param protocol
147 * The specified protocol.
148 * @return The builder.
149 */
c88feda9 150 public @Nullable TmfPacketStreamBuilder getBuilder(TmfPcapProtocol protocol) {
b6eb4dce
VP
151 return fBuilders.get(protocol);
152 }
153
154 /**
155 * Method that indicates if the analysis is still running or has finished.
156 *
157 * @return Whether the analysis is finished or not.
158 */
159 public boolean isFinished() {
160 ITmfEventRequest req = fRequest;
161 if (req == null) {
162 return false;
163 }
164 return req.isCompleted();
165 }
166
167}
This page took 0.055806 seconds and 5 git commands to generate.