| 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.event; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.tracecompass.internal.pcap.core.packet.Packet; |
| 19 | import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol; |
| 20 | import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket; |
| 21 | import org.eclipse.tracecompass.internal.pcap.core.stream.PacketStream; |
| 22 | import org.eclipse.tracecompass.internal.pcap.core.stream.PacketStreamBuilder; |
| 23 | import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol; |
| 24 | import org.eclipse.tracecompass.internal.tmf.pcap.core.util.ProtocolConversion; |
| 25 | |
| 26 | /** |
| 27 | * Class that wraps a PacketStreamBuilder. |
| 28 | * |
| 29 | * @author Vincent Perot |
| 30 | */ |
| 31 | public class TmfPacketStreamBuilder { |
| 32 | |
| 33 | private final PacketStreamBuilder fBuilder; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | * |
| 38 | * @param protocol |
| 39 | * The protocol of the streams to build. |
| 40 | */ |
| 41 | public TmfPacketStreamBuilder(TmfPcapProtocol protocol) { |
| 42 | fBuilder = new PacketStreamBuilder(ProtocolConversion.unwrap(protocol)); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Method that adds an event to this builder. |
| 47 | * |
| 48 | * @param event |
| 49 | * The event to add. |
| 50 | */ |
| 51 | public synchronized void addEventToStream(PcapEvent event) { |
| 52 | Packet packet = event.getPacket().getPacket(PcapProtocol.PCAP); |
| 53 | if (packet == null || !(packet instanceof PcapPacket)) { |
| 54 | return; |
| 55 | } |
| 56 | PcapPacket pcapPacket = (PcapPacket) packet; |
| 57 | fBuilder.addPacketToStream(pcapPacket); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Method that returns the number of streams built. |
| 62 | * |
| 63 | * @return The number of streams built. |
| 64 | */ |
| 65 | public synchronized int getNbStreams() { |
| 66 | return fBuilder.getNbStreams(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Method that returns an iterable on the streams built so far. |
| 71 | * |
| 72 | * @return An iterable on the streams. |
| 73 | */ |
| 74 | public synchronized Iterable<TmfPacketStream> getStreams() { |
| 75 | // We can't store in immutable list since the stream number/content can |
| 76 | // change dynamically. |
| 77 | List<TmfPacketStream> list = new ArrayList<>(); |
| 78 | for (PacketStream stream : fBuilder.getStreams()) { |
| 79 | if (stream != null) { |
| 80 | list.add(new TmfPacketStream(stream)); |
| 81 | } |
| 82 | } |
| 83 | return list; |
| 84 | } |
| 85 | |
| 86 | } |