1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.waarp.ftp.core.session;
19
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import io.netty.channel.Channel;
25 import org.waarp.common.logging.WaarpLogger;
26 import org.waarp.common.logging.WaarpLoggerFactory;
27 import org.waarp.ftp.core.utils.FtpChannelUtils;
28
29
30
31
32
33
34
35
36
37
38
39 public class FtpSessionReference {
40
41
42
43 private static final WaarpLogger logger = WaarpLoggerFactory
44 .getLogger(FtpSessionReference.class);
45
46
47
48
49
50
51
52 public static class P2PAddress {
53
54
55
56 public InetAddress ipOnly;
57
58
59
60
61 public InetSocketAddress fullIp;
62
63
64
65
66
67
68 public P2PAddress(Channel channel) {
69 ipOnly = FtpChannelUtils.getRemoteInetAddress(channel);
70 fullIp = (InetSocketAddress) channel.localAddress();
71 }
72
73
74
75
76
77
78
79 public P2PAddress(InetAddress address,
80 InetSocketAddress inetSocketAddress) {
81 ipOnly = address;
82 fullIp = inetSocketAddress;
83 }
84
85
86
87
88
89 public boolean isValid() {
90 return ipOnly != null && fullIp != null;
91 }
92
93 @Override
94 public boolean equals(Object arg0) {
95 if (arg0 == null) {
96 return false;
97 }
98 if (arg0 instanceof P2PAddress) {
99 P2PAddress p2paddress = (P2PAddress) arg0;
100 if (p2paddress.isValid() && isValid()) {
101 return p2paddress.fullIp.equals(fullIp) &&
102 p2paddress.ipOnly.equals(ipOnly);
103 }
104 }
105 return false;
106 }
107
108 @Override
109 public int hashCode() {
110 return fullIp.hashCode() + ipOnly.hashCode();
111 }
112
113 }
114
115
116
117
118 private final ConcurrentHashMap<P2PAddress, FtpSession> hashMap = new ConcurrentHashMap<P2PAddress, FtpSession>();
119
120
121
122
123
124 public FtpSessionReference() {
125 }
126
127
128
129
130
131
132
133
134 public void setNewFtpSession(InetAddress ipOnly, InetSocketAddress fullIp,
135 FtpSession session) {
136 P2PAddress pAddress = new P2PAddress(ipOnly, fullIp);
137 if (!pAddress.isValid()) {
138 logger.error("Couple invalid in setNewFtpSession: " + ipOnly +
139 " : " + fullIp);
140 return;
141 }
142 hashMap.put(pAddress, session);
143
144 }
145
146
147
148
149
150
151
152 public FtpSession getActiveFtpSession(Channel channel, boolean remove) {
153
154 P2PAddress pAddress = new P2PAddress(((InetSocketAddress) channel
155 .localAddress()).getAddress(), (InetSocketAddress) channel
156 .remoteAddress());
157 if (!pAddress.isValid()) {
158 logger.error("Couple invalid in getActiveFtpSession: " + channel +
159 channel.localAddress() + channel.remoteAddress());
160 return null;
161 }
162
163 if (remove) {
164 return hashMap.remove(pAddress);
165 } else {
166 return hashMap.get(pAddress);
167 }
168 }
169
170
171
172
173
174
175
176 public FtpSession getPassiveFtpSession(Channel channel, boolean remove) {
177
178 P2PAddress pAddress = new P2PAddress(channel);
179 if (!pAddress.isValid()) {
180 logger.error("Couple invalid in getPassiveFtpSession: " + channel);
181 return null;
182 }
183
184 if (remove) {
185 return hashMap.remove(pAddress);
186 } else {
187 return hashMap.get(pAddress);
188 }
189 }
190
191
192
193
194
195
196
197 public void delFtpSession(InetAddress ipOnly, InetSocketAddress fullIp) {
198 P2PAddress pAddress = new P2PAddress(ipOnly, fullIp);
199 if (!pAddress.isValid()) {
200 logger.error("Couple invalid in delFtpSession: " + ipOnly + " : " +
201 fullIp);
202 return;
203 }
204
205 hashMap.remove(pAddress);
206 }
207
208
209
210
211
212
213
214
215 public boolean contains(InetAddress ipOnly, InetSocketAddress fullIp) {
216 P2PAddress pAddress = new P2PAddress(ipOnly, fullIp);
217 if (!pAddress.isValid()) {
218 logger.error("Couple invalid in contains: " + ipOnly + " : " +
219 fullIp);
220 return false;
221 }
222
223 return hashMap.containsKey(pAddress);
224 }
225
226
227
228
229
230 public int sessionsNumber() {
231 return hashMap.size();
232 }
233 }