View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5    * following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of
6    * conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice,
7    * this list of conditions and the following disclaimer in the documentation and/or other materials provided with the
8    * distribution. * Neither the name of the University of Konstanz nor the names of its contributors may be used to
9    * endorse or promote products derived from this software without specific prior written permission.
10   * 
11   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
12   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
13   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
14   * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
17   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18   */
19  package org.jscsi.initiator.connection.state;
20  
21  
22  import java.util.LinkedList;
23  import java.util.Queue;
24  
25  import org.jscsi.exception.InternetSCSIException;
26  import org.jscsi.initiator.connection.Connection;
27  import org.jscsi.parser.OperationCode;
28  import org.jscsi.parser.ProtocolDataUnit;
29  import org.jscsi.parser.datasegment.DataSegmentFactory;
30  import org.jscsi.parser.datasegment.DataSegmentFactory.DataSegmentFormat;
31  import org.jscsi.parser.datasegment.IDataSegment;
32  import org.jscsi.parser.datasegment.IDataSegmentIterator;
33  import org.jscsi.parser.datasegment.IDataSegmentIterator.IDataSegmentChunk;
34  import org.jscsi.parser.datasegment.OperationalTextKey;
35  import org.jscsi.parser.datasegment.SettingsMap;
36  import org.jscsi.parser.login.ISID;
37  import org.jscsi.parser.login.LoginRequestParser;
38  import org.jscsi.parser.login.LoginStage;
39  
40  
41  /**
42   * <h1>LoginRequestState</h1>
43   * <p/>
44   * This state handles a Login Request.
45   * 
46   * @author Volker Wildi
47   */
48  public final class LoginRequestState extends AbstractState {
49  
50      // --------------------------------------------------------------------------
51      // --------------------------------------------------------------------------
52  
53      /**
54       * The maximum version number, which is supported by this iSCSI Implementation (RFC3720).
55       */
56      private static final byte MAXIMUM_VERSION = 0x00;
57  
58      /**
59       * The minimum version number, which is supported by this iSCSI Implementation (RFC3720).
60       */
61      private static final byte MINIMUM_VERSION = 0x00;
62  
63      // --------------------------------------------------------------------------
64      // --------------------------------------------------------------------------
65  
66      /** The Next Stage of the login process. */
67      private final LoginStage nextStage;
68  
69      // --------------------------------------------------------------------------
70      // --------------------------------------------------------------------------
71  
72      /**
73       * Constructor to create a <code>LoginRequestState</code> instance, which uses the given connection for
74       * transmission.
75       * 
76       * @param initConnection The context connection, which is used for the network transmission.
77       * @param initNextStage The next stage to which should transfered to.
78       */
79      public LoginRequestState (final Connection initConnection, final LoginStage initNextStage) {
80  
81          super(initConnection);
82          nextStage = initNextStage;
83      }
84  
85      // --------------------------------------------------------------------------
86      // --------------------------------------------------------------------------
87  
88      /** {@inheritDoc} */
89      public final void execute () throws InternetSCSIException {
90  
91          final SettingsMap loginParameters = connection.getSettings();
92          LOGGER.info("Sending these login parameters:\n" + loginParameters);
93  
94          final int maxRecvDataSegmentLength = connection.getSettingAsInt(OperationalTextKey.MAX_RECV_DATA_SEGMENT_LENGTH);
95          final ISID isid = ISID.createRandom(System.currentTimeMillis());
96  
97          LoginRequestParser loginRequest;
98          boolean continueFlag;
99          // here the finalFlag represents the transitFlag
100         boolean finalFlag;
101         final IDataSegment dataSegment = DataSegmentFactory.create(loginParameters.asByteBuffer(), DataSegmentFormat.TEXT, maxRecvDataSegmentLength);
102         final IDataSegmentIterator iterator = dataSegment.iterator();
103         final Queue<ProtocolDataUnit> protocolDataUnits = new LinkedList<ProtocolDataUnit>();
104 
105         IDataSegmentChunk dataSegmentChunk;
106         ProtocolDataUnit protocolDataUnit;
107         int bytes2Transfer = dataSegment.getLength();
108 
109         while (bytes2Transfer > 0 && iterator.hasNext()) {
110 
111             if (bytes2Transfer <= maxRecvDataSegmentLength) {
112                 // last PDU to send...
113                 dataSegmentChunk = iterator.next(bytes2Transfer);
114                 continueFlag = false;
115             } else {
116                 dataSegmentChunk = iterator.next(maxRecvDataSegmentLength);
117                 continueFlag = true;
118             }
119 
120             finalFlag = !continueFlag;
121             protocolDataUnit = protocolDataUnitFactory.create(true, finalFlag, OperationCode.LOGIN_REQUEST, "None", "None");
122             loginRequest = (LoginRequestParser) protocolDataUnit.getBasicHeaderSegment().getParser();
123 
124             loginRequest.setContinueFlag(continueFlag);
125             loginRequest.setCurrentStageNumber(connection.getSession().getPhase());
126             LOGGER.debug("Phase:\n" + loginRequest.getCurrentStageNumber());
127             if (finalFlag) {
128                 loginRequest.setNextStageNumber(nextStage);
129             }
130             loginRequest.setMaxVersion(MAXIMUM_VERSION);
131             loginRequest.setMinVersion(MINIMUM_VERSION);
132 
133             loginRequest.setInitiatorSessionID(isid);
134             loginRequest.setTargetSessionIdentifyingHandle(connection.getSession().getTargetSessionIdentifyingHandle());
135 
136             protocolDataUnit.setDataSegment(dataSegmentChunk);
137 
138             protocolDataUnits.offer(protocolDataUnit);
139             bytes2Transfer -= maxRecvDataSegmentLength;
140         }
141 
142         connection.send(protocolDataUnits);
143         connection.nextState(new LoginResponseState(connection, nextStage));
144         super.stateFollowing = true;
145         // return true;
146     }
147 
148     // --------------------------------------------------------------------------
149     // --------------------------------------------------------------------------
150     // --------------------------------------------------------------------------
151     // --------------------------------------------------------------------------
152 }