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

//! # RPC Debug Messages
//! 
//! Messages to debug libqaul

use prost::Message;
use super::Rpc;
use crate::utilities::filelogger::FileLogger;
use crate::storage::Storage;
use crate::storage::configuration::Configuration;

/// Import protobuf message definition generated by 
/// the rust module prost-build.
pub mod proto { include!("qaul.rpc.debug.rs"); }


/// RPC Debugging Module
pub struct Debug {

}


impl Debug {
    /// Process incoming RPC request messages for debug module
    pub fn rpc(data: Vec<u8>, _user_id: Vec<u8>) {
        match proto::Debug::decode(&data[..]) {
            Ok(debug) => {
                match debug.message {
                    Some(proto::debug::Message::HeartbeatRequest(_heartbeat_request)) => {
                        // create and return heartbeat message
                        let proto_message = proto::Debug {
                            message: Some( 
                                proto::debug::Message::HeartbeatResponse(proto::HeartbeatResponse {})
                            ),
                        };

                        // 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, crate::rpc::proto::Modules::Debug.into(), "".to_string(), Vec::new() );
                    },
                    Some(proto::debug::Message::Panic(_panic)) => {
                        // panic
                        log::error!("Libqaul will panic");
                        panic!("Libqaul panics for debugging reasons");
                    },
                    Some(proto::debug::Message::LogToFile(log_to_file)) => {
                        if log_to_file.enable {
                            // start log                            
                            FileLogger::enable(true);
                            if Configuration::get_debug_log() == false{
                                Configuration::enable_debug_log(true);
                                Configuration::save();
                                log::error!("starting debug log..");
                            }    
                        }else {
                            // stop log
                            if Configuration::get_debug_log() == true{
                                Configuration::enable_debug_log(false);
                                Configuration::save();
                                log::error!("stop debug log..");
                            }
                            FileLogger::enable(false);
                        }
                    },
                    Some(proto::debug::Message::StoragePathRequest(_storage_path_request)) => {
                        // create and return storage path response message
                        let path = Storage::get_path(); 
                        let proto_message = proto::Debug {
                            message: Some( 
                                proto::debug::Message::StoragePathResponse(proto::StoragePathResponse {storage_path: path})
                            ),
                        };

                        // 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, crate::rpc::proto::Modules::Debug.into(), "".to_string(), Vec::new() );
                    },     
                    _ => {
                        log::error!("Unhandled RPC Debug Message");
                    },
                }
            },
            Err(e) => {
                log::error!("{:?}", e);
            },
        }
    }
}