pcap: Make all the packages internal
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.pcap.core / src / org / eclipse / linuxtools / internal / tmf / pcap / core / event / PcapEvent.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.internal.tmf.pcap.core.event;
14
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.linuxtools.internal.pcap.core.packet.Packet;
24 import org.eclipse.linuxtools.internal.pcap.core.protocol.Protocol;
25 import org.eclipse.linuxtools.internal.tmf.pcap.core.protocol.TmfProtocol;
26 import org.eclipse.linuxtools.internal.tmf.pcap.core.util.ProtocolConversion;
27 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
28 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
29 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
30 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32
33 import com.google.common.collect.ImmutableList;
34
35 /**
36 * Class that extends TmfEvent to allow TMF to use the packets from the parser.
37 * It is a simple TmfEvent that wraps a Packet.
38 *
39 * @author Vincent Perot
40 */
41 public class PcapEvent extends TmfEvent {
42
43 /** Packet Source Field ID */
44 public static final String EVENT_FIELD_PACKET_SOURCE = ":packetsource:"; //$NON-NLS-1$
45 /** Packet Destination Field ID */
46 public static final String EVENT_FIELD_PACKET_DESTINATION = ":packetdestination:"; //$NON-NLS-1$
47 /** Packet Protocol Field ID */
48 public static final String EVENT_FIELD_PACKET_PROTOCOL = ":protocol:"; //$NON-NLS-1$
49
50 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
51
52 private final Packet fPacket;
53 private @Nullable List<TmfProtocol> fList;
54
55 /**
56 * Full constructor.
57 *
58 * @param trace
59 * the parent trace
60 * @param rank
61 * the event rank (in the trace)
62 * @param timestamp
63 * the event timestamp
64 * @param source
65 * the event source
66 * @param type
67 * the event type
68 * @param content
69 * the event content (payload)
70 * @param reference
71 * the event reference
72 * @param packet
73 * The packet contained in this event
74 */
75 public PcapEvent(ITmfTrace trace,
76 long rank,
77 ITmfTimestamp timestamp,
78 String source,
79 TmfEventType type,
80 ITmfEventField content,
81 String reference,
82 Packet packet) {
83
84 super(trace, rank, timestamp, source, type, content, reference);
85 fPacket = packet;
86 }
87
88 /**
89 * Method that returns an immutable map containing all the fields of a
90 * packet at a certain protocol. For instance, to get the Source IP Address,
91 * use:
92 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
93 * It returns null if the protocol is inexistent in the PcapEvent.
94 *
95 * @param protocol
96 * The specified protocol
97 * @return A map containing the fields.
98 */
99 public @Nullable Map<String, String> getFields(TmfProtocol protocol) {
100 Protocol p = ProtocolConversion.unwrap(protocol);
101 Packet packet = fPacket.getPacket(p);
102 if (packet == null) {
103 return null;
104 }
105 return packet.getFields();
106 }
107
108 /**
109 * Method that returns the payload at a certain protocol level. It returns
110 * null if the protocol is inexistent in the PcapEvent.
111 *
112 * @param protocol
113 * The specified protocol
114 * @return The payload as a ByteBuffer.
115 */
116 public @Nullable ByteBuffer getPayload(TmfProtocol protocol) {
117 Protocol p = ProtocolConversion.unwrap(protocol);
118 Packet packet = fPacket.getPacket(p);
119 if (packet == null) {
120 return null;
121 }
122 return packet.getPayload();
123 }
124
125 /**
126 * Method that returns the source endpoint at a certain protocol level. It
127 * returns null if the protocol is inexistent in the PcapEvent.
128 *
129 * @param protocol
130 * The specified protocol
131 * @return The source endpoint.
132 */
133 public @Nullable String getSourceEndpoint(TmfProtocol protocol) {
134 Protocol p = ProtocolConversion.unwrap(protocol);
135 Packet packet = fPacket.getPacket(p);
136 if (packet == null) {
137 return null;
138 }
139 return packet.getSourceEndpoint().toString();
140 }
141
142 /**
143 * Method that returns the destination endpoint at a certain protocol level.
144 * It returns null if the protocol is inexistent in the PcapEvent.
145 *
146 * @param protocol
147 * The specified protocol
148 * @return The destination endpoint.
149 */
150 public @Nullable String getDestinationEndpoint(TmfProtocol protocol) {
151 Protocol p = ProtocolConversion.unwrap(protocol);
152 Packet packet = fPacket.getPacket(p);
153 if (packet == null) {
154 return null;
155 }
156 return packet.getDestinationEndpoint().toString();
157 }
158
159 /**
160 * Method that returns the most encapsulated protocol in this PcapEvent. If
161 * it is an unknown protocol, it returns the last known protocol.
162 *
163 * @return The most encapsulated TmfProtocol.
164 */
165 public TmfProtocol getMostEncapsulatedProtocol() {
166 return ProtocolConversion.wrap(fPacket.getMostEcapsulatedPacket().getProtocol());
167 }
168
169 /**
170 * Method that returns a list of all the protocols in this PcapEvent.
171 *
172 * @return A list containing all the TmfProtocol.
173 */
174 public List<TmfProtocol> getProtocols() {
175 if (fList != null) {
176 return fList;
177 }
178 List<TmfProtocol> list = new ArrayList<>();
179 Packet packet = fPacket;
180
181 // Go to start.
182 while (packet != null && packet.getParentPacket() != null) {
183 packet = packet.getParentPacket();
184 }
185
186 if (packet == null) {
187 @SuppressWarnings("null")
188 @NonNull List<TmfProtocol> emptyList = Collections.EMPTY_LIST;
189 fList = emptyList;
190 return fList;
191 }
192 // Go through all the packets and add them to list.
193 list.add(ProtocolConversion.wrap(packet.getProtocol()));
194 while (packet != null && packet.getChildPacket() != null) {
195 packet = packet.getChildPacket();
196 if (packet != null) {
197 list.add(ProtocolConversion.wrap(packet.getProtocol()));
198 }
199 }
200
201 @SuppressWarnings("null")
202 @NonNull ImmutableList<TmfProtocol> immutableList = ImmutableList.copyOf(list);
203 fList = immutableList;
204 return immutableList;
205 }
206
207 /**
208 * Getter method that returns the packet. This is default visible since it
209 * is only used by tmf.pcap.core and thus should not be visible to other
210 * packages
211 *
212 * @return The packet.
213 */
214 Packet getPacket() {
215 return fPacket;
216 }
217
218 @Override
219 public String toString() {
220 return fPacket.getGlobalSummaryString();
221 }
222
223 /**
224 * Return the signification of the PcapEvent at a specific protocol level.
225 *
226 * @param protocol
227 * The specified protocol.
228 * @return The signification as a String.
229 */
230 public String toString(TmfProtocol protocol) {
231 Protocol p = ProtocolConversion.unwrap(protocol);
232 Packet packet = fPacket.getPacket(p);
233 if (packet == null) {
234 return EMPTY_STRING;
235 }
236 return packet.getLocalSummaryString();
237 }
238 }
This page took 0.036069 seconds and 5 git commands to generate.