<commit_before>/*
 * Copyright (C) 2014 Prometheus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package comunicador;

import com.rti.dds.dynamicdata.DynamicData;
import es.prometheus.dds.DiscoveryChange;
import es.prometheus.dds.DiscoveryChangeStatus;
import es.prometheus.dds.DiscoveryData;
import es.prometheus.dds.DiscoveryListener;
import es.prometheus.dds.Escritor;
import es.prometheus.dds.LectorBase;
import es.prometheus.dds.TopicoControl;
import es.prometheus.dds.TopicoControlFactoria;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servidor de localización por habitación.
 */
public class Servidor extends Thread {
    private static final String SCRIPT_PATH = "detectarcamara.m";
    private static final String FUNC_NAME = "detectarcamara";
    private static final String VIDEO_TOPIC_NAME = "VideoDataTopic";
    private static final String CHILD_TOPIC_NAME = "ChildDataTopic";
    private static final int MAX_TIME = 20*1000;
    
    private final Map<String, ArrayList<DatosSensor>> datosNinos = new HashMap<>();
    private final List<DiscoveryData> ninoSubs = new ArrayList<>();
    private final List<DatosCamara> camaraPubs = new ArrayList<>();
    
    private final String sala;
    private final double ancho;
    private final double largo;
    
    private TriangulacionOctave triangulacion;
    private TopicoControl controlNino;
    private Escritor escritorNino;
    private DynamicData escritorData;
    private TopicoControl controlSensor;
    private LectorBase lectorSensor;
    
    public Servidor(final String sala, double ancho, double largo) {
        this.sala  = sala;
        this.ancho = ancho;
        this.largo = largo;
    }
        
    /**
     * Inicia el programa.
     * 
     * @param args Uno: el nombre de la sala.
     * Dos: Ancho de la sala.
     * Tres: Largo de la sala.
     */
    public static void main(String[] args) {
        if (args.length != 3)
            return;
        
        // Creamos el comunicador de sensor
        double ancho = Double.parseDouble(args[1]);
        double largo = Double.parseDouble(args[2]);
        Servidor servidor = new Servidor(args[0], ancho, largo);
        servidor.start();
        
        // Creamos una hebra para salidas forzosas (Control+C).
    	Runtime.getRuntime().addShutdownHook(new ShutdownThread(servidor));     
    }

    @Override
    public void run() {
        // Inicializa DDS
        this.iniciaDds();
        
        // Inicializa la triangulación
        this.triangulacion = new TriangulacionOctave(
                SCRIPT_PATH,
                FUNC_NAME,
                this.camaraPubs,
                this.ancho,
                this.largo,
                false
        );
    }
    
    /**
     * Libera recursos.
     */
    public void dispose() {
        this.lectorSensor.dispose();
        this.controlSensor.dispose();
        this.controlNino.dispose();
        this.triangulacion.close();
    }
    
