use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by vituperative.
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.JSONRPC2Request in project i2pplus by vituperative.
the class NetworkSettingHandler 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();
Map<String, Object> outParams = new HashMap<String, Object>(4);
boolean restartNeeded = false;
boolean settingsSaved = false;
String inParam;
if (inParams.containsKey("i2p.router.net.ntcp.port")) {
String oldNTCPPort = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_PORT);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.port")) != null) {
if (oldNTCPPort == null || !oldNTCPPort.equals(inParam.trim())) {
Integer newPort;
try {
newPort = Integer.valueOf(inParam);
if (newPort < 1 || newPort > 65535) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ntcp.port\" must be a string representing a number in the range 1-65535. " + inParam + " isn't valid."), req.getID());
}
Map<String, String> config = new HashMap<String, String>();
config.put(NTCPTransport.PROP_I2NP_NTCP_PORT, String.valueOf(newPort));
config.put(NTCPTransport.PROP_I2NP_NTCP_AUTO_PORT, "false");
_context.router().saveConfig(config, null);
restartNeeded = true;
}
settingsSaved = true;
} else {
String sAutoPort = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_AUTO_PORT, "true");
boolean oldAutoPort = "true".equalsIgnoreCase(sAutoPort);
if (oldAutoPort) {
String oldSSUPort = "" + _context.getProperty(UDPTransport.PROP_INTERNAL_PORT, 8887);
outParams.put("i2p.router.net.ntcp.port", oldSSUPort);
} else {
outParams.put("i2p.router.net.ntcp.port", oldNTCPPort);
}
}
}
if (inParams.containsKey("i2p.router.net.ntcp.hostname")) {
String oldNTCPHostname = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_HOSTNAME);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.hostname")) != null) {
if (oldNTCPHostname == null || !oldNTCPHostname.equals(inParam.trim())) {
_context.router().saveConfig(NTCPTransport.PROP_I2NP_NTCP_HOSTNAME, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ntcp.hostname", oldNTCPHostname);
}
}
if (inParams.containsKey("i2p.router.net.ntcp.autoip")) {
String oldNTCPAutoIP = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_AUTO_IP);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.autoip")) != null) {
inParam = inParam.trim().toLowerCase();
if (oldNTCPAutoIP == null || !oldNTCPAutoIP.equals(inParam)) {
if ("always".equals(inParam) || "true".equals(inParam) || "false".equals(inParam)) {
_context.router().saveConfig(NTCPTransport.PROP_I2NP_NTCP_AUTO_IP, inParam);
restartNeeded = true;
} else {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ntcp.autoip\" can only be always, true or false. " + inParam + " isn't valid."), req.getID());
}
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ntcp.autoip", oldNTCPAutoIP);
}
}
if (inParams.containsKey("i2p.router.net.ssu.port")) {
String oldSSUPort = "" + _context.getProperty(UDPTransport.PROP_INTERNAL_PORT, 8887);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.port")) != null) {
if (oldSSUPort == null || !oldSSUPort.equals(inParam.trim())) {
Integer newPort;
try {
newPort = Integer.valueOf(inParam);
if (newPort < 1 || newPort > 65535) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ssu.port\" must be a string representing a number in the range 1-65535. " + inParam + " isn't valid."), req.getID());
}
Map<String, String> config = new HashMap<String, String>();
config.put(UDPTransport.PROP_EXTERNAL_PORT, String.valueOf(newPort));
config.put(UDPTransport.PROP_INTERNAL_PORT, String.valueOf(newPort));
_context.router().saveConfig(config, null);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.port", oldSSUPort);
}
}
if (inParams.containsKey("i2p.router.net.ssu.hostname")) {
String oldSSUHostname = _context.getProperty(UDPTransport.PROP_EXTERNAL_HOST);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.hostname")) != null) {
if (oldSSUHostname == null || !oldSSUHostname.equals(inParam.trim())) {
_context.router().saveConfig(UDPTransport.PROP_EXTERNAL_HOST, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.hostname", oldSSUHostname);
}
}
if (inParams.containsKey("i2p.router.net.ssu.autoip")) {
String oldSSUAutoIP = _context.getProperty(UDPTransport.PROP_SOURCES);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.autoip")) != null) {
inParam = inParam.trim().toLowerCase();
if (oldSSUAutoIP == null || !oldSSUAutoIP.equals(inParam)) {
if (inParam.equals("ssu") || inParam.equals("local,ssu") || inParam.equals("upnp,ssu") || inParam.equals("local,upnp,ssu")) {
_context.router().saveConfig(UDPTransport.PROP_SOURCES, inParam);
restartNeeded = true;
} else {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ssu.autoip\" can only be ssu/local,upnp,ssu/local/ssu/upnp,ssu. " + inParam + " isn't valid."), req.getID());
}
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.autoip", oldSSUAutoIP);
}
}
// Non-setable key.
if (inParams.containsKey("i2p.router.net.ssu.detectedip")) {
if ((inParam = (String) inParams.get("i2p.router.net.ssu.autoip")) == null) {
byte[] ipBytes = _context.router().getRouterInfo().getTargetAddress("SSU").getIP();
try {
InetAddress i = InetAddress.getByAddress(ipBytes);
outParams.put("i2p.router.net.ssu.detectedip", i.getHostAddress());
} catch (UnknownHostException e) {
outParams.put("i2p.router.net.ssu.detectedip", "Failed to parse ip address");
}
}
}
if (inParams.containsKey("i2p.router.net.upnp")) {
String oldUPNP = _context.getProperty(TransportManager.PROP_ENABLE_UPNP);
if ((inParam = (String) inParams.get("i2p.router.net.upnp")) != null) {
if (oldUPNP == null || !oldUPNP.equals(inParam.trim())) {
_context.router().saveConfig(TransportManager.PROP_ENABLE_UPNP, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.upnp", oldUPNP);
}
}
if (inParams.containsKey("i2p.router.net.bw.share")) {
String oldShare = _context.router().getConfigSetting(Router.PROP_BANDWIDTH_SHARE_PERCENTAGE);
if ((inParam = (String) inParams.get("i2p.router.net.bw.share")) != null) {
if (oldShare == null || !oldShare.equals(inParam.trim())) {
Integer percent;
try {
percent = Integer.parseInt(inParam);
if (percent < 0 || percent > 100 || inParam.length() == 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.share\" A positive integer must supplied, \"" + inParam + "\" isn't valid"), req.getID());
}
_context.router().saveConfig(Router.PROP_BANDWIDTH_SHARE_PERCENTAGE, inParam);
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.share", oldShare);
}
}
if (inParams.containsKey("i2p.router.net.bw.in")) {
String oldBWIn = _context.getProperty(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH);
if ((inParam = (String) inParams.get("i2p.router.net.bw.in")) != null) {
Integer rate;
try {
rate = Integer.parseInt(inParam);
if (rate < 0 || inParam.length() == 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.in\" A positive integer must supplied, " + inParam + " isn't valid"), req.getID());
}
Integer burstRate = (rate * BW_BURST_PCT) / 100;
Integer burstSize = (burstRate * BW_BURST_TIME);
if (oldBWIn == null || !oldBWIn.equals(rate.toString())) {
Map<String, String> config = new HashMap<String, String>();
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, rate.toString());
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BURST_BANDWIDTH, burstRate.toString());
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH_PEAK, burstSize.toString());
_context.router().saveConfig(config, null);
_context.bandwidthLimiter().reinitialize();
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.in", oldBWIn);
}
}
if (inParams.containsKey("i2p.router.net.bw.out")) {
String oldBWOut = _context.getProperty(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH);
if ((inParam = (String) inParams.get("i2p.router.net.bw.out")) != null) {
Integer rate;
try {
rate = Integer.parseInt(inParam);
if (rate < 0 || inParam.length() == 0)
throw new NumberFormatException();
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.out\" A positive integer must supplied, " + inParam + " isn't valid"), req.getID());
}
Integer burstRate = (rate * BW_BURST_PCT) / 100;
Integer burstSize = (burstRate * BW_BURST_TIME);
if (oldBWOut == null || !oldBWOut.equals(rate.toString())) {
Map<String, String> config = new HashMap<String, String>();
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, rate.toString());
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BURST_BANDWIDTH, burstRate.toString());
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH_PEAK, burstSize.toString());
_context.router().saveConfig(config, null);
_context.bandwidthLimiter().reinitialize();
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.out", oldBWOut);
}
}
if (inParams.containsKey("i2p.router.net.laptopmode")) {
String oldLaptopMode = _context.getProperty(UDPTransport.PROP_LAPTOP_MODE);
if ((inParam = (String) inParams.get("i2p.router.net.laptopmode")) != null) {
if (oldLaptopMode == null || !oldLaptopMode.equals(inParam.trim())) {
_context.router().saveConfig(UDPTransport.PROP_LAPTOP_MODE, String.valueOf(inParam));
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.laptopmode", oldLaptopMode);
}
}
if (settingsSaved)
_context.router().saveConfig();
outParams.put("SettingsSaved", settingsSaved);
outParams.put("RestartNeeded", restartNeeded);
return new JSONRPC2Response(outParams, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by vituperative.
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.JSONRPC2Request in project i2pplus by vituperative.
the class Dispatcher method process.
@Override
public JSONRPC2Response process(final JSONRPC2Request request, final MessageContext requestCtx) {
long startNanosec = 0;
// Measure request processing time?
if (reportProcTime)
startNanosec = System.nanoTime();
final String method = request.getMethod();
RequestHandler handler = getRequestHandler(method);
if (handler == null) {
// We didn't find a handler for the requested RPC
Object id = request.getID();
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, id);
}
// Process the request
JSONRPC2Response response = handler.process(request, requestCtx);
if (reportProcTime) {
final long procTimeNanosec = System.nanoTime() - startNanosec;
response.appendNonStdAttribute("xProcTime", procTimeNanosec / 1000 + " us");
}
return response;
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project starcoin-java by starcoinorg.
the class JsonRPCClient method getObjectArray.
/**
* 获取对象列表的接口
*
* @param session
* @param method rpc接口的方法名
* @param params rpc接口的参数列表
* @param requestId 请求id
* @param clazz 返回对象的封装类
* @return
* @throws JSONRPC2SessionException
*/
protected List<T> getObjectArray(JSONRPC2Session session, String method, List<Object> params, int requestId, Class<T> clazz) throws JSONRPC2SessionException {
JSONRPC2Request request = new JSONRPC2Request(method, params, requestId);
JSONRPC2Response response = session.send(request);
if (response.indicatesSuccess()) {
Object result = response.getResult();
if (result != null) {
return JSON.parseArray(result.toString(), clazz);
}
}
return null;
}
Aggregations