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.nio.ByteBuffer; 23 24 import org.jscsi.exception.InternetSCSIException; 25 import org.jscsi.initiator.connection.Connection; 26 import org.jscsi.parser.OperationCode; 27 import org.jscsi.parser.ProtocolDataUnit; 28 import org.jscsi.parser.datasegment.OperationalTextKey; 29 import org.jscsi.parser.scsi.SCSICommandDescriptorBlockParser; 30 import org.jscsi.parser.scsi.SCSICommandParser; 31 import org.jscsi.parser.scsi.SCSICommandParser.TaskAttributes; 32 33 34 /** 35 * <h1>ReadRequestState</h1> 36 * <p/> 37 * This state handles a Read Request with some unsolicited data. 38 * 39 * @author Volker Wildi 40 */ 41 public final class ReadRequestState extends AbstractState { 42 43 // -------------------------------------------------------------------------- 44 // -------------------------------------------------------------------------- 45 46 /** The buffer to used for the message transfer. */ 47 private final ByteBuffer buffer; 48 49 /** The task attributes of this read operation. */ 50 private final TaskAttributes taskAttributes; 51 52 /** The expected length in bytes, which should be transfered. */ 53 private final int expectedDataTransferLength; 54 55 /** The logical block address of the beginning of the read operation. */ 56 private final int logicalBlockAddress; 57 58 /** 59 * The number of blocks (This block size is dependent on the size used on the target side.) to read. 60 */ 61 private final short transferLength; 62 63 // -------------------------------------------------------------------------- 64 // -------------------------------------------------------------------------- 65 66 /** 67 * Constructor to create a <code>ReadRequestState</code> instance, which creates a request to the iSCSI Target. 68 * 69 * @param initConnection This is the connection, which is used for the network transmission. 70 * @param initBuffer This buffer should be read. 71 * @param initTaskAttributes The task attributes of this task. 72 * @param initExpectedDataTransferLength The expected length in bytes, which should be transfered. 73 * @param initLogicalBlockAddress The logical block address of the first block to read. 74 * @param initTransferLength The number of blocks to read. 75 */ 76 public ReadRequestState (final Connection initConnection, final ByteBuffer initBuffer, final TaskAttributes initTaskAttributes, final int initExpectedDataTransferLength, final int initLogicalBlockAddress, final short initTransferLength) { 77 78 super(initConnection); 79 buffer = initBuffer; 80 taskAttributes = initTaskAttributes; 81 expectedDataTransferLength = initExpectedDataTransferLength; 82 logicalBlockAddress = initLogicalBlockAddress; 83 transferLength = initTransferLength; 84 } 85 86 // -------------------------------------------------------------------------- 87 // -------------------------------------------------------------------------- 88 89 /** {@inheritDoc} */ 90 public final void execute () throws InternetSCSIException { 91 92 final ProtocolDataUnit protocolDataUnit = protocolDataUnitFactory.create(false, true, OperationCode.SCSI_COMMAND, connection.getSetting(OperationalTextKey.HEADER_DIGEST), connection.getSetting(OperationalTextKey.DATA_DIGEST)); 93 final SCSICommandParser scsi = (SCSICommandParser) protocolDataUnit.getBasicHeaderSegment().getParser(); 94 95 scsi.setReadExpectedFlag(true); 96 scsi.setWriteExpectedFlag(false); 97 scsi.setTaskAttributes(taskAttributes); 98 scsi.setExpectedDataTransferLength(expectedDataTransferLength); 99 scsi.setCommandDescriptorBlock(SCSICommandDescriptorBlockParser.createReadMessage(logicalBlockAddress, transferLength)); 100 101 connection.send(protocolDataUnit); 102 connection.nextState(new ReadResponseState(connection, buffer, 0, 0)); 103 super.stateFollowing = true; 104 // return true; 105 } 106 107 // -------------------------------------------------------------------------- 108 // -------------------------------------------------------------------------- 109 // -------------------------------------------------------------------------- 110 // -------------------------------------------------------------------------- 111 112 }