Commits

Pavel.Sher  committed d89bb6d

new config parameter (without UI) - hostname of the torrent tracker

  • Participants
  • Parent commits 3e87ac5

Comments (0)

Files changed (3)

File server/src/jetbrains/buildServer/artifactsMirror/TorrentConfigurator.java

 
 package jetbrains.buildServer.artifactsMirror;
 
+import jetbrains.buildServer.RootUrlHolder;
 import jetbrains.buildServer.XmlRpcHandlerManager;
 import jetbrains.buildServer.log.Loggers;
 import jetbrains.buildServer.serverSide.BuildServerAdapter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Properties;
 
 public class TorrentConfigurator implements TorrentTrackerConfiguration {
   public final static String TRACKER_ENABLED = "torrent.tracker.enabled";
+  public final static String TRACKER_HOST = "torrent.tracker.host";
   public final static String SEEDER_ENABLED = "torrent.seeder.enabled";
   public final static String FILE_SIZE_THRESHOLD = "file.size.threshold.mb";
   public final static String MAX_NUMBER_OF_SEEDED_TORRENTS = "max.seeded.torrents.number";
   private final ServerTorrentsDirectorySeeder mySeederManager;
   private final TorrentTrackerManager myTrackerManager;
   private final ServerPaths myServerPaths;
+  private final RootUrlHolder myRootUrlHolder;
   private volatile Properties myConfiguration;
 
   public TorrentConfigurator(@NotNull ServerPaths serverPaths,
                              @NotNull ServerTorrentsDirectorySeeder torrentsDirectorySeeder,
                              @NotNull TorrentTrackerManager trackerManager,
                              @NotNull XmlRpcHandlerManager xmlRpcHandlerManager,
-                             @NotNull final ExecutorServices executors) {
+                             @NotNull final ExecutorServices executors,
+                             @NotNull RootUrlHolder rootUrlHolder) {
+    myRootUrlHolder = rootUrlHolder;
     mySeederManager = torrentsDirectorySeeder;
     myTrackerManager = trackerManager;
     myServerPaths = serverPaths;
       public void serverStartup() {
         super.serverStartup();
         if (isEnabled(TRACKER_ENABLED)) {
-          myTrackerManager.startTracker();
+          myTrackerManager.startTracker(getTrackerAddress());
         }
         executors.getLowPriorityExecutorService().submit(new Runnable() {
           public void run() {
     myConfiguration.setProperty(TRACKER_ENABLED, Boolean.toString(enabled));
     if (changed) {
       if (enabled) {
-        myTrackerManager.startTracker();
+        myTrackerManager.startTracker(getTrackerAddress());
       } else {
         myTrackerManager.stopTracker();
       }
   public String getAnnounceUrl() {
     return myTrackerManager.getAnnounceUrl();
   }
+
+  @NotNull
+  private String getTrackerAddress() {
+    String hostName = myConfiguration.getProperty(TRACKER_HOST);
+    if (hostName != null) return hostName;
+
+    try {
+      String rootUrl = myRootUrlHolder.getRootUrl();
+      if (rootUrl.endsWith("/")) rootUrl = rootUrl.substring(0, rootUrl.length()-1);
+
+      URI serverUrl = new URI(rootUrl);
+      return serverUrl.getHost();
+    } catch (URISyntaxException e) {
+      return "127.0.0.1";
+    }
+  }
 }

File server/src/jetbrains/buildServer/artifactsMirror/TorrentTrackerManager.java

 package jetbrains.buildServer.artifactsMirror;
 
-import jetbrains.buildServer.RootUrlHolder;
 import jetbrains.buildServer.artifactsMirror.torrent.TorrentTracker;
 import jetbrains.buildServer.artifactsMirror.torrent.TorrentUtil;
 import org.jetbrains.annotations.NotNull;
 
 public class TorrentTrackerManager {
   private final TorrentTracker myTorrentTracker;
-  private final  RootUrlHolder myRootUrlHolder;
 
-  public TorrentTrackerManager(@NotNull final RootUrlHolder rootUrlHolder) {
+  public TorrentTrackerManager() {
     myTorrentTracker = new TorrentTracker();
-    myRootUrlHolder = rootUrlHolder;
   }
 
-  public void startTracker() {
+  public void startTracker(@NotNull String trackerAddress) {
     stopTracker();
-    myTorrentTracker.start(myRootUrlHolder.getRootUrl());
+    myTorrentTracker.start(trackerAddress);
   }
 
   public void stopTracker() {

File torrent/src/jetbrains/buildServer/artifactsMirror/torrent/TorrentTracker.java

   public TorrentTracker() {
   }
 
-  public void start(@NotNull String rootUrl) {
+  public void start(@NotNull String trackerAddress) {
     int freePort = NetworkUtil.getFreePort(6969);
 
     try {
-      InetAddress serverAddress = getServerAddress(rootUrl);
+      InetAddress serverAddress = getServerAddress(trackerAddress);
       myTracker = new Tracker(new InetSocketAddress(serverAddress, freePort));
       myTracker.setAcceptForeignTorrents(true);
       myTracker.start();
   }
 
   @NotNull
-  public static InetAddress getServerAddress(@NotNull String rootUrl) throws URISyntaxException, UnknownHostException {
-    if (rootUrl.endsWith("/")) rootUrl = rootUrl.substring(0, rootUrl.length()-1);
-    URI serverUrl = new URI(rootUrl);
-    return InetAddress.getByName(serverUrl.getHost());
+  public static InetAddress getServerAddress(@NotNull String trackerAddress) throws UnknownHostException {
+    return InetAddress.getByName(trackerAddress);
   }
 
   public void stop() {