<commit_before>package org.hcjf.io.net.broadcast;

import org.hcjf.bson.BsonDecoder;
import org.hcjf.bson.BsonDocument;
import org.hcjf.bson.BsonEncoder;
import org.hcjf.io.net.InetPortProvider;
import org.hcjf.log.Log;
import org.hcjf.properties.SystemProperties;
import org.hcjf.service.Service;
import org.hcjf.utils.Introspection;

import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.*;

/**
 * @author javaito
 */
public class BroadcastService extends Service<BroadcastConsumer> {

    private static final String IMPLEMENTATION_FIELD_NAME = "implementation";
    private static final String IP_PROTOCOL_VERSION_4 = "4";
    private static final String IP_PROTOCOL_VERSION_6 = "6";

    private static final BroadcastService instance;

    private Map<String, BroadcastInterface> interfaces;
    private BroadcastSender sender;
    private Map<String, BroadcastReceiver> receivers;
    private Map<String, BroadcastConsumer> consumers;
    private boolean shuttingDown;
    private MessageDigest messageDigest;


    static {
        instance = new BroadcastService(SystemProperties.get(SystemProperties.Net.Broadcast.SERVICE_NAME));
    }

    private BroadcastService(String serviceName) {
        super(serviceName, 2);
    }

    public static BroadcastService getInstance() {
        return instance;
    }

    @Override
    protected void init() {
        this.interfaces = new HashMap<>();
        this.consumers = new HashMap<>();
        this.sender = new BroadcastSender();
        this.receivers = new HashMap<>();
        try {
            this.messageDigest = MessageDigest.getInstance(SystemProperties.get(SystemProperties.Net.Broadcast.SIGNATURE_ALGORITHM));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Broadcast signature algorithm not found", e);
        }
        fork(sender);
    }

    public boolean isShuttingDown() {
        return shuttingDown;
    }

    @Override
    protected void shutdown(ShutdownStage stage) {
        this.shuttingDown = true;
    }

    @Override
    public void registerConsumer(BroadcastConsumer consumer) {
        this.consumers.put(consumer.getTaskName(), consumer);
    }

    @Override
    public void unregisterConsumer(BroadcastConsumer consumer) {
        this.consumers.remove(consumer);
    }

