use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class SBC_TagDiscovery method startScan.
private void startScan() {
try {
mon_scansRemaining.enter();
if (scansRemaining > 0) {
return;
}
} finally {
mon_scansRemaining.exit();
}
CoreFactory.addCoreRunningListener(new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
GlobalManager gm = core.getGlobalManager();
try {
try {
mon_scansRemaining.enter();
scansRemaining = 0;
} finally {
mon_scansRemaining.exit();
}
RelatedContentManager rcm = RelatedContentManager.getSingleton();
List<DownloadManager> dms = gm.getDownloadManagers();
for (final DownloadManager dm : dms) {
if (tv == null) {
return;
}
TOTorrent torrent = dm.getTorrent();
if (torrent == null) {
continue;
}
try {
final byte[] hash = torrent.getHash();
try {
mon_scansRemaining.enter();
scansRemaining++;
if (vitalityImage != null && scansRemaining == 1) {
vitalityImage.setVisible(true);
}
if (soTitle != null) {
soTitle.setText(MessageText.getString("tag.discovery.view.heading") + " : Scanning " + scansRemaining);
}
} finally {
mon_scansRemaining.exit();
}
try {
rcm.lookupAttributes(hash, dm.getDownloadState().getNetworks(), new RelatedAttributeLookupListener() {
@Override
public void tagFound(String tag, String network) {
if (DEBUG) {
System.out.println("Tag Search: Found Tag " + tag + " for " + dm.getDisplayName());
}
if (tv == null) {
return;
}
String key = Base32.encode(hash) + tag;
TagManager tm = TagManagerFactory.getTagManager();
TagType tt_manual = tm.getTagType(TagType.TT_DOWNLOAD_MANUAL);
List<Tag> existingDMTags = tt_manual.getTagsForTaggable(dm);
for (Tag existingTag : existingDMTags) {
if (existingTag.getTagName(true).equalsIgnoreCase(tag)) {
return;
}
}
synchronized (mapTagDiscoveries) {
if (!mapTagDiscoveries.containsKey(key)) {
TagDiscovery tagDiscovery = new TagDiscovery(tag, network, dm.getDisplayName(), hash);
mapTagDiscoveries.put(key, tagDiscovery);
ViewTitleInfoManager.refreshTitleInfo(SBC_TagDiscovery.this);
tv.addDataSource(tagDiscovery);
}
}
}
@Override
public void lookupStart() {
if (DEBUG) {
System.out.println("Tag Search: Start" + " for " + dm.getDisplayName());
}
}
@Override
public void lookupFailed(ContentException error) {
if (DEBUG) {
System.out.println("Tag Search: Failed " + error.getMessage() + " for " + dm.getDisplayName());
}
}
@Override
public void lookupComplete() {
decreaseScansRemaining();
if (DEBUG) {
System.out.println("Tag Search: Complete" + " for " + dm.getDisplayName());
}
}
});
} catch (Throwable e) {
// can get here if the scan never gets kicked off (dht unavailable for network etc)
decreaseScansRemaining();
}
} catch (TOTorrentException e) {
e.printStackTrace();
}
}
} catch (ContentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class GlobalManagerImpl method exportDownloadStateToMapSupport.
private Map exportDownloadStateToMapSupport(DownloadManager dm, boolean internal_export) {
DownloadManagerStats dm_stats = dm.getStats();
Map<String, Object> dmMap = new HashMap<>();
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
try {
dmMap.put("torrent_hash", torrent.getHash());
} catch (TOTorrentException e) {
Debug.printStackTrace(e);
}
}
File save_loc = dm.getAbsoluteSaveLocation();
dmMap.put("persistent", new Long(dm.isPersistent() ? 1 : 0));
dmMap.put("torrent", dm.getTorrentFileName());
dmMap.put("save_dir", save_loc.getParent());
dmMap.put("save_file", save_loc.getName());
dmMap.put("maxdl", new Long(dm_stats.getDownloadRateLimitBytesPerSecond()));
dmMap.put("maxul", new Long(dm_stats.getUploadRateLimitBytesPerSecond()));
int state = dm.getState();
if (state == DownloadManager.STATE_ERROR) {
// torrents in error state always come back stopped
state = DownloadManager.STATE_STOPPED;
} else if (dm.getAssumedComplete() && !dm.isForceStart() && state != DownloadManager.STATE_STOPPED) {
state = DownloadManager.STATE_QUEUED;
} else if (state != DownloadManager.STATE_STOPPED && state != DownloadManager.STATE_QUEUED && state != DownloadManager.STATE_WAITING) {
state = DownloadManager.STATE_WAITING;
}
dmMap.put("state", new Long(state));
if (internal_export) {
dmMap.put("position", new Long(dm.getPosition()));
}
dmMap.put("downloaded", new Long(dm_stats.getTotalDataBytesReceived()));
dmMap.put("uploaded", new Long(dm_stats.getTotalDataBytesSent()));
dmMap.put("completedbytes", new Long(dm_stats.getDownloadCompletedBytes()));
dmMap.put("discarded", new Long(dm_stats.getDiscarded()));
dmMap.put("hashfailbytes", new Long(dm_stats.getHashFailBytes()));
dmMap.put("forceStart", new Long(dm.isForceStart() && (dm.getState() != DownloadManager.STATE_CHECKING) ? 1 : 0));
dmMap.put("secondsDownloading", new Long(dm_stats.getSecondsDownloading()));
dmMap.put("secondsOnlySeeding", new Long(dm_stats.getSecondsOnlySeeding()));
// although this has been migrated, keep storing it to allow regression for a while
dmMap.put("uploads", new Long(dm.getMaxUploads()));
dmMap.put("creationTime", new Long(dm.getCreationTime()));
// save file priorities
dm.saveDownload();
List file_priorities = (List) dm.getUserData("file_priorities");
if (file_priorities != null) {
int count = file_priorities.size();
Map<String, String> map_file_priorities = new HashMap<>();
Long priority = (Long) file_priorities.get(0);
int posStart = 0;
int posEnd = 0;
while (posStart < count) {
priority = (Long) file_priorities.get(posStart);
while (posEnd + 1 < count && (Long) file_priorities.get(posEnd + 1) == priority) {
posEnd++;
}
String key = priority.toString();
String val = map_file_priorities.get(key);
if (val == null) {
val = "" + posStart;
} else {
val += "," + posStart;
}
if (posStart != posEnd) {
val += "-" + posEnd;
}
map_file_priorities.put(key, val);
posStart = posEnd + 1;
}
// dmMap.put( "file_priorities" , file_priorities );
dmMap.put("file_priorities_c", map_file_priorities);
}
dmMap.put("allocated", new Long(dm.isDataAlreadyAllocated() ? 1 : 0));
return (dmMap);
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentCreateImpl method getTotalFileSizeSupport.
protected long getTotalFileSizeSupport(File file, String root) throws TOTorrentException {
String name = file.getName();
if (name.equals(".") || name.equals("..")) {
return (0);
}
if (!file.exists()) {
throw (new TOTorrentException("TOTorrentCreate: file '" + file.getName() + "' doesn't exist", TOTorrentException.RT_FILE_NOT_FOUND));
}
if (file.isFile()) {
if (!ignoreFile(name)) {
total_file_count++;
if (root.length() > 0) {
name = root + File.separator + name;
}
File link = linkage_map.get(name);
return (link == null ? file.length() : link.length());
} else {
return (0);
}
} else {
File[] dir_files = file.listFiles();
if (dir_files == null) {
throw (new TOTorrentException("TOTorrentCreate: directory '" + file.getAbsolutePath() + "' returned error when listing files in it", TOTorrentException.RT_FILE_NOT_FOUND));
}
long length = 0;
if (root.length() == 0) {
root = name;
} else {
root = root + File.separator + name;
}
for (int i = 0; i < dir_files.length; i++) {
length += getTotalFileSizeSupport(dir_files[i], root);
}
return (length);
}
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentCreateImpl method processDir.
private int processDir(TOTorrentFileHasher hasher, File dir, List<TOTorrentFileImpl> encoded, String base_name, String root) throws TOTorrentException {
File[] dir_file_list = dir.listFiles();
if (dir_file_list == null) {
throw (new TOTorrentException("TOTorrentCreate: directory '" + dir.getAbsolutePath() + "' returned error when listing files in it", TOTorrentException.RT_FILE_NOT_FOUND));
}
// sort contents so that multiple encodes of a dir always
// generate same torrent
List<File> file_list = new ArrayList<>(Arrays.asList(dir_file_list));
if (file_comparator == null) {
Collections.sort(file_list);
} else {
Collections.sort(file_list, file_comparator);
}
long offset = 0;
int ignored = 0;
for (int i = 0; i < file_list.size(); i++) {
File file = (File) file_list.get(i);
String file_name = file.getName();
if (!(file_name.equals(".") || file_name.equals(".."))) {
if (file.isDirectory()) {
if (root.length() > 0) {
file_name = root + File.separator + file_name;
}
ignored += processDir(hasher, file, encoded, base_name, file_name);
} else {
if (ignoreFile(file_name)) {
ignored++;
} else {
if (root.length() > 0) {
file_name = root + File.separator + file_name;
}
File link = linkage_map.get(base_name + File.separator + file_name);
if (link != null) {
linked_tf_map.put(String.valueOf(encoded.size()), link.getAbsolutePath());
}
long length = hasher.add(link == null ? file : link);
TOTorrentFileImpl tf = new TOTorrentFileImpl(this, i, offset, length, file_name);
offset += length;
if (add_other_hashes) {
byte[] ed2k_digest = hasher.getPerFileED2KDigest();
byte[] sha1_digest = hasher.getPerFileSHA1Digest();
// System.out.println( "file:ed2k = " + ByteFormatter.nicePrint( ed2k_digest, true ));
// System.out.println( "file:sha1 = " + ByteFormatter.nicePrint( sha1_digest, true ));
tf.setAdditionalProperty("sha1", sha1_digest);
tf.setAdditionalProperty("ed2k", ed2k_digest);
}
encoded.add(tf);
}
}
}
}
return (ignored);
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TrackerWebPageResponseImpl method writeTorrent.
@Override
public void writeTorrent(TrackerTorrent tracker_torrent) throws IOException {
try {
TRHostTorrent host_torrent = ((TrackerTorrentImpl) tracker_torrent).getHostTorrent();
TOTorrent torrent = host_torrent.getTorrent();
// make a copy of the torrent
TOTorrent torrent_to_send = TOTorrentFactory.deserialiseFromMap(torrent.serialiseToMap());
// remove any non-standard stuff (e.g. resume data)
torrent_to_send.removeAdditionalProperties();
if (!TorrentUtils.isDecentralised(torrent_to_send)) {
URL[][] url_sets = TRTrackerUtils.getAnnounceURLs();
if (host_torrent.getStatus() != TRHostTorrent.TS_PUBLISHED && url_sets.length > 0) {
if (COConfigurationManager.getBooleanParameter("Tracker Host Add Our Announce URLs")) {
String protocol = torrent_to_send.getAnnounceURL().getProtocol();
for (int i = 0; i < url_sets.length; i++) {
URL[] urls = url_sets[i];
if (urls[0].getProtocol().equalsIgnoreCase(protocol)) {
torrent_to_send.setAnnounceURL(urls[0]);
torrent_to_send.getAnnounceURLGroup().setAnnounceURLSets(new TOTorrentAnnounceURLSet[0]);
for (int j = 1; j < urls.length; j++) {
TorrentUtils.announceGroupsInsertLast(torrent_to_send, new URL[] { urls[j] });
}
break;
}
}
}
}
}
baos.write(BEncoder.encode(torrent_to_send.serialiseToMap()));
setContentType("application/x-bittorrent");
} catch (TOTorrentException e) {
Debug.printStackTrace(e);
throw (new IOException(e.toString()));
}
}
Aggregations