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.parser.OperationCode;
25  import org.jscsi.parser.ProtocolDataUnit;
26  import org.jscsi.parser.datasegment.DataSegmentFactory;
27  import org.jscsi.parser.datasegment.DataSegmentFactory.DataSegmentFormat;
28  import org.jscsi.parser.datasegment.IDataSegment;
29  import org.jscsi.parser.datasegment.IDataSegmentIterator;
30  import org.jscsi.parser.datasegment.IDataSegmentIterator.IDataSegmentChunk;
31  import org.jscsi.parser.datasegment.OperationalTextKey;
32  import org.jscsi.parser.datasegment.SettingsMap;
33  import org.jscsi.parser.text.TextRequestParser;
34  
35  
36  /**
37   * <h1>GetConnectionsRequestState</h1>
38   * <p/>
39   * This state requests a list of all possible connections to a specific target. So it sends a TextRequest PDU with
40   * <code>SendTargets=</code> as the only <code>OperationalTextKey</code> as data segment.
41   * 
42   * @author Volker Wildi
43   */
44  public final class GetConnectionsRequestState extends AbstractState {
45  
46      // --------------------------------------------------------------------------
47      // --------------------------------------------------------------------------
48  
49      /**
50       * Constructor to create a <code>GetConnectionsRequestState</code> instance, which uses the given connection for
51       * transmission.
52       * 
53       * @param initConnection The context connection, which is used for the network transmission.
54       */
55      public GetConnectionsRequestState (final Connection initConnection) {
56  
57          super(initConnection);
58      }
59  
60      // --------------------------------------------------------------------------
61      // --------------------------------------------------------------------------
62  
63      /** {@inheritDoc} */
64      public final void execute () throws InternetSCSIException {
65  
66          final ProtocolDataUnit protocolDataUnit = protocolDataUnitFactory.create(false, true, OperationCode.TEXT_REQUEST, connection.getSetting(OperationalTextKey.HEADER_DIGEST), connection.getSetting(OperationalTextKey.DATA_DIGEST));
67          final TextRequestParser parser = (TextRequestParser) protocolDataUnit.getBasicHeaderSegment().getParser();
68  
69          final SettingsMap settings = new SettingsMap();
70          settings.add(OperationalTextKey.SEND_TARGETS, "");
71  
72          final IDataSegment dataSegment = DataSegmentFactory.create(settings.asByteBuffer(), DataSegmentFormat.TEXT, connection.getSettingAsInt(OperationalTextKey.MAX_RECV_DATA_SEGMENT_LENGTH));
73  
74          int bytes2Process = dataSegment.getLength();
75          for (IDataSegmentIterator dataSegmentIterator = dataSegment.iterator(); dataSegmentIterator.hasNext();) {
76              IDataSegmentChunk dataSegmentChunk = dataSegmentIterator.next(bytes2Process);
77              protocolDataUnit.setDataSegment(dataSegmentChunk);
78              parser.setTargetTransferTag(0xFFFFFFFF);
79          }
80  
81          connection.send(protocolDataUnit);
82          connection.nextState(new GetConnectionsResponseState(connection));
83          super.stateFollowing = true;
84          // return true;
85      }
86  
87      // --------------------------------------------------------------------------
88      // --------------------------------------------------------------------------
89      // --------------------------------------------------------------------------
90      // --------------------------------------------------------------------------
91  
92  }