]> WPIA git - gigi.git/blob - src/org/cacert/gigi/ping/PingerDaemon.java
fix: more testcases from regression a few commits back
[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.DatabaseConnection;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.dbObjects.Domain;
12 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
13 import org.cacert.gigi.dbObjects.DomainPingType;
14 import org.cacert.gigi.util.RandomToken;
15
16 public class PingerDaemon extends Thread {
17
18     HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
19
20     private GigiPreparedStatement searchNeededPings;
21
22     private KeyStore truststore;
23
24     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
25
26     public PingerDaemon(KeyStore truststore) {
27         this.truststore = truststore;
28     }
29
30     @Override
31     public void run() {
32         searchNeededPings = DatabaseConnection.getInstance().prepare("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) AND `domains`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
33         pingers.put(DomainPingType.EMAIL, new EmailPinger());
34         pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
35         pingers.put(DomainPingType.HTTP, new HTTPFetch());
36         pingers.put(DomainPingType.DNS, new DNSPinger());
37
38         while (true) {
39             try {
40                 boolean worked = false;
41                 synchronized (this) {
42                     DomainPingConfiguration conf;
43                     while ((conf = toExecute.peek()) != null) {
44                         worked = true;
45                         handle(conf);
46                         toExecute.remove();
47                     }
48                     notifyAll();
49                 }
50
51                 GigiResultSet rs = searchNeededPings.executeQuery();
52                 while (rs.next()) {
53                     worked = true;
54                     handle(DomainPingConfiguration.getById(rs.getInt("id")));
55                 }
56                 try {
57                     if ( !worked) {
58                         Thread.sleep(5000);
59                     }
60                 } catch (InterruptedException e) {
61                 }
62             } catch (Throwable t) {
63                 t.printStackTrace();
64             }
65         }
66     }
67
68     private void handle(DomainPingConfiguration conf) {
69         DomainPingType type = conf.getType();
70         String config = conf.getInfo();
71         DomainPinger dp = pingers.get(type);
72         if (dp != null) {
73             if (dp instanceof EmailPinger) {
74                 String token = null;
75                 token = RandomToken.generateToken(16);
76                 config = config + ":" + token;
77             }
78             Domain target = conf.getTarget();
79             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
80             try {
81                 dp.ping(target, config, target.getOwner(), conf.getId());
82             } catch (Throwable t) {
83                 t.printStackTrace();
84                 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
85             }
86             System.err.println("done (" + System.currentTimeMillis() + ")");
87         }
88     }
89
90     public synchronized void queue(DomainPingConfiguration toReping) {
91         interrupt();
92         toExecute.add(toReping);
93         while (toExecute.size() > 0) {
94             try {
95                 wait();
96             } catch (InterruptedException e) {
97                 e.printStackTrace();
98             }
99         }
100     }
101 }