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; 20 21 22 import java.nio.ByteBuffer; 23 import java.util.concurrent.Callable; 24 25 import org.jscsi.initiator.connection.phase.IPhase; 26 27 28 /** 29 * <h1>ITask</h1> 30 * <p/> 31 * This interface defines all methods, which a task has to support. 32 * 33 * @author Sebastian Graf, University of Konstanz 34 */ 35 public interface ITask { 36 37 /** 38 * This method is call, when this <code>ITask</code> instance is polled from the head of the <code>taskQueue</code> 39 * to start a task. 40 * 41 * @return Void nothing at all 42 * @throws Exception if any error occurs. 43 */ 44 public Void call () throws Exception; 45 46 } 47 48 49 // -------------------------------------------------------------------------- 50 // -------------------------------------------------------------------------- 51 // -------------------------------------------------------------------------- 52 // -------------------------------------------------------------------------- 53 54 /** 55 * <h1>AbstractTask</h1> 56 * <p/> 57 * This abstract class defines all common methods, for the implementing Tasks. 58 * 59 * @author Volker Wildi 60 */ 61 abstract class AbstractTask implements ITask { 62 63 // -------------------------------------------------------------------------- 64 // -------------------------------------------------------------------------- 65 66 /** The <code>Session</code> instance of this task. */ 67 protected final Session session; 68 69 /** The <code>IPhase</code> instance of this task. */ 70 protected IPhase phase; 71 72 // -------------------------------------------------------------------------- 73 // -------------------------------------------------------------------------- 74 75 /** 76 * Constructor to create a new, empty <code>AbstractTask</code> subclass instance, which is initialized with the 77 * given values. 78 * 79 * @param initCaller The invoking caller of this task. 80 * @param referenceSession The session, where this task is executed in. 81 */ 82 AbstractTask (final Session referenceSession) { 83 84 session = referenceSession; 85 phase = referenceSession.phase; 86 } 87 88 // -------------------------------------------------------------------------- 89 // -------------------------------------------------------------------------- 90 91 /** {@inheritDoc} */ 92 @Override 93 public final String toString () { 94 95 return getClass().getSimpleName(); 96 } 97 98 } 99 100 101 // -------------------------------------------------------------------------- 102 // -------------------------------------------------------------------------- 103 // -------------------------------------------------------------------------- 104 // -------------------------------------------------------------------------- 105 /** 106 * <h1>LoginTask</h1> 107 * <p/> 108 * This defines a LoginTask. 109 * 110 * @author Volker Wildi 111 */ 112 113 final class LoginTask extends AbstractTask { 114 115 // -------------------------------------------------------------------------- 116 // -------------------------------------------------------------------------- 117 118 /** 119 * Constructor to create a new, empty <code>LoginTask</code> instance, which is initialized with the given values. 120 * 121 * 122 * @param referenceSession The session, where this task is executed in. 123 */ 124 LoginTask (final Session referenceSession) { 125 126 super(referenceSession); 127 } 128 129 // -------------------------------------------------------------------------- 130 // -------------------------------------------------------------------------- 131 132 /** {@inheritDoc} */ 133 public final Void call () throws Exception { 134 135 if (phase.login(session)) { 136 session.finishedTask(this); 137 } 138 return null; 139 } 140 141 } 142 143 144 // -------------------------------------------------------------------------- 145 // -------------------------------------------------------------------------- 146 // -------------------------------------------------------------------------- 147 // -------------------------------------------------------------------------- 148 /** 149 * <h1>LogoutTask</h1> 150 * <p/> 151 * This defines a LogoutTask. 152 * 153 * @author Volker Wildi 154 */ 155 final class LogoutTask extends AbstractTask { 156 157 // -------------------------------------------------------------------------- 158 // -------------------------------------------------------------------------- 159 160 /** 161 * Constructor to create a new, empty <code>LogoutTask</code> instance, which is initialized with the given values. 162 * 163 * 164 * @param referenceSession The session, where this task is executed in. 165 */ 166 LogoutTask (final Session referenceSession) { 167 168 super(referenceSession); 169 } 170 171 // -------------------------------------------------------------------------- 172 // -------------------------------------------------------------------------- 173 174 /** {@inheritDoc} */ 175 public final Void call () throws Exception { 176 177 if (phase.logoutSession(this, session)) { 178 session.finishedTask(this); 179 } 180 return null; 181 } 182 } 183 184 185 // -------------------------------------------------------------------------- 186 // -------------------------------------------------------------------------- 187 // -------------------------------------------------------------------------- 188 // -------------------------------------------------------------------------- 189 /** 190 * <h1>IOTask</h1> 191 * <p/> 192 * This defines a IOTask. 193 * 194 * @author Sebastian Graf, University of Konstanz 195 */ 196 abstract class IOTask extends AbstractTask implements Callable<Void> { 197 198 // -------------------------------------------------------------------------- 199 // -------------------------------------------------------------------------- 200 201 /** The buffer to store/read the data of this IOTask. */ 202 protected final ByteBuffer buffer; 203 204 /** The logical block address of the start of this task. */ 205 protected final int logicalBlockAddress; 206 207 /** The length (in bytes) of this task. */ 208 protected final long length; 209 210 // -------------------------------------------------------------------------- 211 // -------------------------------------------------------------------------- 212 213 /** 214 * Constructor to create a new, empty <code>IOTask</code> instance, which is initialized with the given values. 215 * 216 * 217 * @param referenceSession The session, where this task is executed in. 218 * @param dst Destination ByteBuffer. 219 * @param initLogicalBlockAddress Initial logical Block Address. 220 * @param initLength length of buffer 221 */ 222 IOTask (final Session referenceSession, final ByteBuffer dst, final int initLogicalBlockAddress, final long initLength) { 223 224 super(referenceSession); 225 buffer = dst; 226 logicalBlockAddress = initLogicalBlockAddress; 227 length = initLength; 228 229 } 230 } 231 232 233 // -------------------------------------------------------------------------- 234 // -------------------------------------------------------------------------- 235 236 /** 237 * <h1>ReadTask</h1> 238 * <p/> 239 * This defines a ReadTask. 240 * 241 * @author Volker Wildi 242 */ 243 final class ReadTask extends IOTask { 244 245 // -------------------------------------------------------------------------- 246 // -------------------------------------------------------------------------- 247 248 /** 249 * Constructor to create a new, empty <code>ReadTask</code> instance, which is initialized with the given values. 250 * 251 * @param referenceSession The session, where this task is executed in. 252 * @param dst Destination ByteBuffer. 253 * @param initLogicalBlockAddress Initial logical Block Address. 254 * @param initLength length of buffer 255 */ 256 ReadTask (final Session referenceSession, final ByteBuffer dst, final int initLogicalBlockAddress, final long initLength) { 257 258 super(referenceSession, dst, initLogicalBlockAddress, initLength); 259 } 260 261 // -------------------------------------------------------------------------- 262 // -------------------------------------------------------------------------- 263 264 /** {@inheritDoc} */ 265 public final Void call () throws Exception { 266 267 if (phase.read(this, session, buffer, logicalBlockAddress, length)) { 268 session.finishedTask(this); 269 } 270 return null; 271 } 272 } 273 274 275 // -------------------------------------------------------------------------- 276 // -------------------------------------------------------------------------- 277 // -------------------------------------------------------------------------- 278 // -------------------------------------------------------------------------- 279 /** 280 * <h1>WriteTask</h1> 281 * <p/> 282 * This defines a WriteTask. 283 * 284 * @author Volker Wildi 285 */ 286 final class WriteTask extends IOTask { 287 288 // -------------------------------------------------------------------------- 289 // -------------------------------------------------------------------------- 290 291 /** 292 * Constructor to create a new, empty <code>WriteTask</code> instance, which is initialized with the given values. 293 * 294 * 295 * @param referenceSession The session, where this task is executed in. 296 * @param src Source ByteBuffer. 297 * @param initLogicalBlockAddress Initial logical Block Address. 298 * @param initLength length of buffer 299 */ 300 WriteTask (final Session referenceSession, final ByteBuffer src, final int initLogicalBlockAddress, final long initLength) { 301 302 super(referenceSession, src, initLogicalBlockAddress, initLength); 303 } 304 305 // -------------------------------------------------------------------------- 306 // -------------------------------------------------------------------------- 307 308 /** {@inheritDoc} */ 309 public final Void call () throws Exception { 310 311 if (phase.write(this, session, buffer, logicalBlockAddress, length)) { 312 session.finishedTask(this); 313 } 314 return null; 315 } 316 } 317 318 // -------------------------------------------------------------------------- 319 // -------------------------------------------------------------------------- 320 // -------------------------------------------------------------------------- 321 // -------------------------------------------------------------------------- 322 // -------------------------------------------------------------------------- 323 // --------------------------------------------------------------------------