@Override
    public void setPrimaryChannel(Channel primary_channel) {
        if ((this.primaryChannel == null) && (tcpChannel == null)) {
            if (primary_channel != null) {
                this.primaryChannel = primary_channel;
                tcpChannel = primary_channel;
                embededChannel.attr(IS_PRIMARY).set(true);
            }
        } else if ((this.primaryChannel == null) ||
            ((tcpChannel == secondaryChannel)) && (tcpChannel.isActive())) {
            this.primaryChannel = primary_channel;

        } else if ((!this.primaryChannel.isActive()) && (tcpChannel == secondaryChannel)) {
            this.primaryChannel = primary_channel;

        } else if (((!this.primaryChannel.isActive()) && (tcpChannel == this.primaryChannel)) ||
            (primary_channel.isActive())) {
            synchronized (tcpChannel) {                
                tcpChannel.close();
                this.primaryChannel = primary_channel;
                tcpChannel = primary_channel;
                embededChannel.attr(IS_PRIMARY).set(true);

                if (tcpChannel.isActive()) {
                    logger.info("Reassigns the inactive primary channel and send ConnectEvent..");
                    embedCtx.fireUserEventTriggered(new ConnectEvent());
                }
            }
        } else if (primary_channel.isActive()) {
            synchronized (tcpChannel) {
                tcpChannel.close();
                this.primaryChannel = primary_channel;
                tcpChannel = primary_channel;
                embededChannel.attr(IS_PRIMARY).set(true);
                logger.info("Reassigns the primary channel and send ConnectEvent.");
                if (tcpChannel.isActive()) {
                    embedCtx.fireUserEventTriggered(new ConnectEvent());
                }
            }            
        }
    }
Implementation of a multiplexing channel, from an embedded channel to two
possible TCP connections, primary and secondary.
The objective is to allow connections to individual systems
with a two CP (PN CPUs, CP343-1, CP443-1 or similar), or H-type systems
(S7-400H or S7-1500H).
The user App must be in charge of restoring the requests or
subscriptions that it is requesting.
plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java