    private synchronized BroadcastInterface getBroadcastInterface(BroadcastConsumer consumer) throws SocketException {
        Integer port = consumer.getBasePort();
        String interfaceId = BroadcastInterface.createInterfaceId(consumer.getNetInterfaceName(), port);
        BroadcastInterface result = interfaces.get(interfaceId);
        if(result == null) {

            Enumeration interfacesEnumeration = NetworkInterface.getNetworkInterfaces();
            boolean done = false;
            while(interfacesEnumeration.hasMoreElements()){
                NetworkInterface networkInterface = (NetworkInterface) interfacesEnumeration.nextElement();

                if(networkInterface.getName().equals(consumer.getNetInterfaceName())){

                    for(InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()){

                        switch(consumer.getIpVersion()) {
                            case IP_PROTOCOL_VERSION_4: {
                                if(interfaceAddress.getAddress() instanceof Inet4Address){
                                    result = new BroadcastInterface(
                                            consumer.getNetInterfaceName(), port,
                                            interfaceAddress.getAddress(), interfaceAddress.getBroadcast());
                                    interfaces.put(result.getId(), result);
                                    fork(receivers.put(result.getId(), new BroadcastReceiver(result)));
                                    done = true;
                                }
                                break;
                            }
                            case IP_PROTOCOL_VERSION_6: {
                                if(interfaceAddress.getAddress() instanceof Inet6Address){
                                    result = new BroadcastInterface(
                                            consumer.getNetInterfaceName(), port,
                                            interfaceAddress.getAddress(), interfaceAddress.getBroadcast());
                                    interfaces.put(result.getId(), result);
                                    fork(receivers.put(result.getId(), new BroadcastReceiver(result)));
                                    done = true;
                                }
                                break;
                            }<commit_msg>Broadcast service ping test<commit_after>package org.hcjf.io.net.broadcast;

import org.hcjf.bson.BsonDecoder;
import org.hcjf.bson.BsonDocument;
import org.hcjf.bson.BsonEncoder;
import org.hcjf.io.net.InetPortProvider;
import org.hcjf.log.Log;
import org.hcjf.properties.SystemProperties;
import org.hcjf.service.Service;
import org.hcjf.utils.Introspection;

import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.*;

/**
 * @author javaito
 */
public class BroadcastService extends Service<BroadcastConsumer> {

    private static final String IMPLEMENTATION_FIELD_NAME = "implementation";
    private static final String IP_PROTOCOL_VERSION_4 = "4";
    private static final String IP_PROTOCOL_VERSION_6 = "6";

    private static final BroadcastService instance;

    private Map<String, BroadcastInterface> interfaces;
    private BroadcastSender sender;
    private Map<String, BroadcastReceiver> receivers;
    private Map<String, BroadcastConsumer> consumers;
    private boolean shuttingDown;
    private MessageDigest messageDigest;


    static {
        instance = new BroadcastService(SystemProperties.get(SystemProperties.Net.Broadcast.SERVICE_NAME));
    }

    private BroadcastService(String serviceName) {
        super(serviceName, 2);
    }

    public static BroadcastService getInstance() {
        return instance;
    }

    @Override
    protected void init() {
        this.interfaces = new HashMap<>();
        this.consumers = new HashMap<>();
        this.sender = new BroadcastSender();
        this.receivers = new HashMap<>();
        try {
            this.messageDigest = MessageDigest.getInstance(SystemProperties.get(SystemProperties.Net.Broadcast.SIGNATURE_ALGORITHM));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Broadcast signature algorithm not found", e);
        }
        fork(sender);
    }

    public boolean isShuttingDown() {
        return shuttingDown;
    }

    @Override
    protected void shutdown(ShutdownStage stage) {
        this.shuttingDown = true;
    }

    @Override
    public void registerConsumer(BroadcastConsumer consumer) {
        this.consumers.put(consumer.getTaskName(), consumer);
    }

    @Override
    public void unregisterConsumer(BroadcastConsumer consumer) {
        this.consumers.remove(consumer);
    }

    private synchronized BroadcastInterface getBroadcastInterface(BroadcastConsumer consumer) throws SocketException {
        Integer port = consumer.getBasePort();
        String interfaceId = BroadcastInterface.createInterfaceId(consumer.getNetInterfaceName(), port);
        BroadcastInterface result = interfaces.get(interfaceId);
        if(result == null) {

            Enumeration interfacesEnumeration = NetworkInterface.getNetworkInterfaces();
            boolean done = false;
            while(interfacesEnumeration.hasMoreElements()){
                NetworkInterface networkInterface = (NetworkInterface) interfacesEnumeration.nextElement();

                if(networkInterface.getName().equals(consumer.getNetInterfaceName())){

                    for(InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()){

                        switch(consumer.getIpVersion()) {
                            case IP_PROTOCOL_VERSION_4: {
                                if(interfaceAddress.getAddress() instanceof Inet4Address){
                                    result = new BroadcastInterface(
                                            consumer.getNetInterfaceName(), port,
                                            interfaceAddress.getAddress(), interfaceAddress.getBroadcast());
                                    interfaces.put(result.getId(), result);
                                    BroadcastReceiver broadcastReceiver = new BroadcastReceiver(result);
                                    receivers.put(result.getId(), broadcastReceiver);
                                    fork(broadcastReceiver);
                                    done = true;
                                }
                                break;
                            }
                            case IP_PROTOCOL_VERSION_6: {
                                if(interfaceAddress.getAddress() instanceof Inet6Address){
                                    result = new BroadcastInterface(
                                            consumer.getNetInterfaceName(), port,
                                            interfaceAddress.getAddress(), interfaceAddress.getBroadcast());
                                    interfaces.put(result.getId(), result);
                                    BroadcastReceiver broadcastReceiver = new BroadcastReceiver(result);
                                    receivers.put(result.getId(), broadcastReceiver);
                                    fork(broadcastReceiver);
                                    done = true;
                                }
                                break;
                            }
{'fraction_non_alphanumeric': 0.07874328892423942, 'fraction_numerical': 0.001789620202823623, 'mean_word_length': 2.0334740651387215, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '979829', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2271, 'n_words': 469}