Commit | Line | Data |
---|---|---|
5255c030 VP |
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.protocol.tcp; | |
14 | ||
15 | import org.eclipse.jdt.annotation.NonNull; | |
16 | import org.eclipse.jdt.annotation.Nullable; | |
17 | import org.eclipse.linuxtools.pcap.core.endpoint.ProtocolEndpoint; | |
18 | ||
19 | /** | |
20 | * Class that extends the {@link ProtocolEndpoint} class. It represents the | |
21 | * endpoint at a TCP level. | |
22 | * | |
23 | * @author Vincent Perot | |
24 | */ | |
25 | public class TCPEndpoint extends ProtocolEndpoint { | |
26 | ||
27 | private final int fPort; | |
28 | ||
29 | /** | |
30 | * Constructor of the {@link TCPEndpoint} class. It takes a packet to get | |
31 | * its endpoint. Since every packet has two endpoints (source and | |
32 | * destination), the isSourceEndpoint parameter is used to specify which | |
33 | * endpoint to take. | |
34 | * | |
35 | * @param packet | |
36 | * The packet that contains the endpoints. | |
37 | * @param isSourceEndpoint | |
38 | * Whether to take the source or the destination endpoint of the | |
39 | * packet. | |
40 | */ | |
41 | public TCPEndpoint(TCPPacket packet, boolean isSourceEndpoint) { | |
42 | super(packet, isSourceEndpoint); | |
43 | fPort = isSourceEndpoint ? packet.getSourcePort() : packet.getDestinationPort(); | |
44 | } | |
45 | ||
46 | @Override | |
47 | public int hashCode() { | |
48 | final int prime = 31; | |
49 | int result = 1; | |
50 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
51 | if (endpoint == null) { | |
52 | result = 0; | |
53 | } else { | |
54 | result = endpoint.hashCode(); | |
55 | } | |
56 | result = prime * result + fPort; | |
57 | return result; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public boolean equals(@Nullable Object obj) { | |
62 | if (this == obj) { | |
63 | return true; | |
64 | } | |
65 | if (!(obj instanceof TCPEndpoint)) { | |
66 | return false; | |
67 | } | |
68 | ||
69 | TCPEndpoint other = (TCPEndpoint) obj; | |
70 | ||
71 | // Check on layer | |
72 | boolean localEquals = (fPort == other.fPort); | |
73 | if (!localEquals) { | |
74 | return false; | |
75 | } | |
76 | ||
77 | // Check above layers. | |
78 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
79 | if (endpoint != null) { | |
80 | return endpoint.equals(other.getParentEndpoint()); | |
81 | } | |
82 | return true; | |
83 | } | |
84 | ||
85 | @Override | |
86 | public String toString() { | |
87 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
88 | if (endpoint == null) { | |
89 | @SuppressWarnings("null") | |
90 | @NonNull | |
91 | String ret = String.valueOf(fPort); | |
92 | return ret; | |
93 | } | |
94 | return endpoint.toString() + '/' + fPort; | |
95 | } | |
96 | ||
97 | } |