Search in sources :

Example 1 with RpcRequestCallback

use of org.rstudio.core.client.jsonrpc.RpcRequestCallback in project rstudio by rstudio.

the class RemoteServer method sendRequest.

// lowest level sendRequest method -- called from the main workbench
// in two scenarios: direct internal call and servicing a proxied
// request from a satellite window
private RpcRequest sendRequest(String sourceWindow, String scope, String method, JSONArray params, boolean redactLog, final RpcResponseHandler responseHandler, final RetryHandler retryHandler) {
    // ensure we are listening for events. note that we do this here
    // because we are no longer so aggressive about retrying on failed
    // get_events calls. therefore, if we retry and fail a few times
    // we may need to restart event listening.
    ensureListeningForEvents();
    // create request
    String rserverURL = getApplicationURL(scope) + "/" + method;
    RpcRequest rpcRequest = new RpcRequest(rserverURL, method, params, null, redactLog, sourceWindow, clientId_, clientVersion_);
    if (isDisconnected())
        return rpcRequest;
    // send the request
    rpcRequest.send(new RpcRequestCallback() {

        public void onError(RpcRequest request, RpcError error) {
            // ignore errors if we are disconnected
            if (isDisconnected())
                return;
            // error and then retry
            if (resolveRpcErrorAndRetry(error, retryHandler))
                return;
            // first crack goes to globally registered rpc error handlers
            if (!handleRpcErrorInternally(error)) {
                // no global handlers processed it, send on to caller
                responseHandler.onResponseReceived(RpcResponse.create(error));
            }
        }

        public void onResponseReceived(final RpcRequest request, RpcResponse response) {
            //   - handler was cancelled
            if (isDisconnected())
                return;
            // check for error
            if (response.getError() != null) {
                // ERROR: explicit error returned by server
                RpcError error = response.getError();
                // error and then retry
                if (resolveRpcErrorAndRetry(error, retryHandler))
                    return;
                // give first crack to internal handlers, then forward to caller
                if (!handleRpcErrorInternally(error))
                    responseHandler.onResponseReceived(response);
            } else if (response.getAsyncHandle() != null) {
                serverEventListener_.registerAsyncHandle(response.getAsyncHandle(), request, this);
            } else // no error, process the result
            {
                // no error, forward to caller
                responseHandler.onResponseReceived(response);
                // to the queue by the call)
                if (eventsPending(response))
                    serverEventListener_.ensureEvents();
            }
        }
    });
    // return the request
    return rpcRequest;
}
Also used : RpcRequest(org.rstudio.core.client.jsonrpc.RpcRequest) RpcError(org.rstudio.core.client.jsonrpc.RpcError) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) RpcRequestCallback(org.rstudio.core.client.jsonrpc.RpcRequestCallback) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse)

Aggregations

JsArrayString (com.google.gwt.core.client.JsArrayString)1 JSONString (com.google.gwt.json.client.JSONString)1 RpcError (org.rstudio.core.client.jsonrpc.RpcError)1 RpcRequest (org.rstudio.core.client.jsonrpc.RpcRequest)1 RpcRequestCallback (org.rstudio.core.client.jsonrpc.RpcRequestCallback)1 RpcResponse (org.rstudio.core.client.jsonrpc.RpcResponse)1