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.io.IOException;
23  
24  import org.jscsi.exception.InternetSCSIException;
25  import org.jscsi.initiator.connection.Connection;
26  import org.jscsi.parser.ProtocolDataUnit;
27  import org.jscsi.parser.logout.LogoutResponse;
28  import org.jscsi.parser.logout.LogoutResponseParser;
29  
30  
31  /**
32   * <h1>LogoutResponseState</h1>
33   * <p/>
34   * This state handles a Logout Response.
35   * 
36   * @author Volker Wildi
37   */
38  public final class LogoutResponseState extends AbstractState {
39  
40      // --------------------------------------------------------------------------
41      // --------------------------------------------------------------------------
42  
43      /**
44       * Constructor to create a <code>LogoutResponseState</code> instance, which uses the given connection for
45       * transmission.
46       * 
47       * @param initConnection The context connection, which is used for the network transmission.
48       */
49      public LogoutResponseState (final Connection initConnection) {
50  
51          super(initConnection);
52      }
53  
54      // --------------------------------------------------------------------------
55      // --------------------------------------------------------------------------
56  
57      /** {@inheritDoc} */
58      public final void execute () throws InternetSCSIException {
59  
60          final ProtocolDataUnit protocolDataUnit = connection.receive();
61  
62          if (!(protocolDataUnit.getBasicHeaderSegment().getParser() instanceof LogoutResponseParser)) { throw new InternetSCSIException("This PDU type (" + protocolDataUnit.getBasicHeaderSegment().getParser() + ") is not expected. "); }
63  
64          final LogoutResponseParser parser = (LogoutResponseParser) protocolDataUnit.getBasicHeaderSegment().getParser();
65  
66          if (parser.getResponse() == LogoutResponse.CONNECTION_CLOSED_SUCCESSFULLY) {
67              // exception rethrow
68              try {
69                  // FIXME: Implement Connection close
70                  connection.getSession().close();
71              } catch (IOException e) {
72                  throw new InternetSCSIException("Closing the connection throws this error: " + e.getLocalizedMessage());
73              }
74          } else {
75              throw new InternetSCSIException("The connection could not be closed successfully.");
76          }
77  
78          // return false;
79      }
80  
81      // --------------------------------------------------------------------------
82      // --------------------------------------------------------------------------
83      // --------------------------------------------------------------------------
84      // --------------------------------------------------------------------------
85  
86  }