use of com.biglybt.core.tracker.host.TRHostTorrent in project BiglyBT by BiglySoftware.
the class TorrentCommand method execute.
@Override
public void execute(String commandName, ConsoleInput ci, List<String> args) {
if (!args.isEmpty()) {
String subcommand = (String) args.remove(0);
if (ci.torrents.isEmpty()) {
ci.out.println("> Command '" + getCommandName() + "': No torrents in list (Maybe you forgot to 'show torrents' first).");
} else {
String name;
DownloadManager dm;
try {
int number = Integer.parseInt(subcommand);
if ((number > 0) && (number <= ci.torrents.size())) {
dm = (DownloadManager) ci.torrents.get(number - 1);
if (dm.getDisplayName() == null)
name = "?";
else
name = dm.getDisplayName();
performCommandIfAllowed(ci, args, dm, "#" + subcommand, name);
} else
ci.out.println("> Command '" + getCommandName() + "': Torrent #" + subcommand + " unknown.");
} catch (NumberFormatException e) {
if ("all".equalsIgnoreCase(subcommand)) {
Iterator torrent = ci.torrents.iterator();
while (torrent.hasNext()) {
dm = (DownloadManager) torrent.next();
if (dm.getDisplayName() == null)
name = "?";
else
name = dm.getDisplayName();
performCommandIfAllowed(ci, args, dm, subcommand, name);
}
} else if ("hash".equalsIgnoreCase(subcommand)) {
String hash = (String) args.remove(0);
List torrents = ci.getGlobalManager().getDownloadManagers();
boolean foundit = false;
Iterator torrent = torrents.iterator();
while (torrent.hasNext()) {
dm = (DownloadManager) torrent.next();
if (hash.equals(TorrentUtils.nicePrintTorrentHash(dm.getTorrent(), true))) {
if (dm.getDisplayName() == null)
name = "?";
else
name = dm.getDisplayName();
// FIXME: check user permission here and fix it to take torrent hash instead of subcommand
performCommandIfAllowed(ci, args, dm, hash, name);
foundit = true;
break;
}
}
if (!foundit) {
// second check for
TRHost host = ci.getCore().getTrackerHost();
if (host != null) {
TRHostTorrent[] h_torrents = host.getTorrents();
for (int i = 0; i < h_torrents.length; i++) {
TRHostTorrent ht = h_torrents[i];
if (hash.equals(TorrentUtils.nicePrintTorrentHash(ht.getTorrent(), true))) {
name = TorrentUtils.getLocalisedName(ht.getTorrent());
// FIXME: check user permission here and fix it to take torrent hash instead of subcommand
performCommandIfAllowed(ci, args, ht, hash, name);
foundit = true;
break;
}
}
}
}
if (!foundit) {
ci.out.println("> Command '" + getCommandName() + "': Hash '" + hash + "' unknown.");
}
} else {
ci.out.println("> Command '" + getCommandName() + "': Subcommand '" + subcommand + "' unknown.");
}
}
}
} else {
ci.out.println("> Missing subcommand for '" + getCommandName() + "'");
printHelp(ci.out, args);
}
}
use of com.biglybt.core.tracker.host.TRHostTorrent in project BiglyBT by BiglySoftware.
the class TorrentPublish method performCommand.
@Override
protected boolean performCommand(ConsoleInput ci, DownloadManager dm, List args) {
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
try {
TRHost host = ci.core.getTrackerHost();
TRHostTorrent existing = host.getHostTorrent(torrent);
if (existing == null) {
host.publishTorrent(torrent);
} else {
try {
existing.remove();
} catch (Throwable e) {
e.printStackTrace();
}
}
} catch (TRHostException e) {
e.printStackTrace(ci.out);
return false;
}
return true;
}
return false;
}
use of com.biglybt.core.tracker.host.TRHostTorrent in project BiglyBT by BiglySoftware.
the class TRHostConfigImpl method loadConfig.
protected void loadConfig(TRHostTorrentFinder finder) {
try {
this_mon.enter();
loading = true;
Map map = FileUtil.readResilientConfigFile("tracker.config");
List torrents = (List) map.get("torrents");
if (torrents == null) {
return;
}
Iterator iter = torrents.iterator();
while (iter.hasNext()) {
Map t_map = (Map) iter.next();
Long persistent_l = (Long) t_map.get("persistent");
boolean persistent = persistent_l == null || persistent_l.longValue() == 1;
Long passive_l = (Long) t_map.get("passive");
boolean passive = passive_l != null && passive_l.longValue() == 1;
Long dateadded_l = (Long) t_map.get("dateadded");
long date_added = dateadded_l == null ? SystemTime.getCurrentTime() : dateadded_l.longValue();
byte[] hash = (byte[]) t_map.get("hash");
if (persistent) {
int state = ((Long) t_map.get("status")).intValue();
if (state == TRHostTorrent.TS_FAILED) {
state = TRHostTorrent.TS_STOPPED;
}
TOTorrent torrent = finder.lookupTorrent(hash);
if (torrent == null && passive) {
byte[] file_b = (byte[]) t_map.get("torrent_file");
if (file_b != null) {
try {
File file = new File(new String(file_b, Constants.BYTE_ENCODING));
torrent = TorrentUtils.readFromFile(file, true, true);
} catch (Throwable e) {
// torrent might have been deleted, don't barf out errors
// Debug.printStackTrace( e );
}
}
}
if (torrent != null) {
TRHostTorrent ht = host.addTorrent(torrent, state, true, passive, date_added);
if (ht instanceof TRHostTorrentHostImpl) {
TRHostTorrentHostImpl hth = (TRHostTorrentHostImpl) ht;
recoverStats(hth, t_map);
}
} else {
if (COConfigurationManager.getBooleanParameter("Tracker Public Enable")) {
host.addExternalTorrent(hash, state, date_added);
}
}
} else {
// store stats for later
saved_stats.put(new HashWrapper(hash), t_map);
}
}
} catch (Exception e) {
Debug.printStackTrace(e);
} finally {
loading = false;
this_mon.exit();
}
}
use of com.biglybt.core.tracker.host.TRHostTorrent in project BiglyBT by BiglySoftware.
the class TRHostConfigImpl method saveConfig.
protected void saveConfig(boolean immediate) {
if (loading) {
return;
}
synchronized (saved_stats_to_delete) {
if (saved_stats_to_delete.size() > 0) {
Map saved_stats_copy = new HashMap(saved_stats);
for (int i = 0; i < saved_stats_to_delete.size(); i++) {
saved_stats_copy.remove(saved_stats_to_delete.get(i));
}
saved_stats_to_delete.clear();
saved_stats = saved_stats_copy;
}
}
if (immediate || save_outstanding) {
save_outstanding = false;
try {
Map map = new HashMap();
List list = new ArrayList();
TRHostTorrent[] torrents = host.getTorrents();
List stats_entries = new ArrayList();
Set added = new HashSet();
for (int i = 0; i < torrents.length; i++) {
try {
TRHostTorrent torrent = (TRHostTorrent) torrents[i];
added.add(torrent.getTorrent().getHashWrapper());
StringBuffer stats_entry = new StringBuffer(2048);
byte[] hash = torrent.getTorrent().getHash();
byte[] name = torrent.getTorrent().getName();
int status = torrent.getStatus();
long completed = torrent.getCompletedCount();
long announces = torrent.getAnnounceCount();
long scrapes = torrent.getScrapeCount();
long uploaded = torrent.getTotalUploaded();
long downloaded = torrent.getTotalDownloaded();
long bytes_in = torrent.getTotalBytesIn();
long bytes_out = torrent.getTotalBytesOut();
long date_added = torrent.getDateAdded();
int seed_count = torrent.getSeedCount();
int non_seed_count = torrent.getLeecherCount();
Map t_map = new HashMap();
t_map.put("persistent", new Long(torrent.isPersistent() ? 1 : 0));
t_map.put("passive", new Long(torrent.isPassive() ? 1 : 0));
if (torrent.isPassive()) {
try {
String file = TorrentUtils.getTorrentFileName(torrent.getTorrent());
t_map.put("torrent_file", file.getBytes(Constants.BYTE_ENCODING));
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
t_map.put("hash", hash);
t_map.put("dateadded", new Long(date_added));
t_map.put("status", new Long(status));
list.add(t_map);
Map s_map = new HashMap();
t_map.put("stats", s_map);
s_map.put("completed", new Long(completed));
s_map.put("announces", new Long(announces));
s_map.put("scrapes", new Long(scrapes));
s_map.put("uploaded", new Long(uploaded));
s_map.put("downloaded", new Long(downloaded));
s_map.put("bytesin", new Long(bytes_in));
s_map.put("bytesout", new Long(bytes_out));
stats_entry.append(new String(name, Constants.DEFAULT_ENCODING));
stats_entry.append(",");
stats_entry.append(ByteFormatter.nicePrint(hash, true));
stats_entry.append(",");
stats_entry.append(status);
stats_entry.append(",");
stats_entry.append(seed_count);
stats_entry.append(",");
stats_entry.append(non_seed_count);
stats_entry.append(",");
stats_entry.append(completed);
stats_entry.append(",");
stats_entry.append(announces);
stats_entry.append(",");
stats_entry.append(scrapes);
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtc(uploaded));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtc(downloaded));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtcPerSec(torrent.getAverageUploaded()));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtcPerSec(torrent.getAverageDownloaded()));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtc(torrent.getTotalLeft()));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtc(bytes_in));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtc(bytes_out));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtcPerSec(torrent.getAverageBytesIn()));
stats_entry.append(",");
stats_entry.append(DisplayFormatters.formatByteCountToKiBEtcPerSec(torrent.getAverageBytesOut()));
stats_entry.append("\r\n");
stats_entries.add(stats_entry);
} catch (TOTorrentException e) {
Debug.printStackTrace(e);
}
}
// now save any non-recovered stats for a while in case the torrent
// gets re-added in the near future
Iterator it = saved_stats.keySet().iterator();
long now = SystemTime.getCurrentTime();
while (it.hasNext()) {
HashWrapper hash = (HashWrapper) it.next();
if (added.contains(hash)) {
continue;
}
Map t_map = (Map) saved_stats.get(hash);
Long backup = (Long) t_map.get("backup_time");
if (backup == null) {
backup = new Long(now);
t_map.put("backup_time", backup);
}
if (now - backup.longValue() < BACKUP_RETENTION_PERIOD) {
list.add(t_map);
added.add(hash);
}
}
map.put("torrents", list);
try {
save_lock_mon.enter();
if (torrents.length == 0) {
if (config_exists) {
FileUtil.deleteResilientConfigFile("tracker.config");
config_exists = false;
}
} else {
config_exists = true;
FileUtil.writeResilientConfigFile("tracker.config", map);
}
if (COConfigurationManager.getBooleanParameter("Tracker Log Enable") && stats_entries.size() > 0) {
try {
String timeStamp = "[" + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").format(new Date()) + "] ";
PrintWriter pw = null;
File file_name = new File(log_dir.concat(File.separator).concat(LOG_FILE_NAME));
try {
pw = new PrintWriter(new FileWriter(file_name, true));
for (int i = 0; i < stats_entries.size(); i++) {
StringBuffer stats_entry = (StringBuffer) stats_entries.get(i);
String str = timeStamp + stats_entry.toString();
pw.print(str);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
} finally {
if (pw != null) {
try {
pw.close();
} catch (Throwable e) {
}
}
}
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
} finally {
save_lock_mon.exit();
}
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
}
use of com.biglybt.core.tracker.host.TRHostTorrent in project BiglyBT by BiglySoftware.
the class PluginCoreUtils method convert.
public static Object convert(Object datasource, boolean toCore) {
if (datasource instanceof Object[]) {
Object[] array = (Object[]) datasource;
Object[] newArray = new Object[array.length];
for (int i = 0; i < array.length; i++) {
Object o = array[i];
newArray[i] = convert(o, toCore);
}
return newArray;
}
try {
if (toCore) {
if (datasource instanceof com.biglybt.core.download.DownloadManager) {
return datasource;
}
if (datasource instanceof DownloadImpl) {
return ((DownloadImpl) datasource).getDownload();
}
if (datasource instanceof com.biglybt.core.disk.DiskManager) {
return datasource;
}
if (datasource instanceof DiskManagerImpl) {
return ((DiskManagerImpl) datasource).getDiskmanager();
}
if (datasource instanceof PEPeerManager) {
return datasource;
}
if (datasource instanceof PeerManagerImpl) {
return ((PeerManagerImpl) datasource).getDelegate();
}
if (datasource instanceof PEPeer) {
return datasource;
}
if (datasource instanceof PeerImpl) {
return ((PeerImpl) datasource).getPEPeer();
}
if (datasource instanceof com.biglybt.core.disk.DiskManagerFileInfo) {
return datasource;
}
if (datasource instanceof com.biglybt.pifimpl.local.disk.DiskManagerFileInfoImpl) {
return ((com.biglybt.pifimpl.local.disk.DiskManagerFileInfoImpl) datasource).getCore();
}
if (datasource instanceof TRHostTorrent) {
return datasource;
}
if (datasource instanceof TrackerTorrentImpl) {
((TrackerTorrentImpl) datasource).getHostTorrent();
}
} else {
// to PI
if (datasource instanceof com.biglybt.core.download.DownloadManager) {
return wrap((com.biglybt.core.download.DownloadManager) datasource);
}
if (datasource instanceof DownloadImpl) {
return datasource;
}
if (datasource instanceof com.biglybt.core.disk.DiskManager) {
return wrap((com.biglybt.core.disk.DiskManager) datasource);
}
if (datasource instanceof DiskManagerImpl) {
return datasource;
}
if (datasource instanceof PEPeerManager) {
return wrap((PEPeerManager) datasource);
}
if (datasource instanceof PeerManagerImpl) {
return datasource;
}
if (datasource instanceof PEPeer) {
return PeerManagerImpl.getPeerForPEPeer((PEPeer) datasource);
}
if (datasource instanceof Peer) {
return datasource;
}
if (datasource instanceof com.biglybt.core.disk.DiskManagerFileInfo) {
DiskManagerFileInfo fileInfo = (com.biglybt.core.disk.DiskManagerFileInfo) datasource;
if (fileInfo != null) {
try {
DownloadManager dm = fileInfo.getDownloadManager();
return new com.biglybt.pifimpl.local.disk.DiskManagerFileInfoImpl(dm == null ? null : DownloadManagerImpl.getDownloadStatic(dm), fileInfo);
} catch (DownloadException e) {
/* Ignore */
}
}
}
if (datasource instanceof com.biglybt.pifimpl.local.disk.DiskManagerFileInfoImpl) {
return datasource;
}
if (datasource instanceof TRHostTorrent) {
TRHostTorrent item = (TRHostTorrent) datasource;
return new TrackerTorrentImpl(item);
}
if (datasource instanceof TrackerTorrentImpl) {
return datasource;
}
}
} catch (Throwable t) {
Debug.out(t);
}
return datasource;
}
Aggregations