view src/org/tn5250j/framework/transport/SocketConnector.java @ 354:3c16508419fe

update jsocks to 2011-03-19
author Carl Byington <carl@five-ten-sg.com>
date Fri, 01 Aug 2014 11:39:46 -0700
parents 77ac18bc1b2f
children
line wrap: on
line source


/**
 * @(#)SocketConnector.java
 * @author Stephen M. Kennedy
 *
 * Copyright:    Copyright (c) 2001
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */
package org.tn5250j.framework.transport;

import java.net.Socket;

import com.five_ten_sg.connectbot.service.TerminalBridge;
import com.five_ten_sg.connectbot.service.TerminalManager;

import org.tn5250j.framework.transport.SSL.SSLImplementation;
import org.tn5250j.TN5250jConstants;
import android.util.Log;


public class SocketConnector {
    private static final String TAG = "SocketConnector";

    /**
     * Creates a new instance that creates a plain socket by default.
     */
    public SocketConnector() {
    }

    /**
     * Create a new client Socket to the given destination, port and sslType of
     * encryption.
     * @param destination
     * @param port
     * @return a new client socket, or null if
     */
    public Socket createSocket(String destination, int port, String sslType, String homeDirectory, TerminalBridge bridge, TerminalManager manager) {
        Socket socket = null;
        Exception ex = null;

        if (sslType == null || sslType.trim().length() == 0 ||
                sslType.toUpperCase().equals(TN5250jConstants.SSL_TYPE_NONE)) {
            Log.i(TAG, "Creating Plain Socket");

            try {
                // Use Socket Constructor!!! SocketFactory for jdk 1.4
                socket = new Socket(destination, port);
            }
            catch (Exception e) {
                ex = e;
            }
        }
        else {    //SSL SOCKET
            Log.i(TAG, "Creating SSL [" + sslType + "] Socket");
            SSLInterface sslIf = null;

            try {
                sslIf = (SSLInterface) new SSLImplementation(bridge, manager);
            }
            catch (Exception e) {
                ex = new Exception("Failed to create SSLInterface Instance. " +
                                   "Message is [" + e.getMessage() + "]");
            }

            if (sslIf != null) {
                sslIf.init(sslType, homeDirectory);
                socket = sslIf.createSSLSocket(destination, port);
            }
        }

        if (ex != null) {
            Log.e(TAG, "exception", ex);
        }

        if (socket == null) {
            Log.w(TAG, "No socket was created");
        }

        return socket;
    }


}