Skip to content

Commit

Permalink
Clean relays up (#27)
Browse files Browse the repository at this point in the history
* Flatten

* Cleanup noop relay
  • Loading branch information
buinauskas authored Oct 8, 2022
1 parent 36cf27d commit f695c27
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 51 deletions.
File renamed without changes.
54 changes: 37 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@

mod error;
mod event;
pub mod relay;

mod http;
mod udp;

pub use self::error::*;
pub use self::event::*;
pub use self::relay::*;
pub use self::http::*;
pub use self::udp::*;

use std::{
hint::spin_loop,
sync::atomic::{AtomicUsize, Ordering},
};

static mut RELAY: &dyn Relay = &Noop;
static mut RELAY: &dyn Relay = &NopRelay;
static STATE: AtomicUsize = AtomicUsize::new(0);

const UNINITIALIZED: usize = 0;
Expand Down Expand Up @@ -91,30 +94,18 @@ where

fn relay() -> &'static dyn Relay {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: Noop = Noop;
static NOP: NopRelay = NopRelay;
&NOP
} else {
unsafe { RELAY }
}
}

/// Initializes [`Relay`] for the whole application
/// Sets the global [Relay]
pub fn set_relay<T: 'static + Relay>(relay: T) -> Result<(), SetRelayError> {
set_relay_inner(|| Box::leak(Box::new(relay)))
}

/// Tracks the actual event
pub fn track<T>(event: Event<T>) -> Result<(), Error>
where
T: std::fmt::Debug + serde::Serialize,
{
let event_vec = serde_json::to_vec(&event)?;

relay().transport(event.base, event_vec);

Ok(())
}

/// The type returned by [`set_relay`] if [`set_relay`] has already been called.
pub struct SetRelayError(());

Expand All @@ -131,3 +122,32 @@ impl std::fmt::Display for SetRelayError {
}

impl std::error::Error for SetRelayError {}

/// Tracks the actual event
pub fn track<T>(event: Event<T>) -> Result<(), Error>
where
T: std::fmt::Debug + serde::Serialize,
{
let event_vec = serde_json::to_vec(&event)?;

relay().transport(event.base, event_vec);

Ok(())
}

/// Trait for event transportation
pub trait Relay {
/// Accepts event, serialized in JSON, in a form of bytes.
///
/// Use these bytes to send the event over the wire using protocols, such as:
/// - HTTP
/// - TCP
/// - UDP
fn transport(&self, event_base: EventBase, event: Vec<u8>);
}

struct NopRelay;

impl Relay for NopRelay {
fn transport(&self, _: EventBase, _: Vec<u8>) {}
}
18 changes: 0 additions & 18 deletions src/relay/mod.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/relay/noop.rs

This file was deleted.

File renamed without changes.

0 comments on commit f695c27

Please sign in to comment.