1 package hudson.jnlp; 2 3 import hudson.remoting.Channel; 4 5 import java.io.DataOutputStream ; 6 import java.io.IOException ; 7 import java.net.HttpURLConnection ; 8 import java.net.Socket ; 9 import java.net.URL ; 10 import java.util.concurrent.ExecutorService ; 11 import java.util.concurrent.Executors ; 12 13 16 public class Engine extends Thread { 17 private final ExecutorService executor = Executors.newCachedThreadPool(); 18 19 private Listener listener; 20 private final String host; 21 private final String hudsonUrl; 22 private final String secretKey; 23 private final String slaveName; 24 25 public Engine(Listener listener, String host, String hudsonUrl, String secretKey, String 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 HttpURLConnection con = (HttpURLConnection )new URL (hudsonUrl).openConnection(); 39 con.connect(); 40 String port = con.getHeaderField("X-Hudson-JNLP-Port"); 41 if(con.getResponseCode()!=200 42 || port ==null) { 43 listener.error(new Exception (hudsonUrl+" is not Hudson: "+con.getResponseMessage())); 44 return; 45 } 46 47 listener.status("Connecting"); 48 Socket s = new Socket (host, Integer.parseInt(port)); 49 listener.status("Handshaking"); 50 51 DataOutputStream dos = new DataOutputStream (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 while(true) { 62 Thread.sleep(1000*10); 63 try { 64 con = (HttpURLConnection )new URL (hudsonUrl).openConnection(); 65 con.connect(); 66 if(con.getResponseCode()!=200) 67 continue; 68 } catch (IOException e) { 69 continue; 70 } 71 break; 72 } 73 } 74 } catch (Throwable e) { 75 listener.error(e); 76 } 77 } 78 } 79 | Popular Tags |