btf: Move the plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.pcap.core / src / org / eclipse / tracecompass / internal / pcap / core / stream / PacketStreamBuilder.java
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.pcap.core.stream;
14
15 import java.io.IOException;
16 import java.nio.file.Path;
17 import java.util.HashMap;
18 import java.util.LinkedList;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpoint;
23 import org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpointPair;
24 import org.eclipse.tracecompass.internal.pcap.core.filter.IPacketFilter;
25 import org.eclipse.tracecompass.internal.pcap.core.filter.PacketFilterByProtocol;
26 import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException;
27 import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
28 import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
29 import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket;
30 import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
31 import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
32
33 /**
34 * Class that parse an entire pcap file to build the different streams.
35 *
36 * @author Vincent Perot
37 */
38 public class PacketStreamBuilder {
39
40 private final IPacketFilter fPacketFilter;
41 private final PcapProtocol fProtocol;
42
43 private final Map<Integer, PacketStream> fStreams;
44 private final Map<ProtocolEndpointPair, Integer> fIDs;
45 private int fCurrentId;
46
47 /**
48 * Main constructor.
49 *
50 * @param protocol
51 * The protocol of the builder.
52 */
53 public PacketStreamBuilder(PcapProtocol protocol) {
54 fCurrentId = 0;
55 fProtocol = protocol;
56 fPacketFilter = new PacketFilterByProtocol(protocol);
57 fStreams = new HashMap<>();
58 fIDs = new HashMap<>();
59 }
60
61 /**
62 * Method that returns a particular stream based on its ID.
63 *
64 * @param id
65 * The ID of the stream.
66 * @return The stream that has the specified ID.
67 */
68 public synchronized @Nullable PacketStream getStream(int id) {
69 return fStreams.get(id);
70 }
71
72 /**
73 * Method that returns a particular stream based on its endpoints. It
74 * returns null if no corresponding stream is found.
75 *
76 * @param endpointA
77 * The first endpoint of the stream.
78 * @param endpointB
79 * The second endpoint of the stream.
80 *
81 * @return The stream that has the specified endpoints. Return Null if no
82 * stream is found between the two endpoints.
83 */
84 public synchronized @Nullable PacketStream getStream(ProtocolEndpoint endpointA, ProtocolEndpoint endpointB) {
85 ProtocolEndpointPair set = new ProtocolEndpointPair(endpointA, endpointB);
86 int id = fIDs.get(set);
87 return fStreams.get(id);
88 }
89
90 /**
91 * Method that returns all the streams at the specified protocol level.
92 *
93 * @return The streams as a list.
94 */
95 public synchronized Iterable<PacketStream> getStreams() {
96 Iterable<PacketStream> iterable = new LinkedList<>(fStreams.values());
97 return iterable;
98 }
99
100 /**
101 * Method that is called when the filter accepts a packet. This methods add
102 * the packet to a stream based on its characteristics.
103 *
104 * @param packet
105 * The packet to be added.
106 */
107 public synchronized void addPacketToStream(PcapPacket packet) {
108 if (fPacketFilter.accepts(packet)) {
109 @Nullable Packet newPacket = packet.getPacket(fProtocol);
110 if (newPacket == null) {
111 return;
112 }
113 ProtocolEndpointPair endpointSet = new ProtocolEndpointPair(newPacket);
114 if (!fIDs.containsKey(endpointSet)) {
115 fIDs.put(endpointSet, fCurrentId);
116 fStreams.put(fCurrentId, new PacketStream(fProtocol, fCurrentId, endpointSet));
117 fStreams.get(fCurrentId).add(packet);
118 fCurrentId++;
119 } else {
120 Integer id = fIDs.get(endpointSet);
121 fStreams.get(id).add(packet);
122 }
123 }
124 return;
125 }
126
127 /**
128 * Getter method for the protocol of the stream builder.
129 *
130 * @return The protocol.
131 */
132 public PcapProtocol getProtocol() {
133 return fProtocol;
134 }
135
136 /**
137 * Method that clears the builder.
138 */
139 public void clear() {
140 fStreams.clear();
141 fIDs.clear();
142 fCurrentId = 0;
143 }
144
145 /**
146 * Method that returns the number of streams built.
147 *
148 * @return The number of streams built.
149 */
150 public synchronized int getNbStreams() {
151 return fStreams.size();
152 }
153
154 /**
155 * Method that parse an entire file and build the streams contained in the
156 * file.
157 *
158 * @param filePath
159 * The file path.
160 * @throws IOException
161 * When an IO error occurs.
162 * @throws BadPcapFileException
163 * When the PcapFile is not valid.
164 */
165 public synchronized void parsePcapFile(Path filePath) throws IOException, BadPcapFileException {
166 try (PcapFile pcapFile = new PcapFile(filePath);) {
167 while (pcapFile.hasNextPacket()) { // not eof
168 PcapPacket packet;
169 try {
170 packet = pcapFile.parseNextPacket();
171 if (packet == null) {
172 return;
173 }
174 addPacketToStream(packet);
175 } catch (BadPacketException e) {
176 // Ignore packet. Do nothing.
177 }
178 }
179 }
180
181 }
182 }
This page took 0.034857 seconds and 5 git commands to generate.