I2PSnark: Use new BW Limits message, remove router.jar dependencies

This commit is contained in:
zzz
2009-03-25 23:19:37 +00:00
parent 5eda7c30fc
commit 5414d41de4
8 changed files with 72 additions and 20 deletions

View File

@ -18,7 +18,6 @@
<!-- Depend on classes instead of jars where available -->
<classpath>
<pathelement location="../../../core/java/build/obj" />
<pathelement location="../../../router/java/build/obj" />
<pathelement location="../../ministreaming/java/build/obj" />
<pathelement location="../../jetty/jettylib/org.mortbay.jetty.jar" />
<pathelement location="../../jetty/jettylib/javax.servlet.jar" />
@ -32,7 +31,7 @@
srcdir="./src"
debug="true" deprecation="on" source="1.5" target="1.5"
destdir="./build/obj"
classpath="../../../core/java/build/i2p.jar:../../../router/java/build/router.jar:../../jetty/jettylib/org.mortbay.jetty.jar:../../jetty/jettylib/javax.servlet.jar:../../ministreaming/java/build/mstreaming.jar" />
classpath="../../../core/java/build/i2p.jar:../../jetty/jettylib/org.mortbay.jetty.jar:../../jetty/jettylib/javax.servlet.jar:../../ministreaming/java/build/mstreaming.jar" />
</target>
<target name="jar" depends="builddep, compile">
<jar destfile="./build/i2psnark.jar" basedir="./build/obj" includes="**/*.class" excludes="**/*Servlet.class">

View File

@ -0,0 +1,44 @@
/*
* Released into the public domain
* with no warranty of any kind, either expressed or implied.
*/
package org.klomp.snark;
import java.util.Arrays;
import java.util.Properties;
import net.i2p.I2PAppContext;
import net.i2p.client.I2PSessionException;
import net.i2p.client.I2PClient;
import net.i2p.client.I2PSession;
import net.i2p.client.I2PSimpleClient;
/**
* Connect via I2CP and ask the router the bandwidth limits.
*
* The call is blocking and returns null on failure.
* Timeout is set to 5 seconds in I2PSimpleSession but it should be much faster.
*
* @author zzz
*/
class BWLimits {
public static int[] getBWLimits(String host, int port) {
int[] rv = null;
try {
I2PClient client = new I2PSimpleClient();
Properties opts = new Properties();
opts.put(I2PClient.PROP_TCP_HOST, host);
opts.put(I2PClient.PROP_TCP_PORT, "" + port);
I2PSession session = client.createSession(null, opts);
session.connect();
rv = session.bandwidthLimits();
session.destroySession();
} catch (I2PSessionException ise) {}
return rv;
}
public static void main(String args[]) {
System.out.println(Arrays.toString(getBWLimits("127.0.0.1", 7654)));
}
}

View File

