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