i2psnark:

- Update session options when max bandwidth changes
  - BufferedStream cleanup and comments
  - torrent name display tweak
  - findbugs
This commit is contained in:
zzz
2011-01-10 17:14:34 +00:00
parent 158063658c
commit 6670209cb3
11 changed files with 48 additions and 43 deletions

View File

@ -179,11 +179,8 @@ public class ConnectionAcceptor implements Runnable
try {
InputStream in = _socket.getInputStream();
OutputStream out = _socket.getOutputStream();
if (true) {
// this is for the readahead in PeerAcceptor.connection()
in = new BufferedInputStream(in);
//out = new BufferedOutputStream(out);
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Handling socket from " + _socket.getPeerDestination().calculateHash().toBase64());
peeracceptor.connection(_socket, in, out);

View File

@ -71,6 +71,7 @@ public class I2PSnarkUtil {
public static final String DEFAULT_OPENTRACKERS = "http://tracker.welterde.i2p/a";
public static final int DEFAULT_MAX_UP_BW = 8; //KBps
public static final int MAX_CONNECTIONS = 16; // per torrent
private static final String PROP_MAX_BW = "i2cp.outboundBytesPerSecond";
private static final boolean ENABLE_DHT = true;
public I2PSnarkUtil(I2PAppContext ctx) {
@ -132,9 +133,21 @@ public class I2PSnarkUtil {
_configured = true;
}
/**
* @param KBps
*/
public void setMaxUpBW(int limit) {
_maxUpBW = limit;
_opts.put(PROP_MAX_BW, Integer.toString(limit * (1024 * 6 / 5))); // add a little for overhead
_configured = true;
if (_manager != null) {
I2PSession sess = _manager.getSession();
if (sess != null) {
Properties newProps = new Properties();
newProps.putAll(_opts);
sess.updateOptions(newProps);
}
}
}
public void setMaxConnections(int limit) {
@ -154,6 +167,10 @@ public class I2PSnarkUtil {
public int getEepProxyPort() { return _proxyPort; }
public boolean getEepProxySet() { return _shouldProxy; }
public int getMaxUploaders() { return _maxUploaders; }
/**
* @return KBps
*/
public int getMaxUpBW() { return _maxUpBW; }
public int getMaxConnections() { return _maxConnections; }
public int getStartupDelay() { return _startupDelay; }
@ -166,7 +183,7 @@ public class I2PSnarkUtil {
// try to find why reconnecting after stop
if (_log.shouldLog(Log.DEBUG))
_log.debug("Connecting to I2P", new Exception("I did it"));
Properties opts = new Properties();
Properties opts = _context.getProperties();
if (_opts != null) {
for (Iterator iter = _opts.keySet().iterator(); iter.hasNext(); ) {
String key = (String)iter.next();

View File

@ -189,7 +189,7 @@ public class MetaInfo
if (val == null)
throw new InvalidBEncodingException("Missing length number");
long len = val.getLong();
m_lengths.add(new Long(len));
m_lengths.add(Long.valueOf(len));
l += len;
val = (BEValue)desc.get("path");
@ -438,7 +438,7 @@ public class MetaInfo
info.put("piece length", Integer.valueOf(piece_length));
info.put("pieces", piece_hashes);
if (files == null)
info.put("length", new Long(length));
info.put("length", Long.valueOf(length));
else
{
List l = new ArrayList();

View File

@ -20,7 +20,6 @@
package org.klomp.snark;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@ -231,19 +230,8 @@ public class Peer implements Comparable
throw new IOException("Unable to reach " + peerID);
}
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream(); //new BufferedOutputStream(sock.getOutputStream());
if (true) {
// buffered output streams are internally synchronized, so we can't get through to the underlying
// I2PSocket's MessageOutputStream to close() it if we are blocking on a write(...). Oh, and the
// buffer is unnecessary anyway, as unbuffered access lets the streaming lib do the 'right thing'.
//out = new BufferedOutputStream(out);
in = new BufferedInputStream(sock.getInputStream());
}
//BufferedInputStream bis
// = new BufferedInputStream(sock.getInputStream());
//BufferedOutputStream bos
// = new BufferedOutputStream(sock.getOutputStream());
byte [] id = handshake(in, out); //handshake(bis, bos);
OutputStream out = sock.getOutputStream();
byte [] id = handshake(in, out);
byte [] expected_id = peerID.getID();
if (expected_id == null) {
peerID.setID(id);
@ -328,7 +316,7 @@ public class Peer implements Comparable
* Sets DataIn/OutputStreams, does the handshake and returns the id
* reported by the other side.
*/
private byte[] handshake(InputStream in, OutputStream out) //BufferedInputStream bis, BufferedOutputStream bos)
private byte[] handshake(InputStream in, OutputStream out)
throws IOException
{
din = new DataInputStream(in);

View File

@ -210,6 +210,7 @@ class PeerCheckerTask extends TimerTask
if (_util.getDHT() != null && (_runCount % 5) == 0) {
_util.getDHT().announce(coordinator.getInfoHash(), peer.getPeerID().getDestHash());
}
// send PEX
}
// Resync actual uploaders value

View File

@ -1185,7 +1185,7 @@ public class PeerCoordinator implements PeerListener
* @since 0.8.4
*/
public void gotPeers(Peer peer, List<PeerID> peers) {
// spin off thread or timer task to do a new Peer() and an addPeer() for each one
}
/** Return number of allowed uploaders for this torrent.

View File

@ -35,8 +35,8 @@ class Piece implements Comparable {
@Override
public boolean equals(Object o) {
if (o instanceof Piece) {
if (o == null) return false;
if (o instanceof Piece) {
return this.id == ((Piece)o).id;
}
return false;

View File

@ -108,7 +108,7 @@ public class Snark
} catch (Throwable t) {
System.out.println("OOM in the OOM");
}
System.exit(0);
//System.exit(0);
}
}
@ -980,7 +980,6 @@ public class Snark
(" <file> \tEither a local .torrent metainfo file to download");
System.out.println
(" \tor (with --share) a file to share.");
System.exit(-1);
}
/**
@ -1117,11 +1116,12 @@ public class Snark
coordinator.setWantedPieces();
}
/** SnarkSnutdown callback unused */
public void shutdown()
{
// Should not be necessary since all non-deamon threads should
// have died. But in reality this does not always happen.
System.exit(0);
//System.exit(0);
}
public interface CompleteListener {

View File

@ -247,11 +247,9 @@ public class SnarkManager implements Snark.CompleteListener {
i2cpOpts.put(pair.substring(0, split), pair.substring(split+1));
}
}
if (i2cpHost != null) {
_util.setI2CPConfig(i2cpHost, i2cpPort, i2cpOpts);
if (_log.shouldLog(Log.DEBUG))
_log.debug("Configuring with I2CP options " + i2cpOpts);
}
//I2PSnarkUtil.instance().setI2CPConfig("66.111.51.110", 7654, new Properties());
//String eepHost = _config.getProperty(PROP_EEP_HOST);
//int eepPort = getInt(PROP_EEP_PORT, 4444);
@ -338,6 +336,7 @@ public class SnarkManager implements Snark.CompleteListener {
}
}
// FIXME do this even if == null
if (i2cpHost != null) {
int oldI2CPPort = _util.getI2CPPort();
String oldI2CPHost = _util.getI2CPHost();
@ -383,6 +382,7 @@ public class SnarkManager implements Snark.CompleteListener {
Properties p = new Properties();
p.putAll(opts);
_util.setI2CPConfig(i2cpHost, port, p);
_util.setMaxUpBW(getInt(PROP_UPBW_MAX, DEFAULT_MAX_UP_BW));
addMessage(_("I2CP and tunnel changes will take effect after stopping all torrents"));
if (_log.shouldLog(Log.DEBUG))
_log.debug("i2cp host [" + i2cpHost + "] i2cp port " + port + " opts [" + opts
@ -396,6 +396,7 @@ public class SnarkManager implements Snark.CompleteListener {
p.putAll(opts);
addMessage(_("I2CP settings changed to {0}", i2cpHost + ":" + port + " (" + i2cpOpts.trim() + ")"));
_util.setI2CPConfig(i2cpHost, port, p);
_util.setMaxUpBW(getInt(PROP_UPBW_MAX, DEFAULT_MAX_UP_BW));
boolean ok = _util.connect();
if (!ok) {
addMessage(_("Unable to connect with the new settings, reverting to the old I2CP settings"));

View File

@ -105,7 +105,7 @@ public class Storage
{
long length = lengths[i];
total += length;
lengthsList.add(new Long(length));
lengthsList.add(Long.valueOf(length));
}
piece_size = MIN_PIECE_SIZE;
@ -753,6 +753,7 @@ public class Storage
openRAF(nr, false); // RW
// XXX - Is this the best way to make sure we have enough space for
// the whole file?
if (listener != null)
listener.storageCreateFile(this, names[nr], lengths[nr]);
final int ZEROBLOCKSIZE = metainfo.getPieceLength(0);
byte[] zeros;

View File

@ -755,8 +755,12 @@ public class I2PSnarkServlet extends Default {
filename = filename.substring(0, i);
String fullFilename = filename;
if (filename.length() > MAX_DISPLAYED_FILENAME_LENGTH) {
fullFilename = new String(filename);
filename = filename.substring(0, MAX_DISPLAYED_FILENAME_LENGTH) + "&hellip;";
String start = filename.substring(0, MAX_DISPLAYED_FILENAME_LENGTH);
if (start.indexOf(" ") < 0 && start.indexOf("-") < 0) {
// browser has nowhere to break it
fullFilename = filename;
filename = start + "&hellip;";
}
}
long total = snark.getTotalLength();
// Early typecast, avoid possibly overflowing a temp integer
@ -1035,7 +1039,7 @@ public class I2PSnarkServlet extends Default {
} else {
pct = (float) 101.0;
// until we get the metainfo we don't know how many pieces there are
out.write("??");
//out.write("??");
}
out.write("</td>\n\t");
out.write("<td class=\"snarkTorrentStatus " + rowClass + "\">");
@ -1134,7 +1138,6 @@ public class I2PSnarkServlet extends Default {
}
private void writeAddForm(PrintWriter out, HttpServletRequest req) throws IOException {
String uri = req.getRequestURI();
String newURL = req.getParameter("newURL");
if ( (newURL == null) || (newURL.trim().length() <= 0) )
newURL = "";
@ -1175,7 +1178,6 @@ public class I2PSnarkServlet extends Default {
}
private void writeSeedForm(PrintWriter out, HttpServletRequest req) throws IOException {
String uri = req.getRequestURI();
String baseFile = req.getParameter("baseFile");
if (baseFile == null || baseFile.trim().length() <= 0)
baseFile = "";
@ -1235,7 +1237,6 @@ public class I2PSnarkServlet extends Default {
}
private void writeConfigForm(PrintWriter out, HttpServletRequest req) throws IOException {
String uri = req.getRequestURI();
String dataDir = _manager.getDataDir().getAbsolutePath();
boolean autoStart = _manager.shouldAutoStart();
boolean useOpenTrackers = _manager.util().shouldUseOpenTrackers();
@ -1914,8 +1915,7 @@ private static class FetchAndAdd implements Runnable {
return;
}
// don't hold object from this MetaInfo
String name = new String(info.getName());
String name = info.getName();
name = Storage.filterName(name);
name = name + ".torrent";
File torrentFile = new File(_manager.getDataDir(), name);