i2psnark: Synch operations on BitField byte array

This commit is contained in:
zzz
2017-12-07 19:44:56 +00:00
parent 7f673bb254
commit 1f569b7359
3 changed files with 31 additions and 12 deletions

View File

@ -71,6 +71,8 @@ public class BitField
* affect this BitField. Note that some bits at the end of the byte * affect this BitField. Note that some bits at the end of the byte
* array are supposed to be always unset if they represent bits * array are supposed to be always unset if they represent bits
* bigger then the size of the bitfield. * bigger then the size of the bitfield.
*
* Caller should synch on this and copy!
*/ */
public byte[] getFieldBytes() public byte[] getFieldBytes()
{ {

View File

@ -22,6 +22,7 @@ package org.klomp.snark;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
@ -355,12 +356,25 @@ class PeerConnectionOut implements Runnable
void sendBitfield(BitField bitfield) void sendBitfield(BitField bitfield)
{ {
boolean fast = peer.supportsFast(); boolean fast = peer.supportsFast();
if (fast && bitfield.complete()) { boolean all = false;
boolean none = false;
byte[] data = null;
synchronized(bitfield) {
if (fast && bitfield.complete()) {
all = true;
} else if (fast && bitfield.count() <= 0) {
none = true;
} else {
byte[] d = bitfield.getFieldBytes();
data = Arrays.copyOf(d, d.length);
}
}
if (all) {
sendHaveAll(); sendHaveAll();
} else if (fast && bitfield.count() <= 0) { } else if (none) {
sendHaveNone(); sendHaveNone();
} else { } else {
Message m = new Message(bitfield.getFieldBytes()); Message m = new Message(data);
addMessage(m); addMessage(m);
} }
} }

View File

@ -1671,7 +1671,8 @@ public class SnarkManager implements CompleteListener, ClientApp {
} }
if (autoStart) { if (autoStart) {
startTorrent(ih); startTorrent(ih);
addMessage(_t("Fetching {0}", name)); if (false)
addMessage(_t("Fetching {0}", name));
DHT dht = _util.getDHT(); DHT dht = _util.getDHT();
boolean shouldWarn = _util.connected() && boolean shouldWarn = _util.connected() &&
_util.getOpenTrackers().isEmpty() && _util.getOpenTrackers().isEmpty() &&
@ -2053,14 +2054,16 @@ public class SnarkManager implements CompleteListener, ClientApp {
if (config.getProperty(PROP_META_ADDED) == null) if (config.getProperty(PROP_META_ADDED) == null)
config.setProperty(PROP_META_ADDED, now); config.setProperty(PROP_META_ADDED, now);
String bfs; String bfs;
if (bitfield.complete()) { synchronized(bitfield) {
bfs = "."; if (bitfield.complete()) {
if (config.getProperty(PROP_META_COMPLETED) == null) bfs = ".";
config.setProperty(PROP_META_COMPLETED, now); if (config.getProperty(PROP_META_COMPLETED) == null)
} else { config.setProperty(PROP_META_COMPLETED, now);
byte[] bf = bitfield.getFieldBytes(); } else {
bfs = Base64.encode(bf); byte[] bf = bitfield.getFieldBytes();
config.remove(PROP_META_COMPLETED); bfs = Base64.encode(bf);
config.remove(PROP_META_COMPLETED);
}
} }
config.setProperty(PROP_META_BITFIELD, bfs); config.setProperty(PROP_META_BITFIELD, bfs);
config.setProperty(PROP_META_PRESERVE_NAMES, Boolean.toString(preserveNames)); config.setProperty(PROP_META_PRESERVE_NAMES, Boolean.toString(preserveNames));