Only print on chage, print on good, too
[dnsseed-rust] / src / datastore.rs
index 6527942d15856b3a9ee8e564219971c35e283e57..1dd976aa4e8863a33d60fca0b6eed9a4f01002bb 100644 (file)
@@ -46,9 +46,10 @@ pub enum RegexSetting {
 }
 
 struct Node {
-       state: AddressState,
-       last_services: u64,
        last_update: Instant,
+       last_good: Instant, // Ignored unless state is Good or WasGood
+       last_services: u64,
+       state: AddressState,
 }
 
 struct Nodes {
@@ -200,6 +201,7 @@ impl Store {
                                        },
                                        last_services,
                                        last_update: Instant::now(),
+                                       last_good: Instant::now(),
                                };
                                if node.state == AddressState::Good {
                                        for i in 0..64 {
@@ -256,6 +258,7 @@ impl Store {
                                                        state: AddressState::Untested,
                                                        last_services: 0,
                                                        last_update: cur_time,
+                                                       last_good: Instant::now(),
                                                });
                                                nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, socketaddr));
                                        },
@@ -267,12 +270,15 @@ impl Store {
                }
        }
 
-       pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) {
+       pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> bool {
                let mut nodes_lock = self.nodes.write().unwrap();
                let nodes = nodes_lock.borrow_mut();
                let state_ref = nodes.nodes_to_state.get_mut(&addr).unwrap();
+               let ret = state != state_ref.state;
                state_ref.last_update = Instant::now();
-               if state_ref.state == AddressState::Good && state != AddressState::Good {
+               if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
+                               && state != AddressState::Good
+                               && state_ref.last_good >= state_ref.last_update + Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
                        state_ref.state = AddressState::WasGood;
                        for i in 0..64 {
                                if state_ref.last_services & (1 << i) != 0 {
@@ -291,9 +297,12 @@ impl Store {
                                                nodes.good_node_services.get_mut(&i).unwrap().remove(&addr);
                                        }
                                }
+                               state_ref.last_services = services;
+                               state_ref.last_good = state_ref.last_update;
                        }
                        nodes.state_next_scan.get_mut(&state).unwrap().push((state_ref.last_update, addr));
                }
+               ret
        }
 
        pub fn save_data(&'static self) -> impl Future<Item=(), Error=()> {
@@ -363,7 +372,7 @@ impl Store {
                        {
                                let nodes = self.nodes.read().unwrap();
                                let mut rng = thread_rng();
-                               for i in 1u64..10u64 {
+                               for i in &[1u64, 4, 5, 8, 9, 12, 13, 1024, 1025, 1028, 1029, 1032, 1033, 1036, 1037] {
                                        let mut v6_set = Vec::new();
                                        let mut v4_set = Vec::new();
                                        if i.count_ones() == 1 {
@@ -420,10 +429,10 @@ impl Store {
                                                        .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect();
                                        }
                                        for a in v4_set {
-                                               dns_buff += &format!("x{:x}.dnsseed.bluematt.me\tIN\tA\t{}\n", i, a);
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{}\n", i, a);
                                        }
                                        for a in v6_set {
-                                               dns_buff += &format!("x{:x}.dnsseed.bluematt.me\tIN\tAAAA\t{}\n", i, a);
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
                                        }
                                }
                        }