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
use prost::Message;
use super::rpc::Rpc;
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.feed.rs"); }
pub struct Feed {}
impl Feed {
pub fn cli(command: &str) {
match command {
cmd if cmd.starts_with("send ") => {
Self::send_feed_message(cmd.strip_prefix("send ").unwrap().to_string());
},
cmd if cmd.starts_with("list") => {
match cmd.strip_prefix("list ") {
Some(index_str) => {
if let Ok(index) = index_str.parse::<u64>() {
Self::request_feed_list(index);
}
else {
log::error!("feed list index is not a valid number");
}
},
None => {
Self::request_feed_list(0);
}
}
},
_ => log::error!("unknown feed command"),
}
}
fn send_feed_message(message_text: String) {
let proto_message = proto::Feed {
message: Some(proto::feed::Message::Send(
proto::SendMessage{
content: message_text,
}
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
Rpc::send_message(buf, super::rpc::proto::Modules::Feed.into(), "".to_string());
}
fn request_feed_list(last_index: u64) {
let proto_message = proto::Feed {
message: Some(proto::feed::Message::Request(
proto::FeedMessageRequest{
last_received: Vec::new(),
last_index,
}
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
Rpc::send_message(buf, super::rpc::proto::Modules::Feed.into(), "".to_string());
}
pub fn rpc(data: Vec<u8>) {
match proto::Feed::decode(&data[..]) {
Ok(feed) => {
match feed.message {
Some(proto::feed::Message::Received(proto_feedlist)) => {
println!("====================================");
println!("Received Feed Messages");
println!("------------------------------------");
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);
},
}
}
}