Skip to content

Commit

Permalink
[FIXED JENKINS-10611] fall back to HTTP if direct TCP connection doesn't
Browse files Browse the repository at this point in the history
work.
  • Loading branch information
kohsuke committed Aug 10, 2011
1 parent 47e82f1 commit 5f756c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -70,6 +70,9 @@
(<a href="https://github.com/jenkinsci/jenkins/pull/195">pull request 195</a>)
<li class=rfe>
Added a dignosis CLI command to report the current granted authorities.
<li class=rfe>
If CLI fails to connect via a JNLP Slave port, fall back to HTTP full-duplex.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-10611">issue 10611</a>)
<li class=rfe>
Fixed unclear text for Tabs with no jobs
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-9330">issue 9330</a>)
Expand Down
63 changes: 40 additions & 23 deletions cli/src/main/java/hudson/cli/CLI.java
Expand Up @@ -63,6 +63,7 @@
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.logging.Level.*;
Expand All @@ -89,34 +90,21 @@ public CLI(URL jenkins, ExecutorService exec) throws IOException, InterruptedExc
ownsPool = exec==null;
pool = exec!=null ? exec : Executors.newCachedThreadPool();

Channel channel = null;
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("CLI connection to "+jenkins, pool,
new BufferedInputStream(new SocketInputStream(s)),
new BufferedOutputStream(new SocketOutputStream(s)));
} else {
try {
channel = connectViaCliPort(jenkins, url, clip);
} catch (IOException e) {
LOGGER.log(Level.FINE,"Failed to connect via CLI port. Falling back to HTTP",e);
}
}
if (channel==null) {
// connect via HTTP
LOGGER.fine("Trying to connect to "+url+" via HTTP");
url+="cli";
jenkins = new URL(url);

FullDuplexHttpStream con = new FullDuplexHttpStream(jenkins);
channel = new Channel("Chunked connection to "+jenkins,
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();
channel = connectViaHttp(url);
}
this.channel = channel;

// execute the command
entryPoint = (CliEntryPoint)channel.waitForRemoteProperty(CliEntryPoint.class.getName());
Expand All @@ -125,6 +113,35 @@ protected void onDead() {
throw new IOException(Messages.CLI_VersionMismatch());
}

private Channel connectViaHttp(String url) throws IOException {
LOGGER.fine("Trying to connect to "+url+" via HTTP");
url+="cli";
URL jenkins = new URL(url);

FullDuplexHttpStream con = new FullDuplexHttpStream(jenkins);
Channel ch = new Channel("Chunked connection to "+jenkins,
pool,con.getInputStream(),con.getOutputStream());
new PingThread(ch,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();
return ch;
}

private Channel connectViaCliPort(URL jenkins, String url, int clip) throws IOException {
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");

return new Channel("CLI connection to "+jenkins, pool,
new BufferedInputStream(new SocketInputStream(s)),
new BufferedOutputStream(new SocketOutputStream(s)));
}

/**
* If the server advertises CLI port, returns it.
*/
Expand Down

0 comments on commit 5f756c6

Please sign in to comment.