Search in sources :

Example 16 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
}
Also used : RateStat(net.i2p.stat.RateStat) HashMap(java.util.HashMap) Rate(net.i2p.stat.Rate) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Example 17 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Example 18 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) Router(net.i2p.router.Router) RouterVersion(net.i2p.router.RouterVersion) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2p.i2p by i2p.

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;
}
Also used : JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response)

Example 20 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

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());
}
Also used : RateStat(net.i2p.stat.RateStat) HashMap(java.util.HashMap) Rate(net.i2p.stat.Rate) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Aggregations

JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)31 JSONRPC2Error (com.thetransactioncompany.jsonrpc2.JSONRPC2Error)24 HashMap (java.util.HashMap)24 Map (java.util.Map)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONRPC2Request (com.thetransactioncompany.jsonrpc2.JSONRPC2Request)4 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ClientAppManager (net.i2p.app.ClientAppManager)3 AuthToken (net.i2p.i2pcontrol.security.AuthToken)3 Router (net.i2p.router.Router)3 RouterVersion (net.i2p.router.RouterVersion)3 ReseedChecker (net.i2p.router.networkdb.reseed.ReseedChecker)3 Rate (net.i2p.stat.Rate)3 RateStat (net.i2p.stat.RateStat)3 UpdateManager (net.i2p.update.UpdateManager)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1