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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use prost::Message;
use libqaul;
use super::rpc::Rpc;
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.debug.rs"); }
pub struct Debug {}
impl Debug {
pub fn cli(command: &str) {
match command {
cmd if cmd.starts_with("rpc sent") => {
Self::rpc_sent();
},
cmd if cmd.starts_with("rpc queued") => {
Self::rpc_queued();
},
cmd if cmd.starts_with("heartbeat") => {
Self::heartbeat_send();
},
cmd if cmd.starts_with("panic") => {
Self::panic();
},
cmd if cmd.starts_with("log enable") => {
Self::debug_log_enable_send();
},
cmd if cmd.starts_with("log disable") => {
Self::debug_log_disable_send();
},
cmd if cmd.starts_with("path") => {
Self::debug_path_send();
},
_ => log::error!("unknown debug command"),
}
}
fn rpc_sent() {
let count = libqaul::api::send_rpc_count();
println!("{} RPC messages sent by this client to libqaul", count);
}
fn rpc_queued() {
let count = libqaul::api::receive_rpc_queued();
println!("{} RPC messages in libqaul's queue", count);
}
fn heartbeat_send() {
let proto_message = proto::Debug {
message: Some(proto::debug::Message::HeartbeatRequest(
proto::HeartbeatRequest{}
)),
};
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::Debug.into(), "".to_string());
}
fn debug_log_enable_send(){
let proto_message = proto::Debug {
message: Some(proto::debug::Message::LogToFile(
proto::LogToFile{enable: true}
)),
};
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::Debug.into(), "".to_string());
}
fn debug_log_disable_send(){
let proto_message = proto::Debug {
message: Some(proto::debug::Message::LogToFile(
proto::LogToFile{enable: false}
)),
};
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::Debug.into(), "".to_string());
}
fn debug_path_send(){
let proto_message = proto::Debug {
message: Some(proto::debug::Message::StoragePathRequest(
proto::StoragePathRequest{}
)),
};
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::Debug.into(), "".to_string());
}
fn panic() {
let proto_message = proto::Debug {
message: Some(proto::debug::Message::Panic(
proto::Panic{}
)),
};
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::Debug.into(), "".to_string());
}
pub fn rpc(data: Vec<u8>) {
match proto::Debug::decode(&data[..]) {
Ok(debug) => {
match debug.message {
Some(proto::debug::Message::HeartbeatResponse(_heartbeat)) => {
println!("Heartbeat response received");
},
Some(proto::debug::Message::StoragePathResponse(storage_path_response)) => {
println!("Storage Path: {}", storage_path_response.storage_path);
},
_ => {
log::error!("unprocessable RPC debug message");
},
}
},
Err(error) => {
log::error!("{:?}", error);
},
}
}
}