dhust/
lib.rs

1use std::{time::Duration, sync::{Arc, Mutex}, net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4}};
2use tokio::time::sleep;
3use log::trace;
4
5mod chord_node;
6
7/// Wrapper around a chord node 
8pub struct Chord {
9    data: Arc<Mutex<chord_node::ChordNode>>
10}
11
12impl Chord {
13    pub async fn new() -> Self {
14        let c = Arc::new(Mutex::new( chord_node::ChordNode {  } ));
15
16        let c_ff = Arc::clone(&c); 
17        let c_st = Arc::clone(&c); 
18        let c_cp = Arc::clone(&c); 
19
20        // Spawn the fix_fingers task
21        tokio::spawn(async move {
22            Chord::fix_fingers(c_ff).await;
23        });
24
25        tokio::spawn(async move {
26            Chord::stabilize(c_st).await;
27        });
28
29        tokio::spawn(async move {
30            Chord::check_pred(c_cp).await;
31        });
32
33        Self {
34            data: c
35        }
36    }
37 
38    pub async fn lookups (&self, key: &str) -> SocketAddr {
39        let t = self.data.lock();
40        SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 2), 54321))
41    }
42
43    async fn fix_fingers(c: Arc<Mutex<chord_node::ChordNode>>) {
44        loop {
45            trace!("Fixing fingers...");
46
47            // Sleep for a specified interval before running the operation again
48            sleep(Duration::from_secs(5)).await;
49        }
50    }
51
52    async fn stabilize(c: Arc<Mutex<chord_node::ChordNode>>) {
53        loop {
54            trace!("Stabilizing...");
55
56            // Sleep for a specified interval before running the operation again
57            sleep(Duration::from_secs(10)).await;
58        }
59    }
60
61    async fn check_pred(c: Arc<Mutex<chord_node::ChordNode>>) {
62        loop {
63            // Perform the fix fingers operation here
64            trace!("Checking predecessor...");
65
66            // Sleep for a specified interval before running the operation again
67            sleep(Duration::from_secs(15)).await;
68        }
69    }
70}