<commit_before>/*
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * 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 opendct.sagetv;

import opendct.config.Config;
import opendct.config.ExitCode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class SageTVDiscovery implements Runnable {
    private static final Logger logger = LogManager.getLogger(SageTVDiscovery.class);
    private static AtomicBoolean running = new AtomicBoolean(false);
    private static int broadcastPort = Config.getInteger("sagetv.encoder_discovery_port", 8271);
    private boolean useLoopback = Config.getBoolean("sagetv.use_automatic_loopback", true);
    private static Thread sageTVDiscoveryThread = new Thread(new SageTVDiscovery());
    private static int encoderPort = 0;

    public static boolean isRunning() {
        return running.get();
    }

    /**
     * Starts the SageTV encoder discovery broadcast.
     */
    public static void startDiscoveryBroadcast(int encoderPort) {
        logger.entry();

        // We are only building the configuration file.
        if (Config.isConfigOnly()) {
            logger.exit();
            return;
        }

        if (running.getAndSet(true)) {
            logger.warn("Discovery broadcast is already running.");
            return;
        }

        SageTVDiscovery.encoderPort = encoderPort;
        sageTVDiscoveryThread.setName("SageTVDiscovery-" + sageTVDiscoveryThread.getId());
        sageTVDiscoveryThread.start();

        logger.exit();
    }

    /**
     * Stops the SageTV encoder discovery broadcast and blocks until the thread is stopped.
     */
    public static void stopDiscoveryBroadcast() {
        logger.entry();

        // We are only building the configuration file.
        if (Config.isConfigOnly()) {
            logger.exit();
            return;
        }

        try {<commit_msg>Run the SageTV discovery thread as a daemon so it can't hang the shutdown process.<commit_after>/*
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * 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 opendct.sagetv;

import opendct.config.Config;
import opendct.config.ExitCode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class SageTVDiscovery implements Runnable {
    private static final Logger logger = LogManager.getLogger(SageTVDiscovery.class);
    private static AtomicBoolean running = new AtomicBoolean(false);
    private static int broadcastPort = Config.getInteger("sagetv.encoder_discovery_port", 8271);
    private boolean useLoopback = Config.getBoolean("sagetv.use_automatic_loopback", true);
    private static Thread sageTVDiscoveryThread = new Thread(new SageTVDiscovery());
    private static int encoderPort = 0;

    public static boolean isRunning() {
        return running.get();
    }

    /**
     * Starts the SageTV encoder discovery broadcast.
     */
    public static void startDiscoveryBroadcast(int encoderPort) {
        logger.entry(encoderPort);

        // We are only building the configuration file.
        if (Config.isConfigOnly()) {
            logger.exit();
            return;
        }

        if (running.getAndSet(true)) {
            logger.warn("Discovery broadcast is already running.");
            return;
        }

        SageTVDiscovery.encoderPort = encoderPort;
        sageTVDiscoveryThread.setName("SageTVDiscovery-" + sageTVDiscoveryThread.getId());
        sageTVDiscoveryThread.setDaemon(true);
        sageTVDiscoveryThread.start();

        logger.exit();
    }

    /**
     * Stops the SageTV encoder discovery broadcast and blocks until the thread is stopped.
     */
    public static void stopDiscoveryBroadcast() {
        logger.entry();

        // We are only building the configuration file.
        if (Config.isConfigOnly()) {
            logger.exit();
            return;
        }

        try {
{'fraction_non_alphanumeric': 0.08509077571978728, 'fraction_numerical': 0.005501558774986246, 'mean_word_length': 3.1255673222390317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5894101', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1430, 'n_words': 463}