tmf: Initial commit of Pcap Parser
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core / src / org / eclipse / linuxtools / 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.linuxtools.pcap.core.stream;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
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;
22
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.
26
27 /**
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.
32 *
33 * @author Vincent Perot
34 */
35 public class PacketStream {
36
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;
42
43 /**
44 * Constructor of a packet stream.
45 *
46 * @param protocol
47 * The protocol of the packets of the stream. This is needed
48 * because the definition of a stream is protocol-dependent.
49 * @param id
50 * The id of this stream.
51 * @param endpointPair
52 * The common endpoints of the packets in this stream.
53 */
54 PacketStream(Protocol protocol, int id, ProtocolEndpointPair endpointPair) {
55 fProtocol = protocol;
56 fListIndex = new ArrayList<>();
57 fId = id;
58 fEndpointPair = endpointPair;
59 }
60
61 /**
62 * Add a packet unique ID to the stream.
63 *
64 * @param packet
65 * The packet unique ID that must be added.
66 */
67 synchronized void add(PcapPacket packet) {
68 fListIndex.add(new PacketUniqueID(packet));
69 }
70
71 /**
72 * Get a packet unique ID in file from the stream.
73 *
74 * @param index
75 * The index in the stream of the packet to be retrieved.
76 * @return The retrieved packet unique ID.
77 */
78 public synchronized PacketUniqueID get(int index) {
79 PacketUniqueID id = fListIndex.get(index);
80 if (id == null) {
81 throw new IllegalStateException("PacketUniqueID is null!"); //$NON-NLS-1$
82 }
83 return id;
84 }
85
86 /**
87 * Get the Protocol of this stream.
88 *
89 * @return The protocol of this stream.
90 */
91 public Protocol getProtocol() {
92 return fProtocol;
93 }
94
95 /**
96 * Method that returns the non-unique ID of this stream.
97 *
98 * @return the non-unique ID of this stream.
99 */
100 public int getID() {
101 return fId;
102 }
103
104 /**
105 * Method that returns the unique ID of this stream.
106 *
107 * @return the unique ID of this stream.
108 */
109 public String getUniqueID() {
110 return fProtocol.getShortName() + '.' + fId;
111 }
112
113 /**
114 * Method that returns the endpoint pair of the stream.
115 *
116 * @return The endpoint pair of the stream.
117 */
118 public ProtocolEndpointPair getEndpointPair() {
119 return fEndpointPair;
120 }
121
122 // TODO return also the endpoint set.
123 @Override
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$
127
128 for (int i = 0; i < fListIndex.size(); i++) {
129 sb.append(fListIndex.get(i) + ", "); //$NON-NLS-1$
130 }
131
132 String string = sb.toString();
133 if (string == null) {
134 return EMPTY_STRING;
135 }
136 return string;
137
138 }
139
140 /**
141 * Method that returns the number of packets in the stream.
142 *
143 * @return The number of packets in the stream.
144 */
145 public synchronized int size() {
146 return fListIndex.size();
147 }
148
149 }
This page took 0.034531 seconds and 5 git commands to generate.