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);
}
Aggregations