/*
** Module   : TransportManager.java
** Abstract : factory to return working logical connections 
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090113 Factory to return working logical connections. This hides the specific classes that are 
**                  used to implement the underlying transport.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.transport;

import com.goldencode.harness.*;

/**
 * Factory to return working logical connections. This hides the specific
 * classes that are used to implement the underlying transport.
 */
public class TransportManager
{
   /**
    * Factory method to create, connect and return a logical connection of
    * the proper type.
    *
    * @param    cfg
    *           Connection configuration.
    * @param    rc
    *           Object in which to store the details of failures.
    *
    * @return   A logical connection of the proper type or <code>null</code>
    *           on any failure.
    */
   public static Connection connect(TerminalSessionSettings cfg, FailureNotification rc)
   {
      Connection transport = null;
      
      if (cfg != null)
      {
         switch (cfg.protocol)
         {
            case TELNET:
               rc.setResult(ExitCode.INVALID_CONTENT);
               rc.setDetails("Telnet protocol is not supported.");
               break;
            case SSH2:
               transport = new SSH2Transport();
               break;
         }
         
         if (transport != null)
         {
            if (!transport.connect(cfg, rc))
            {
               // the details are already logged in rc
               transport = null;
            }
         }
      }
      else
      {
         rc.setResult(ExitCode.MISSING_CONTENT);
         rc.setDetails("Missing session configuration for connect().");
      }
      
      return transport;
   }
}
