/*
 * Copyright 2016-2020 Chronicle Software
 *
 * https://chronicle.software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.openhft.chronicle.network;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.network.api.TcpHandler;
import net.openhft.chronicle.network.api.session.SessionDetailsProvider;
import net.openhft.chronicle.network.api.session.SubHandler;
import net.openhft.chronicle.wire.*;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

public class HeaderTcpHandler<T extends NetworkContext<T>> implements TcpHandler<T> {

    public static final String HANDLER = "handler";
    private static final Logger LOG = LoggerFactory.getLogger(HeaderTcpHandler.class);
    @NotNull
    private final TcpEventHandler<T> handlerManager;
    @NotNull
    private final Function<Object, TcpHandler<T>> handlerFunction;
    private AtomicBoolean isClosed = new AtomicBoolean();

    public HeaderTcpHandler(@NotNull final TcpEventHandler<T> handlerManager,
                            @NotNull final Function<Object, TcpHandler<T>> handlerFunction) {
        this.handlerManager = handlerManager;
        this.handlerFunction = handlerFunction;
    }

    @Override
    public void process(@NotNull Bytes in, @NotNull Bytes out, T nc) {

        assert nc.wireType() != null;

        // the type of the header
        final Wire inWire = nc.wireType().apply(in);

        long start = in.readPosition();

        Object o = null;
        try (final DocumentContext dc = inWire.readingDocument()) {

            if (!dc.isPresent())
                return;

            if (YamlLogging.showServerReads())
                LOG.info("nc.isAcceptor=" + nc.isAcceptor() +
                        ", read:\n" + Wires.fromSizePrefixedBlobs(in, start, in.readLimit() - start));
            final TcpHandler handler;
            final long readPosition = inWire.bytes().readPosition();
            @NotNull final ValueIn read = inWire.read(() -> HANDLER);

            if (dc.isMetaData() && read.isTyped())
                o = read.typedMarshallable();
            else {
                inWire.bytes().readPosition(readPosition);
                o = toSessionDetails(inWire);
            }

            handler = handlerFunction.apply(o);

            if (handler instanceof NetworkContextManager)
                ((NetworkContextManager<T>) handler).nc(nc);

            handlerManager.tcpHandler(handler);

        } catch (Exception e) {
            if (isClosed())
                return;
            if (o instanceof SubHandler) {
                Jvm.warn().on(getClass(), "SubHandler " + o + " sent before UberHandler, closing.");
                close();

            } else {
                Jvm.warn().on(getClass(), "wirein=" + Wires.fromSizePrefixedBlobs(inWire), e);
            }
        }
    }

    @NotNull
    private SessionDetailsProvider toSessionDetails(@NotNull Wire inWire) {
        @NotNull VanillaSessionDetails sd = new VanillaSessionDetails();
        sd.readMarshallable(inWire);
        return sd;
    }

    @Override
    public boolean isClosed() {
        return isClosed.get();
    }

    @Override
    public void close() {
        isClosed.set(true);
    }
}

@@ -756,16 +756,70 @@
 re.Jvm;%0A
+import net.openhft.chronicle.core.io.SimpleCloseable;%0A
 import n
@@ -1138,58 +1138,8 @@
 y;%0A%0A
-import java.util.concurrent.atomic.AtomicBoolean;%0A
 impo
@@ -1229,16 +1229,40 @@
 text%3CT%3E%3E
+ extends SimpleCloseable
  impleme
@@ -1570,66 +1570,8 @@
 ion;
-%0A    private AtomicBoolean isClosed = new AtomicBoolean();
 %0A%0A  
@@ -3719,166 +3719,7 @@
    %7D
-%0A%0A    @Override%0A    public boolean isClosed() %7B%0A        return isClosed.get();%0A    %7D%0A%0A    @Override%0A    public void close() %7B%0A        isClosed.set(true);%0A    %7D
 %0A%7D%0A

Make more resource trace whether they were closed properly or not, https://github.com/OpenHFT/Chronicle-Core/issues/116