1 package org.jscsi.target.connection;
2
3
4 import java.io.IOException;
5 import java.nio.channels.SocketChannel;
6 import java.security.DigestException;
7 import java.util.concurrent.Callable;
8
9 import javax.naming.OperationNotSupportedException;
10
11 import org.jscsi.exception.InternetSCSIException;
12 import org.jscsi.parser.OperationCode;
13 import org.jscsi.parser.ProtocolDataUnit;
14 import org.jscsi.target.connection.phase.TargetFullFeaturePhase;
15 import org.jscsi.target.connection.phase.TargetLoginPhase;
16 import org.jscsi.target.connection.phase.TargetPhase;
17 import org.jscsi.target.connection.stage.fullfeature.PingStage;
18 import org.jscsi.target.connection.stage.fullfeature.ReadStage;
19 import org.jscsi.target.settings.ConnectionSettingsNegotiator;
20 import org.jscsi.target.settings.SessionSettingsNegotiator;
21 import org.jscsi.target.settings.Settings;
22 import org.jscsi.target.settings.SettingsException;
23 import org.jscsi.target.util.FastByteArrayProvider;
24 import org.jscsi.target.util.SerialArithmeticNumber;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public interface Connection extends Callable<Void> {
41
42 Settings getSettings ();
43
44 SerialArithmeticNumber getStatusSequenceNumber ();
45
46 boolean isLeadingConnection ();
47
48 ProtocolDataUnit receivePdu () throws DigestException , InternetSCSIException , IOException , SettingsException;
49
50 void sendPdu (ProtocolDataUnit pDataUnit) throws InterruptedException , IOException , InternetSCSIException;
51
52 ConnectionSettingsNegotiator getConnectionSettingsNegotiator ();
53
54 void setSession (TargetSession pSession);
55
56 TargetSession getTargetSession ();
57
58 void setStatusSequenceNumber (int pStatusSequenceNumber);
59
60 void initializeConnectionSettingsNegotiator (SessionSettingsNegotiator pSettingsNegotiator);
61
62 byte[] getDataInArray (int pLength);
63
64 public boolean stop();
65
66 public static class TargetConnection implements Connection {
67
68 private static final Logger LOGGER = LoggerFactory.getLogger(TargetConnection.class);
69
70
71
72
73 private TargetSession targetSession;
74
75
76
77
78 TargetSenderWorker senderWorker;
79
80
81
82
83
84 private ConnectionSettingsNegotiator connectionSettingsNegotiator;
85
86
87
88
89 private TargetPhase phase;
90
91
92
93
94 private SerialArithmeticNumber statusSequenceNumber;
95
96
97
98
99
100 private FastByteArrayProvider dataInArrayProvider = new FastByteArrayProvider(4);
101
102
103
104
105
106
107
108 private final boolean isLeadingConnection;
109
110
111
112
113 private ProtocolDataUnit lastReceivedPDU;
114
115
116
117
118
119
120
121
122 public TargetConnection (SocketChannel socketChannel, final boolean isLeadingConnection) {
123 this.isLeadingConnection = isLeadingConnection;
124 senderWorker = new TargetSenderWorker(this, socketChannel);
125 }
126
127
128
129
130
131
132
133
134 public byte[] getDataInArray (final int length) {
135 return dataInArrayProvider.getArray(length);
136 }
137
138
139
140
141
142
143 TargetSession getSession () {
144 return targetSession;
145 }
146
147
148
149
150
151
152 public void setSession (TargetSession session) {
153 this.targetSession = session;
154 senderWorker.setSession(session);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 public ProtocolDataUnit receivePdu () throws DigestException , InternetSCSIException , IOException , SettingsException {
169 lastReceivedPDU = senderWorker.receiveFromWire();
170
171 if (lastReceivedPDU.getBasicHeaderSegment().getOpCode().equals(OperationCode.NOP_OUT)) {
172 try {
173
174
175
176
177 new PingStage(new TargetFullFeaturePhase(this)).execute(lastReceivedPDU);
178 } catch (InterruptedException e) {}
179 lastReceivedPDU = senderWorker.receiveFromWire();
180 }
181
182
183
184
185 return lastReceivedPDU;
186 }
187
188
189
190
191
192
193
194
195
196 public void sendPdu (ProtocolDataUnit pdu) throws InterruptedException , IOException , InternetSCSIException {
197
198
199
200 senderWorker.sendOverWire(pdu);
201 }
202
203
204
205
206
207
208
209
210 public Void call () {
211
212 try {
213
214 phase = new TargetLoginPhase(this);
215 if (phase.execute(lastReceivedPDU)) {
216 LOGGER.debug("Login Phase successful");
217
218
219 final Settings settings = getSettings();
220 if (isLeadingConnection) targetSession.setSessionType(SessionType.getSessionType(settings.getSessionType()));
221 targetSession.setTargetName(settings.getTargetName());
222
223 phase = new TargetFullFeaturePhase(this);
224
225 phase.execute();
226 }
227 senderWorker.close();
228 } catch (OperationNotSupportedException | IOException | InterruptedException | InternetSCSIException | DigestException
229 | SettingsException e) {
230 LOGGER.error("Exception throws", e);
231 }
232
233 targetSession.removeTargetConnection(this);
234
235 LOGGER.debug("closed connection");
236
237 return null;
238 }
239
240 public TargetSession getTargetSession () {
241 return targetSession;
242 }
243
244
245
246
247
248
249
250 public boolean isLeadingConnection () {
251 return isLeadingConnection;
252 }
253
254
255
256
257
258
259 public void initializeConnectionSettingsNegotiator (final SessionSettingsNegotiator sessionSettingsNegotiator) {
260 connectionSettingsNegotiator = new ConnectionSettingsNegotiator(sessionSettingsNegotiator);
261 }
262
263
264
265
266
267
268 public Settings getSettings () {
269 return connectionSettingsNegotiator.getSettings();
270 }
271
272 public ConnectionSettingsNegotiator getConnectionSettingsNegotiator () {
273 return connectionSettingsNegotiator;
274 }
275
276 public SerialArithmeticNumber getStatusSequenceNumber () {
277 return statusSequenceNumber;
278 }
279
280 public void setStatusSequenceNumber (final int statusSequenceNumber) {
281 this.statusSequenceNumber = new SerialArithmeticNumber(statusSequenceNumber);
282 }
283
284 public boolean stop(){
285 if(phase instanceof TargetFullFeaturePhase){
286 ((TargetFullFeaturePhase)phase).stop();
287 return true;
288 }
289
290 return false;
291 }
292 }
293 }