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;
20  
21  
22  import java.net.InetSocketAddress;
23  import java.util.concurrent.Executors;
24  
25  import org.jscsi.exception.NoSuchSessionException;
26  import org.jscsi.initiator.connection.Connection;
27  import org.jscsi.initiator.connection.Session;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * <h1>LinkFactory</h1>
34   * <p>
35   * Implements a Factory which enables the Initiator to switch between multithreaded and singlethreaded modes.
36   * </p>
37   * 
38   * @author Patrice Matthias Brend'amour
39   */
40  
41  public final class LinkFactory {
42  
43      // --------------------------------------------------------------------------
44      // --------------------------------------------------------------------------
45  
46      /** The logger interface. */
47      private static final Logger LOGGER = LoggerFactory.getLogger(LinkFactory.class);
48  
49      /** The calling Initiator. */
50      private final Initiator initiator;
51  
52      // --------------------------------------------------------------------------
53      // --------------------------------------------------------------------------
54  
55      /**
56       * Default Constructor to create a Linkfactory. Creates everything MultiThreaded and MultiConnectioned
57       * 
58       * @param initiat The calling Initiator
59       */
60  
61      public LinkFactory (final Initiator initiat) {
62  
63          this.initiator = initiat;
64      }
65  
66      /**
67       * Method to create and return a new, empty <code>Session</code> object with the configured layer of threading.
68       * 
69       * @param initConfiguration The configuration to use within this session.
70       * @param initTargetName The name of the iSCSI Target.
71       * @param inetAddress The <code>InetAddress</code> of the leading connection of this session.
72       * @return AbsSession The SessionObject.
73       */
74      public final Session getSession (final Configuration initConfiguration, final String initTargetName, final InetSocketAddress inetAddress) {
75  
76          try {
77              // Create a new Session
78              final Session newSession = new Session(this, initConfiguration, initTargetName, inetAddress, Executors.newSingleThreadExecutor());
79              return newSession;
80          } catch (Exception e) {
81              LOGGER.error("This exception is thrown: " + e);
82              e.printStackTrace();
83              return null;
84          }
85  
86      }
87  
88      /**
89       * Method to create and return a new, empty <code>Connection</code> object with the configured layer of threading.
90       * 
91       * @param session Reference to the <code>AbsSession</code> object, which contains this connection.
92       * @param initConfiguration The configuration to use within this connection.
93       * @param inetAddress The <code>InetSocketAddress</code> to which this connection should established.
94       * @param initConnectionID The ID of this connection.
95       * @return AbsConnection The Connection Object.
96       */
97      public final Connection getConnection (final Session session, final Configuration initConfiguration, final InetSocketAddress inetAddress, final short initConnectionID) {
98  
99          try {
100             final Connection newConnection = new Connection(session, initConfiguration, inetAddress, initConnectionID);
101             return newConnection;
102         } catch (Exception e) {
103             LOGGER.error("This exception is thrown: " + e);
104             e.printStackTrace();
105             return null;
106         }
107     }
108 
109     // --------------------------------------------------------------------------
110     // --------------------------------------------------------------------------
111 
112     /**
113      * Adds a dying <code>Session</code> instance to the Queue.
114      * 
115      * @param session The name of the session, which instance you want.
116      */
117     public final void closedSession (final Session session) {
118 
119         try {
120             initiator.removeSession(session);
121         } catch (NoSuchSessionException e) {
122             // DO NOTHING
123         }
124 
125     }
126 }