Changeset 1255


Ignore:
Timestamp:
Oct 22, 2010, 2:50:11 PM (13 years ago)
Author:
Nicklas Nordborg
Message:

It is now possible to upload a torrent file and register it with the bittorrent client. Sometimes it will start downloading, sometimes not. No progress reporting is done and no uploading of files to BASE.

Location:
extensions/net.sf.basedb.torrent/trunk
Files:
9 added
4 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.torrent/trunk/.classpath

    r1251 r1255  
    77  <classpathentry kind="lib" path="lib/compile/BASE2Webclient.jar"/>
    88  <classpathentry kind="lib" path="lib/compile/slf4j-api-1.5.2.jar"/>
     9  <classpathentry kind="lib" path="lib/compile/servlet-api.jar"/>
    910  <classpathentry kind="output" path="bin"/>
    1011</classpath>
  • extensions/net.sf.basedb.torrent/trunk/META-INF/extensions.xml

    r1246 r1255  
    3838  </about>
    3939  <extension
    40     id="net.sf.basedb.clients.torrent.service.TorrentService"
     40    id="net.sf.basedb.clients.torrent.service"
    4141    extends="net.sf.basedb.clients.web.services"
    4242    >
     
    4848    </action-factory>
    4949  </extension>
     50  <extension
     51    id="net.sf.basedb.clients.bittorrent.upload"
     52    extends="net.sf.basedb.clients.web.toolbar.list.file"
     53    >
     54    <about>
     55      <name>Upload torrent</name>
     56      <description>Adds a 'Upload torrent' button to the File manager</description>
     57    </about>
     58    <index>10</index>
     59    <action-factory>
     60      <factory-class>net.sf.basedb.clients.web.extensions.toolbar.FixedButtonFactory</factory-class>
     61      <parameters>
     62        <title>Upload torrent</title>
     63        <onClick>Main.openPopup('$HOME$/upload_torrent.jsp?ID=' + getSessionId(), 'UploadTorrent', 600, 400)</onClick>
     64      </parameters>
     65    </action-factory>
     66  </extension>
    5067</extensions>
  • extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentService.java

    r1253 r1255  
    2626
    2727import hpbtc.protocol.processor.Client;
    28 
     28import hpbtc.protocol.torrent.Torrent;
     29
     30import java.io.File;
    2931import java.io.IOException;
     32import java.io.InputStream;
    3033import java.net.InetAddress;
    3134import java.net.InetSocketAddress;
     35import java.util.ArrayList;
     36import java.util.Collections;
     37import java.util.List;
    3238import java.util.Properties;
    33 
     39import java.util.TimerTask;
     40
     41import net.sf.basedb.core.Application;
    3442import net.sf.basedb.core.Config;
    3543import net.sf.basedb.core.ConfigurationException;
     44import net.sf.basedb.core.DbControl;
     45import net.sf.basedb.core.SessionControl;
     46import net.sf.basedb.util.FileUtil;
    3647import net.sf.basedb.util.Values;
    3748
     
    89100  private final String address;
    90101  private final int port;
     102  private final String username;
     103  private final String password;
     104 
    91105  private volatile boolean isRunning;
    92106  private Client btClient;
    93107  private InetSocketAddress listenAddress;
     108  private List<TorrentManager> torrentManagers;
     109  private SessionControl sc;
     110  private volatile TimerTask serviceTask;
    94111 
    95112  /**
     
    106123    this.address = Values.getStringOrNull(properties.getProperty("torrents.listen-address"));
    107124    this.port = Values.getInt(properties.getProperty("torrents.listen-port"));
     125    this.username = "root";
     126    this.password = "root";
    108127  }
    109128
     
    126145    log.debug("Listen address: " + address);
    127146    log.debug("Listen port: " + port);
     147    log.debug("Username: " + username);
    128148   
    129149    // Check if the working directory exists...
     
    142162      try
    143163      {
    144         tmpClient = new Client();
     164        // Login to BASE
     165        log.debug("Logging in to BASE as user '" + username + "'");
     166        sc = Application.newSessionControl(null, "local-bittorrent", null);
     167        sc.login(username, password, "Bittorrent download service", false);
     168        log.debug("Login successful");
     169
     170        // Create a bittorrent client
    145171        InetAddress tmpAddress = null;
    146172        if (address != null) tmpAddress = InetAddress.getByName(address);
    147173        InetSocketAddress socketAddress = new InetSocketAddress(tmpAddress, port);
     174        log.debug("Creating bittorrent client on " + socketAddress);
     175        tmpClient = new Client();
    148176        listenAddress = tmpClient.startProtocol(socketAddress);
    149177        log.debug("Connected to: " + listenAddress);
     
    155183      }
    156184     
    157       btClient = tmpClient;
    158      
    159     isRunning = true;
     185      this.btClient = tmpClient;
     186    this.torrentManagers = Collections.synchronizedList(new ArrayList<TorrentManager>());
     187    this.isRunning = true;
    160188    log.info("Bittorrent Download Service has been started");
    161189  }
     
    169197    if (!isRunning()) return;
    170198    log.info("Stopping Bittorrent Download Service");
    171    
     199
     200    isRunning = false;
     201
     202    if (serviceTask != null) serviceTask.cancel();
     203    serviceTask = null;
     204    if (sc != null) closeSessionControl();
     205    sc = null;
     206    if (btClient != null) closeBtClient();
     207    btClient = null;
     208    torrentManagers.clear();
     209    torrentManagers = null;
     210    service = null;
     211    log.info("Bittorrent Download Service has been stopped");
     212  }
     213
     214  /**
     215    Safely close the session control. Catches all exceptions but log
     216    them to the logger.
     217  */
     218  private void closeSessionControl()
     219  {
    172220    try
    173221    {
    174       if (btClient != null)
    175       {
    176         btClient.stopProtocol();
    177         btClient = null;
    178       }
    179     }
    180     finally
    181     {
    182       isRunning = false;
    183       service = null;
    184       log.info("Bittorrent Download Service has been stopped");
    185     }
    186   }
    187 
     222      sc.close();
     223    }
     224    catch (Throwable t)
     225    {
     226      log.warn("Exception when closing SessionControl" , t);
     227    }
     228  }
     229 
     230  /**
     231    Safely close the Bittorrent client. Catches all exceptions but log
     232    them to the logger.
     233  */
     234  private void closeBtClient()
     235  {
     236    try
     237    {
     238      btClient.stopProtocol();
     239    }
     240    catch (Throwable t)
     241    {
     242      log.warn("Exception when closing Bittorrent client" , t);
     243    }
     244  }
     245
     246 
     247  /**
     248    Create a new torrent manager for the torrent with the given name.
     249    This method will create a new temporary working directory for the
     250    torrent manager to store the torrent, the downloaded files and
     251    other data.
     252   
     253    @param torrentName Usually the filename of the torrent file
     254    @return A new torrent manager
     255    @throws IOException If the temporary working directory can't be
     256      created
     257    @throws IllegalStateException If the service has not been started
     258  */
     259  public synchronized TorrentManager newTorrentManager(String torrentName)
     260    throws IOException
     261  {
     262    if (!isRunning()) throw new IllegalStateException("The service has not been started.");
     263   
     264    // Create a sub-dir for this torrent
     265    String tmpDir = torrentName;
     266    if (torrentName.endsWith(".torrent"))
     267    {
     268      tmpDir = torrentName.substring(0, torrentName.length()-8);
     269    }
     270    File subDir = FileUtil.createTempDirectory(tmpDir, ".torrent", workDir);
     271   
     272    // Create a Torrent manager
     273    TorrentManager manager = new TorrentManager(this, subDir, torrentName);
     274    torrentManagers.add(manager);
     275   
     276    // Create the service task if needed
     277      if (serviceTask == null)
     278      {
     279          serviceTask = Application.getScheduler().schedule(new TorrentServiceStateCheckerTask(this), 10000, 10000, false);
     280      }
     281    return manager;
     282  }
     283
     284  /**
     285    Remove an existing torrent manager. If this is the last one,
     286    the service task will be closed.
     287  */
     288  synchronized void remove(TorrentManager manager)
     289  {
     290    torrentManagers.remove(manager);
     291    if (torrentManagers.size() == 0)
     292    {
     293      serviceTask.cancel();
     294      serviceTask = null;
     295    }
     296  }
     297
     298  /**
     299    Get a cloned list of torrent managers since the original list
     300    may be modified at any time by other threads.
     301  */
     302  List<TorrentManager> getManagers()
     303  {
     304    return new ArrayList<TorrentManager>(torrentManagers);
     305  }
     306 
     307  /**
     308    Create a new DbControl.
     309  */
     310  DbControl newDbControl()
     311  {
     312    return sc.newDbControl();
     313  }
     314 
     315  /**
     316    Start the actual download of a torrent. Called by the {@link TorrentServiceStateCheckerTask}
     317    when it has detected a {@link TorrentManager} with {@link TorrentState#READY_TO_DOWNLOAD}
     318    state.
     319  */
     320  void startDownload(TorrentManager manager)
     321  {
     322
     323    try
     324    {
     325      // Register torrent with btClient
     326      File downloadDir = manager.getDownloadDir();
     327      downloadDir.mkdir();
     328      InputStream in = FileUtil.getInputStream(manager.getTorrentFile());
     329      Torrent torrent = btClient.download(in, downloadDir.getAbsolutePath());
     330     
     331      // Set torrent manager state to DOWNLOADING
     332      manager.setTorrent(torrent);
     333    }
     334    catch (Exception ex)
     335    {
     336      // Close the torrent manager
     337      manager.close(ex);
     338      log.warn("Could not start download of " + manager.getName(), ex);
     339    }
     340  }
    188341}
  • extensions/net.sf.basedb.torrent/trunk/torrent.properties

    r1253 r1255  
    1515torrents.listen-port  =
    1616
     17# Username and password that the service should use to login to BASE.
     18# The user needs the same permissions as a job agent user.
     19torrents.username =
     20torrents.password =
Note: See TracChangeset for help on using the changeset viewer.