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