1 package club.wpia.gigi.ping;
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
8 import club.wpia.gigi.database.DatabaseConnection;
9 import club.wpia.gigi.database.DatabaseConnection.Link;
10 import club.wpia.gigi.database.GigiPreparedStatement;
11 import club.wpia.gigi.database.GigiResultSet;
12 import club.wpia.gigi.dbObjects.Domain;
13 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
14 import club.wpia.gigi.dbObjects.DomainPingType;
16 public class PingerDaemon extends Thread {
18 HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
20 private KeyStore truststore;
22 private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
24 public PingerDaemon(KeyStore truststore) {
25 this.truststore = truststore;
30 try (Link l = DatabaseConnection.newLink(false)) {
32 } catch (InterruptedException e) {
37 public void runWithConnection() {
39 pingers.put(DomainPingType.EMAIL, new EmailPinger());
40 pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
41 pingers.put(DomainPingType.HTTP, new HTTPFetch());
42 pingers.put(DomainPingType.DNS, new DNSPinger());
46 boolean worked = false;
48 DomainPingConfiguration conf;
49 while ((conf = toExecute.peek()) != null) {
56 try (GigiPreparedStatement searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" //
57 + " FROM `pingconfig` AS `pc`" //
58 + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" //
59 + " WHERE `d`.`deleted` IS NULL" //
60 + " AND `pc`.`deleted` IS NULL" //
61 + " AND NOT EXISTS (" //
62 + " SELECT 1 FROM `domainPinglog` AS `dpl`" //
63 + " WHERE `dpl`.`configId` = `pc`.`id`" //
64 + " AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')")) {
66 GigiResultSet rs = searchNeededPings.executeQuery();
69 handle(DomainPingConfiguration.getById(rs.getInt("id")));
76 } catch (InterruptedException e) {
78 } catch (Throwable t) {
82 } catch (InterruptedException e) {
88 private void handle(DomainPingConfiguration conf) {
89 DomainPingType type = conf.getType();
90 String config = conf.getInfo();
91 DomainPinger dp = pingers.get(type);
93 Domain target = conf.getTarget();
94 System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
96 dp.ping(target, config, target.getOwner(), conf.getId());
97 } catch (Throwable t) {
99 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
101 System.err.println("done (" + System.currentTimeMillis() + ")");
105 public synchronized void queue(DomainPingConfiguration toReping) {
107 toExecute.add(toReping);
108 while (toExecute.size() > 0) {
111 } catch (InterruptedException e) {