tatami 0.1.2

A library for creating satellites and interacting with Tatami protocols.
Documentation
use libp2p::futures::StreamExt;
use tokio_stream::wrappers::WatchStream;
use tracing::error;

mod handle_shoji_command;
mod handle_swarm_event;

use crate::Satellite;

impl Satellite {
	/// Runs the Satellite.
	/// The satellite needs to be running in order to be useful, which you can do in a thread.
	/// "Running" means listening to Swarm events and responding to Shoji commands.
	pub async fn run(mut self) -> ! {
		let mut event_stream = WatchStream::new(self.event_watcher.clone());
		loop {
			tokio::select! {
				// Swarm events
				Some(event) = self.swarm.0.next() => {
					if let Err(e) = self.handle_swarm_event(event) {
						error!("Handling swarm events failed: {}", e);
					}
				}
				// Shoji commands
				 Some(command) = self.shoji_command_reciever.recv() => {
					if let Err(e) = self.handle_shoji_command(command) {
						error!("Handling shoji's commands failed: {}", e);
					}
				}
				// Tatami Events
				Some(event) = event_stream.next() => {
					self.config.modules.iter().for_each(|module| {
						let res = module.lock();
						match res {
							Ok(mut module) => {
								module.on_events(
									event.clone(),
									&self
								);
							},
							Err(e) => {
								error!("Module mutex was poisoned: {}", e);
							},
						}
					})
				}
			}
		}
	}
}