@ -36,7 +36,6 @@ import java.util.Timer;
import java.util.TimerTask;
import net.i2p.I2PAppContext;
import net.i2p.router.client.ClientManagerFacadeImpl;
import net.i2p.client.streaming.I2PServerSocket;
import net.i2p.data.Destination;
import net.i2p.util.I2PThread;
@ -261,9 +260,9 @@ public class Snark
public Snark(I2PAppContext ctx, Properties opts, String torrent,
StorageListener slistener, boolean start, String rootDir) {
this(new I2PSnarkUtil(ctx), torrent, null, -1, slistener, null, null, null, null, false, rootDir);
String host = opts.getProperty(ClientManagerFacadeImpl.PROP_CLIENT_HOST);
String host = opts.getProperty("i2cp.hostname");
int port = 0;
String s = opts.getProperty(ClientManagerFacadeImpl.PROP_CLIENT_PORT);
String s = opts.getProperty("i2cp.port");
if (s != null) {
try {
port = Integer.parseInt(s);

View File

@ -18,7 +18,6 @@ import java.util.TreeMap;
import net.i2p.I2PAppContext;
import net.i2p.data.Base64;
import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
@ -147,16 +146,6 @@ public class SnarkManager implements Snark.CompleteListener {
_config.setProperty(PROP_EEP_PORT, "4444");
if (!_config.containsKey(PROP_UPLOADERS_TOTAL))
_config.setProperty(PROP_UPLOADERS_TOTAL, "" + Snark.MAX_TOTAL_UPLOADERS);
if (!_config.containsKey(PROP_UPBW_MAX)) {
try {
if (_context instanceof RouterContext)
_config.setProperty(PROP_UPBW_MAX, "" + (((RouterContext)_context).bandwidthLimiter().getOutboundKBytesPerSecond() / 2));
else
_config.setProperty(PROP_UPBW_MAX, "" + DEFAULT_MAX_UP_BW);
} catch (NoClassDefFoundError ncdfe) {
_config.setProperty(PROP_UPBW_MAX, "" + DEFAULT_MAX_UP_BW);
}
}
if (!_config.containsKey(PROP_DIR))
_config.setProperty(PROP_DIR, "i2psnark");
if (!_config.containsKey(PROP_AUTO_START))
@ -164,6 +153,15 @@ public class SnarkManager implements Snark.CompleteListener {
updateConfig();
}
/** call from DirMonitor since loadConfig() is called before router I2CP is up */
private void getBWLimit() {
if (!_config.containsKey(PROP_UPBW_MAX)) {
int[] limits = BWLimits.getBWLimits(_util.getI2CPHost(), _util.getI2CPPort());
if (limits != null && limits[1] > 0)
_util.setMaxUpBW(limits[1]);
}
}
private void updateConfig() {
String i2cpHost = _config.getProperty(PROP_I2CP_HOST);
int i2cpPort = getInt(PROP_I2CP_PORT, 7654);
@ -619,6 +617,9 @@ public class SnarkManager implements Snark.CompleteListener {
_messages.remove(0);
}
// here because we need to delay until I2CP is up
// although the user will see the default until then
getBWLimit();
while (true) {
File dir = getDataDir();
_log.debug("Directory Monitor loop over " + dir.getAbsolutePath());

View File

@ -143,6 +143,11 @@ public interface I2PSession {
*/
public Destination lookupDest(Hash h) throws I2PSessionException;
/**
* Get the current bandwidth limits
*/
public int[] bandwidthLimits() throws I2PSessionException;
/** See I2PSessionMuxedImpl for details */
public void addSessionListener(I2PSessionListener lsnr, int proto, int port);
/** See I2PSessionMuxedImpl for details */

View File

@ -656,6 +656,10 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
return null;
}
public int[] bandwidthLimits() throws I2PSessionException {
return null;
}
protected void updateActivity() {
_lastActivity = _context.clock().now();
if (_isReduced) {

View File

@ -130,7 +130,7 @@ class I2PSimpleSession extends I2PSessionImpl2 {
return null;
_bwReceivedLock = new Object();
sendMessage(new GetBandwidthLimitsMessage());
for (int i = 0; i < 5 && !_destReceived; i++) {
for (int i = 0; i < 5 && !_bwReceived; i++) {
try {
synchronized (_bwReceivedLock) {
_bwReceivedLock.wait(1000);

View File

@ -280,15 +280,15 @@ class ClientMessageEventListener implements I2CPMessageReader.I2CPMessageEventLi
}
/**
* Divide router limit by 2 for overhead.
* Divide router limit by 1.75 for overhead.
* This could someday give a different answer to each client.
* But it's not enforced anywhere.
*/
private void handleGetBWLimits(I2CPMessageReader reader, GetBandwidthLimitsMessage message) {
if (_log.shouldLog(Log.INFO))
_log.info("Got BW Limits request");
int in = _context.bandwidthLimiter().getInboundKBytesPerSecond() / 2;
int out = _context.bandwidthLimiter().getOutboundKBytesPerSecond() / 2;
int in = _context.bandwidthLimiter().getInboundKBytesPerSecond() * 4 / 7;
int out = _context.bandwidthLimiter().getOutboundKBytesPerSecond() * 4 / 7;
BandwidthLimitsMessage msg = new BandwidthLimitsMessage(in, out);
try {
_runner.doSend(msg);