提交 31cbef43 编写于 作者: K kohsuke

improved OO-ness by using ClockDifference.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3920 71c3de6d-444a-0410-be80-ed276b4c234a
上级 802af115
......@@ -222,7 +222,7 @@
<dependency>
<groupId>org.kohsuke.stapler</groupId>
<artifactId>stapler</artifactId>
<version>1.40</version>
<version>1.41-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
......
......@@ -44,6 +44,7 @@ import hudson.util.CopyOnWriteMap;
import hudson.util.FormFieldValidator;
import hudson.util.MultipartFormDataParser;
import hudson.util.XStream2;
import hudson.util.ClockDifference;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
......@@ -713,8 +714,8 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
return new FilePath(new File(item.getRootDir(),"workspace"));
}
public long getClockDifference() throws IOException {
return 0;
public ClockDifference getClockDifference() {
return ClockDifference.ZERO;
}
public boolean isUseSecurity() {
......
......@@ -4,6 +4,7 @@ import hudson.Launcher;
import hudson.FilePath;
import hudson.node_monitors.NodeMonitor;
import hudson.util.EnumConverter;
import hudson.util.ClockDifference;
import org.apache.commons.beanutils.ConvertUtils;
import java.util.Set;
......@@ -81,13 +82,11 @@ public interface Node {
* Estimates the clock difference with this slave.
*
* @return
* difference in milli-seconds.
* a positive value indicates that the master is ahead of the slave,
* and negative value indicates otherwise.
* always non-null.
* @throws InterruptedException
* if the operation is aborted.
*/
long getClockDifference() throws IOException, InterruptedException;
ClockDifference getClockDifference() throws IOException, InterruptedException;
public enum Mode {
NORMAL("Utilize this slave as much as possible"),
......
......@@ -18,6 +18,7 @@ import hudson.util.NullStream;
import hudson.util.RingBufferLogHandler;
import hudson.util.StreamCopyThread;
import hudson.util.StreamTaskListener;
import hudson.util.ClockDifference;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
......@@ -222,9 +223,9 @@ public final class Slave implements Node, Serializable {
return Hudson.getInstance().getLabel(name);
}
public long getClockDifference() throws IOException, InterruptedException {
public ClockDifference getClockDifference() throws IOException, InterruptedException {
VirtualChannel channel = getComputer().getChannel();
if(channel==null) return 0; // can't check
if(channel==null) return null; // can't check
long startTime = System.currentTimeMillis();
long slaveTime = channel.call(new Callable<Long,RuntimeException>() {
......@@ -234,36 +235,7 @@ public final class Slave implements Node, Serializable {
});
long endTime = System.currentTimeMillis();
return (startTime+endTime)/2 - slaveTime;
}
/**
* Gets the clock difference in HTML string.
*/
public String getClockDifferenceString() {
try {
long diff = getClockDifference();
if(-1000<diff && diff <1000)
return "In sync"; // clock is in sync
long abs = Math.abs(diff);
String s = Util.getTimeSpanString(abs);
if(diff<0)
s += " ahead";
else
s += " behind";
if(abs>100*60) // more than a minute difference
s = "<span class='error'>"+s+"</span>";
return s;
} catch (IOException e) {
return "<span class='error'>Unable to check</span>";
} catch (InterruptedException e) {
return "<span class='error'>Unable to check</span>";
}
return new ClockDifference((startTime+endTime)/2 - slaveTime);
}
public Computer createComputer() {
......
package hudson.util;
import hudson.Util;
import hudson.model.Node;
import java.io.IOException;
/**
* Represents a clock difference. Immutable.
......@@ -50,4 +53,27 @@ public final class ClockDifference {
if(isDangerous()) s = "<span class=error>"+s+"</span>";
return s;
}
public static String toHtml(Node d) {
try {
return d.getClockDifference().toHtml();
} catch (IOException e) {
return FAILED_HTML;
} catch (InterruptedException e) {
return FAILED_HTML;
}
}
/**
* Gets the clock difference in HTML string.
* This version handles null {@link ClockDifference}.
*/
public static String toHtml(ClockDifference d) {
if(d==null) return FAILED_HTML;
return d.toHtml();
}
public static final ClockDifference ZERO = new ClockDifference(0);
private static final String FAILED_HTML = "<span class='error'>Failed to heck</span>";
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册