Allow the historian to track ownership of any type of data
This commit is contained in:
parent
0443b39264
commit
c6134cc25b
|
@ -6,7 +6,7 @@ use std::thread::sleep;
|
|||
use std::time::Duration;
|
||||
use std::sync::mpsc::SendError;
|
||||
|
||||
fn create_log(hist: &Historian) -> Result<(), SendError<Event<Sha256Hash>>> {
|
||||
fn create_log(hist: &Historian<Sha256Hash>) -> Result<(), SendError<Event<Sha256Hash>>> {
|
||||
sleep(Duration::from_millis(15));
|
||||
let data = Sha256Hash::default();
|
||||
hist.sender.send(Event::Discovery { data })?;
|
||||
|
|
|
@ -9,11 +9,12 @@ use std::thread::JoinHandle;
|
|||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use log::{hash, hash_event, Entry, Event, Sha256Hash};
|
||||
use serde::Serialize;
|
||||
|
||||
pub struct Historian {
|
||||
pub sender: Sender<Event<Sha256Hash>>,
|
||||
pub receiver: Receiver<Entry<Sha256Hash>>,
|
||||
pub thread_hdl: JoinHandle<(Entry<Sha256Hash>, ExitReason)>,
|
||||
pub struct Historian<T> {
|
||||
pub sender: Sender<Event<T>>,
|
||||
pub receiver: Receiver<Entry<T>>,
|
||||
pub thread_hdl: JoinHandle<(Entry<T>, ExitReason)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
@ -21,12 +22,12 @@ pub enum ExitReason {
|
|||
RecvDisconnected,
|
||||
SendDisconnected,
|
||||
}
|
||||
fn log_event(
|
||||
sender: &Sender<Entry<Sha256Hash>>,
|
||||
fn log_event<T: Serialize + Clone>(
|
||||
sender: &Sender<Entry<T>>,
|
||||
num_hashes: &mut u64,
|
||||
end_hash: &mut Sha256Hash,
|
||||
event: Event<Sha256Hash>,
|
||||
) -> Result<(), (Entry<Sha256Hash>, ExitReason)> {
|
||||
event: Event<T>,
|
||||
) -> Result<(), (Entry<T>, ExitReason)> {
|
||||
*end_hash = hash_event(end_hash, &event);
|
||||
let entry = Entry {
|
||||
end_hash: *end_hash,
|
||||
|
@ -40,15 +41,15 @@ fn log_event(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn log_events(
|
||||
receiver: &Receiver<Event<Sha256Hash>>,
|
||||
sender: &Sender<Entry<Sha256Hash>>,
|
||||
fn log_events<T: Serialize + Clone>(
|
||||
receiver: &Receiver<Event<T>>,
|
||||
sender: &Sender<Entry<T>>,
|
||||
num_hashes: &mut u64,
|
||||
end_hash: &mut Sha256Hash,
|
||||
epoch: SystemTime,
|
||||
num_ticks: &mut u64,
|
||||
ms_per_tick: Option<u64>,
|
||||
) -> Result<(), (Entry<Sha256Hash>, ExitReason)> {
|
||||
) -> Result<(), (Entry<T>, ExitReason)> {
|
||||
use std::sync::mpsc::TryRecvError;
|
||||
loop {
|
||||
if let Some(ms) = ms_per_tick {
|
||||
|
@ -79,12 +80,12 @@ fn log_events(
|
|||
|
||||
/// A background thread that will continue tagging received Event messages and
|
||||
/// sending back Entry messages until either the receiver or sender channel is closed.
|
||||
pub fn create_logger(
|
||||
pub fn create_logger<T: 'static + Serialize + Clone + Send>(
|
||||
start_hash: Sha256Hash,
|
||||
ms_per_tick: Option<u64>,
|
||||
receiver: Receiver<Event<Sha256Hash>>,
|
||||
sender: Sender<Entry<Sha256Hash>>,
|
||||
) -> JoinHandle<(Entry<Sha256Hash>, ExitReason)> {
|
||||
receiver: Receiver<Event<T>>,
|
||||
sender: Sender<Entry<T>>,
|
||||
) -> JoinHandle<(Entry<T>, ExitReason)> {
|
||||
use std::thread;
|
||||
thread::spawn(move || {
|
||||
let mut end_hash = start_hash;
|
||||
|
@ -109,7 +110,7 @@ pub fn create_logger(
|
|||
})
|
||||
}
|
||||
|
||||
impl Historian {
|
||||
impl<T: 'static + Serialize + Clone + Send> Historian<T> {
|
||||
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
|
||||
use std::sync::mpsc::channel;
|
||||
let (sender, event_receiver) = channel();
|
||||
|
@ -157,7 +158,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_historian_closed_sender() {
|
||||
let zero = Sha256Hash::default();
|
||||
let hist = Historian::new(&zero, None);
|
||||
let hist = Historian::<u8>::new(&zero, None);
|
||||
drop(hist.receiver);
|
||||
hist.sender.send(Event::Tick).unwrap();
|
||||
assert_eq!(
|
||||
|
|
Loading…
Reference in New Issue