From c00706f4e80df2376064c4aa4e74b9568dc58d3f Mon Sep 17 00:00:00 2001 From: kohsuke Date: Wed, 9 Jun 2010 19:33:39 +0000 Subject: [PATCH] added a method to obtain a channel back to the master. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@31873 71c3de6d-444a-0410-be80-ed276b4c234a --- .../java/hudson/slaves/SlaveComputer.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/src/main/java/hudson/slaves/SlaveComputer.java b/core/src/main/java/hudson/slaves/SlaveComputer.java index da50ccb750..f4072a9725 100644 --- a/core/src/main/java/hudson/slaves/SlaveComputer.java +++ b/core/src/main/java/hudson/slaves/SlaveComputer.java @@ -24,6 +24,7 @@ package hudson.slaves; import hudson.model.*; +import hudson.model.Hudson.MasterComputer; import hudson.remoting.Channel; import hudson.remoting.VirtualChannel; import hudson.remoting.Callable; @@ -548,8 +549,31 @@ public class SlaveComputer extends Computer { // ignore this error. } + Channel.current().setProperty("slave",Boolean.TRUE); // indicate that this side of the channel is the slave side. + return null; } private static final long serialVersionUID = 1L; } + + /** + * Obtains a {@link VirtualChannel} that allows some computation to be performed on the master. + * This method can be called from any thread on the master, or from slave (more precisely, + * it only works from the remoting request-handling thread in slaves, which means if you've started + * separate thread on slaves, that'll fail.) + * + * @return null if the calling thread doesn't have any trace of where its master is. + * @since 1.362 + */ + public static VirtualChannel getChannelToMaster() { + if (Hudson.getInstance()!=null) + return MasterComputer.localChannel; + + // if this method is called from within the slave computation thread, this should work + Channel c = Channel.current(); + if (c!=null && c.getProperty("slave")==Boolean.TRUE) + return c; + + return null; + } }