use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class GlobalManagerImpl method loadDownload.
public DownloadManager loadDownload(Map mDownload, int currentDownload, int nbDownloads, GlobalMangerProgressListener progress_listener, boolean debug) {
try {
byte[] torrent_hash = (byte[]) mDownload.get("torrent_hash");
Long lPersistent = (Long) mDownload.get("persistent");
boolean persistent = lPersistent == null || lPersistent.longValue() == 1;
String fileName = new String((byte[]) mDownload.get("torrent"), Constants.DEFAULT_ENCODING);
if (progress_listener != null && SystemTime.getCurrentTime() - lastListenerUpdate > 100) {
lastListenerUpdate = SystemTime.getCurrentTime();
String shortFileName = fileName;
try {
File f = new File(fileName);
shortFileName = f.getName();
} catch (Exception e) {
// TODO: handle exception
}
progress_listener.reportPercent(100 * currentDownload / nbDownloads);
progress_listener.reportCurrentTask(MessageText.getString("splash.loadingTorrent") + " " + currentDownload + " " + MessageText.getString("splash.of") + " " + nbDownloads + " : " + shortFileName);
}
// migration from using a single savePath to a separate dir and file entry
String torrent_save_dir;
String torrent_save_file;
byte[] torrent_save_dir_bytes = (byte[]) mDownload.get("save_dir");
if (torrent_save_dir_bytes != null) {
byte[] torrent_save_file_bytes = (byte[]) mDownload.get("save_file");
torrent_save_dir = new String(torrent_save_dir_bytes, Constants.DEFAULT_ENCODING);
if (torrent_save_file_bytes != null) {
torrent_save_file = new String(torrent_save_file_bytes, Constants.DEFAULT_ENCODING);
} else {
torrent_save_file = null;
}
} else {
byte[] savePathBytes = (byte[]) mDownload.get("path");
torrent_save_dir = new String(savePathBytes, Constants.DEFAULT_ENCODING);
torrent_save_file = null;
}
int state = DownloadManager.STATE_WAITING;
if (debug) {
state = DownloadManager.STATE_STOPPED;
} else {
if (mDownload.containsKey("state")) {
state = ((Long) mDownload.get("state")).intValue();
if (state != DownloadManager.STATE_STOPPED && state != DownloadManager.STATE_QUEUED && state != DownloadManager.STATE_WAITING)
state = DownloadManager.STATE_QUEUED;
} else {
int stopped = ((Long) mDownload.get("stopped")).intValue();
if (stopped == 1) {
state = DownloadManager.STATE_STOPPED;
}
}
}
Long seconds_downloading = (Long) mDownload.get("secondsDownloading");
boolean has_ever_been_started = seconds_downloading != null && seconds_downloading.longValue() > 0;
if (torrent_hash != null) {
saved_download_manager_state.put(new HashWrapper(torrent_hash), mDownload);
}
if (persistent) {
List file_priorities;
Map map_file_priorities = (Map) mDownload.get("file_priorities_c");
if (map_file_priorities != null) {
// We don't know how many files, so we need to build array
Long[] array_file_priorities = new Long[0];
for (Object key : map_file_priorities.keySet()) {
long priority = Long.parseLong(key.toString());
String indexRanges = new String((byte[]) map_file_priorities.get(key), "utf-8");
String[] rangesStrings = indexRanges.split(",");
if (array_file_priorities.length == 0 && rangesStrings.length > 1) {
// going to be at least the length of # of ranges
array_file_priorities = new Long[rangesStrings.length];
}
for (String rangeString : rangesStrings) {
String[] ranges = rangeString.split("-");
int start = Integer.parseInt(ranges[0]);
int end = ranges.length == 1 ? start : Integer.parseInt(ranges[1]);
if (end >= array_file_priorities.length) {
array_file_priorities = enlargeLongArray(array_file_priorities, end + 1);
}
Arrays.fill(array_file_priorities, start, end + 1, priority);
}
}
file_priorities = Arrays.asList(array_file_priorities);
} else {
file_priorities = (List) mDownload.get("file_priorities");
}
final DownloadManager dm = DownloadManagerFactory.create(this, torrent_hash, fileName, torrent_save_dir, torrent_save_file, state, true, true, has_ever_been_started, file_priorities);
if (addDownloadManager(dm, false, false) == dm) {
return (dm);
}
}
} catch (UnsupportedEncodingException e1) {
// Do nothing and process next.
} catch (Throwable e) {
Logger.log(new LogEvent(LOGID, "Error while loading downloads. " + "One download may not have been added to the list.", e));
}
return (null);
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class GlobalManagerImpl method loadDownloads.
void loadDownloads() {
try {
if (this.cripple_downloads_config) {
loadingComplete = true;
loadingSem.releaseForever();
return;
}
try {
DownloadManagerStateFactory.loadGlobalStateCache();
int triggerOnCount = 2;
ArrayList<DownloadManager> downloadsAdded = new ArrayList<>();
lastListenerUpdate = 0;
try {
if (progress_listener != null) {
progress_listener.reportCurrentTask(MessageText.getString("splash.loadingTorrents"));
}
Map map = FileUtil.readResilientConfigFile("downloads.config");
boolean debug = Boolean.getBoolean("debug");
Iterator iter = null;
// v2.0.3.0+ vs older mode
List downloads = (List) map.get("downloads");
int nbDownloads;
if (downloads == null) {
// No downloads entry, then use the old way
iter = map.values().iterator();
nbDownloads = map.size();
} else {
// New way, downloads stored in a list
iter = downloads.iterator();
nbDownloads = downloads.size();
}
int currentDownload = 0;
while (iter.hasNext()) {
currentDownload++;
Map mDownload = (Map) iter.next();
DownloadManager dm = loadDownload(mDownload, currentDownload, nbDownloads, progress_listener, debug);
if (dm != null) {
downloadsAdded.add(dm);
if (downloadsAdded.size() >= triggerOnCount) {
triggerOnCount *= 2;
triggerAddListener(downloadsAdded);
downloadsAdded.clear();
}
}
}
// This is set to true by default, but once the downloads have been loaded, we have no reason to ever
// to do this check again - we only want to do it once to upgrade the state of existing downloads
// created before this code was around.
COConfigurationManager.setParameter("Set Completion Flag For Completed Downloads On Start", false);
// load pause/resume state
ArrayList pause_data = (ArrayList) map.get("pause_data");
if (pause_data != null) {
try {
paused_list_mon.enter();
for (int i = 0; i < pause_data.size(); i++) {
Object pd = pause_data.get(i);
byte[] key;
boolean force;
if (pd instanceof byte[]) {
// old style, migration purposes
key = (byte[]) pause_data.get(i);
force = false;
} else {
Map m = (Map) pd;
key = (byte[]) m.get("hash");
force = ((Long) m.get("force")).intValue() == 1;
}
paused_list.add(new Object[] { new HashWrapper(key), Boolean.valueOf(force) });
}
} finally {
paused_list_mon.exit();
}
}
// Someone could have mucked with the config file and set weird positions,
// so fix them up.
fixUpDownloadManagerPositions();
Logger.log(new LogEvent(LOGID, "Loaded " + managers_cow.size() + " torrents"));
} catch (Throwable e) {
// there's been problems with corrupted download files stopping AZ from starting
// added this to try and prevent such foolishness
Debug.printStackTrace(e);
} finally {
loadingComplete = true;
triggerAddListener(downloadsAdded);
loadingSem.releaseForever();
}
} finally {
DownloadManagerStateFactory.discardGlobalStateCache();
}
} finally {
taggable_life_manager.initialized(getResolvedTaggables());
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class TorrentFolderWatcher method importAddedFiles.
void importAddedFiles() {
Core core = CoreFactory.getSingleton();
try {
this_mon.enter();
if (!running) {
return;
}
GlobalManager global_manager = _global_manager;
if (global_manager == null || !core.isStarted()) {
return;
}
com.biglybt.pif.download.DownloadManager plugin_dm = core.getPluginManager().getDefaultPluginInterface().getDownloadManager();
boolean save_torrents_default = COConfigurationManager.getBooleanParameter("Save Torrent Files");
String torrent_save_path = COConfigurationManager.getStringParameter("General_sDefaultTorrent_Directory");
int start_state = COConfigurationManager.getBooleanParameter("Start Watched Torrents Stopped") ? DownloadManager.STATE_STOPPED : DownloadManager.STATE_QUEUED;
int num_folders = COConfigurationManager.getIntParameter("Watch Torrent Folder Path Count", 1);
List<File> folders = new ArrayList<>();
List<String> tags = new ArrayList<>();
for (int i = 0; i < num_folders; i++) {
String folder_path = COConfigurationManager.getStringParameter("Watch Torrent Folder Path" + (i == 0 ? "" : (" " + i)));
File folder = null;
if (folder_path != null && folder_path.length() > 0) {
folder = new File(folder_path);
if (!folder.isDirectory()) {
if (!folder.exists()) {
FileUtil.mkdirs(folder);
}
if (!folder.isDirectory()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Watch Torrent Folder Path] " + "does not exist or " + "is not a dir"));
folder = null;
}
}
}
if (folder != null) {
folders.add(folder);
String tag = COConfigurationManager.getStringParameter("Watch Torrent Folder Tag" + (i == 0 ? "" : (" " + i)), null);
if (tag != null && tag.trim().length() == 0) {
tag = null;
}
tags.add(tag);
}
}
if (folders.isEmpty()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Watch Torrent Folder Path] not configured"));
return;
}
String data_save_path = COConfigurationManager.getStringParameter("Default save path");
File f = null;
if (data_save_path != null && data_save_path.length() > 0) {
f = new File(data_save_path);
// Path is not an existing directory.
if (!f.isDirectory()) {
if (!f.exists()) {
FileUtil.mkdirs(f);
}
// If path is still not a directory, abort.
if (!f.isDirectory()) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Default save path] does not exist or is not a dir"));
}
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "[Default save path] does not exist or is not a dir"));
return;
}
}
}
// If we get here, and this is true, then data_save_path isn't valid.
if (f == null) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Default save path] needs to be set for auto-.torrent-import to work"));
}
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "[Default save path] needs to be set for auto-.torrent-import to work"));
}
for (int i = 0; i < to_delete.size(); i++) {
TOTorrent torrent = (TOTorrent) to_delete.get(i);
try {
TorrentUtils.delete(torrent);
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
to_delete.clear();
for (int folder_index = 0; folder_index < folders.size(); folder_index++) {
File folder = folders.get(folder_index);
final String tag_name = tags.get(folder_index);
// if we are saving torrents to the same location as we import them from
// then we can't assume that its safe to delete the torrent after import!
boolean save_torrents = save_torrents_default;
if (torrent_save_path.length() == 0 || new File(torrent_save_path).getAbsolutePath().equals(folder.getAbsolutePath()) || !new File(torrent_save_path).isDirectory()) {
save_torrents = false;
}
String[] currentFileList = folder.list(filename_filter);
if (currentFileList == null) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "There was a problem trying to get a listing of torrents from " + folder));
} else {
for (int i = 0; i < currentFileList.length; i++) {
if (!running) {
return;
}
File file = new File(folder, currentFileList[i]);
if (file.getName().toLowerCase(Locale.US).endsWith(".magnet")) {
handleMagnet(file);
} else {
try {
TOTorrent torrent = TorrentUtils.readFromFile(file, false);
if (global_manager.getDownloadManager(torrent) != null) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, file.getAbsolutePath() + " is already being downloaded"));
// we can't touch the torrent file as it is (probably)
// being used for the download
} else if (plugin_dm.lookupDownloadStub(torrent.getHash()) != null) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, file.getAbsolutePath() + " is an archived download"));
if (!save_torrents) {
File imported = new File(folder, file.getName() + ".imported");
TorrentUtils.move(file, imported);
} else {
to_delete.add(torrent);
}
} else {
final DownloadManagerInitialisationAdapter dmia = new DownloadManagerInitialisationAdapter() {
@Override
public int getActions() {
return (ACT_ASSIGNS_TAGS);
}
@Override
public void initialised(DownloadManager dm, boolean for_seeding) {
if (tag_name != null) {
TagManager tm = TagManagerFactory.getTagManager();
TagType tt = tm.getTagType(TagType.TT_DOWNLOAD_MANUAL);
Tag tag = tt.getTag(tag_name, true);
try {
if (tag == null) {
tag = tt.createTag(tag_name, true);
}
tag.addTaggable(dm);
} catch (Throwable e) {
Debug.out(e);
}
}
}
};
byte[] hash = null;
try {
hash = torrent.getHash();
} catch (Exception e) {
}
if (!save_torrents) {
File imported = new File(folder, file.getName() + ".imported");
TorrentUtils.move(file, imported);
global_manager.addDownloadManager(imported.getAbsolutePath(), hash, data_save_path, start_state, true, false, dmia);
} else {
global_manager.addDownloadManager(file.getAbsolutePath(), hash, data_save_path, start_state, true, false, dmia);
// add torrent for deletion, since there will be a
// saved copy elsewhere
to_delete.add(torrent);
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Auto-imported " + file.getAbsolutePath()));
}
} catch (Throwable e) {
Debug.out("Failed to auto-import torrent file '" + file.getAbsolutePath() + "' - " + Debug.getNestedExceptionMessage(e));
Debug.printStackTrace(e);
}
}
}
}
}
} finally {
this_mon.exit();
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class DownloadManagerStateImpl method setPeerSourcePermitted.
@Override
public void setPeerSourcePermitted(String peerSource, boolean enabled) {
if (!getFlag(FLAG_ALLOW_PERMITTED_PEER_SOURCE_CHANGES)) {
Logger.log(new LogEvent(torrent, LOGID, "Attempt to modify permitted peer sources denied as disabled '" + TorrentUtils.getLocalisedName(torrent) + "'"));
return;
}
if (!enabled) {
setPeerSourceEnabled(peerSource, false);
}
List values = getListAttributeSupport(AT_PEER_SOURCES_DENIED);
if (values == null) {
if (!enabled) {
values = new ArrayList();
values.add(peerSource);
setListAttribute(AT_PEER_SOURCES_DENIED, values);
}
} else {
if (enabled) {
values.remove(peerSource);
} else {
if (!values.contains(peerSource)) {
values.add(peerSource);
}
}
setListAttribute(AT_PEER_SOURCES_DENIED, values);
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class NetworkAdminImpl method checkNetworkInterfaces.
protected boolean checkNetworkInterfaces(boolean first_time, boolean force) {
boolean changed = false;
try {
List<NetworkInterface> x = NetUtils.getNetworkInterfaces();
boolean fire_stuff = false;
synchronized (getni_lock) {
if (last_getni_result != x) {
last_getni_result = x;
if (x.size() == 0 && old_network_interfaces == null) {
} else if (x.size() == 0) {
old_network_interfaces = null;
changed = true;
} else if (old_network_interfaces == null) {
Set<NetworkInterface> new_network_interfaces = new HashSet<>();
new_network_interfaces.addAll(x);
old_network_interfaces = new_network_interfaces;
changed = true;
} else {
Set<NetworkInterface> new_network_interfaces = new HashSet<>();
for (NetworkInterface ni : x) {
if (!old_network_interfaces.contains(ni)) {
changed = true;
}
new_network_interfaces.add(ni);
}
if (old_network_interfaces.size() != new_network_interfaces.size()) {
changed = true;
}
old_network_interfaces = new_network_interfaces;
}
if (changed || force) {
boolean newV6 = false;
boolean newV4 = false;
Set<NetworkInterface> interfaces = old_network_interfaces;
long now = SystemTime.getMonotonousTime();
List<AddressHistoryRecord> a_history = new ArrayList<>();
if (interfaces != null) {
Iterator<NetworkInterface> it = interfaces.iterator();
while (it.hasNext()) {
NetworkInterface ni = it.next();
Enumeration addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ia = (InetAddress) addresses.nextElement();
a_history.add(new AddressHistoryRecord(ni, ia, now));
if (ia.isLoopbackAddress()) {
continue;
}
if (ia instanceof Inet6Address && !ia.isLinkLocalAddress()) {
if (IPv6_enabled) {
newV6 = true;
}
} else if (ia instanceof Inet4Address) {
newV4 = true;
}
}
}
}
synchronized (address_history) {
address_history_update_time = now;
for (AddressHistoryRecord entry : a_history) {
String name = entry.getAddress().getHostAddress();
AddressHistoryRecord existing = address_history.get(name);
if (existing == null) {
address_history.put(name, entry);
} else {
existing.setLastSeen(now);
}
}
Iterator<AddressHistoryRecord> it = address_history.values().iterator();
while (it.hasNext()) {
AddressHistoryRecord entry = it.next();
long age = now - entry.getLastSeen();
if (age > 10 * 60 * 1000) {
it.remove();
}
}
}
supportsIPv4 = newV4;
supportsIPv6 = newV6;
Logger.log(new LogEvent(LOGID, "NetworkAdmin: ipv4 supported: " + supportsIPv4 + "; ipv6: " + supportsIPv6 + "; probing v6+nio functionality"));
if (newV6) {
ServerSocketChannel channel = ServerSocketChannel.open();
try {
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(anyLocalAddressIPv6, 0));
Logger.log(new LogEvent(LOGID, "NetworkAdmin: testing nio + ipv6 bind successful"));
supportsIPv6withNIO = true;
} catch (Exception e) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "nio + ipv6 test failed", e));
supportsIPv6withNIO = false;
}
channel.close();
} else
supportsIPv6withNIO = false;
if (!first_time) {
Logger.log(new LogEvent(LOGID, "NetworkAdmin: network interfaces have changed"));
}
fire_stuff = true;
}
}
}
if (fire_stuff) {
firePropertyChange(NetworkAdmin.PR_NETWORK_INTERFACES);
checkDefaultBindAddress(first_time);
}
} catch (Throwable e) {
}
return (changed);
}
Aggregations