1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! Feed Requester
//!
//! As feed messages flooded in the entire network.
//! In case a user joins the network newly or a feed message
//! was missed, the latest feed id's are synchronized via the
//! qaul router info service.
//!
//! With each routing information the last feed messages are
//! advertised and can be requested from the sending node.

use libp2p::PeerId;
use state::Storage;
use std::collections::VecDeque;
use std::sync::RwLock;

/// mutable state of user requester
pub static USERREQUESTER: Storage<RwLock<UserRequester>> = Storage::new();

/// mutable state of the user responser
pub static USERRESPONSER: Storage<RwLock<UserResponser>> = Storage::new();

/// User Request Structure
pub struct UserRequest {
    pub neighbour_id: PeerId,
    pub user_ids: Vec<Vec<u8>>,
}

/// User Requester Module
pub struct UserRequester {
    pub to_send: VecDeque<UserRequest>,
}

impl UserRequester {
    /// Initialize and create the ring buffer.
    pub fn init() {
        let user_requester = UserRequester {
            to_send: VecDeque::new(),
        };
        USERREQUESTER.set(RwLock::new(user_requester));
    }

    /// Add a message to the ring buffer for sending.
    pub fn add(neighbour_id: &PeerId, user_ids: &Vec<Vec<u8>>) {
        let msg = UserRequest {
            neighbour_id: neighbour_id.clone(),
            user_ids: user_ids.clone(),
        };

        // add it to sending queue
        let mut user_requester = USERREQUESTER.get().write().unwrap();
        user_requester.to_send.push_back(msg);
    }
}

/// User Response Structure
pub struct UserResponse {
    pub neighbour_id: PeerId,
    pub users: super::router_net_proto::UserInfoTable,
}

/// Feed Responder
pub struct UserResponser {
    pub to_send: VecDeque<UserResponse>,
}

impl UserResponser {
    /// Initialize and create the ring buffer.
    pub fn init() {
        let user_responser = UserResponser {
            to_send: VecDeque::new(),
        };
        USERRESPONSER.set(RwLock::new(user_responser));
    }

    /// Add a message to the ring buffer for sending.
    pub fn add(neighbour_id: &PeerId, table: &super::router_net_proto::UserInfoTable) {
        let msg = UserResponse {
            neighbour_id: neighbour_id.clone(),
            users: table.clone(),
        };
        // add it to sending queue
        let mut user_responser = USERRESPONSER.get().write().unwrap();
        user_responser.to_send.push_back(msg);
    }
}