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 org.jscsi.exception.InternetSCSIException;
23  import org.jscsi.initiator.connection.Connection;
24  import org.jscsi.initiator.connection.TargetCapacityInformations;
25  import org.jscsi.parser.OperationCode;
26  import org.jscsi.parser.ProtocolDataUnit;
27  import org.jscsi.parser.datasegment.OperationalTextKey;
28  import org.jscsi.parser.scsi.SCSICommandDescriptorBlockParser;
29  import org.jscsi.parser.scsi.SCSICommandParser;
30  import org.jscsi.parser.scsi.SCSICommandParser.TaskAttributes;
31  
32  
33  /**
34   * <h1>CapacityRequestState</h1>
35   * <p/>
36   * This state handles a Capacity Request to retrieve the block size and the size of the iSCSI Device.
37   * 
38   * @author Volker Wildi
39   */
40  public final class CapacityRequestState extends AbstractState {
41  
42      // --------------------------------------------------------------------------
43      // --------------------------------------------------------------------------
44  
45      /** The sent command has a fixed size of <code>8</code> bytes. */
46      private static final int EXPECTED_DATA_TRANSFER_LENGTH = 0x08;
47  
48      // --------------------------------------------------------------------------
49      // --------------------------------------------------------------------------
50  
51      /**
52       * This object contains the informations about the capacity of the connected target.
53       */
54  
55      private final TargetCapacityInformations capacityInformation;
56  
57      private final TaskAttributes taskAttributes;
58  
59      // --------------------------------------------------------------------------
60      // --------------------------------------------------------------------------
61  
62      /**
63       * Constructor to create a new, empty <code>CapacityRequestState</code> instance.
64       * 
65       * @param initConnection This is the connection, which is used for the network transmission.
66       * @param initCapacityInformation Store the informations about that iSCSI Device in this instance.
67       * @param initTaskAttributes The task attributes, which are used with task.
68       */
69      public CapacityRequestState (final Connection initConnection, final TargetCapacityInformations initCapacityInformation, final TaskAttributes initTaskAttributes) {
70  
71          super(initConnection);
72          capacityInformation = initCapacityInformation;
73          taskAttributes = initTaskAttributes;
74      }
75  
76      // --------------------------------------------------------------------------
77      // --------------------------------------------------------------------------
78  
79      /** {@inheritDoc} */
80      public final void execute () throws InternetSCSIException {
81  
82          final ProtocolDataUnit protocolDataUnit = protocolDataUnitFactory.create(false, true, OperationCode.SCSI_COMMAND, connection.getSetting(OperationalTextKey.HEADER_DIGEST), connection.getSetting(OperationalTextKey.DATA_DIGEST));
83          final SCSICommandParser scsi = (SCSICommandParser) protocolDataUnit.getBasicHeaderSegment().getParser();
84  
85          scsi.setReadExpectedFlag(true);
86          scsi.setWriteExpectedFlag(false);
87          scsi.setTaskAttributes(taskAttributes);
88  
89          scsi.setExpectedDataTransferLength(EXPECTED_DATA_TRANSFER_LENGTH);
90  
91          scsi.setCommandDescriptorBlock(SCSICommandDescriptorBlockParser.createReadCapacityMessage());
92  
93          connection.send(protocolDataUnit);
94          connection.nextState(new CapacityResponseState(connection, capacityInformation));
95          super.stateFollowing = true;
96          // return true;
97      }
98  
99      // --------------------------------------------------------------------------
100     // --------------------------------------------------------------------------
101     // --------------------------------------------------------------------------
102     // --------------------------------------------------------------------------
103 
104 }