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.phase; 20 21 22 import java.nio.ByteBuffer; 23 24 import org.jscsi.initiator.connection.ITask; 25 import org.jscsi.initiator.connection.Session; 26 import org.jscsi.initiator.connection.TargetCapacityInformations; 27 import org.jscsi.parser.login.LoginStage; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30 31 32 /** 33 * A State Pattern. Each phase of the iSCSI Protocol must implement this interface. 34 * 35 * @author Volker Wildi 36 */ 37 public interface IPhase { 38 39 // -------------------------------------------------------------------------- 40 // -------------------------------------------------------------------------- 41 42 /** 43 * This method handles the login of a connection (if possible). 44 * 45 * @param session The context object of the current session. 46 * @throws Exception if any error occurs. 47 * @return true if task is finished 48 */ 49 public boolean login (final Session session) throws Exception; 50 51 /** 52 * This method handles the logout of a connection (if possible) (if possible in the current phase). 53 * 54 * @param session The context object of the current session. 55 * @param connectionID The ID of the connection to close. 56 * @return true if task is finished 57 * @throws Exception if any error occurs. 58 */ 59 public boolean logoutConnection (final Session session, final short connectionID) throws Exception; 60 61 /** 62 * This method handles the logout of the whole session (with all its connections) (if possible in the current 63 * phase). 64 * 65 * @param task The calling Task 66 * @param session The context object of the current session. 67 * @return true if task is finished 68 * @throws Exception if any error occurs. 69 */ 70 public boolean logoutSession (final ITask task, final Session session) throws Exception; 71 72 /** 73 * This method handles a read operation within this session (if possible in the current phase). 74 * 75 * @param task The calling Task 76 * @param session The context object of the current session. 77 * @param dst The buffer to store the read data. 78 * @param logicalBlockAddress The logical block address to start the read operation. 79 * @param length The number of bytes to read. 80 * @return true if task is finished 81 * @throws Exception if any error occurs. 82 */ 83 public boolean read (final ITask task, final Session session, final ByteBuffer dst, final int logicalBlockAddress, final long length) throws Exception; 84 85 /** 86 * This method handles a write operation within this session (if possible in the current phase). 87 * 88 * @param task The calling Task 89 * @param session The context object of the current session. 90 * @param src Write the remaining bytes to the iSCSI Target. 91 * @param logicalBlockAddress The logical block address to start the write operation. 92 * @param length The number of bytes to write. 93 * @return true if task is finished 94 * @throws Exception if any error occurs. 95 */ 96 public boolean write (final ITask task, final Session session, final ByteBuffer src, final int logicalBlockAddress, final long length) throws Exception; 97 98 /** 99 * This method handles the <code>TargetCapacityInformations</code> within this session (if possible in the current 100 * phase). 101 * 102 * @param session The context object of the current session. 103 * @param capacityInformation A <code>TargetCapacityInformations</code> instance to store these informations. 104 * @return true if task is finished 105 * @throws Exception if any error occurs. 106 */ 107 public boolean getCapacity (final Session session, final TargetCapacityInformations capacityInformation) throws Exception; 108 109 /** 110 * Returns the current stage. 111 * 112 * @return The current stage. 113 * @see LoginStage 114 */ 115 public LoginStage getStage (); 116 117 // -------------------------------------------------------------------------- 118 // -------------------------------------------------------------------------- 119 // -------------------------------------------------------------------------- 120 // -------------------------------------------------------------------------- 121 } 122 123 124 /** 125 * This abstract class contains the basic implementation of a phase. Each method should throw an 126 * <code>UnsupportedOperationException</code> to indicate incomplete behavior. 127 * 128 * @author Volker Wildi 129 */ 130 abstract class AbstractPhase implements IPhase { 131 132 // -------------------------------------------------------------------------- 133 // -------------------------------------------------------------------------- 134 135 /** The Logger interface. */ 136 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractPhase.class); 137 138 // -------------------------------------------------------------------------- 139 // -------------------------------------------------------------------------- 140 141 /** 142 * Default constructor to create a new, empty <code>AbstractPhase</code> object. 143 */ 144 protected AbstractPhase () { 145 146 } 147 148 // -------------------------------------------------------------------------- 149 // -------------------------------------------------------------------------- 150 151 /** {@inheritDoc} */ 152 public boolean login (final Session session) throws Exception { 153 154 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 155 } 156 157 /** {@inheritDoc} */ 158 public boolean logoutConnection (final Session session, final short connectionID) throws Exception { 159 160 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 161 } 162 163 /** {@inheritDoc} */ 164 public boolean logoutSession (final ITask task, final Session session) throws Exception { 165 166 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 167 } 168 169 /** {@inheritDoc} */ 170 public boolean read (final ITask task, final Session session, final ByteBuffer dst, final int logicalBlockAddress, final long length) throws Exception { 171 172 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 173 174 } 175 176 /** {@inheritDoc} */ 177 public boolean write (final ITask task, final Session session, final ByteBuffer src, final int logicalBlockAddress, final long length) throws Exception { 178 179 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 180 } 181 182 /** {@inheritDoc} */ 183 public boolean getCapacity (final Session session, final TargetCapacityInformations capacityInformation) throws Exception { 184 185 throw new UnsupportedOperationException("This operation is not possible in the current phase."); 186 } 187 188 // -------------------------------------------------------------------------- 189 // -------------------------------------------------------------------------- 190 191 /** {@inheritDoc} */ 192 public LoginStage getStage () { 193 194 throw new UnsupportedOperationException(); 195 } 196 197 // -------------------------------------------------------------------------- 198 // -------------------------------------------------------------------------- 199 // -------------------------------------------------------------------------- 200 // -------------------------------------------------------------------------- 201 }