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 | ||
93d1d135 | 13 | package org.eclipse.linuxtools.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; | |
93d1d135 AM |
21 | import org.eclipse.linuxtools.internal.tmf.pcap.core.event.PcapEvent; |
22 | import org.eclipse.linuxtools.internal.tmf.pcap.core.event.TmfPacketStreamBuilder; | |
c88feda9 | 23 | import org.eclipse.linuxtools.internal.tmf.pcap.core.protocol.TmfPcapProtocol; |
93d1d135 | 24 | import org.eclipse.linuxtools.internal.tmf.pcap.core.trace.PcapTrace; |
b6eb4dce VP |
25 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule; |
26 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
27 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
28 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
29 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
30 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
31 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
32 | import org.eclipse.linuxtools.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); | |
91 | if (getTrace() == null) { | |
92 | return false; | |
93 | } | |
94 | ||
95 | ITmfEventRequest request = fRequest; | |
96 | if ((request != null) && (!request.isCompleted())) { | |
97 | request.cancel(); | |
98 | } | |
99 | ||
100 | request = new TmfEventRequest(PcapEvent.class, | |
101 | TmfTimeRange.ETERNITY, 0L, ITmfEventRequest.ALL_DATA, | |
102 | ITmfEventRequest.ExecutionType.BACKGROUND) { | |
103 | ||
104 | @Override | |
105 | public void handleData(ITmfEvent data) { | |
106 | // Called for each event | |
107 | super.handleData(data); | |
108 | if (!(data instanceof PcapEvent)) { | |
109 | return; | |
110 | } | |
111 | PcapEvent event = (PcapEvent) data; | |
c88feda9 | 112 | for (TmfPcapProtocol protocol : fBuilders.keySet()) { |
b6eb4dce VP |
113 | fBuilders.get(protocol).addEventToStream(event); |
114 | } | |
115 | ||
116 | } | |
117 | }; | |
118 | getTrace().sendRequest(request); | |
119 | fRequest = request; | |
120 | try { | |
121 | request.waitForCompletion(); | |
122 | } catch (InterruptedException e) { | |
123 | // Request was canceled. | |
124 | return false; | |
125 | } | |
126 | ||
127 | return !mon.isCanceled() && !request.isCancelled() && !request.isFailed(); | |
128 | ||
129 | } | |
130 | ||
131 | @Override | |
132 | protected void canceling() { | |
133 | ITmfEventRequest req = fRequest; | |
134 | if ((req != null) && (!req.isCompleted())) { | |
135 | req.cancel(); | |
136 | } | |
137 | } | |
138 | ||
139 | /** | |
140 | * Getter method that returns the packet builder associated to a particular | |
141 | * protocol. | |
142 | * | |
143 | * @param protocol | |
144 | * The specified protocol. | |
145 | * @return The builder. | |
146 | */ | |
c88feda9 | 147 | public @Nullable TmfPacketStreamBuilder getBuilder(TmfPcapProtocol protocol) { |
b6eb4dce VP |
148 | return fBuilders.get(protocol); |
149 | } | |
150 | ||
151 | /** | |
152 | * Method that indicates if the analysis is still running or has finished. | |
153 | * | |
154 | * @return Whether the analysis is finished or not. | |
155 | */ | |
156 | public boolean isFinished() { | |
157 | ITmfEventRequest req = fRequest; | |
158 | if (req == null) { | |
159 | return false; | |
160 | } | |
161 | return req.isCompleted(); | |
162 | } | |
163 | ||
164 | } |