]> WPIA git - gigi.git/blob - src/club/wpia/gigi/ping/PingerDaemon.java
51cc985838b23cd318274e4dcdddbbefcf13ca86
[gigi.git] / src / club / wpia / gigi / ping / PingerDaemon.java
1 package club.wpia.gigi.ping;
2
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
7
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;
15 import club.wpia.gigi.util.RandomToken;
16
17 public class PingerDaemon extends Thread {
18
19     HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
20
21     private KeyStore truststore;
22
23     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
24
25     public PingerDaemon(KeyStore truststore) {
26         this.truststore = truststore;
27     }
28
29     @Override
30     public void run() {
31         try (Link l = DatabaseConnection.newLink(false)) {
32             runWithConnection();
33         } catch (InterruptedException e) {
34             e.printStackTrace();
35         }
36     }
37
38     public void runWithConnection() {
39
40         pingers.put(DomainPingType.EMAIL, new EmailPinger());
41         pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
42         pingers.put(DomainPingType.HTTP, new HTTPFetch());
43         pingers.put(DomainPingType.DNS, new DNSPinger());
44
45         while (true) {
46             try {
47                 boolean worked = false;
48                 synchronized (this) {
49                     DomainPingConfiguration conf;
50                     while ((conf = toExecute.peek()) != null) {
51                         worked = true;
52                         handle(conf);
53                         toExecute.remove();
54                     }
55                     notifyAll();
56                 }
57                 try (GigiPreparedStatement searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" //
58                         + " FROM `pingconfig` AS `pc`" //
59                         + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" //
60                         + " WHERE `d`.`deleted` IS NULL" //
61                         + "  AND `pc`.`deleted` IS NULL" //
62                         + "  AND NOT EXISTS (" //
63                         + "    SELECT 1 FROM `domainPinglog` AS `dpl`" //
64                         + "    WHERE `dpl`.`configId` = `pc`.`id`" //
65                         + "     AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')")) {
66
67                     GigiResultSet rs = searchNeededPings.executeQuery();
68                     while (rs.next()) {
69                         worked = true;
70                         handle(DomainPingConfiguration.getById(rs.getInt("id")));
71                     }
72                 }
73                 try {
74                     if ( !worked) {
75                         Thread.sleep(5000);
76                     }
77                 } catch (InterruptedException e) {
78                 }
79             } catch (Throwable t) {
80                 t.printStackTrace();
81                 try {
82                     Thread.sleep(5000);
83                 } catch (InterruptedException e) {
84                 }
85             }
86         }
87     }
88
89     private void handle(DomainPingConfiguration conf) {
90         DomainPingType type = conf.getType();
91         String config = conf.getInfo();
92         DomainPinger dp = pingers.get(type);
93         if (dp != null) {
94             if (dp instanceof EmailPinger) {
95                 String token = null;
96                 token = RandomToken.generateToken(16);
97                 config = config + ":" + token;
98             }
99             Domain target = conf.getTarget();
100             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
101             try {
102                 dp.ping(target, config, target.getOwner(), conf.getId());
103             } catch (Throwable t) {
104                 t.printStackTrace();
105                 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
106             }
107             System.err.println("done (" + System.currentTimeMillis() + ")");
108         }
109     }
110
111     public synchronized void queue(DomainPingConfiguration toReping) {
112         interrupt();
113         toExecute.add(toReping);
114         while (toExecute.size() > 0) {
115             try {
116                 wait();
117             } catch (InterruptedException e) {
118                 e.printStackTrace();
119             }
120         }
121     }
122 }