forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 8
/
DNSMultiCast.java
92 lines (77 loc) · 3.34 KB
/
DNSMultiCast.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package hudson;
import jenkins.util.SystemProperties;
import jenkins.model.Jenkins;
import jenkins.model.Jenkins.MasterComputer;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import java.io.Closeable;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Registers a DNS multi-cast service-discovery support.
*
* @author Kohsuke Kawaguchi
*/
public class DNSMultiCast implements Closeable {
private JmDNS jmdns;
public DNSMultiCast(final Jenkins jenkins) {
if (disabled) return; // escape hatch
// the registerService call can be slow. run these asynchronously
MasterComputer.threadPoolForRemoting.submit(new Callable<Object>() {
public Object call() {
try {
jmdns = JmDNS.create();
Map<String,String> props = new HashMap<String, String>();
String rootURL = jenkins.getRootUrl();
if (rootURL==null) return null;
props.put("url", rootURL);
try {
props.put("version",String.valueOf(Jenkins.getVersion()));
} catch (IllegalArgumentException e) {
// failed to parse the version number
}
TcpSlaveAgentListener tal = jenkins.getTcpSlaveAgentListener();
if (tal!=null)
props.put("slave-port",String.valueOf(tal.getPort()));
props.put("server-id", jenkins.getLegacyInstanceId());
URL jenkins_url = new URL(rootURL);
int jenkins_port = jenkins_url.getPort();
if (jenkins_port == -1) {
jenkins_port = 80;
}
if (jenkins_url.getPath().length() > 0) {
props.put("path", jenkins_url.getPath());
}
jmdns.registerService(ServiceInfo.create("_hudson._tcp.local.","jenkins",
jenkins_port,0,0,props)); // for backward compatibility
jmdns.registerService(ServiceInfo.create("_jenkins._tcp.local.","jenkins",
jenkins_port,0,0,props));
// Make Jenkins appear in Safari's Bonjour bookmarks
jmdns.registerService(ServiceInfo.create("_http._tcp.local.","Jenkins",
jenkins_port,0,0,props));
} catch (IOException e) {
LOGGER.log(Level.INFO, "Cannot advertise service to DNS multi-cast, skipping: {0}", e);
LOGGER.log(Level.FINE, null, e);
}
return null;
}
});
}
public void close() {
if (jmdns!=null) {
// try {
jmdns.abort();
jmdns = null;
// } catch (final IOException e) {
// LOGGER.log(Level.WARNING,"Failed to close down JmDNS instance!",e);
// }
}
}
private static final Logger LOGGER = Logger.getLogger(DNSMultiCast.class.getName());
public static boolean disabled = SystemProperties.getBoolean(DNSMultiCast.class.getName()+".disabled");
}