<commit_before>/* File:        $Id$
 * Date:        $Date$
 * Revision:    $Revision$
 * Author:      $Author$
 *
 * The Netarchive Suite - Software to harvest and preserve websites
 * Copyright 2004-2010 <NAME> and Statsbiblioteket, Denmark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 
 *  USA
 */
package dk.netarkivet.archive.bitarchive;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dk.netarkivet.archive.ArchiveSettings;
import dk.netarkivet.common.distribute.ChannelID;
import dk.netarkivet.common.distribute.RemoteFile;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.utils.CleanupIF;
import dk.netarkivet.common.utils.ExceptionUtils;
import dk.netarkivet.common.utils.FileUtils;
import dk.netarkivet.common.utils.Settings;
import dk.netarkivet.common.utils.StringUtils;
import dk.netarkivet.common.utils.batch.FileBatchJob;

/**
 * Class representing the monitor for bitarchives. The monitor is used for
 * sending out and combining the results of executing batch jobs.
 *
 * Registers outgoing batchjobs to bitarchives, and handles replies from
 * bitarchives, finally notifying observers when all bitarchives have replied,
 * or when the batch times out, after a time specified in settings.
 *
 * We wait for replies from bitarchives that are considered live when the batch
 * begins. A bitarchive is considered live if we have heard any activity from it
 * within a time specified in settings.
 */
public class BitarchiveMonitor extends Observable implements CleanupIF {
    /**
     * The current instance.
     */
    private static BitarchiveMonitor instance;
    
    /**
     * The time of the latest sign of life received from each bitarchive.
     */
    private Map<String, Long> bitarchiveSignsOfLife =
            Collections.synchronizedMap(new HashMap<String, Long>());

    /**
     * The acceptable delay in milliseconds between signs of life.
     */
    private final long acceptableSignOfLifeDelay;


    /**
     * Map from the ID of batch jobs sent to bitarchives, to tuple class of
     * status for this batch job. The Map contains all batch jobs currently
     * running.
     */
    private Map<String, BatchJobStatus> runningBatchJobs =
            Collections.synchronizedMap(new HashMap<String, BatchJobStatus>());

    /**
     * The timer for keeping track of running batchjobs.
     */
    protected Timer batchTimer = new Timer(true);
    <commit_msg>Followup to NS-173<commit_after>/* File:        $Id$
 * Date:        $Date$
 * Revision:    $Revision$
 * Author:      $Author$
 *
 * The Netarchive Suite - Software to harvest and preserve websites
 * Copyright 2004-2010 <NAME> and Statsbiblioteket, Denmark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 
 *  USA
 */
package dk.netarkivet.archive.bitarchive;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dk.netarkivet.archive.ArchiveSettings;
import dk.netarkivet.common.distribute.ChannelID;
import dk.netarkivet.common.distribute.RemoteFile;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.utils.CleanupIF;
import dk.netarkivet.common.utils.ExceptionUtils;
import dk.netarkivet.common.utils.FileUtils;
import dk.netarkivet.common.utils.Settings;
import dk.netarkivet.common.utils.StringUtils;
import dk.netarkivet.common.utils.batch.FileBatchJob;

/**
 * Class representing the monitor for bitarchives. The monitor is used for
 * sending out and combining the results of executing batch jobs.
 *
 * Registers outgoing batchjobs to bitarchives, and handles replies from
 * bitarchives, finally notifying observers when all bitarchives have replied,
 * or when the batch times out, after a time specified in settings.
 *
 * We wait for replies from bitarchives that are considered live when the batch
 * begins. A bitarchive is considered live if we have heard any activity from it
 * within a time specified in settings.
 */
public class BitarchiveMonitor extends Observable implements CleanupIF {
    /**
     * The current instance.
     */
    private static BitarchiveMonitor instance;
    
    /**
     * The time of the latest sign of life received from each bitarchive.
     */
    private Map<String, Long> bitarchiveSignsOfLife =
            Collections.synchronizedMap(new HashMap<String, Long>());

    /**
     * The acceptable delay in milliseconds between signs of life.
     */
    private final long acceptableSignOfLifeDelay;


    /**
     * Map from the ID of batch jobs sent to bitarchives, to tuple class of
     * status for this batch job. The Map contains all batch jobs currently
     * running.
     */
    private Map<String, BatchJobStatus> runningBatchJobs =
            Collections.synchronizedMap(new HashMap<String, BatchJobStatus>());

    /**
     * Whether the timer will be created as a daemon-thread.
     */
    private static final boolean IS_DAEMON = true;
    
    /**
     * The timer for keeping track of running batchjobs.
     */
    protected final Timer batchTimer = new Timer(IS_DAEMON);
    
{'fraction_non_alphanumeric': 0.07617372480043295, 'fraction_numerical': 0.006088485996482208, 'mean_word_length': 3.7536977491961414, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7587793', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2010, 'n_words': 768}