KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > jnlp > Engine


1 package hudson.jnlp;
2
3 import hudson.remoting.Channel;
4
5 import java.io.DataOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.net.HttpURLConnection JavaDoc;
8 import java.net.Socket JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.concurrent.ExecutorService JavaDoc;
11 import java.util.concurrent.Executors JavaDoc;
12
13 /**
14  * @author Kohsuke Kawaguchi
15  */

16 public class Engine extends Thread JavaDoc {
17     private final ExecutorService JavaDoc executor = Executors.newCachedThreadPool();
18
19     private Listener listener;
20     private final String JavaDoc host;
21     private final String JavaDoc hudsonUrl;
22     private final String JavaDoc secretKey;
23     private final String JavaDoc slaveName;
24
25     public Engine(Listener listener, String JavaDoc host, String JavaDoc hudsonUrl, String JavaDoc secretKey, String JavaDoc slaveName) {
26         this.listener = listener;
27         this.host = host;
28         this.hudsonUrl = hudsonUrl;
29         this.secretKey = secretKey;
30         this.slaveName = slaveName;
31     }
32
33     public void run() {
34         try {
35             while(true) {
36                 listener.status("Locating Server");
37                 // find out the TCP port
38
HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(hudsonUrl).openConnection();
39                 con.connect();
40                 String JavaDoc port = con.getHeaderField("X-Hudson-JNLP-Port");
41                 if(con.getResponseCode()!=200
42                 || port ==null) {
43                     listener.error(new Exception JavaDoc(hudsonUrl+" is not Hudson: "+con.getResponseMessage()));
44                     return;
45                 }
46
47                 listener.status("Connecting");
48                 Socket JavaDoc s = new Socket JavaDoc(host, Integer.parseInt(port));
49                 listener.status("Handshaking");
50
51                 DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(s.getOutputStream());
52                 dos.writeUTF(secretKey);
53                 dos.writeUTF(slaveName);
54
55                 Channel channel = new Channel("channel", executor, s.getInputStream(), s.getOutputStream());
56                 listener.status("Connected");
57                 channel.join();
58                 listener.status("Terminated");
59
60                 // try to connect back to the server every 10 secs.
61
while(true) {
62                     Thread.sleep(1000*10);
63                     try {
64                         con = (HttpURLConnection JavaDoc)new URL JavaDoc(hudsonUrl).openConnection();
65                         con.connect();
66                         if(con.getResponseCode()!=200)
67                         continue;
68                     } catch (IOException JavaDoc e) {
69                         continue;
70                     }
71                     break;
72                 }
73             }
74         } catch (Throwable JavaDoc e) {
75             listener.error(e);
76         }
77     }
78 }
79
Popular Tags