I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 326b9998 authored by zzz's avatar zzz
Browse files

make YKGenerator non-static; control from ElGamalEngine

parent 3b64ce74
No related branches found
No related tags found
No related merge requests found
...@@ -57,6 +57,7 @@ import net.i2p.util.SimpleByteCache; ...@@ -57,6 +57,7 @@ import net.i2p.util.SimpleByteCache;
public class ElGamalEngine { public class ElGamalEngine {
private final Log _log; private final Log _log;
private final I2PAppContext _context; private final I2PAppContext _context;
private final YKGenerator _ykgen;
/** /**
* The ElGamal engine should only be constructed and accessed through the * The ElGamal engine should only be constructed and accessed through the
...@@ -73,28 +74,26 @@ public class ElGamalEngine { ...@@ -73,28 +74,26 @@ public class ElGamalEngine {
new long[] { 60 * 60 * 1000}); new long[] { 60 * 60 * 1000});
_context = context; _context = context;
_log = context.logManager().getLog(ElGamalEngine.class); _log = context.logManager().getLog(ElGamalEngine.class);
_ykgen = new YKGenerator(context);
} }
/** /**
* Note that this stops the singleton precalc thread. * Note that this stops the precalc thread and it cannot be restarted.
* You don't want to do this if there are multiple routers in the JVM.
* Fix this if you care. See Router.shutdown().
* @since 0.8.8 * @since 0.8.8
*/ */
public void shutdown() { public void shutdown() {
YKGenerator.shutdown(); _ykgen.shutdown();
} }
/** /**
* Only required if shutdown() previously called. * This is now a noop. Cannot be restarted.
* @since 0.8.8 * @since 0.8.8
*/ */
public static void restart() { public void restart() {
YKGenerator.restart();
} }
private BigInteger[] getNextYK() { private BigInteger[] getNextYK() {
return YKGenerator.getNextYK(); return _ykgen.getNextYK();
} }
/** encrypt the data to the public key /** encrypt the data to the public key
......
...@@ -38,13 +38,13 @@ import net.i2p.util.NativeBigInteger; ...@@ -38,13 +38,13 @@ import net.i2p.util.NativeBigInteger;
*/ */
class YKGenerator { class YKGenerator {
//private final static Log _log = new Log(YKGenerator.class); //private final static Log _log = new Log(YKGenerator.class);
private static final int MIN_NUM_BUILDERS; private final int MIN_NUM_BUILDERS;
private static final int MAX_NUM_BUILDERS; private final int MAX_NUM_BUILDERS;
private static final int CALC_DELAY; private final int CALC_DELAY;
private static final LinkedBlockingQueue<BigInteger[]> _values; private final LinkedBlockingQueue<BigInteger[]> _values;
private static Thread _precalcThread; private final Thread _precalcThread;
private static I2PAppContext ctx; private final I2PAppContext ctx;
private static volatile boolean _isRunning; private volatile boolean _isRunning;
public final static String PROP_YK_PRECALC_MIN = "crypto.yk.precalc.min"; public final static String PROP_YK_PRECALC_MIN = "crypto.yk.precalc.min";
public final static String PROP_YK_PRECALC_MAX = "crypto.yk.precalc.max"; public final static String PROP_YK_PRECALC_MAX = "crypto.yk.precalc.max";
...@@ -53,8 +53,8 @@ class YKGenerator { ...@@ -53,8 +53,8 @@ class YKGenerator {
public final static int DEFAULT_YK_PRECALC_MAX = 50; public final static int DEFAULT_YK_PRECALC_MAX = 50;
public final static int DEFAULT_YK_PRECALC_DELAY = 200; public final static int DEFAULT_YK_PRECALC_DELAY = 200;
static { public YKGenerator(I2PAppContext context) {
ctx = I2PAppContext.getGlobalContext(); ctx = context;
// add to the defaults for every 128MB of RAM, up to 1GB // add to the defaults for every 128MB of RAM, up to 1GB
long maxMemory = Runtime.getRuntime().maxMemory(); long maxMemory = Runtime.getRuntime().maxMemory();
...@@ -73,15 +73,6 @@ class YKGenerator { ...@@ -73,15 +73,6 @@ class YKGenerator {
// _log.debug("ElGamal YK Precalc (minimum: " + MIN_NUM_BUILDERS + " max: " + MAX_NUM_BUILDERS + ", delay: " // _log.debug("ElGamal YK Precalc (minimum: " + MIN_NUM_BUILDERS + " max: " + MAX_NUM_BUILDERS + ", delay: "
// + CALC_DELAY + ")"); // + CALC_DELAY + ")");
startPrecalc();
}
/**
* Caller must synch on class
* @since 0.8.8
*/
private static void startPrecalc() {
ctx = I2PAppContext.getGlobalContext();
ctx.statManager().createRateStat("crypto.YKUsed", "Need a YK from the queue", "Encryption", new long[] { 60*60*1000 }); ctx.statManager().createRateStat("crypto.YKUsed", "Need a YK from the queue", "Encryption", new long[] { 60*60*1000 });
ctx.statManager().createRateStat("crypto.YKEmpty", "YK queue empty", "Encryption", new long[] { 60*60*1000 }); ctx.statManager().createRateStat("crypto.YKEmpty", "YK queue empty", "Encryption", new long[] { 60*60*1000 });
_precalcThread = new I2PThread(new YKPrecalcRunner(MIN_NUM_BUILDERS, MAX_NUM_BUILDERS), _precalcThread = new I2PThread(new YKPrecalcRunner(MIN_NUM_BUILDERS, MAX_NUM_BUILDERS),
...@@ -92,39 +83,27 @@ class YKGenerator { ...@@ -92,39 +83,27 @@ class YKGenerator {
} }
/** /**
* Note that this stops the singleton precalc thread. * Note that this stops the precalc thread
* You don't want to do this if there are multiple routers in the JVM. * and it cannot be restarted.
* Fix this if you care. See Router.shutdown().
* @since 0.8.8 * @since 0.8.8
*/ */
public static void shutdown() { public void shutdown() {
_isRunning = false; _isRunning = false;
_precalcThread.interrupt(); _precalcThread.interrupt();
_values.clear(); _values.clear();
} }
/** private final int getSize() {
* Only required if shutdown() previously called.
* @since 0.8.8
*/
public static void restart() {
synchronized(YKGenerator.class) {
if (!_isRunning)
startPrecalc();
}
}
private static final int getSize() {
return _values.size(); return _values.size();
} }
/** @return true if successful, false if full */ /** @return true if successful, false if full */
private static final boolean addValues(BigInteger yk[]) { private final boolean addValues(BigInteger yk[]) {
return _values.offer(yk); return _values.offer(yk);
} }
/** @return rv[0] = Y; rv[1] = K */ /** @return rv[0] = Y; rv[1] = K */
public static BigInteger[] getNextYK() { public BigInteger[] getNextYK() {
ctx.statManager().addRateData("crypto.YKUsed", 1, 0); ctx.statManager().addRateData("crypto.YKUsed", 1, 0);
BigInteger[] rv = _values.poll(); BigInteger[] rv = _values.poll();
if (rv != null) if (rv != null)
...@@ -136,7 +115,7 @@ class YKGenerator { ...@@ -136,7 +115,7 @@ class YKGenerator {
private final static BigInteger _two = new NativeBigInteger(1, new byte[] { 0x02}); private final static BigInteger _two = new NativeBigInteger(1, new byte[] { 0x02});
/** @return rv[0] = Y; rv[1] = K */ /** @return rv[0] = Y; rv[1] = K */
private static final BigInteger[] generateYK() { private final BigInteger[] generateYK() {
NativeBigInteger k = null; NativeBigInteger k = null;
BigInteger y = null; BigInteger y = null;
//long t0 = 0; //long t0 = 0;
...@@ -186,7 +165,7 @@ class YKGenerator { ...@@ -186,7 +165,7 @@ class YKGenerator {
****/ ****/
/** the thread */ /** the thread */
private static class YKPrecalcRunner implements Runnable { private class YKPrecalcRunner implements Runnable {
private final int _minSize; private final int _minSize;
private final int _maxSize; private final int _maxSize;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment