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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Feed module functions

use prost::Message;
use super::rpc::Rpc;

/// include generated protobuf RPC rust definition file
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.feed.rs"); }

/// feed module function handling
pub struct Feed {}

impl Feed {
    /// CLI command interpretation
    /// 
    /// The CLI commands of feed module are processed here
    pub fn cli(command: &str) {
        match command {
            // send feed message
            cmd if cmd.starts_with("send ") => {
                Self::send_feed_message(cmd.strip_prefix("send ").unwrap().to_string());
            },
            // request feed list
            cmd if cmd.starts_with("list") => {
                match cmd.strip_prefix("list ") {
                    Some(index_str) => {
                        if let Ok(index) = index_str.parse::<u64>() {
                            // request messages
                            Self::request_feed_list(index);
                        }
                        else {
                            log::error!("feed list index is not a valid number");
                        }
                    },
                    None => {
                        // request all messages
                        Self::request_feed_list(0);
                    }
                }
            },
            // unknown command
            _ => log::error!("unknown feed command"),
        }
    }

    /// create and send feed message via rpc
    fn send_feed_message(message_text: String) {
        // create feed send message
        let proto_message = proto::Feed {
            message: Some(proto::feed::Message::Send(
                proto::SendMessage{
                    content: message_text,
                }
            )),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");

        // send message
        Rpc::send_message(buf, super::rpc::proto::Modules::Feed.into(), "".to_string());
    }

    /// request feed list via rpc
    fn request_feed_list(last_index: u64) {
        // create feed list request message
        let proto_message = proto::Feed {
            message: Some(proto::feed::Message::Request(
                proto::FeedMessageRequest{
                    last_received: Vec::new(),
                    last_index,
                }
            )),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");

        // send message
        Rpc::send_message(buf, super::rpc::proto::Modules::Feed.into(), "".to_string());
    }

    /// Process received RPC message
    /// 
    /// Decodes received protobuf encoded binary RPC message
    /// of the feed module.
    pub fn rpc(data: Vec<u8>) {
        match proto::Feed::decode(&data[..]) {
            Ok(feed) => {
                match feed.message {
                    Some(proto::feed::Message::Received(proto_feedlist)) => {
                        // List header
                        println!("====================================");
                        println!("Received Feed Messages");
                        println!("------------------------------------");

                        // print all messages in the feed list
                        for message in proto_feedlist.feed_message {
                            print!{"[{}] ", message.index};
                            println!("Time Sent - {}", message.time_sent);
                            println!("Timestamp Sent - {}", message.timestamp_sent);
                            println!("Time Received - {}", message.time_received);
                            println!("Timestamp Received - {}", message.timestamp_received);
                            println!("Message ID {}", message.message_id_base58);
                            println!("From {}", message.sender_id_base58);
                            println!("\t{}", message.content);
                            println!("");
                        }
                    }
                    _ => {
                        log::error!("unprocessable RPC feed message");
                    },
                }    
            },
            Err(error) => {
                log::error!("{:?}", error);
            },
        }
    }
}