use of com.biglybt.plugin.net.buddy.PartialBuddy in project BiglyBT by BiglySoftware.
the class BuddyPluginTracker method removePartialBuddy.
public void removePartialBuddy(Download download, Peer peer) {
PartialBuddy pb = new PartialBuddy(this, peer);
String key = pb.getKey();
boolean removed = false;
synchronized (online_buddies) {
PartialBuddyData pbd = partial_buddies.get(key);
if (pbd == null || !pbd.downloads.remove(download)) {
return;
}
pb = pbd.pb;
if (pbd.downloads.isEmpty()) {
partial_buddies.remove(key);
removed = true;
}
}
if (removed) {
try {
if (plugin.getPeersAreLANLocal() && peer.isLANLocal()) {
AddressUtils.removeLANRateLimitAddress(InetAddress.getByName(pb.ip));
peer.resetLANLocalStatus();
}
} catch (Throwable e) {
Debug.out(e);
;
}
unmarkBuddyPeer(peer);
plugin.logMessage("Partial buddy removed: " + download.getName() + " - " + pb);
plugin.partialBuddyRemoved(pb);
} else {
plugin.partialBuddyChanged(pb);
}
}
use of com.biglybt.plugin.net.buddy.PartialBuddy in project BiglyBT by BiglySoftware.
the class BuddyPluginTracker method addPartialBuddy.
public void addPartialBuddy(Download download, Peer peer) {
PartialBuddy pb = new PartialBuddy(this, peer);
String key = pb.getKey();
boolean is_new = false;
synchronized (online_buddies) {
PartialBuddyData pbd = partial_buddies.get(key);
if (pbd == null) {
pbd = new PartialBuddyData(pb);
partial_buddies.put(key, pbd);
is_new = true;
} else {
pb = pbd.pb;
}
List<Download> dls = pbd.downloads;
if (dls.contains(download)) {
return;
}
dls.add(download);
}
if (is_new) {
try {
if (plugin.getPeersAreLANLocal()) {
AddressUtils.addLANRateLimitAddress(InetAddress.getByName(pb.ip));
if (!peer.isLANLocal()) {
peer.resetLANLocalStatus();
}
Peer[] peers = download.getPeerManager().getPeers(pb.ip);
for (Peer p : peers) {
if (p != peer && !p.isLANLocal()) {
p.resetLANLocalStatus();
}
}
}
} catch (Throwable e) {
Debug.out(e);
;
}
markBuddyPeer(download, peer);
plugin.logMessage("Partial buddy added: " + download.getName() + " - " + pb);
plugin.partialBuddyAdded(pb);
} else {
plugin.partialBuddyChanged(pb);
}
}
use of com.biglybt.plugin.net.buddy.PartialBuddy in project BiglyBT by BiglySoftware.
the class BuddyPluginTracker method doTracking.
protected void doTracking() {
if (!(plugin_enabled && tracker_enabled)) {
return;
}
Map<BuddyPluginBuddy, List<Download>> peers_to_check = new HashMap<>();
Map<PartialBuddy, List<Download>> partials_to_check = new HashMap<>();
Set<Download> active_set = new HashSet<>();
synchronized (online_buddies) {
Iterator<BuddyPluginBuddy> it = online_buddies.iterator();
while (it.hasNext()) {
BuddyPluginBuddy buddy = it.next();
BuddyTrackingData buddy_data = getBuddyData(buddy);
Map<Download, Boolean> active = buddy_data.getDownloadsToTrack();
if (active.size() > 0) {
Iterator<Map.Entry<Download, Boolean>> it2 = active.entrySet().iterator();
List<Download> check_peers = new ArrayList<>();
while (it2.hasNext()) {
Map.Entry<Download, Boolean> entry = it2.next();
Download dl = entry.getKey();
boolean check_peer = entry.getValue();
if (check_peer) {
check_peers.add(dl);
}
active_set.add(dl);
}
if (check_peers.size() > 0) {
peers_to_check.put(buddy, check_peers);
}
}
}
for (PartialBuddyData pbd : partial_buddies.values()) {
active_set.addAll(pbd.downloads);
}
}
synchronized (actively_tracking) {
Iterator<Download> it = active_set.iterator();
while (it.hasNext()) {
Download dl = it.next();
if (!actively_tracking.contains(dl)) {
actively_tracking.add(dl);
trackPeers(dl);
}
}
it = actively_tracking.iterator();
while (it.hasNext()) {
Download dl = (Download) it.next();
if (!active_set.contains(dl)) {
it.remove();
untrackPeers(dl);
}
}
}
// check peer connections
Iterator<Map.Entry<BuddyPluginBuddy, List<Download>>> it = peers_to_check.entrySet().iterator();
boolean lan = plugin.getPeersAreLANLocal();
while (it.hasNext()) {
Map.Entry<BuddyPluginBuddy, List<Download>> entry = it.next();
BuddyPluginBuddy buddy = entry.getKey();
if (!buddy.isOnline(false)) {
continue;
}
InetAddress ip = buddy.getAdjustedIP();
if (ip == null) {
continue;
}
int tcp_port = buddy.getTCPPort();
int udp_port = buddy.getUDPPort();
List<Download> downloads = (List<Download>) entry.getValue();
for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i);
PeerManager pm = download.getPeerManager();
if (pm == null) {
continue;
}
Peer[] existing_peers = pm.getPeers(ip.getHostAddress());
boolean connected = false;
for (int j = 0; j < existing_peers.length; j++) {
Peer peer = existing_peers[j];
if (peer.getTCPListenPort() == tcp_port || peer.getUDPListenPort() == udp_port) {
if (lan && !peer.isLANLocal()) {
AddressUtils.addLANRateLimitAddress(ip);
peer.resetLANLocalStatus();
}
connected = true;
break;
}
}
if (connected) {
log(download.getName() + " - peer " + ip.getHostAddress() + " already connected");
continue;
}
log(download.getName() + " - connecting to peer " + ip.getHostAddress());
PEPeerManager c_pm = PluginCoreUtils.unwrap(pm);
Map user_data = new LightHashMap();
user_data.put(PEER_KEY, download);
user_data.put(Peer.PR_PRIORITY_CONNECTION, Boolean.TRUE);
c_pm.addPeer(ip.getHostAddress(), tcp_port, udp_port, true, user_data);
}
}
}
use of com.biglybt.plugin.net.buddy.PartialBuddy in project BiglyBT by BiglySoftware.
the class BuddyPluginTracker method isBuddy.
protected int isBuddy(Peer peer) {
PartialBuddy pb = new PartialBuddy(this, peer);
String peer_ip = peer.getIp();
List<String> ips = AddressUtils.getLANAddresses(peer_ip);
synchronized (online_buddies) {
if (partial_buddies.containsKey(pb)) {
return (BUDDY_YES);
}
int result = BUDDY_NO;
outer: for (int i = 0; i < ips.size(); i++) {
String ip = ips.get(i);
List<BuddyPluginBuddy> buddies = online_buddy_ips.get(ip);
if (buddies != null) {
if (peer.getTCPListenPort() == 0 && peer.getUDPListenPort() == 0) {
result = BUDDY_MAYBE;
} else {
for (int j = 0; j < buddies.size(); j++) {
BuddyPluginBuddy buddy = (BuddyPluginBuddy) buddies.get(j);
if (buddy.getTCPPort() == peer.getTCPListenPort() && buddy.getTCPPort() != 0) {
result = BUDDY_YES;
break outer;
}
if (buddy.getUDPPort() == peer.getUDPListenPort() && buddy.getUDPPort() != 0) {
result = BUDDY_YES;
break outer;
}
}
}
}
}
return (result);
}
}
use of com.biglybt.plugin.net.buddy.PartialBuddy in project BiglyBT by BiglySoftware.
the class BuddyPluginTracker method removePartialBuddy.
public void removePartialBuddy(PartialBuddy pb) {
String key = pb.getKey();
PartialBuddyData pbd;
synchronized (online_buddies) {
pbd = partial_buddies.remove(key);
if (pbd == null) {
return;
}
}
boolean do_lan = plugin.getPeersAreLANLocal();
try {
if (do_lan) {
AddressUtils.removeLANRateLimitAddress(InetAddress.getByName(pb.ip));
}
} catch (Throwable e) {
Debug.out(e);
;
}
List<Peer> peers = new ArrayList<>();
for (Download download : pbd.downloads) {
PeerManager pm = download.getPeerManager();
if (pm != null) {
Peer[] ps = pm.getPeers();
for (Peer p : ps) {
if (key.equals(new PartialBuddy(this, p).getKey())) {
peers.add(p);
}
}
}
}
for (Peer peer : peers) {
unmarkBuddyPeer(peer);
if (do_lan && peer.isLANLocal()) {
peer.resetLANLocalStatus();
}
}
plugin.logMessage("Partial buddy removed: " + pb);
plugin.partialBuddyRemoved(pb);
}
Aggregations