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
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Debug module functions

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

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

/// debug module function handling
pub struct Debug {}

impl Debug {
    /// CLI command interpretation
    /// 
    /// The CLI commands of debug module are processed here
    pub fn cli(command: &str) {
        match command {
            // print sent rpc messages
            cmd if cmd.starts_with("rpc sent") => {
                Self::rpc_sent();
            },
            // print queued rpc messages
            cmd if cmd.starts_with("rpc queued") => {
                Self::rpc_queued();
            },
            // send a heartbeat
            cmd if cmd.starts_with("heartbeat") => {
                Self::heartbeat_send();
            },
            // let libqaul panic
            cmd if cmd.starts_with("panic") => {
                Self::panic();
            },
            // enable libqaul logging to file
            cmd if cmd.starts_with("log enable") => {
                Self::debug_log_enable_send();
            },
            // disable libqaul logging to file
            cmd if cmd.starts_with("log disable") => {
                Self::debug_log_disable_send();
            },
            // request storage path location
            cmd if cmd.starts_with("path") => {
                Self::debug_path_send();
            },            
            // unknown command
            _ => log::error!("unknown debug command"),
        }
    }

    /// print rpc message counter of messages sent
    /// from client to libqaul
    fn rpc_sent() {
        let count = libqaul::api::send_rpc_count();
        println!("{} RPC messages sent by this client to libqaul", count);
    }

    /// print rpc message counter of queued messages 
    /// in the message output of libqaul
    fn rpc_queued() {
        let count = libqaul::api::receive_rpc_queued();
        println!("{} RPC messages in libqaul's queue", count);
    }

    /// print rpc message counter of messages sent
    /// from client to libqaul
    fn heartbeat_send() {
        // create heartbeat message
        let proto_message = proto::Debug {
            message: Some(proto::debug::Message::HeartbeatRequest(
                proto::HeartbeatRequest{}
            )),
        };

        // 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::Debug.into(), "".to_string());
    }

    fn debug_log_enable_send(){
        // create log enable message
        let proto_message = proto::Debug {
            message: Some(proto::debug::Message::LogToFile(
                proto::LogToFile{enable: true}
            )),
        };

        // 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::Debug.into(), "".to_string());
    }

    fn debug_log_disable_send(){
        // create log enable message
        let proto_message = proto::Debug {
            message: Some(proto::debug::Message::LogToFile(
                proto::LogToFile{enable: false}
            )),
        };

        // 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::Debug.into(), "".to_string());        
    }

    fn debug_path_send(){
        // create StoragePathRequest
        let proto_message = proto::Debug {
            message: Some(proto::debug::Message::StoragePathRequest(
                proto::StoragePathRequest{}
            )),
        };
        // 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::Debug.into(), "".to_string());        
    }

    /// send a debugging message to libqaul that
    /// let's it panic.
    fn panic() {
        // create panic message
        let proto_message = proto::Debug {
            message: Some(proto::debug::Message::Panic(
                proto::Panic{}
            )),
        };

        // 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::Debug.into(), "".to_string());
    }

    /// Process received RPC message
    /// 
    /// Decodes received protobuf encoded binary RPC message
    /// of the debug module.
    pub fn rpc(data: Vec<u8>) {
        match proto::Debug::decode(&data[..]) {
            Ok(debug) => {
                match debug.message {
                    Some(proto::debug::Message::HeartbeatResponse(_heartbeat)) => {
                        // print confirmation
                        println!("Heartbeat response received");
                    },
                    Some(proto::debug::Message::StoragePathResponse(storage_path_response)) => {
                        // printout path
                        println!("Storage Path: {}", storage_path_response.storage_path);
                    },
                    _ => {
                        log::error!("unprocessable RPC debug message");
                    },
                }    
            },
            Err(error) => {
                log::error!("{:?}", error);
            },
        }
    }
}