use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.
the class RouterManagerHandler method process.
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
if (_context == null) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
}
Map<String, Object> inParams = req.getNamedParams();
final Map<String, Object> outParams = new HashMap<String, Object>(4);
if (inParams.containsKey("Shutdown")) {
outParams.put("Shutdown", null);
(new Thread() {
@Override
public void run() {
try {
Thread.sleep(SHUTDOWN_WAIT);
} catch (InterruptedException e) {
}
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD));
_context.router().shutdown(Router.EXIT_HARD);
}
}).start();
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("Restart")) {
outParams.put("Restart", null);
(new Thread() {
@Override
public void run() {
try {
Thread.sleep(SHUTDOWN_WAIT);
} catch (InterruptedException e) {
}
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
_context.router().shutdown(Router.EXIT_HARD_RESTART);
}
}).start();
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("ShutdownGraceful")) {
outParams.put("ShutdownGraceful", null);
(new Thread() {
@Override
public void run() {
try {
Thread.sleep(SHUTDOWN_WAIT);
} catch (InterruptedException e) {
}
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
_context.router().shutdownGracefully();
}
}).start();
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("RestartGraceful")) {
outParams.put("RestartGraceful", null);
(new Thread() {
@Override
public void run() {
try {
Thread.sleep(SHUTDOWN_WAIT);
} catch (InterruptedException e) {
}
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
}
}).start();
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("Reseed")) {
outParams.put("Reseed", null);
(new Thread() {
@Override
public void run() {
ReseedChecker reseeder = new ReseedChecker(_context);
reseeder.requestReseed();
}
}).start();
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("FindUpdates")) {
Thread t = new Thread() {
@Override
public void run() {
ClientAppManager clmgr = I2PAppContext.getCurrentContext().clientAppManager();
if (clmgr == null) {
outParams.put("FindUpdates", "ClientAppManager is null");
return;
}
UpdateManager upmgr = (UpdateManager) clmgr.getRegisteredApp(UpdateManager.APP_NAME);
if (upmgr == null) {
outParams.put("FindUpdates", "UpdateManager is null");
return;
}
boolean updateIsAvailable = upmgr.checkAvailable(UpdateType.ROUTER_SIGNED) != null;
outParams.put("FindUpdates", updateIsAvailable);
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
}
return new JSONRPC2Response(outParams, req.getID());
}
if (inParams.containsKey("Update")) {
Thread t = new Thread() {
@Override
public void run() {
ClientAppManager clmgr = I2PAppContext.getCurrentContext().clientAppManager();
if (clmgr == null) {
outParams.put("Update", "ClientAppManager is null");
return;
}
UpdateManager upmgr = (UpdateManager) clmgr.getRegisteredApp(UpdateManager.APP_NAME);
if (upmgr == null) {
outParams.put("Update", "UpdateManager is null");
return;
}
boolean updateStarted = upmgr.update(UpdateType.ROUTER_SIGNED);
if (!updateStarted) {
outParams.put("Update", "Update not started");
return;
}
boolean isUpdating = upmgr.isUpdateInProgress(UpdateType.ROUTER_SIGNED);
while (isUpdating) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
isUpdating = upmgr.isUpdateInProgress(UpdateType.ROUTER_SIGNED);
}
outParams.put("Update", upmgr.getStatus());
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
}
return new JSONRPC2Response(outParams, req.getID());
}
return new JSONRPC2Response(outParams, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.
the class EchoHandler method process.
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("Echo")) {
JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
Map<String, Object> inParams = req.getNamedParams();
String echo = (String) inParams.get("Echo");
Map<String, Object> outParams = new HashMap<String, Object>(4);
outParams.put("Result", echo);
return new JSONRPC2Response(outParams, req.getID());
} else {
// Method name not supported
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.
the class GetRateHandler method process.
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("GetRate")) {
JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
Map<String, Object> inParams = req.getNamedParams();
String input = (String) inParams.get("Stat");
if (input == null) {
return new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, req.getID());
}
Number p = (Number) inParams.get("Period");
if (p == null)
return new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, req.getID());
long period = p.longValue();
RateStat rateStat = I2PAppContext.getGlobalContext().statManager().getRate(input);
// If RateStat or the requested period doesn't already exist, create them.
if (rateStat == null || rateStat.getRate(period) == null) {
long[] tempArr = new long[1];
tempArr[0] = period;
I2PAppContext.getGlobalContext().statManager().createRequiredRateStat(input, "I2PControl", "I2PControl", tempArr);
rateStat = I2PAppContext.getGlobalContext().statManager().getRate(input);
}
if (rateStat.getRate(period) == null)
return new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, req.getID());
Map<String, Object> outParams = new HashMap<String, Object>(4);
Rate rate = rateStat.getRate(period);
rate.coalesce();
outParams.put("Result", rate.getAverageValue());
return new JSONRPC2Response(outParams, req.getID());
}
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.
the class I2PControlHandler method process.
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
/**
** only if we enable host/port changes
* if (_context == null) {
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(),
* "RouterContext was not initialized. Query failed"),
* req.getID());
* }
***
*/
Map<String, Object> inParams = req.getNamedParams();
Map<String, Object> outParams = new HashMap<String, Object>(4);
boolean restartNeeded = false;
boolean settingsSaved = false;
String inParam;
if (inParams.containsKey("i2pcontrol.password")) {
if ((inParam = (String) inParams.get("i2pcontrol.password")) != null) {
if (_secMan.setPasswd(inParam)) {
outParams.put("i2pcontrol.password", null);
settingsSaved = true;
}
}
}
/**
**
* if (inParams.containsKey("i2pcontrol.address")) {
* String oldAddress = _conf.getConf("i2pcontrol.listen.address", "127.0.0.1");
* if ((inParam = (String) inParams.get("i2pcontrol.address")) != null) {
* if ((oldAddress == null || !inParam.equals(oldAddress.toString()) &&
* (inParam.equals("0.0.0.0") || inParam.equals("127.0.0.1")))) {
* InetAddress[] newAddress;
*
* try {
* newAddress = InetAddress.getAllByName(inParam);
* } catch (UnknownHostException e) {
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
* "\"i2pcontrol.address\" must be a string representing a hostname or ipaddress. " + inParam + " isn't valid."),
* req.getID());
* }
* try {
* SslSocketConnector ssl = I2PControlController.buildSslListener(inParam, _conf.getConf("i2pcontrol.listen.port", 7650));
* I2PControlController.clearListeners();
* I2PControlController.replaceListener(ssl);
* _conf.setConf("i2pcontrol.listen.address", inParam);
*
* ConfigurationManager.writeConfFile();
* outParams.put("i2pcontrol.address", null);
* settingsSaved = true;
* } catch (Exception e) {
* _conf.setConf("i2pcontrol.listen.address", oldAddress);
* try {
* SslSocketConnector ssl = I2PControlController.buildSslListener(inParam, _conf.getConf("i2pcontrol.listen.port", 7650));
* I2PControlController.clearListeners();
* I2PControlController.replaceListener(ssl);
* } catch (Exception e2) {
* _log.log(Log.CRIT, "Unable to resume server on previous listening ip.");
* }
* _log.error("Client tried to set listen address to, " + newAddress.toString() + " which isn't valid.", e);
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
* "\"i2pcontrol.address\" has been set to an invalid address, reverting. "), req.getID());
* }
* }
* } else {
* outParams.put("i2pcontrol.address", oldAddress);
* }
* outParams.put("RestartNeeded", restartNeeded);
* }
***
*/
outParams.put("SettingsSaved", settingsSaved);
return new JSONRPC2Response(outParams, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.
the class RouterInfoHandler method process.
@SuppressWarnings("unchecked")
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
if (_context == null) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
}
Map<String, Object> inParams = req.getNamedParams();
Map outParams = new HashMap();
if (inParams.containsKey("i2p.router.version")) {
try {
Class rvClass = Class.forName("net.i2p.router.RouterVersion");
java.lang.reflect.Field field = rvClass.getDeclaredField("FULL_VERSION");
String fullVersion = (String) field.get(new RouterVersion());
outParams.put("i2p.router.version", fullVersion);
}// Ignore
catch (Exception e) {
}
}
if (inParams.containsKey("i2p.router.uptime")) {
Router router = _context.router();
if (router == null) {
outParams.put("i2p.router.uptime", 0);
} else {
outParams.put("i2p.router.uptime", router.getUptime());
}
}
if (inParams.containsKey("i2p.router.status")) {
outParams.put("i2p.router.status", _context.throttle().getLocalizedTunnelStatus());
}
if (inParams.containsKey("i2p.router.net.status")) {
outParams.put("i2p.router.net.status", getNetworkStatus().ordinal());
}
if (inParams.containsKey("i2p.router.net.bw.inbound.1s")) {
outParams.put("i2p.router.net.bw.inbound.1s", _context.bandwidthLimiter().getReceiveBps());
}
if (inParams.containsKey("i2p.router.net.bw.outbound.1s")) {
outParams.put("i2p.router.net.bw.outbound.1s", _context.bandwidthLimiter().getSendBps());
}
if (inParams.containsKey("i2p.router.net.bw.inbound.15s")) {
outParams.put("i2p.router.net.bw.inbound.15s", _context.bandwidthLimiter().getReceiveBps15s());
}
if (inParams.containsKey("i2p.router.net.bw.outbound.15s")) {
outParams.put("i2p.router.net.bw.outbound.15s", _context.bandwidthLimiter().getSendBps15s());
}
if (inParams.containsKey("i2p.router.net.tunnels.participating")) {
outParams.put("i2p.router.net.tunnels.participating", _context.tunnelManager().getParticipatingCount());
}
if (inParams.containsKey("i2p.router.netdb.knownpeers")) {
// Why max(-1, 0) is used I don't know, it is the implementation used in the router console.
outParams.put("i2p.router.netdb.knownpeers", Math.max(_context.netDb().getKnownRouters() - 1, 0));
}
if (inParams.containsKey("i2p.router.netdb.activepeers")) {
outParams.put("i2p.router.netdb.activepeers", _context.commSystem().countActivePeers());
}
if (inParams.containsKey("i2p.router.netdb.fastpeers")) {
outParams.put("i2p.router.netdb.fastpeers", _context.profileOrganizer().countFastPeers());
}
if (inParams.containsKey("i2p.router.netdb.highcapacitypeers")) {
outParams.put("i2p.router.netdb.highcapacitypeers", _context.profileOrganizer().countHighCapacityPeers());
}
if (inParams.containsKey("i2p.router.netdb.isreseeding")) {
outParams.put("i2p.router.netdb.isreseeding", Boolean.valueOf(System.getProperty("net.i2p.router.web.ReseedHandler.reseedInProgress")).booleanValue());
}
return new JSONRPC2Response(outParams, req.getID());
}
Aggregations