    /**
     * Inicializa la entidades de DDS.
     */
    private void iniciaDds() {
        // Inicia las entidades del tópico de sensores:
        // Recopilar todos los datos de una sala (filtrado por sala).
        this.controlSensor = TopicoControlFactoria.crearControlDinamico(
                "MisParticipantes::ParticipanteServidor",
                "SensorDataTopic");
        String[] params = new String[] { "'" + this.sala + "'" };
        this.lectorSensor = new LectorBase(controlSensor, "sala =  %0", params) {
            @Override
            protected void getDatos(DynamicData sample) {
                onSensorDataReceived(sample);
            }
        };
        
        // Inicia las entidades del tópico de niños:
        // Publicar datos de triangulación de cada niño.
        this.controlNino = TopicoControlFactoria.crearControlDinamico(<commit_msg>Comunicador: Ejecución de triangulación en nueva hebra para no bloquear.<commit_after>/*
 * Copyright (C) 2014 Prometheus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package comunicador;

import com.rti.dds.dynamicdata.DynamicData;
import es.prometheus.dds.DiscoveryChange;
import es.prometheus.dds.DiscoveryChangeStatus;
import es.prometheus.dds.DiscoveryData;
import es.prometheus.dds.DiscoveryListener;
import es.prometheus.dds.Escritor;
import es.prometheus.dds.LectorBase;
import es.prometheus.dds.TopicoControl;
import es.prometheus.dds.TopicoControlFactoria;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servidor de localización por habitación.
 */
public class Servidor extends Thread {
    private static final String SCRIPT_PATH = "detectarcamara.m";
    private static final String FUNC_NAME = "detectarcamara";
    private static final String VIDEO_TOPIC_NAME = "VideoDataTopic";
    private static final String CHILD_TOPIC_NAME = "ChildDataTopic";
    private static final int MAX_TIME = 20*1000;
    
    private final Map<String, ArrayList<DatosSensor>> datosNinos = new HashMap<>();
    private final List<DiscoveryData> ninoSubs = new ArrayList<>();
    private final List<DatosCamara> camaraPubs = new ArrayList<>();
    
    private final String sala;
    private final double ancho;
    private final double largo;
    
    private TriangulacionOctave triangulacion;
    private TopicoControl controlNino;
    private Escritor escritorNino;
    private DynamicData escritorData;
    private TopicoControl controlSensor;
    private LectorBase lectorSensor;
    
    public Servidor(final String sala, double ancho, double largo) {
        this.sala  = sala;
        this.ancho = ancho;
        this.largo = largo;
    }
        
    /**
     * Inicia el programa.
     * 
     * @param args Uno: el nombre de la sala.
     * Dos: Ancho de la sala.
     * Tres: Largo de la sala.
     */
    public static void main(String[] args) {
        if (args.length != 3)
            return;
        
        // Creamos el comunicador de sensor
        double ancho = Double.parseDouble(args[1]);
        double largo = Double.parseDouble(args[2]);
        Servidor servidor = new Servidor(args[0], ancho, largo);
        servidor.start();
        
        // Creamos una hebra para salidas forzosas (Control+C).
    	Runtime.getRuntime().addShutdownHook(new ShutdownThread(servidor));     
    }

    @Override
    public void run() {
        // Inicializa DDS
        this.iniciaDds();
        
        // Inicializa la triangulación
        this.triangulacion = new TriangulacionOctave(
                SCRIPT_PATH,
                FUNC_NAME,
                this.camaraPubs,
                this.ancho,
                this.largo,
                false
        );
    }
    
    /**
     * Libera recursos.
     */
    public void dispose() {
        this.lectorSensor.dispose();
        this.controlSensor.dispose();
        this.controlNino.dispose();
        this.triangulacion.close();
    }
    
    /**
     * Inicializa la entidades de DDS.
     */
    private void iniciaDds() {
        // Inicia las entidades del tópico de sensores:
        // Recopilar todos los datos de una sala (filtrado por sala).
        this.controlSensor = TopicoControlFactoria.crearControlDinamico(
                "MisParticipantes::ParticipanteServidor",
                "SensorDataTopic");
        String[] params = new String[] { "'" + this.sala + "'" };
        this.lectorSensor = new LectorBase(controlSensor, "sala =  %0", params) {
            @Override
            protected void getDatos(final DynamicData sample) {
                // Ejecuto todo en una nueva hebra para que no se bloque DDS
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        onSensorDataReceived(sample);
                    }
                });
                t.start();
            }
        };
        
        // Inicia las entidades del tópico de niños:
        // Publicar datos de triangulación de cada niño.
        this.controlNino = TopicoControlFactoria.crearControlDinamico(
{'fraction_non_alphanumeric': 0.0835259508300063, 'fraction_numerical': 0.006303845345660853, 'mean_word_length': 2.457682528151108, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1547602', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2726, 'n_words': 827}