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
.tracecompass
.internal
.pcap
.core
.stream
;
15 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.endpoint
.ProtocolEndpointPair
;
16 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
17 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
18 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapPacket
;
20 import com
.google
.common
.math
.DoubleMath
;
22 // TODO decide if default modifier a good idea. This allows only the
23 // stream builder to call that method (and any class that is added to this
24 // package). This effectively makes the stream read-only.
27 * Class that represents a packet stream, which is a collection of packets that
28 * share the same endpoints. The endpoints of a packet are protocol-dependent.
29 * For example, a TCP stream is a collection of packets that share the same MAC
30 * address, IP address, and Port couple.
32 * @author Vincent Perot
34 public class PacketStream
{
36 private static final double SECOND_TO_NANOSECOND
= 1000000000.0;
37 private static final double DELTA
= 0.000000001;
38 private final PcapProtocol fProtocol
;
39 private final int fId
;
40 private final ProtocolEndpointPair fEndpointPair
;
42 private long fNbPacketsAtoB
;
43 private long fNbPacketsBtoA
;
44 private long fNbBytesAtoB
;
45 private long fNbBytesBtoA
;
46 private long fStartTime
;
47 private long fEndTime
;
50 * Constructor of a packet stream.
53 * The protocol of the packets of the stream. This is needed
54 * because the definition of a stream is protocol-dependent.
56 * The id of this stream.
58 * The common endpoints of the packets in this stream.
60 PacketStream(PcapProtocol protocol
, int id
, ProtocolEndpointPair endpointPair
) {
63 fEndpointPair
= endpointPair
;
68 fStartTime
= Long
.MAX_VALUE
;
69 fEndTime
= Long
.MIN_VALUE
;
73 * Add a packet to the stream.
76 * The packet that must be added.
78 synchronized void add(PcapPacket packet
) {
80 Packet newPacket
= packet
.getPacket(fProtocol
);
81 if (newPacket
== null) {
85 // Update packet and byte number
86 if (fEndpointPair
.getFirstEndpoint().equals(newPacket
.getSourceEndpoint()) &&
87 fEndpointPair
.getSecondEndpoint().equals(newPacket
.getDestinationEndpoint())) {
89 fNbBytesAtoB
+= packet
.getOriginalLength();
90 } else if (fEndpointPair
.getFirstEndpoint().equals(newPacket
.getDestinationEndpoint()) &&
91 fEndpointPair
.getSecondEndpoint().equals(newPacket
.getSourceEndpoint())) {
93 fNbBytesBtoA
+= packet
.getOriginalLength();
95 throw new IllegalStateException();
98 // Update start and stop time
99 // Stream timestamp is ALWAYS in nanoseconds.
101 switch (packet
.getTimestampScale()) {
103 timestamp
= packet
.getTimestamp() * 1000;
106 timestamp
= packet
.getTimestamp();
109 throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$
111 fStartTime
= Math
.min(fStartTime
, timestamp
);
112 fEndTime
= Math
.max(fEndTime
, timestamp
);
116 * Get the Protocol of this stream.
118 * @return The protocol of this stream.
120 public PcapProtocol
getProtocol() {
125 * Method that returns the non-unique ID of this stream.
127 * @return the non-unique ID of this stream.
134 * Method that returns the unique ID of this stream.
136 * @return the unique ID of this stream.
138 public String
getUniqueID() {
139 return fProtocol
.getShortName() + '.' + fId
;
143 * Method that returns the endpoint pair of the stream.
145 * @return The endpoint pair of the stream.
147 public ProtocolEndpointPair
getEndpointPair() {
148 return fEndpointPair
;
151 // TODO return also the endpoint set.
153 public synchronized String
toString() {
154 StringBuilder sb
= new StringBuilder();
155 sb
.append("Stream " + getUniqueID() + ", Number of Packets: " + getNbPackets() + "\n"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
157 return sb
.toString();
162 * Get the number of packets going from the first endpoint to the second.
164 * @return The number of packets from A to B.
166 public synchronized long getNbPacketsAtoB() {
167 return fNbPacketsAtoB
;
171 * Get the number of packets going from the second endpoint to the first.
173 * @return The number of packets from B to A.
175 public synchronized long getNbPacketsBtoA() {
176 return fNbPacketsBtoA
;
180 * Get the total number of packets in this stream.
182 * @return The total number of packets.
184 public synchronized long getNbPackets() {
185 return fNbPacketsAtoB
+ fNbPacketsBtoA
;
189 * Get the number of bytes going from the first endpoint to the second.
191 * @return The number of bytes from A to B.
193 public synchronized long getNbBytesAtoB() {
198 * Get the number of bytes going from the second endpoint to the first.
200 * @return The number of bytes from B to A.
202 public synchronized long getNbBytesBtoA() {
207 * Get the total number of bytes in this stream.
209 * @return The total number of bytes.
211 public synchronized long getNbBytes() {
212 return fNbBytesAtoB
+ fNbBytesBtoA
;
216 * Get the start time of this stream, in nanoseconds relative to epoch.
218 * @return The start time.
220 public synchronized long getStartTime() {
225 * Get the stop time of this stream, in nanoseconds relative to epoch.
227 * @return The stop time.
229 public synchronized long getStopTime() {
234 * Get the duration of this stream, in seconds
236 * @return The duration of this stream.
238 public synchronized double getDuration() {
239 return (fEndTime
- fStartTime
) / SECOND_TO_NANOSECOND
;
243 * Get the the average byte per second from A to B.
245 * @return the average byte per second from A to B.
247 public synchronized double getBPSAtoB() {
248 if (DoubleMath
.fuzzyEquals(getDuration(), 0, DELTA
)) {
251 return fNbBytesAtoB
/ getDuration();
255 * Get the the average byte per second from B to A.
257 * @return the average byte per second from B to A.
259 public synchronized double getBPSBtoA() {
260 if (DoubleMath
.fuzzyEquals(getDuration(), 0, DELTA
)) {
263 return fNbBytesBtoA
/ getDuration();