Search in sources :

Example 31 with JSONRPC2Request

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.

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());
}
Also used : ReseedChecker(net.i2p.router.networkdb.reseed.ReseedChecker) HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) ClientAppManager(net.i2p.app.ClientAppManager) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) UpdateManager(net.i2p.update.UpdateManager)

Example 32 with JSONRPC2Request

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.

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 33 with JSONRPC2Request

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project starcoin-java by starcoinorg.

the class JsonRPCClient method getObjectParseJackson.

/**
 * 获取单个对象的接口(返回用Jackson封装,某些json格式用fastjson解析有问题,故此增加此方法)
 *
 * @param session
 * @param method    rpc接口的方法名
 * @param params    rpc接口的参数列表
 * @param requestId 请求id
 * @param clazz     返回对象的封装类
 * @return
 * @throws JSONRPC2SessionException
 */
protected T getObjectParseJackson(JSONRPC2Session session, String method, List<Object> params, int requestId, Class<T> clazz) throws JSONRPC2SessionException, IOException {
    JSONRPC2Request request = new JSONRPC2Request(method, params, requestId);
    JSONRPC2Response response = session.send(request);
    if (response.indicatesSuccess()) {
        Object result = response.getResult();
        if (result != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(result.toString(), clazz);
        }
    }
    return null;
}
Also used : JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONObject(com.alibaba.fastjson.JSONObject) JSONRPC2Request(com.thetransactioncompany.jsonrpc2.JSONRPC2Request) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 34 with JSONRPC2Request

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project starcoin-java by starcoinorg.

the class JsonRPCClient method getSubObject.

/**
 * 获取单个对象的属性值接口
 *
 * @param session
 * @param method    rpc接口的方法名
 * @param params    rpc接口的参数列表
 * @param requestId 请求id
 * @param clazz     返回对象的封装类
 * @return
 * @throws JSONRPC2SessionException
 */
protected T getSubObject(JSONRPC2Session session, String method, List<Object> params, int requestId, String subKey, 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) {
            JSONObject jb = JSON.parseObject(result.toString());
            return jb.getObject(subKey, clazz);
        }
    }
    return null;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONObject(com.alibaba.fastjson.JSONObject) JSONRPC2Request(com.thetransactioncompany.jsonrpc2.JSONRPC2Request)

Aggregations

JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)31 JSONRPC2Error (com.thetransactioncompany.jsonrpc2.JSONRPC2Error)24 HashMap (java.util.HashMap)24 JSONRPC2Request (com.thetransactioncompany.jsonrpc2.JSONRPC2Request)7 Map (java.util.Map)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONRPC2Session (com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session)3 JSONRPC2SessionException (com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException)3 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