Merge "upd: Reduce Boilerplate in translated SprintfCommands"
[gigi.git] / src / org / cacert / gigi / ping / PingerDaemon.java
1 package org.cacert.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 org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10 import org.cacert.gigi.dbObjects.Domain;
11 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
12 import org.cacert.gigi.dbObjects.DomainPingType;
13 import org.cacert.gigi.util.RandomToken;
14
15 public class PingerDaemon extends Thread {
16
17     HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
18
19     private GigiPreparedStatement searchNeededPings;
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         searchNeededPings = new GigiPreparedStatement("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL OR `domainPinglog`.`when` < CURRENT_TIMESTAMP - interval '6 mons') AND `domains`.`deleted` IS NULL AND `pingconfig`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
32         pingers.put(DomainPingType.EMAIL, new EmailPinger());
33         pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
34         pingers.put(DomainPingType.HTTP, new HTTPFetch());
35         pingers.put(DomainPingType.DNS, new DNSPinger());
36
37         while (true) {
38             try {
39                 boolean worked = false;
40                 synchronized (this) {
41                     DomainPingConfiguration conf;
42                     while ((conf = toExecute.peek()) != null) {
43                         worked = true;
44                         handle(conf);
45                         toExecute.remove();
46                     }
47                     notifyAll();
48                 }
49
50                 GigiResultSet rs = searchNeededPings.executeQuery();
51                 while (rs.next()) {
52                     worked = true;
53                     handle(DomainPingConfiguration.getById(rs.getInt("id")));
54                 }
55                 try {
56                     if ( !worked) {
57                         Thread.sleep(5000);
58                     }
59                 } catch (InterruptedException e) {
60                 }
61             } catch (Throwable t) {
62                 t.printStackTrace();
63             }
64         }
65     }
66
67     private void handle(DomainPingConfiguration conf) {
68         DomainPingType type = conf.getType();
69         String config = conf.getInfo();
70         DomainPinger dp = pingers.get(type);
71         if (dp != null) {
72             if (dp instanceof EmailPinger) {
73                 String token = null;
74                 token = RandomToken.generateToken(16);
75                 config = config + ":" + token;
76             }
77             Domain target = conf.getTarget();
78             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
79             try {
80                 dp.ping(target, config, target.getOwner(), conf.getId());
81             } catch (Throwable t) {
82                 t.printStackTrace();
83                 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
84             }
85             System.err.println("done (" + System.currentTimeMillis() + ")");
86         }
87     }
88
89     public synchronized void queue(DomainPingConfiguration toReping) {
90         interrupt();
91         toExecute.add(toReping);
92         while (toExecute.size() > 0) {
93             try {
94                 wait();
95             } catch (InterruptedException e) {
96                 e.printStackTrace();
97             }
98         }
99     }
100 }