Changeset 1255
- Timestamp:
- Oct 22, 2010, 2:50:11 PM (13 years ago)
- 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 7 7 <classpathentry kind="lib" path="lib/compile/BASE2Webclient.jar"/> 8 8 <classpathentry kind="lib" path="lib/compile/slf4j-api-1.5.2.jar"/> 9 <classpathentry kind="lib" path="lib/compile/servlet-api.jar"/> 9 10 <classpathentry kind="output" path="bin"/> 10 11 </classpath> -
extensions/net.sf.basedb.torrent/trunk/META-INF/extensions.xml
r1246 r1255 38 38 </about> 39 39 <extension 40 id="net.sf.basedb.clients.torrent.service .TorrentService"40 id="net.sf.basedb.clients.torrent.service" 41 41 extends="net.sf.basedb.clients.web.services" 42 42 > … … 48 48 </action-factory> 49 49 </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> 50 67 </extensions> -
extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentService.java
r1253 r1255 26 26 27 27 import hpbtc.protocol.processor.Client; 28 28 import hpbtc.protocol.torrent.Torrent; 29 30 import java.io.File; 29 31 import java.io.IOException; 32 import java.io.InputStream; 30 33 import java.net.InetAddress; 31 34 import java.net.InetSocketAddress; 35 import java.util.ArrayList; 36 import java.util.Collections; 37 import java.util.List; 32 38 import java.util.Properties; 33 39 import java.util.TimerTask; 40 41 import net.sf.basedb.core.Application; 34 42 import net.sf.basedb.core.Config; 35 43 import net.sf.basedb.core.ConfigurationException; 44 import net.sf.basedb.core.DbControl; 45 import net.sf.basedb.core.SessionControl; 46 import net.sf.basedb.util.FileUtil; 36 47 import net.sf.basedb.util.Values; 37 48 … … 89 100 private final String address; 90 101 private final int port; 102 private final String username; 103 private final String password; 104 91 105 private volatile boolean isRunning; 92 106 private Client btClient; 93 107 private InetSocketAddress listenAddress; 108 private List<TorrentManager> torrentManagers; 109 private SessionControl sc; 110 private volatile TimerTask serviceTask; 94 111 95 112 /** … … 106 123 this.address = Values.getStringOrNull(properties.getProperty("torrents.listen-address")); 107 124 this.port = Values.getInt(properties.getProperty("torrents.listen-port")); 125 this.username = "root"; 126 this.password = "root"; 108 127 } 109 128 … … 126 145 log.debug("Listen address: " + address); 127 146 log.debug("Listen port: " + port); 147 log.debug("Username: " + username); 128 148 129 149 // Check if the working directory exists... … … 142 162 try 143 163 { 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 145 171 InetAddress tmpAddress = null; 146 172 if (address != null) tmpAddress = InetAddress.getByName(address); 147 173 InetSocketAddress socketAddress = new InetSocketAddress(tmpAddress, port); 174 log.debug("Creating bittorrent client on " + socketAddress); 175 tmpClient = new Client(); 148 176 listenAddress = tmpClient.startProtocol(socketAddress); 149 177 log.debug("Connected to: " + listenAddress); … … 155 183 } 156 184 157 btClient = tmpClient;158 159 isRunning = true;185 this.btClient = tmpClient; 186 this.torrentManagers = Collections.synchronizedList(new ArrayList<TorrentManager>()); 187 this.isRunning = true; 160 188 log.info("Bittorrent Download Service has been started"); 161 189 } … … 169 197 if (!isRunning()) return; 170 198 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 { 172 220 try 173 221 { 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 } 188 341 } -
extensions/net.sf.basedb.torrent/trunk/torrent.properties
r1253 r1255 15 15 torrents.listen-port = 16 16 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. 19 torrents.username = 20 torrents.password =
Note: See TracChangeset
for help on using the changeset viewer.