Search in sources :

Example 6 with RpcError

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

the class RemoteServerEventListener method doListen.

private void doListen() {
    // abort if we are no longer running
    if (!isListening_)
        return;
    // setup request callback (save reference for cancellation)
    activeRequestCallback_ = new ServerRequestCallback<JsArray<ClientEvent>>() {

        @Override
        public void onResponseReceived(JsArray<ClientEvent> events) {
            // keep watchdog appraised of successful receipt of events
            watchdog_.notifyResponseReceived();
            try {
                // only processs events if we are still listening
                if (isListening_ && (events != null)) {
                    for (int i = 0; i < events.length(); i++) {
                        // is dispatched
                        if (!isListening_)
                            return;
                        // disppatch event
                        ClientEvent event = events.get(i);
                        dispatchEvent(event);
                        lastEventId_ = event.getId();
                    }
                }
            }// listen() again after processing
             catch (Throwable e) {
                GWT.log("ERROR: Processing client events", e);
            }
            // listen for more events
            listen();
        }

        @Override
        public void onError(ServerError error) {
            // stop listening for events
            stop();
            // if this was server unavailable then signal event and return
            if (error.getCode() == ServerError.UNAVAILABLE) {
                ServerUnavailableEvent event = new ServerUnavailableEvent();
                server_.getEventBus().fireEvent(event);
                return;
            }
            // result in our server getting hammered with requests)
            if (listenErrorCount_++ <= 5) {
                Timer startTimer = new Timer() {

                    @Override
                    public void run() {
                        // by some other means (e.g. ensureListening, etc)
                        if (!isListening_)
                            start();
                    }
                };
                startTimer.schedule(500);
            } else // otherwise reset the listen error count and remain stopped
            {
                listenErrorCount_ = 0;
            }
        }
    };
    // retry handler (restart listener)
    RetryHandler retryHandler = new RetryHandler() {

        public void onRetry() {
            // need to do a full restart to ensure that the existing
            // activeRequest_ and activeRequestCallback_ are cleaned up
            // and all state is reset correctly
            restart();
        }

        public void onError(RpcError error) {
            // error while attempting to recover, to be on the safe side
            // we simply stop listening for events. if rather than stopping 
            // we restarted we would open ourselves up to a situation
            // where we keep hitting the same error over and over again.
            stop();
        }
    };
    // send request
    activeRequest_ = server_.getEvents(lastEventId_, activeRequestCallback_, retryHandler);
}
Also used : JsArray(com.google.gwt.core.client.JsArray) Timer(com.google.gwt.user.client.Timer) ServerError(org.rstudio.studio.client.server.ServerError) RpcError(org.rstudio.core.client.jsonrpc.RpcError)

Aggregations

RpcError (org.rstudio.core.client.jsonrpc.RpcError)6 RpcResponse (org.rstudio.core.client.jsonrpc.RpcResponse)5 JSONString (com.google.gwt.json.client.JSONString)3 JsArrayString (com.google.gwt.core.client.JsArrayString)2 RequestLogEntry (org.rstudio.core.client.jsonrpc.RequestLogEntry)2 RpcResponseHandler (org.rstudio.core.client.jsonrpc.RpcResponseHandler)2 GWT (com.google.gwt.core.client.GWT)1 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 JsArray (com.google.gwt.core.client.JsArray)1 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 JSONArray (com.google.gwt.json.client.JSONArray)1 JSONObject (com.google.gwt.json.client.JSONObject)1 Timer (com.google.gwt.user.client.Timer)1 FormPanel (com.google.gwt.user.client.ui.FormPanel)1 SubmitCompleteEvent (com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent)1 SubmitCompleteHandler (com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler)1 WindowEx (org.rstudio.core.client.dom.WindowEx)1 JsArrayEx (org.rstudio.core.client.js.JsArrayEx)1 RpcRequest (org.rstudio.core.client.jsonrpc.RpcRequest)1 RpcRequestCallback (org.rstudio.core.client.jsonrpc.RpcRequestCallback)1