| 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 | |
| 13 | package org.eclipse.tracecompass.internal.tmf.pcap.core.analysis; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.List; |
| 17 | import java.util.Map; |
| 18 | |
| 19 | import org.eclipse.core.runtime.IProgressMonitor; |
| 20 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 21 | import org.eclipse.jdt.annotation.Nullable; |
| 22 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent; |
| 23 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.TmfPacketStreamBuilder; |
| 24 | import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol; |
| 25 | import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace; |
| 26 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
| 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 28 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; |
| 29 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; |
| 30 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; |
| 31 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
| 32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
| 34 | |
| 35 | /** |
| 36 | * A pcap-specific analysis that parse an entire trace to find all the streams. |
| 37 | * |
| 38 | * @author Vincent Perot |
| 39 | */ |
| 40 | public 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; |
| 48 | private final Map<TmfPcapProtocol, TmfPacketStreamBuilder> fBuilders; |
| 49 | |
| 50 | /** |
| 51 | * The default constructor. It initializes all variables. |
| 52 | */ |
| 53 | public StreamListAnalysis() { |
| 54 | super(); |
| 55 | fBuilders = new HashMap<>(); |
| 56 | for (TmfPcapProtocol protocol : TmfPcapProtocol.values()) { |
| 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; |
| 78 | List<ITmfTrace> traces = experiment.getTraces(); |
| 79 | for (ITmfTrace expTrace : traces) { |
| 80 | if (expTrace instanceof PcapTrace) { |
| 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); |
| 92 | ITmfTrace trace = getTrace(); |
| 93 | if (trace == null) { |
| 94 | /* This analysis was cancelled in the meantime */ |
| 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; |
| 115 | for (Map.Entry<TmfPcapProtocol, TmfPacketStreamBuilder> entry : fBuilders.entrySet()) { |
| 116 | entry.getValue().addEventToStream(event); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | }; |
| 121 | trace.sendRequest(request); |
| 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 | */ |
| 150 | public @Nullable TmfPacketStreamBuilder getBuilder(TmfPcapProtocol protocol) { |
| 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 | } |