package com.jscape.util.net.connection;

import com.jscape.util.SuppressObfuscation;
import com.jscape.util.net.TransportAddress;

import java.io.IOException;
import java.util.Map;

/**
 * @author Alex Usun
 */
public interface Connection
        extends AutoCloseable {

    long creationTime();

    TransportAddress localAddress();

    TransportAddress remoteAddress();

    Map<Object, Object> attributes();

    boolean closed();

    void close();

    @SuppressObfuscation
    class ConnectionException
            extends IOException {

        private static final long serialVersionUID = 4758387160856108206L;

        public static ConnectionException wrap(Throwable e) {
            return e instanceof ConnectionException ? (ConnectionException) e : new ConnectionException(e);
        }

        public ConnectionException() {
        }

        public ConnectionException(String message) {
            super(message);
        }

        public ConnectionException(String message, Throwable cause) {
            super(message, cause);
        }

        public ConnectionException(Throwable cause) {
            super(cause.getMessage() != null ? cause.getMessage() : cause.toString(), cause);
        }
    }

    @SuppressObfuscation
    class ConnectionClosedException
            extends ConnectionException {

        private static final long serialVersionUID = -829334007397425715L;
    }

    @SuppressObfuscation
    class EOFException
            extends ConnectionException {

    }

    @SuppressObfuscation
    class ReadTimeoutException
            extends ConnectionException {

        private static final long serialVersionUID = 2205265655191868904L;

        public ReadTimeoutException() {
        }

        public ReadTimeoutException(String message) {
            super(message);
        }

        public ReadTimeoutException(String message, Throwable cause) {
            super(message, cause);
        }

        public ReadTimeoutException(Throwable cause) {
            super(cause.getMessage() != null ? cause.getMessage() : cause.toString(), cause);
        }
    }

    @SuppressObfuscation
    class WriteException
            extends ConnectionException {

        private static final long serialVersionUID = -4356467152902486156L;

        public WriteException() {
        }

        public WriteException(String message) {
            super(message);
        }

        public WriteException(String message, Throwable cause) {
            super(message, cause);
        }

        public WriteException(Throwable cause) {
            super(cause.getMessage() != null ? cause.getMessage() : cause.toString(), cause);
        }
    }
}
