diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index ba74e6b4c2..803a4d2008 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -29,15 +29,21 @@ import hudson.remoting.RemoteOutputStream; import hudson.remoting.PingThread; import java.net.URL; +import java.net.URLConnection; +import java.net.Socket; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.ArrayList; +import java.util.logging.Logger; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.DataOutputStream; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; /** * CLI entry point to Hudson. @@ -57,20 +63,38 @@ public class CLI { public CLI(URL hudson, ExecutorService exec) throws IOException, InterruptedException { String url = hudson.toExternalForm(); if(!url.endsWith("/")) url+='/'; - url+="cli"; - hudson = new URL(url); - FullDuplexHttpStream con = new FullDuplexHttpStream(hudson); ownsPool = exec==null; pool = exec!=null ? exec : Executors.newCachedThreadPool(); - channel = new Channel("Chunked connection to "+hudson, - pool,con.getInputStream(),con.getOutputStream()); - new PingThread(channel,30*1000) { - protected void onDead() { - // noop. the point of ping is to keep the connection alive - // as most HTTP servers have a rather short read time out - } - }.start(); + + int clip = getCliTcpPort(url); + if(clip>=0) { + // connect via CLI port + String host = new URL(url).getHost(); + LOGGER.fine("Trying to connect directly via TCP/IP to port "+clip+" of "+host); + Socket s = new Socket(host,clip); + DataOutputStream dos = new DataOutputStream(s.getOutputStream()); + dos.writeUTF("Protocol:CLI-connect"); + + channel = new Channel("channel", pool, + new BufferedInputStream(s.getInputStream()), + new BufferedOutputStream(s.getOutputStream())); + } else { + // connect via HTTP + LOGGER.fine("Trying to connect to "+url+" via HTTP"); + url+="cli"; + hudson = new URL(url); + + FullDuplexHttpStream con = new FullDuplexHttpStream(hudson); + channel = new Channel("Chunked connection to "+hudson, + pool,con.getInputStream(),con.getOutputStream()); + new PingThread(channel,30*1000) { + protected void onDead() { + // noop. the point of ping is to keep the connection alive + // as most HTTP servers have a rather short read time out + } + }.start(); + } // execute the command entryPoint = (CliEntryPoint)channel.waitForRemoteProperty(CliEntryPoint.class.getName()); @@ -79,6 +103,17 @@ public class CLI { throw new IOException(Messages.CLI_VersionMismatch()); } + /** + * If the server advertises CLI port, returns it. + */ + private int getCliTcpPort(String url) throws IOException { + URLConnection head = new URL(url).openConnection(); + head.connect(); + String p = head.getHeaderField("X-Hudson-CLI-Port"); + if(p==null) return -1; + return Integer.parseInt(p); + } + public void close() throws IOException, InterruptedException { channel.close(); channel.join(); @@ -132,4 +167,6 @@ public class CLI { System.err.println(Messages.CLI_Usage()); System.exit(-1); } + + private static final Logger LOGGER = Logger.getLogger(CLI.class.getName()); } diff --git a/core/src/main/java/hudson/TcpSlaveAgentListener.java b/core/src/main/java/hudson/TcpSlaveAgentListener.java index 00feea7f4a..bd030a8b18 100644 --- a/core/src/main/java/hudson/TcpSlaveAgentListener.java +++ b/core/src/main/java/hudson/TcpSlaveAgentListener.java @@ -24,9 +24,15 @@ package hudson; import hudson.model.Hudson; +import hudson.model.Computer; import hudson.slaves.SlaveComputer; import hudson.remoting.Channel; +import hudson.remoting.SocketOutputStream; +import hudson.remoting.SocketInputStream; import hudson.remoting.Channel.Listener; +import hudson.remoting.Channel.Mode; +import hudson.cli.CliManagerImpl; +import hudson.cli.CliEntryPoint; import java.io.DataInputStream; import java.io.IOException; @@ -40,7 +46,7 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * Listens to incoming TCP connections from JNLP slave agents. + * Listens to incoming TCP connections from JNLP slave agents and CLI. * *
@@ -151,6 +157,8 @@ public final class TcpSlaveAgentListener extends Thread {
String protocol = s.substring(9);
if(protocol.equals("JNLP-connect")) {
runJnlpConnect(in, out);
+ } else if(protocol.equals("CLI-connect")) {
+ runCliConnect(in, out);
} else {
error(out, "Unknown protocol:" + s);
}
@@ -174,6 +182,19 @@ public final class TcpSlaveAgentListener extends Thread {
}
}
+ /**
+ * Handles CLI connection request.
+ */
+ private void runCliConnect(DataInputStream in, PrintWriter out) throws IOException, InterruptedException {
+ out.println("Welcome");
+ Channel channel = new Channel("CLI channel from " + s.getInetAddress(),
+ Computer.threadPoolForRemoting, Mode.BINARY,
+ new BufferedInputStream(new SocketInputStream(this.s)),
+ new BufferedOutputStream(new SocketOutputStream(this.s)), null, true);
+ channel.setProperty(CliEntryPoint.class.getName(),new CliManagerImpl(null));
+ channel.join();
+ }
+
/**
* Handles JNLP slave agent connection request.
*/
diff --git a/core/src/main/resources/hudson/model/View/index.jelly b/core/src/main/resources/hudson/model/View/index.jelly
index 29cd7c3ee9..d6873e732a 100644
--- a/core/src/main/resources/hudson/model/View/index.jelly
+++ b/core/src/main/resources/hudson/model/View/index.jelly
@@ -25,6 +25,10 @@ THE SOFTWARE.