File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\UDPBroadcastThread.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson;
25
26 import hudson.model.Hudson;
27 import jenkins.model.Jenkins;
28 import hudson.util.OneShotEvent;
29
30 import java.io.IOException;
31 import java.net.BindException;
32 import java.net.DatagramPacket;
33 import java.net.InetAddress;
34 import java.net.MulticastSocket;
35 import java.net.SocketAddress;
36 import java.net.UnknownHostException;
37 import java.nio.channels.ClosedByInterruptException;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40
41 /**
42 * Monitors a UDP multicast broadcast and respond with the location of the Hudson service.
43 *
44 * <p>
45 * Useful for auto-discovery of Hudson in the network.
46 *
47 * @author Kohsuke Kawaguchi
48 */
49 public class UDPBroadcastThread extends Thread {
50 private final Jenkins jenkins;
51
52 public final OneShotEvent ready = new OneShotEvent();
53 private MulticastSocket mcs;
54 private boolean shutdown;
55
56 /**
57 * @deprecated as of 1.416
58 * Use {@link #UDPBroadcastThread(Jenkins)}
59 */
60 public UDPBroadcastThread(Hudson jenkins) throws IOException {
61 this((Jenkins)jenkins);
62 }
63
64 public UDPBroadcastThread(Jenkins jenkins) throws IOException {
65 super("Jenkins UDP "+PORT+" monitoring thread");
66 this.jenkins = jenkins;
67 mcs = new MulticastSocket(PORT);
68 }
69
70 @Override
71 public void run() {
72 try {
73 mcs.joinGroup(MULTICAST);
74 ready.signal();
75
76 while(true) {
77 byte[] buf = new byte[2048];
78 DatagramPacket p = new DatagramPacket(buf,buf.length);
79 mcs.receive(p);
80
81 SocketAddress sender = p.getSocketAddress();
82
83 // prepare a response
84 TcpSlaveAgentListener tal = jenkins.getTcpSlaveAgentListener();
85
86 StringBuilder rsp = new StringBuilder("<hudson>");
87 tag(rsp,"version", Jenkins.VERSION);
88 tag(rsp,"url", jenkins.getRootUrl());
89 tag(rsp,"server-id", jenkins.getLegacyInstanceId());
90 tag(rsp,"slave-port",tal==null?null:tal.getPort());
91
92 for (UDPBroadcastFragment f : UDPBroadcastFragment.all())
93 f.buildFragment(rsp,sender);
94
95 rsp.append("</hudson>");
96
97 byte[] response = rsp.toString().getBytes("UTF-8");
98 mcs.send(new DatagramPacket(response,response.length,sender));
99 }
100 } catch (ClosedByInterruptException e) {
101 // shut down
102 } catch (BindException e) {
103 // if we failed to listen to UDP, just silently abandon it, as a stack trace
104 // makes people unnecessarily concerned, for a feature that currently does no good.
105 LOGGER.log(Level.WARNING, "Failed to listen to UDP port "+PORT,e);
106 } catch (IOException e) {
107 if (shutdown) return; // forcibly closed
108 LOGGER.log(Level.WARNING, "UDP handling problem",e);
109 }
110 }
111
112 private void tag(StringBuilder buf, String tag, Object value) {
113 if(value==null) return;
114 buf.append('<').append(tag).append('>').append(value).append("</").append(tag).append('>');
115 }
116
117 public void shutdown() {
118 shutdown = true;
119 mcs.close();
120 interrupt();
121 }
122
123 public static final int PORT = Integer.getInteger("hudson.udp",33848);
124
125 private static final Logger LOGGER = Logger.getLogger(UDPBroadcastThread.class.getName());
126
127 /**
128 * Multicast socket address.
129 */
130 public static InetAddress MULTICAST;
131
132 static {
133 try {
134 MULTICAST = InetAddress.getByAddress(new byte[]{(byte)239, (byte)77, (byte)124, (byte)213});
135 } catch (UnknownHostException e) {
136 throw new Error(e);
137 }
138 }
139 }
140