pcap: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.pcap.core / src / org / eclipse / tracecompass / internal / pcap / core / stream / PacketStream.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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpointPair;
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
22 import com.google.common.math.DoubleMath;
23
24 // TODO decide if default modifier a good idea. This allows only the
25 // stream builder to call that method (and any class that is added to this
26 // package). This effectively makes the stream read-only.
27
28 /**
29 * Class that represents a packet stream, which is a collection of packets that
30 * share the same endpoints. The endpoints of a packet are protocol-dependent.
31 * For example, a TCP stream is a collection of packets that share the same MAC
32 * address, IP address, and Port couple.
33 *
34 * @author Vincent Perot
35 */
36 public class PacketStream {
37
38 private static final double SECOND_TO_NANOSECOND = 1000000000.0;
39 private static final double DELTA = 0.000000001;
40 private final PcapProtocol fProtocol;
41 private final int fId;
42 private final ProtocolEndpointPair fEndpointPair;
43
44 private long fNbPacketsAtoB;
45 private long fNbPacketsBtoA;
46 private long fNbBytesAtoB;
47 private long fNbBytesBtoA;
48 private long fStartTime;
49 private long fEndTime;
50
51 /**
52 * Constructor of a packet stream.
53 *
54 * @param protocol
55 * The protocol of the packets of the stream. This is needed
56 * because the definition of a stream is protocol-dependent.
57 * @param id
58 * The id of this stream.
59 * @param endpointPair
60 * The common endpoints of the packets in this stream.
61 */
62 PacketStream(PcapProtocol protocol, int id, ProtocolEndpointPair endpointPair) {
63 fProtocol = protocol;
64 fId = id;
65 fEndpointPair = endpointPair;
66 fNbPacketsAtoB = 0;
67 fNbPacketsBtoA = 0;
68 fNbBytesAtoB = 0;
69 fNbBytesBtoA = 0;
70 fStartTime = Long.MAX_VALUE;
71 fEndTime = Long.MIN_VALUE;
72 }
73
74 /**
75 * Add a packet to the stream.
76 *
77 * @param packet
78 * The packet that must be added.
79 */
80 synchronized void add(PcapPacket packet) {
81
82 Packet newPacket = packet.getPacket(fProtocol);
83 if (newPacket == null) {
84 return;
85 }
86
87 // Update packet and byte number
88 if (fEndpointPair.getFirstEndpoint().equals(newPacket.getSourceEndpoint()) &&
89 fEndpointPair.getSecondEndpoint().equals(newPacket.getDestinationEndpoint())) {
90 fNbPacketsAtoB++;
91 fNbBytesAtoB += packet.getOriginalLength();
92 } else if (fEndpointPair.getFirstEndpoint().equals(newPacket.getDestinationEndpoint()) &&
93 fEndpointPair.getSecondEndpoint().equals(newPacket.getSourceEndpoint())) {
94 fNbPacketsBtoA++;
95 fNbBytesBtoA += packet.getOriginalLength();
96 } else {
97 throw new IllegalStateException();
98 }
99
100 // Update start and stop time
101 // Stream timestamp is ALWAYS in nanoseconds.
102 long timestamp;
103 switch (packet.getTimestampScale()) {
104 case MICROSECOND:
105 timestamp = packet.getTimestamp() * 1000;
106 break;
107 case NANOSECOND:
108 timestamp = packet.getTimestamp();
109 break;
110 default:
111 throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$
112 }
113 fStartTime = Math.min(fStartTime, timestamp);
114 fEndTime = Math.max(fEndTime, timestamp);
115 }
116
117 /**
118 * Get the Protocol of this stream.
119 *
120 * @return The protocol of this stream.
121 */
122 public PcapProtocol getProtocol() {
123 return fProtocol;
124 }
125
126 /**
127 * Method that returns the non-unique ID of this stream.
128 *
129 * @return the non-unique ID of this stream.
130 */
131 public int getID() {
132 return fId;
133 }
134
135 /**
136 * Method that returns the unique ID of this stream.
137 *
138 * @return the unique ID of this stream.
139 */
140 public String getUniqueID() {
141 return fProtocol.getShortName() + '.' + fId;
142 }
143
144 /**
145 * Method that returns the endpoint pair of the stream.
146 *
147 * @return The endpoint pair of the stream.
148 */
149 public ProtocolEndpointPair getEndpointPair() {
150 return fEndpointPair;
151 }
152
153 // TODO return also the endpoint set.
154 @Override
155 public synchronized String toString() {
156 StringBuilder sb = new StringBuilder();
157 sb.append("Stream " + getUniqueID() + ", Number of Packets: " + getNbPackets() + "\n"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
158
159 return checkNotNull(sb.toString());
160
161 }
162
163 /**
164 * Get the number of packets going from the first endpoint to the second.
165 *
166 * @return The number of packets from A to B.
167 */
168 public synchronized long getNbPacketsAtoB() {
169 return fNbPacketsAtoB;
170 }
171
172 /**
173 * Get the number of packets going from the second endpoint to the first.
174 *
175 * @return The number of packets from B to A.
176 */
177 public synchronized long getNbPacketsBtoA() {
178 return fNbPacketsBtoA;
179 }
180
181 /**
182 * Get the total number of packets in this stream.
183 *
184 * @return The total number of packets.
185 */
186 public synchronized long getNbPackets() {
187 return fNbPacketsAtoB + fNbPacketsBtoA;
188 }
189
190 /**
191 * Get the number of bytes going from the first endpoint to the second.
192 *
193 * @return The number of bytes from A to B.
194 */
195 public synchronized long getNbBytesAtoB() {
196 return fNbBytesAtoB;
197 }
198
199 /**
200 * Get the number of bytes going from the second endpoint to the first.
201 *
202 * @return The number of bytes from B to A.
203 */
204 public synchronized long getNbBytesBtoA() {
205 return fNbBytesBtoA;
206 }
207
208 /**
209 * Get the total number of bytes in this stream.
210 *
211 * @return The total number of bytes.
212 */
213 public synchronized long getNbBytes() {
214 return fNbBytesAtoB + fNbBytesBtoA;
215 }
216
217 /**
218 * Get the start time of this stream, in nanoseconds relative to epoch.
219 *
220 * @return The start time.
221 */
222 public synchronized long getStartTime() {
223 return fStartTime;
224 }
225
226 /**
227 * Get the stop time of this stream, in nanoseconds relative to epoch.
228 *
229 * @return The stop time.
230 */
231 public synchronized long getStopTime() {
232 return fEndTime;
233 }
234
235 /**
236 * Get the duration of this stream, in seconds
237 *
238 * @return The duration of this stream.
239 */
240 public synchronized double getDuration() {
241 return (fEndTime - fStartTime) / SECOND_TO_NANOSECOND;
242 }
243
244 /**
245 * Get the the average byte per second from A to B.
246 *
247 * @return the average byte per second from A to B.
248 */
249 public synchronized double getBPSAtoB() {
250 if (DoubleMath.fuzzyEquals(getDuration(), 0, DELTA)) {
251 return 0;
252 }
253 return fNbBytesAtoB / getDuration();
254 }
255
256 /**
257 * Get the the average byte per second from B to A.
258 *
259 * @return the average byte per second from B to A.
260 */
261 public synchronized double getBPSBtoA() {
262 if (DoubleMath.fuzzyEquals(getDuration(), 0, DELTA)) {
263 return 0;
264 }
265 return fNbBytesBtoA / getDuration();
266 }
267
268 }
This page took 0.053668 seconds and 5 git commands to generate.