1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.stream
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.linuxtools
.pcap
.core
.endpoint
.ProtocolEndpointPair
;
19 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.PacketUniqueID
;
20 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
21 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.pcap
.PcapPacket
;
23 // TODO decide if default modifier a good idea. This allows only the
24 // stream builder to call that method (and any class that is added to this
25 // package). This effectively makes the stream read-only.
28 * Class that represents a packet stream, which is a collection of packets that
29 * share the same endpoints. The endpoints of a packet are protocol-dependent.
30 * For example, a TCP stream is a collection of packets that share the same MAC
31 * address, IP address, and Port couple.
33 * @author Vincent Perot
35 public class PacketStream
{
37 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
38 private final List
<PacketUniqueID
> fListIndex
;
39 private final Protocol fProtocol
;
40 private final int fId
;
41 private final ProtocolEndpointPair fEndpointPair
;
44 * Constructor of a packet stream.
47 * The protocol of the packets of the stream. This is needed
48 * because the definition of a stream is protocol-dependent.
50 * The id of this stream.
52 * The common endpoints of the packets in this stream.
54 PacketStream(Protocol protocol
, int id
, ProtocolEndpointPair endpointPair
) {
56 fListIndex
= new ArrayList
<>();
58 fEndpointPair
= endpointPair
;
62 * Add a packet unique ID to the stream.
65 * The packet unique ID that must be added.
67 synchronized void add(PcapPacket packet
) {
68 fListIndex
.add(new PacketUniqueID(packet
));
72 * Get a packet unique ID in file from the stream.
75 * The index in the stream of the packet to be retrieved.
76 * @return The retrieved packet unique ID.
78 public synchronized PacketUniqueID
get(int index
) {
79 PacketUniqueID id
= fListIndex
.get(index
);
81 throw new IllegalStateException("PacketUniqueID is null!"); //$NON-NLS-1$
87 * Get the Protocol of this stream.
89 * @return The protocol of this stream.
91 public Protocol
getProtocol() {
96 * Method that returns the non-unique ID of this stream.
98 * @return the non-unique ID of this stream.
105 * Method that returns the unique ID of this stream.
107 * @return the unique ID of this stream.
109 public String
getUniqueID() {
110 return fProtocol
.getShortName() + '.' + fId
;
114 * Method that returns the endpoint pair of the stream.
116 * @return The endpoint pair of the stream.
118 public ProtocolEndpointPair
getEndpointPair() {
119 return fEndpointPair
;
122 // TODO return also the endpoint set.
124 public synchronized String
toString() {
125 StringBuilder sb
= new StringBuilder();
126 sb
.append("Stream " + getUniqueID() + ", Number of Packets: " + fListIndex
.size() + "\n"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
128 for (int i
= 0; i
< fListIndex
.size(); i
++) {
129 sb
.append(fListIndex
.get(i
) + ", "); //$NON-NLS-1$
132 String string
= sb
.toString();
133 if (string
== null) {
141 * Method that returns the number of packets in the stream.
143 * @return The number of packets in the stream.
145 public synchronized int size() {
146 return fListIndex
.size();