]> WPIA git - gigi.git/blob - src/org/cacert/gigi/ping/PingerDaemon.java
Merge branch 'felix-work'
[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.DomainPingConfiguration.PingType;
14 import org.cacert.gigi.util.RandomToken;
15
16 public class PingerDaemon extends Thread {
17
18     HashMap<PingType, DomainPinger> pingers = new HashMap<>();
19
20     private GigiPreparedStatement searchNeededPings;
21
22     private GigiPreparedStatement enterPingResult;
23
24     private KeyStore truststore;
25
26     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
27
28     public PingerDaemon(KeyStore truststore) {
29         this.truststore = truststore;
30     }
31
32     @Override
33     public void run() {
34         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");
35         enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO domainPinglog SET configId=?, state=?, result=?, challenge=?");
36         pingers.put(PingType.EMAIL, new EmailPinger());
37         pingers.put(PingType.SSL, new SSLPinger(truststore));
38         pingers.put(PingType.HTTP, new HTTPFetch());
39         pingers.put(PingType.DNS, new DNSPinger());
40
41         while (true) {
42             synchronized (this) {
43                 DomainPingConfiguration conf;
44                 while ((conf = toExecute.peek()) != null) {
45                     handle(conf);
46                     toExecute.remove();
47                 }
48                 notifyAll();
49             }
50
51             GigiResultSet rs = searchNeededPings.executeQuery();
52             while (rs.next()) {
53                 handle(DomainPingConfiguration.getById(rs.getInt("id")));
54             }
55             try {
56                 Thread.sleep(5000);
57             } catch (InterruptedException e) {
58             }
59         }
60     }
61
62     private void handle(DomainPingConfiguration conf) {
63         PingType type = conf.getType();
64         String config = conf.getInfo();
65         DomainPinger dp = pingers.get(type);
66         if (dp != null) {
67             String token = null;
68             if (dp instanceof EmailPinger) {
69                 token = RandomToken.generateToken(16);
70                 config = config + ":" + token;
71             }
72             enterPingResult.setInt(1, conf.getId());
73             Domain target = conf.getTarget();
74             String resp = dp.ping(target, config, target.getOwner());
75             enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed");
76             enterPingResult.setString(3, resp);
77             enterPingResult.setString(4, token);
78             enterPingResult.execute();
79         }
80     }
81
82     public synchronized void queue(DomainPingConfiguration toReping) {
83         interrupt();
84         toExecute.add(toReping);
85         while (toExecute.size() > 0) {
86             try {
87                 wait();
88             } catch (InterruptedException e) {
89                 e.printStackTrace();
90             }
91         }
92     }
93 }