/*
 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.net;

/*
 * This is custom SocketImplFactory implementation to create custom socket instances.
 * It is needed to simulate connections delay on Java sockets layer.
 *
 */
public class CustomSocketImplFactory implements SocketImplFactory {

    /*
     * Connection delay (in millis)
     */
    private int connectionDelay = 0;

    /*
     * Connection delay is simulated if there is specified class in stack trace
     */
    private String className;

    public CustomSocketImplFactory() {
    }

    /*
     * Constructor
     *
     * @param connectionDelay Connection delay (in seconds)
     * 
     * @param className Class name
     */
    public CustomSocketImplFactory(int connectionDelay, String className) {
        this.className = className;
        setConnectionDelay(connectionDelay);
    }

    @Override
    public SocketImpl createSocketImpl() {
        return new CustomPlainSocketImpl(connectionDelay, className);
    }

    /*
     * Set connection delay (in seconds)
     */
    public void setConnectionDelay(int connectionDelay) {
        this.connectionDelay = connectionDelay * 1000;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}
