/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2026 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use std::sync::Arc;
use smol::{future::Future, lock::Mutex, Executor, Task};
use tracing::{debug, trace};
use super::super::channel::ChannelPtr;
use crate::Result;
/// Pointer to protocol jobs manager
pub type ProtocolJobsManagerPtr = Arc;
pub struct ProtocolJobsManager {
name: &'static str,
channel: ChannelPtr,
tasks: Mutex>>>,
}
impl ProtocolJobsManager {
/// Create a new protocol jobs manager
pub fn new(name: &'static str, channel: ChannelPtr) -> ProtocolJobsManagerPtr {
Arc::new(Self { name, channel, tasks: Mutex::new(vec![]) })
}
/// Returns configured name
pub fn name(self: Arc) -> &'static str {
self.name
}
/// Runs the task on an executor
pub fn start(self: Arc, executor: Arc>) {
executor.spawn(self.handle_stop()).detach()
}
/// Spawns a new task and adds it to the internal queue
pub async fn spawn<'a, F>(&self, future: F, executor: Arc>)
where
F: Future