package com.tsi.comm.server.xnet;

import lombok.Data;

@Data
public abstract class NettyServerConfig {

    protected String bindingAddr = "";
    protected int bindingPort = 9091;
    protected int backlog = 0;
    protected int acceptThreads = 0;
    protected int workerThreads = 0;
    protected int rcvBuf = 0;
    protected int sndBuf = 0;
    protected int readerIdleTimeSeconds = 0;
    protected int writerIdleTimeSeconds = 0;
    protected int allIdleTimeSeconds = 0;
    protected int connectTimeoutSeconds = 0;

    protected void configure() {
        final int NETTY_DEFAULT_EVENT_THREADS  = Runtime.getRuntime().availableProcessors() * 2;  // Netty Default == 0

        if (this.bindingAddr.isEmpty()) {
            this.bindingAddr = "0.0.0.0";
        }
        if (this.backlog == 0) {
            this.backlog = 1024;
        }

        if (this.acceptThreads == 0) {
//            this.acceptThreads = NETTY_DEFAULT_EVENT_THREADS;
            this.acceptThreads = 2;
        }
        if (this.workerThreads == 0) {
            this.workerThreads = NETTY_DEFAULT_EVENT_THREADS;
        }

        if (this.rcvBuf == 0) {
            this.rcvBuf = Short.MAX_VALUE / 2;
        }
        if (this.sndBuf == 0) {
            this.sndBuf = Short.MAX_VALUE / 2;
        }
        if (this.readerIdleTimeSeconds < 5) {
            this.readerIdleTimeSeconds = 5;
        }
    }
}
