Added reseed support via periodic thread that reseeds if deemed necessary.

Changed 'Apply' button size in ConfigurationTab.
This commit is contained in:
dev
2011-08-01 07:11:30 +00:00
parent e6253a47d8
commit 39811fa537
5 changed files with 132 additions and 14 deletions

View File

@ -4,20 +4,15 @@ package net.i2p.itoopie;
* Main.java * Main.java
*/ */
import java.awt.Font;
import java.util.Arrays;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap; import java.util.Timer;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.FontUIResource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -43,7 +38,7 @@ import net.i2p.itoopie.i2pcontrol.methods.RouterManager.ROUTER_MANAGER;
import net.i2p.itoopie.i2pcontrol.methods.SetI2PControl; import net.i2p.itoopie.i2pcontrol.methods.SetI2PControl;
import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting; import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting;
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager; import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
import net.i2p.itoopie.security.CertificateHelper; import net.i2p.itoopie.maintenance.ReseedMonitor;
import net.i2p.itoopie.security.ItoopieHostnameVerifier; import net.i2p.itoopie.security.ItoopieHostnameVerifier;
/** /**
@ -54,6 +49,7 @@ public class Main {
///Manages the lifetime of the tray icon. ///Manages the lifetime of the tray icon.
private TrayManager trayManager = null; private TrayManager trayManager = null;
private static ConfigurationManager _conf; private static ConfigurationManager _conf;
private static Timer reseedMonitor;
private static Log _log; private static Log _log;
/** /**
@ -99,7 +95,12 @@ public class Main {
// Popup Main window. // Popup Main window.
WindowHandler.toggleFrames(); WindowHandler.toggleFrames();
testStuff(); // Delete Me
reseedMonitor = new Timer();
// Start running periodic task after 2 minutes, run periodically every 10th minute.
reseedMonitor.scheduleAtFixedRate(new ReseedMonitor(), 2*60*1000, 10*60*1000);
//testStuff(); // Delete Me
} }
@SuppressWarnings("static-access") @SuppressWarnings("static-access")

View File

@ -95,7 +95,7 @@ public class ConfigurationTab extends TabLogoPanel {
final JButton btnApply = new JButton(Transl._("Apply")); final JButton btnApply = new JButton(Transl._("Apply"));
add(btnApply); add(btnApply);
btnApply.setBounds(450, 272, 100, 24); btnApply.setBounds(442, 272, 82, 24);
btnApply.addActionListener(new ActionListener(){ btnApply.addActionListener(new ActionListener(){
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {

View File

@ -4,7 +4,7 @@ import java.util.HashMap;
/** /**
* Describes the ways a I2P router can be restarted. * Describes the ways a I2P router operation can be altered.
* @author hottuna * @author hottuna
*/ */
public class I2PControl{ public class I2PControl{
@ -12,7 +12,7 @@ public class I2PControl{
/** /**
* Describes the ways a I2P router can be restarted. * Describes the ways a I2P router operation can be altered.
* @author hottuna * @author hottuna
*/ */
public enum I2P_CONTROL implements Remote{ public enum I2P_CONTROL implements Remote{
@ -22,7 +22,13 @@ public class I2PControl{
PORT { public boolean isReadable(){ return false;} PORT { public boolean isReadable(){ return false;}
public boolean isWritable(){ return true;} public boolean isWritable(){ return true;}
public String toString() { return "i2pcontrol.port"; }} public String toString() { return "i2pcontrol.port"; }},
ADDRESS { public boolean isReadable(){ return false;}
public boolean isWritable(){ return true;}
public String toString() { return "i2pcontrol.address"; }}
}; };
static { static {

View File

@ -36,6 +36,8 @@ public class SetRouterManager {
for (Entry<ROUTER_MANAGER,String> e : set){ for (Entry<ROUTER_MANAGER,String> e : set){
if(e.getKey().isWritable()){ if(e.getKey().isWritable()){
outParams.put(e.getKey().toString(), e.getValue()); outParams.put(e.getKey().toString(), e.getValue());
} else if (e.getKey().isReadable()){
outParams.put(e.getKey().toString(), null);
} }
} }
req.setParams(outParams); req.setParams(outParams);

View File

@ -0,0 +1,109 @@
package net.i2p.itoopie.maintenance;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.TimerTask;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
import net.i2p.itoopie.i2pcontrol.methods.RouterManager.ROUTER_MANAGER;
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
/*
timer = new Timer();
// Start running periodic task after 20 minutes, run periodically every 10th minute.
timer.scheduleAtFixedRate(new Sweeper(), 1000*60*20, 1000*60*10);
*/
/**
* Monitors the amount of peers the remote I2P-node has and initiates a reseed if deemed needed.
* @author hottuna
*
*/
public class ReseedMonitor extends TimerTask{
private static final Long MIN_KNOWN_PEERS = new Long(30);
private Log _log;
public void ReseedMonitor(){
_log = LogFactory.getLog(ReseedMonitor.class);
}
@Override
public void run(){
try {
EnumMap<ROUTER_INFO, Object> em = GetRouterInfo.execute(ROUTER_INFO.KNOWN_PEERS, ROUTER_INFO.IS_RESEEDING);
Long knownPeers = (Long) em.get(ROUTER_INFO.KNOWN_PEERS);
Boolean isReseeding = (Boolean) em.get(ROUTER_INFO.IS_RESEEDING);
if (knownPeers != null && isReseeding != null){
if (knownPeers < MIN_KNOWN_PEERS){
HashMap<ROUTER_MANAGER, String> hm = new HashMap<ROUTER_MANAGER, String>();
hm.put(ROUTER_MANAGER.RESEED, null);
SetRouterManager.execute(hm);
_log.info("Reseed initiated..");
}
}
} catch (InvalidPasswordException e) {
_log.error("Password denied by remote I2PControl host.");
} catch (JSONRPC2SessionException e) {
_log.error("Error connecting to remote I2PControl host.");
}
}
public static void main(String[] args){
System.out.println("Reading config file..");
ConfigurationManager _conf = ConfigurationManager.getInstance();
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
_conf.parseConfigStr("server.hostname=127.0.0.1");
_conf.parseConfigStr("server.port=7650");
_conf.parseConfigStr("server.target=jsonrpc");
try {
EnumMap<ROUTER_INFO, Object> em = GetRouterInfo.execute(ROUTER_INFO.KNOWN_PEERS, ROUTER_INFO.IS_RESEEDING);
Long knownPeers = (Long) em.get(ROUTER_INFO.KNOWN_PEERS);
Boolean isReseeding = (Boolean) em.get(ROUTER_INFO.IS_RESEEDING);
System.out.println("Known peers: " + knownPeers);
System.out.println("Is reseeding: " + isReseeding);
System.out.println("Initiating reseed...");
HashMap<ROUTER_MANAGER, String> hm = new HashMap<ROUTER_MANAGER, String>();
hm.put(ROUTER_MANAGER.RESEED, null);
SetRouterManager.execute(hm);
System.out.println("Waiting...");
Thread.sleep(1000);
System.out.println("Initiating second reseed...");
hm = new HashMap<ROUTER_MANAGER, String>();
hm.put(ROUTER_MANAGER.RESEED, null);
SetRouterManager.execute(hm);
System.out.println("Waiting...");
Thread.sleep(1000);
em = GetRouterInfo.execute(ROUTER_INFO.KNOWN_PEERS, ROUTER_INFO.IS_RESEEDING);
isReseeding = (Boolean) em.get(ROUTER_INFO.IS_RESEEDING);
System.out.println("Is reseeding: " + isReseeding);
if (knownPeers != null && isReseeding != null){
if (knownPeers < MIN_KNOWN_PEERS){
}
}
} catch (InvalidPasswordException e) {
e.printStackTrace();
} catch (JSONRPC2SessionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}