<commit_before>// Copyright 2015-2016 ZFilexfer Developers. See the COPYRIGHT file at the
// top-level directory of this distribution and at
// https://intecture.io/COPYRIGHT.
//
// Licensed under the Mozilla Public License 2.0 <LICENSE or
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.

use chunk::Chunk;
use czmq::{ZMsg, ZSock, ZSys};
use error::{Error, Result};
use std::sync::{Arc, RwLock};
use std::thread::{JoinHandle, spawn};
use std::time::Instant;

#[cfg(not(test))]
const CHUNK_TIMEOUT: u64 = 60;
#[cfg(test)]
const CHUNK_TIMEOUT: u64 = 1;

pub struct Arbitrator {
    router: ZSock,
    queue: Arc<RwLock<Vec<TimedChunk>>>,
    timer_handle: Option<JoinHandle<()>>,
    timer_comm: ZSock,
    slots: u32,
}

impl Drop for Arbitrator {
    fn drop(&mut self) {
        // Ignore failure as it means the thread has already
        // terminated.
        let _ = self.timer_comm.signal(0);
        if self.timer_handle.is_some() {
            self.timer_handle.take().unwrap().join().unwrap();
        }
    }
}

impl Arbitrator {
    pub fn new(router: ZSock, upload_slots: u32) -> Result<Arbitrator> {
        let (comm_front, comm_back) = try!(ZSys::create_pipe());
        comm_front.set_sndtimeo(Some(1000));
        comm_front.set_linger(0);
        comm_back.set_rcvtimeo(Some(1000)); // Remember that this timeout controls the Timer loop speed!
        comm_back.set_linger(0);

        let lock = Arc::new(RwLock::new(Vec::new()));
        let timer = try!(Timer::new(comm_back, lock.clone()));

        Ok(Arbitrator {
            router: router,
            queue: lock,
            timer_handle: Some(spawn(move|| timer.run())),
            timer_comm: comm_front,
            slots: upload_slots,
        })
    }<commit_msg>Trivial cleanup of Arbitrator Drop trait imp<commit_after>// Copyright 2015-2016 ZFilexfer Developers. See the COPYRIGHT file at the
// top-level directory of this distribution and at
// https://intecture.io/COPYRIGHT.
//
// Licensed under the Mozilla Public License 2.0 <LICENSE or
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.

use chunk::Chunk;
use czmq::{ZMsg, ZSock, ZSys};
use error::{Error, Result};
use std::sync::{Arc, RwLock};
use std::thread::{JoinHandle, spawn};
use std::time::Instant;

#[cfg(not(test))]
const CHUNK_TIMEOUT: u64 = 60;
#[cfg(test)]
const CHUNK_TIMEOUT: u64 = 1;

pub struct Arbitrator {
    router: ZSock,
    queue: Arc<RwLock<Vec<TimedChunk>>>,
    timer_handle: Option<JoinHandle<()>>,
    timer_comm: ZSock,
    slots: u32,
}

impl Drop for Arbitrator {
    fn drop(&mut self) {
        // Ignore failure as it means the thread has already
        // terminated.
        let _ = self.timer_comm.signal(0);
        if let Some(h) = self.timer_handle.take() {
            h.join().unwrap();
        }
    }
}

impl Arbitrator {
    pub fn new(router: ZSock, upload_slots: u32) -> Result<Arbitrator> {
        let (comm_front, comm_back) = try!(ZSys::create_pipe());
        comm_front.set_sndtimeo(Some(1000));
        comm_front.set_linger(0);
        comm_back.set_rcvtimeo(Some(1000)); // Remember that this timeout controls the Timer loop speed!
        comm_back.set_linger(0);

        let lock = Arc::new(RwLock::new(Vec::new()));
        let timer = try!(Timer::new(comm_back, lock.clone()));

        Ok(Arbitrator {
            router: router,
            queue: lock,
            timer_handle: Some(spawn(move|| timer.run())),
            timer_comm: comm_front,
            slots: upload_slots,
        })
    }
{'fraction_non_alphanumeric': 0.13841961852861034, 'fraction_numerical': 0.018528610354223433, 'mean_word_length': 2.9430719656283566, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2177810', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1194, 'n_words': 308}