2011-06-20 12:38:52 +00:00
|
|
|
package net.i2p.itoopie;
|
2011-06-20 07:12:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Main.java
|
|
|
|
*/
|
|
|
|
|
2011-07-08 09:52:09 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-07-01 06:34:21 +00:00
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
2011-06-20 07:12:36 +00:00
|
|
|
import javax.swing.SwingUtilities;
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
import javax.swing.UnsupportedLookAndFeelException;
|
|
|
|
|
2011-06-27 11:56:29 +00:00
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import com.thetransactioncompany.jsonrpc2.JSONRPC2Error;
|
2011-07-08 09:52:09 +00:00
|
|
|
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
|
2011-06-27 11:56:29 +00:00
|
|
|
|
2011-07-01 08:19:14 +00:00
|
|
|
import net.i2p.itoopie.configuration.ConfigurationManager;
|
2011-07-08 09:52:09 +00:00
|
|
|
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
|
2011-07-05 12:32:08 +00:00
|
|
|
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
|
2011-06-27 11:56:29 +00:00
|
|
|
import net.i2p.itoopie.i2pcontrol.JSONInterface;
|
2011-07-08 09:52:09 +00:00
|
|
|
import net.i2p.itoopie.i2pcontrol.JSONInterface.NETWORK_INFO;
|
2011-07-01 06:34:21 +00:00
|
|
|
import net.i2p.itoopie.security.CertificateHelper;
|
2011-06-27 11:56:29 +00:00
|
|
|
|
2011-06-20 07:12:36 +00:00
|
|
|
/**
|
|
|
|
* The main class of the application.
|
|
|
|
*/
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
///Manages the lifetime of the tray icon.
|
|
|
|
private TrayManager trayManager = null;
|
2011-06-27 11:56:29 +00:00
|
|
|
private static ConfigurationManager _conf;
|
|
|
|
private static Log _log;
|
2011-07-01 06:34:21 +00:00
|
|
|
public static final boolean isDebug = true;
|
2011-06-20 07:12:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the tray icon code (loads tray icon in the tray area).
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public void startUp() throws Exception {
|
|
|
|
trayManager = TrayManager.getInstance();
|
|
|
|
trayManager.startManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
beginStartup(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main method launching the application.
|
|
|
|
*/
|
|
|
|
public static void beginStartup(String[] args) {
|
|
|
|
System.setProperty("java.awt.headless", "false");
|
2011-06-27 11:56:29 +00:00
|
|
|
_conf = ConfigurationManager.getInstance();
|
|
|
|
_log = LogFactory.getLog(Main.class);
|
2011-07-01 06:34:21 +00:00
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(CertificateHelper.getHostnameVerifier());
|
2011-06-27 11:56:29 +00:00
|
|
|
|
2011-07-12 18:56:49 +00:00
|
|
|
|
2011-06-20 07:12:36 +00:00
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
|
} catch (InstantiationException ex) {
|
|
|
|
} catch (IllegalAccessException ex) {
|
|
|
|
} catch (UnsupportedLookAndFeelException ex) {
|
|
|
|
}
|
|
|
|
|
2011-07-12 18:56:49 +00:00
|
|
|
|
|
|
|
final Main main = new Main();
|
|
|
|
main.launchForeverLoop();
|
|
|
|
//We'll be doing GUI work, so let's stay in the event dispatcher thread.
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
main.startUp();
|
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
//log.error("Failed while running desktopgui!", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("static-access")
|
|
|
|
public static void beginShutdown(){
|
|
|
|
_conf.writeConfFile();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoids the app terminating because no Window is opened anymore.
|
|
|
|
* More info: http://java.sun.com/javase/6/docs/api/java/awt/doc-files/AWTThreadIssues.html#Autoshutdown
|
|
|
|
*/
|
|
|
|
public void launchForeverLoop() {
|
|
|
|
Runnable r = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Object o = new Object();
|
|
|
|
synchronized (o) {
|
|
|
|
o.wait();
|
|
|
|
}
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Thread t = new Thread(r);
|
|
|
|
t.setDaemon(false);
|
|
|
|
t.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void testStuff(){
|
2011-07-05 14:10:19 +00:00
|
|
|
_conf.parseConfigStr("server.hostname=127.0.0.1");
|
|
|
|
_conf.parseConfigStr("server.port=5555");
|
|
|
|
_conf.parseConfigStr("server.target=jsonrpc");
|
2011-06-20 07:12:36 +00:00
|
|
|
|
2011-06-30 07:34:02 +00:00
|
|
|
|
2011-07-08 09:52:09 +00:00
|
|
|
// Test basic echo method
|
2011-06-27 11:56:29 +00:00
|
|
|
try {
|
2011-07-08 09:52:09 +00:00
|
|
|
String str = JSONInterface.getEcho("Echo this mofo!");
|
|
|
|
System.out.println("Echo response: " + str);
|
2011-07-05 12:32:08 +00:00
|
|
|
}catch (InvalidPasswordException e) {
|
|
|
|
e.printStackTrace();
|
2011-07-08 09:52:09 +00:00
|
|
|
} catch (JSONRPC2SessionException e) {
|
|
|
|
System.out.println("Connection failed..");
|
2011-06-27 11:56:29 +00:00
|
|
|
}
|
|
|
|
|
2011-07-08 09:52:09 +00:00
|
|
|
// Test reading a rateStat
|
2011-06-27 11:56:29 +00:00
|
|
|
try {
|
2011-07-08 09:52:09 +00:00
|
|
|
Double dbl = JSONInterface.getRateStat("bw.sendRate", 3600000L);
|
|
|
|
System.out.println("rateStat: " + dbl);
|
2011-07-05 12:32:08 +00:00
|
|
|
} catch (InvalidPasswordException e) {
|
|
|
|
e.printStackTrace();
|
2011-07-08 09:52:09 +00:00
|
|
|
} catch (JSONRPC2SessionException e) {
|
|
|
|
System.out.println("Connection failed..");
|
|
|
|
} catch (InvalidParametersException e) {
|
|
|
|
System.out.println("Bad parameters sent..");
|
2011-06-27 11:56:29 +00:00
|
|
|
}
|
2011-06-20 07:12:36 +00:00
|
|
|
|
2011-07-08 09:52:09 +00:00
|
|
|
// Test reading all settings
|
|
|
|
try {
|
|
|
|
HashMap hm = JSONInterface.getNetworkInfo(JSONInterface.NETWORK_INFO.values());
|
|
|
|
System.out.println("getNetworkInfo: All: ");
|
|
|
|
Set<Entry> set = hm.entrySet();
|
|
|
|
for (Entry e : set){
|
|
|
|
System.out.println(e.getKey() +":"+ e.getValue());
|
|
|
|
}
|
|
|
|
} catch (InvalidPasswordException e1) {
|
|
|
|
//e1.printStackTrace();
|
|
|
|
} catch (JSONRPC2SessionException e) {
|
|
|
|
System.out.println("Connection failed..");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test saving all settings
|
|
|
|
try {
|
|
|
|
HashMap<NETWORK_INFO, String> hm = new HashMap<NETWORK_INFO,String>();
|
|
|
|
|
|
|
|
List<NETWORK_INFO> list = Arrays.asList(NETWORK_INFO.values());
|
|
|
|
for (NETWORK_INFO i : list){
|
|
|
|
hm.put(i, "66"); // 66 is an arbitrary number that should work for most fields.
|
|
|
|
}
|
|
|
|
HashMap nextHM= JSONInterface.setNetworkSetting(hm);
|
|
|
|
System.out.println("setNetworkInfo: All: ");
|
|
|
|
Set<Entry> set = nextHM.entrySet();
|
|
|
|
for (Entry e : set){
|
|
|
|
System.out.println(e.getKey() +":"+ e.getValue());
|
|
|
|
}
|
|
|
|
} catch (InvalidPasswordException e){
|
|
|
|
//e.printStackTrace();
|
|
|
|
} catch (JSONRPC2SessionException e){
|
|
|
|
//e.printStackTrace();
|
|
|
|
System.out.println("Connection failed..");
|
|
|
|
} catch (InvalidParametersException e) {
|
|
|
|
System.out.println("Bad parameters sent..");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually test saving all(?) settings
|
|
|
|
try {
|
|
|
|
HashMap<NETWORK_INFO, String> hm = new HashMap<NETWORK_INFO,String>();
|
|
|
|
hm.put(NETWORK_INFO.BW_IN, "666");
|
|
|
|
hm.put(NETWORK_INFO.BW_OUT, "666");
|
|
|
|
hm.put(NETWORK_INFO.BW_SHARE, "66");
|
|
|
|
hm.put(NETWORK_INFO.DETECTED_IP, "66.66.66.66");
|
|
|
|
hm.put(NETWORK_INFO.LAPTOP_MODE, "true");
|
|
|
|
hm.put(NETWORK_INFO.TCP_AUTOIP, "always");
|
|
|
|
hm.put(NETWORK_INFO.TCP_HOSTNAME, "66.66.66.66");
|
|
|
|
hm.put(NETWORK_INFO.TCP_PORT, "66");
|
|
|
|
hm.put(NETWORK_INFO.UDP_AUTO_IP, "local,upnp,ssu");
|
|
|
|
hm.put(NETWORK_INFO.UDP_HOSTNAME, "66.66.66.66");
|
|
|
|
hm.put(NETWORK_INFO.UDP_PORT, "66");
|
|
|
|
hm.put(NETWORK_INFO.UPNP, "true");
|
|
|
|
|
|
|
|
HashMap nextHM= JSONInterface.setNetworkSetting(hm);
|
|
|
|
System.out.println("setNetworkInfo: Manual: ");
|
|
|
|
Set<Entry> set = nextHM.entrySet();
|
|
|
|
for (Entry e : set){
|
|
|
|
System.out.println(e.getKey() +":"+ e.getValue());
|
|
|
|
}
|
|
|
|
} catch (InvalidPasswordException e){
|
|
|
|
//e.printStackTrace();
|
|
|
|
} catch (JSONRPC2SessionException e){
|
|
|
|
//e.printStackTrace();
|
|
|
|
System.out.println("Connection failed..");
|
|
|
|
} catch (InvalidParametersException e) {
|
|
|
|
System.out.println("Bad parameters sent..");
|
|
|
|
}
|
2011-06-20 07:12:36 +00:00
|
|
|
}
|
|
|
|
}
|