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.event; |
b6eb4dce VP |
14 | |
15 | import java.util.ArrayList; | |
4c4e2816 AM |
16 | import java.util.stream.Collectors; |
17 | import java.util.stream.StreamSupport; | |
b6eb4dce | 18 | |
71f2817f AM |
19 | import org.eclipse.tracecompass.internal.pcap.core.packet.Packet; |
20 | import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol; | |
21 | import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket; | |
71f2817f | 22 | import org.eclipse.tracecompass.internal.pcap.core.stream.PacketStreamBuilder; |
2bdf0193 AM |
23 | import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol; |
24 | import org.eclipse.tracecompass.internal.tmf.pcap.core.util.ProtocolConversion; | |
b6eb4dce VP |
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 | */ | |
c88feda9 | 41 | public TmfPacketStreamBuilder(TmfPcapProtocol protocol) { |
b6eb4dce VP |
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) { | |
c88feda9 | 52 | Packet packet = event.getPacket().getPacket(PcapProtocol.PCAP); |
bba6b570 | 53 | if (!(packet instanceof PcapPacket)) { |
b6eb4dce VP |
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. | |
4c4e2816 AM |
77 | return StreamSupport.stream(fBuilder.getStreams().spliterator(), false) |
78 | .map(e -> new TmfPacketStream(e)) | |
79 | .collect(Collectors.toCollection(ArrayList::new)); | |
b6eb4dce | 80 | } |
b6eb4dce | 81 | } |