Initial checkin
[dnsseed-rust] / src / printer.rs
1 use std::collections::LinkedList;
2 use std::sync::{Arc, Mutex};
3 use std::io::Write;
4
5 use crate::datastore::{Store, AddressState, U64Setting, StringSetting};
6
7 pub enum Stat {
8         HeaderCount(u64),
9         NewConnection,
10         ConnectionClosed,
11 }
12
13 struct Stats {
14         lines: LinkedList<String>,
15         header_count: u64,
16         connection_count: u64,
17 }
18
19 pub struct Printer {
20         stats: Arc<Mutex<Stats>>,
21 }
22
23 impl Printer {
24         pub fn new(store: &'static Store) -> Printer {
25                 let stats: Arc<Mutex<Stats>> = Arc::new(Mutex::new(Stats {
26                         lines: LinkedList::new(),
27                         header_count: 0,
28                         connection_count: 0,
29                 }));
30                 let thread_arc = Arc::clone(&stats);
31                 std::thread::spawn(move || {
32                         loop {
33                                 std::thread::sleep(std::time::Duration::from_secs(1));
34
35                                 let stdout = std::io::stdout();
36                                 let mut out = stdout.lock();
37
38                                 let stats = thread_arc.lock().unwrap();
39
40                                 out.write_all(b"\x1b[2J\x1b[;H\n").expect("stdout broken?");
41                                 for line in stats.lines.iter() {
42                                         out.write_all(line.as_bytes()).expect("stdout broken?");
43                                         out.write_all(b"\n").expect("stdout broken?");
44                                 }
45
46                                 out.write_all(b"\nNode counts by status:\n").expect("stdout broken?");
47                                 out.write_all(format!("Untested:               {}\n", store.get_node_count(AddressState::Untested)
48                                                 ).as_bytes()).expect("stdout broken?");
49                                 out.write_all(format!("Low Block Count:        {}\n", store.get_node_count(AddressState::LowBlockCount)
50                                                 ).as_bytes()).expect("stdout broken?");
51                                 out.write_all(format!("High Block Count:       {}\n", store.get_node_count(AddressState::HighBlockCount)
52                                                 ).as_bytes()).expect("stdout broken?");
53                                 out.write_all(format!("Low Version:            {}\n", store.get_node_count(AddressState::LowVersion)
54                                                 ).as_bytes()).expect("stdout broken?");
55                                 out.write_all(format!("Bad Version:            {}\n", store.get_node_count(AddressState::BadVersion)
56                                                 ).as_bytes()).expect("stdout broken?");
57                                 out.write_all(format!("Not Full Node:          {}\n", store.get_node_count(AddressState::NotFullNode)
58                                                 ).as_bytes()).expect("stdout broken?");
59                                 out.write_all(format!("Protocol Violation:     {}\n", store.get_node_count(AddressState::ProtocolViolation)
60                                                 ).as_bytes()).expect("stdout broken?");
61                                 out.write_all(format!("Timeout:                {}\n", store.get_node_count(AddressState::Timeout)
62                                                 ).as_bytes()).expect("stdout broken?");
63                                 out.write_all(format!("Timeout During Request: {}\n", store.get_node_count(AddressState::TimeoutDuringRequest)
64                                                 ).as_bytes()).expect("stdout broken?");
65                                 out.write_all(format!("Good:                   {}\n", store.get_node_count(AddressState::Good)
66                                                 ).as_bytes()).expect("stdout broken?");
67                                 out.write_all(format!("WasGood:                {}\n", store.get_node_count(AddressState::WasGood)
68                                                 ).as_bytes()).expect("stdout broken?");
69
70                                 out.write_all(format!(
71                                                 "\nCurrent connections open/in progress: {}\n", stats.connection_count).as_bytes()).expect("stdout broken?");
72                                 out.write_all(format!(
73                                                 "Connections opened each second: {} (\"c x\" to change to x seconds)\n", store.get_u64(U64Setting::ConnsPerSec)
74                                                 ).as_bytes()).expect("stdout broken?");
75                                 out.write_all(format!(
76                                                 "Current block count: {}\n", stats.header_count).as_bytes()).expect("stdout broken?");
77
78                                 out.write_all(format!(
79                                                 "Timeout for full run (in seconds): {} (\"t x\" to change to x seconds)\n", store.get_u64(U64Setting::RunTimeout)
80                                                 ).as_bytes()).expect("stdout broken?");
81                                 out.write_all(format!(
82                                                 "Minimum protocol version: {} (\"v x\" to change value to x)\n", store.get_u64(U64Setting::MinProtocolVersion)
83                                                 ).as_bytes()).expect("stdout broken?");
84                                 out.write_all(format!(
85                                                 "Subversion match regex: {} (\"s x\" to change value to x)\n", store.get_string(StringSetting::SubverRegex)
86                                                 ).as_bytes()).expect("stdout broken?");
87
88                                 out.write_all(b"\nRetry times (in seconds):\n").expect("stdout broken?");
89                                 out.write_all(format!(
90                                                 "Untested:               {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::Untested))
91                                                 ).as_bytes()).expect("stdout broken?");
92                                 out.write_all(format!(
93                                                 "Low Block Count:        {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::LowBlockCount))
94                                                 ).as_bytes()).expect("stdout broken?");
95                                 out.write_all(format!(
96                                                 "High Block Count        {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::HighBlockCount))
97                                                 ).as_bytes()).expect("stdout broken?");
98                                 out.write_all(format!(
99                                                 "Low Version:            {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::LowVersion))
100                                                 ).as_bytes()).expect("stdout broken?");
101                                 out.write_all(format!(
102                                                 "Bad Version:            {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::BadVersion))
103                                                 ).as_bytes()).expect("stdout broken?");
104                                 out.write_all(format!(
105                                                 "Not Full Node:          {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::NotFullNode))
106                                                 ).as_bytes()).expect("stdout broken?");
107                                 out.write_all(format!(
108                                                 "Protocol Violation:     {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::ProtocolViolation))
109                                                 ).as_bytes()).expect("stdout broken?");
110                                 out.write_all(format!(
111                                                 "Timeout:                {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::Timeout))
112                                                 ).as_bytes()).expect("stdout broken?");
113                                 out.write_all(format!(
114                                                 "Timeout During Request: {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest))
115                                                 ).as_bytes()).expect("stdout broken?");
116                                 out.write_all(format!(
117                                                 "Good:                   {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::Good))
118                                                 ).as_bytes()).expect("stdout broken?");
119                                 out.write_all(format!(
120                                                 "Was Good:               {}\n", store.get_u64(U64Setting::RescanInterval(AddressState::WasGood))
121                                                 ).as_bytes()).expect("stdout broken?");
122
123                                 out.write_all(b"\nCommands:\n").expect("stdout broken?");
124                                 out.write_all(b"q: quit\n").expect("stdout broken?");
125                                 out.write_all(format!(
126                                                 "r x y: Change retry time for status x (int value, see retry times section for name mappings) to y (in seconds)\n"
127                                                 ).as_bytes()).expect("stdout broken?");
128                                 out.write_all(format!(
129                                                 "w x: Change the amount of time a node is considered WAS_GOOD after it fails to x from {} (in seconds)\n",
130                                                 store.get_u64(U64Setting::WasGoodTimeout)
131                                                 ).as_bytes()).expect("stdout broken?");
132                                 out.write_all(b"p: Enable/disable updating these stats\n").expect("stdout broken?");
133                                 out.write_all(b"a x: Scan node x\n").expect("stdout broken?");
134                                 out.write_all(b"\x1b[s").expect("stdout broken?"); // Save cursor position and provide a blank line before cursor
135                                 out.write_all(b"\x1b[;H\x1b[2K").expect("stdout broken?");
136                                 out.write_all(b"Most recent log:\n").expect("stdout broken?");
137                                 out.write_all(b"\x1b[u").expect("stdout broken?"); // Restore cursor position and go up one line
138
139                                 out.flush().expect("stdout broken?");
140                         }
141                 });
142                 Printer {
143                         stats,
144                 }
145         }
146
147         pub fn add_line(&self, line: String, _err: bool) {
148                 let mut stats = self.stats.lock().unwrap();
149                 stats.lines.push_back(line);
150                 if stats.lines.len() > 50 {
151                         stats.lines.pop_front();
152                 }
153         }
154
155         pub fn set_stat(&self, s: Stat) {
156                 match s {
157                         Stat::HeaderCount(c) => self.stats.lock().unwrap().header_count = c,
158                         Stat::NewConnection => self.stats.lock().unwrap().connection_count += 1,
159                         Stat::ConnectionClosed => self.stats.lock().unwrap().connection_count -= 1,
160                 }
161         }
162 }