I2PSnark: Use new BW Limits message, remove router.jar dependencies
This commit is contained in:
@ -18,7 +18,6 @@
|
|||||||
<!-- Depend on classes instead of jars where available -->
|
<!-- Depend on classes instead of jars where available -->
|
||||||
<classpath>
|
<classpath>
|
||||||
<pathelement location="../../../core/java/build/obj" />
|
<pathelement location="../../../core/java/build/obj" />
|
||||||
<pathelement location="../../../router/java/build/obj" />
|
|
||||||
<pathelement location="../../ministreaming/java/build/obj" />
|
<pathelement location="../../ministreaming/java/build/obj" />
|
||||||
<pathelement location="../../jetty/jettylib/org.mortbay.jetty.jar" />
|
<pathelement location="../../jetty/jettylib/org.mortbay.jetty.jar" />
|
||||||
<pathelement location="../../jetty/jettylib/javax.servlet.jar" />
|
<pathelement location="../../jetty/jettylib/javax.servlet.jar" />
|
||||||
@ -32,7 +31,7 @@
|
|||||||
srcdir="./src"
|
srcdir="./src"
|
||||||
debug="true" deprecation="on" source="1.5" target="1.5"
|
debug="true" deprecation="on" source="1.5" target="1.5"
|
||||||
destdir="./build/obj"
|
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>
|
||||||
<target name="jar" depends="builddep, compile">
|
<target name="jar" depends="builddep, compile">
|
||||||
<jar destfile="./build/i2psnark.jar" basedir="./build/obj" includes="**/*.class" excludes="**/*Servlet.class">
|
<jar destfile="./build/i2psnark.jar" basedir="./build/obj" includes="**/*.class" excludes="**/*Servlet.class">
|
||||||
|
44
apps/i2psnark/java/src/org/klomp/snark/BWLimits.java
Normal file
44
apps/i2psnark/java/src/org/klomp/snark/BWLimits.java
Normal 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)));
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,6 @@ import java.util.Timer;
|
|||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.router.client.ClientManagerFacadeImpl;
|
|
||||||
import net.i2p.client.streaming.I2PServerSocket;
|
import net.i2p.client.streaming.I2PServerSocket;
|
||||||
import net.i2p.data.Destination;
|
import net.i2p.data.Destination;
|
||||||
import net.i2p.util.I2PThread;
|
import net.i2p.util.I2PThread;
|
||||||
@ -261,9 +260,9 @@ public class Snark
|
|||||||
public Snark(I2PAppContext ctx, Properties opts, String torrent,
|
public Snark(I2PAppContext ctx, Properties opts, String torrent,
|
||||||
StorageListener slistener, boolean start, String rootDir) {
|
StorageListener slistener, boolean start, String rootDir) {
|
||||||
this(new I2PSnarkUtil(ctx), torrent, null, -1, slistener, null, null, null, null, false, 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;
|
int port = 0;
|
||||||
String s = opts.getProperty(ClientManagerFacadeImpl.PROP_CLIENT_PORT);
|
String s = opts.getProperty("i2cp.port");
|
||||||
if (s != null) {
|
if (s != null) {
|
||||||
try {
|
try {
|
||||||
port = Integer.parseInt(s);
|
port = Integer.parseInt(s);
|
||||||
|
@ -18,7 +18,6 @@ import java.util.TreeMap;
|
|||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.router.RouterContext;
|
|
||||||
import net.i2p.util.I2PAppThread;
|
import net.i2p.util.I2PAppThread;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
@ -147,22 +146,21 @@ public class SnarkManager implements Snark.CompleteListener {
|
|||||||
_config.setProperty(PROP_EEP_PORT, "4444");
|
_config.setProperty(PROP_EEP_PORT, "4444");
|
||||||
if (!_config.containsKey(PROP_UPLOADERS_TOTAL))
|
if (!_config.containsKey(PROP_UPLOADERS_TOTAL))
|
||||||
_config.setProperty(PROP_UPLOADERS_TOTAL, "" + Snark.MAX_TOTAL_UPLOADERS);
|
_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))
|
if (!_config.containsKey(PROP_DIR))
|
||||||
_config.setProperty(PROP_DIR, "i2psnark");
|
_config.setProperty(PROP_DIR, "i2psnark");
|
||||||
if (!_config.containsKey(PROP_AUTO_START))
|
if (!_config.containsKey(PROP_AUTO_START))
|
||||||
_config.setProperty(PROP_AUTO_START, DEFAULT_AUTO_START);
|
_config.setProperty(PROP_AUTO_START, DEFAULT_AUTO_START);
|
||||||
updateConfig();
|
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() {
|
private void updateConfig() {
|
||||||
String i2cpHost = _config.getProperty(PROP_I2CP_HOST);
|
String i2cpHost = _config.getProperty(PROP_I2CP_HOST);
|
||||||
@ -619,6 +617,9 @@ public class SnarkManager implements Snark.CompleteListener {
|
|||||||
_messages.remove(0);
|
_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) {
|
while (true) {
|
||||||
File dir = getDataDir();
|
File dir = getDataDir();
|
||||||
_log.debug("Directory Monitor loop over " + dir.getAbsolutePath());
|
_log.debug("Directory Monitor loop over " + dir.getAbsolutePath());
|
||||||
|
@ -143,6 +143,11 @@ public interface I2PSession {
|
|||||||
*/
|
*/
|
||||||
public Destination lookupDest(Hash h) throws I2PSessionException;
|
public Destination lookupDest(Hash h) throws I2PSessionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current bandwidth limits
|
||||||
|
*/
|
||||||
|
public int[] bandwidthLimits() throws I2PSessionException;
|
||||||
|
|
||||||
/** See I2PSessionMuxedImpl for details */
|
/** See I2PSessionMuxedImpl for details */
|
||||||
public void addSessionListener(I2PSessionListener lsnr, int proto, int port);
|
public void addSessionListener(I2PSessionListener lsnr, int proto, int port);
|
||||||
/** See I2PSessionMuxedImpl for details */
|
/** See I2PSessionMuxedImpl for details */
|
||||||
|
@ -656,6 +656,10 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int[] bandwidthLimits() throws I2PSessionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected void updateActivity() {
|
protected void updateActivity() {
|
||||||
_lastActivity = _context.clock().now();
|
_lastActivity = _context.clock().now();
|
||||||
if (_isReduced) {
|
if (_isReduced) {
|
||||||
|
@ -130,7 +130,7 @@ class I2PSimpleSession extends I2PSessionImpl2 {
|
|||||||
return null;
|
return null;
|
||||||
_bwReceivedLock = new Object();
|
_bwReceivedLock = new Object();
|
||||||
sendMessage(new GetBandwidthLimitsMessage());
|
sendMessage(new GetBandwidthLimitsMessage());
|
||||||
for (int i = 0; i < 5 && !_destReceived; i++) {
|
for (int i = 0; i < 5 && !_bwReceived; i++) {
|
||||||
try {
|
try {
|
||||||
synchronized (_bwReceivedLock) {
|
synchronized (_bwReceivedLock) {
|
||||||
_bwReceivedLock.wait(1000);
|
_bwReceivedLock.wait(1000);
|
||||||
|
@ -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.
|
* This could someday give a different answer to each client.
|
||||||
* But it's not enforced anywhere.
|
* But it's not enforced anywhere.
|
||||||
*/
|
*/
|
||||||
private void handleGetBWLimits(I2CPMessageReader reader, GetBandwidthLimitsMessage message) {
|
private void handleGetBWLimits(I2CPMessageReader reader, GetBandwidthLimitsMessage message) {
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO))
|
||||||
_log.info("Got BW Limits request");
|
_log.info("Got BW Limits request");
|
||||||
int in = _context.bandwidthLimiter().getInboundKBytesPerSecond() / 2;
|
int in = _context.bandwidthLimiter().getInboundKBytesPerSecond() * 4 / 7;
|
||||||
int out = _context.bandwidthLimiter().getOutboundKBytesPerSecond() / 2;
|
int out = _context.bandwidthLimiter().getOutboundKBytesPerSecond() * 4 / 7;
|
||||||
BandwidthLimitsMessage msg = new BandwidthLimitsMessage(in, out);
|
BandwidthLimitsMessage msg = new BandwidthLimitsMessage(in, out);
|
||||||
try {
|
try {
|
||||||
_runner.doSend(msg);
|
_runner.doSend(msg);
|
||||||
|
Reference in New Issue
Block a user