private void serviceBrowser(String response, Boolean done, String responseId, PrintWriter writer,
SlaveBrowser browser) throws JsonParseException {
addResponseId(responseId, browser);
browser.heartBeat();
Command command = null;
if (isResponseValid(response)) {
Response res = gson.fromJson(response, Response.class);
logger.trace("response type: " + res.getResponseType());
// TODO (corysmith): Replace this with polymorphism,
// using the response type to create disposable actions.
switch (res.getResponseType()) {
case BROWSER_READY:
handleFileLoadResult(browser, res);
// TODO(corysmith): Move the loading of files to a browser into the
// server
browser.addResponse(
new Response(ResponseType.FILE_LOAD_RESULT.toString(), res.getResponse(), browser
.getBrowserInfo(), "", res.getExecutionTime()), false);
browser.ready();
break;
case FILE_LOAD_RESULT:
handleFileLoadResult(browser, res);
browser.addResponse(res, done);
break;
case NOOP:
break;
case LOG:
BrowserLog log = gson.fromJson(res.getResponse(), res.getGsonType());
if (log.getLevel() == 1000) {
logger.info("Error in browser: " + res.toString());
} else {
logger.info("Message from the browser: " + res.toString());
}
browser.addResponse(res, done);
break;
// reset the browsers fileset.
case RESET_RESULT:
browser.resetFileSet();
logger.debug("Clearing fileset for {}", browser);
handleFileLoadResult(browser, res);
// queue the load results for the next command to be run.
browser.addResponse(
new Response(ResponseType.FILE_LOAD_RESULT.toString(), res.getResponse(), browser
.getBrowserInfo(), "", res.getExecutionTime()), false);
browser.addResponse(res, done);
break;
case UNKNOWN:
logger.error("Recieved Unknown: " + response);
browser.addResponse(res, done);
break;
case BROWSER_PANIC:
logger.debug("Browser panic for {}", res.toString());
default:
browser.addResponse(res, done);
break;
}
logger.trace("Received:\n done: {} \n res:\n {}\n", new Object[] {done, res});
}
if (isResponseIdValid(responseId) && !done && !isResponseValid(response)) {
logger.trace("Streaming query for ids {} from {}", streamedResponses.get(browser), browser);
}
// TODO(corysmith): What do we do?
if (!isResponseValid(response) && done && browser.isCommandRunning()) {
logger.error("Streaming ending, but no response sent for {} while running {}",
browser,
browser.getCommandRunning());
}
// TODO(corysmith): Refactoring the streaming into a separate layer.
if (!done) { // we are still streaming, so we respond with the streaming
// acknowledge.
// this is independent of receiving an actual response.
final String jsonResponse = gson.toJson(new BrowserStreamAcknowledged(streamedResponses.get(browser)));
logger.trace("sending jsonResponse {}", jsonResponse);
writer.print(jsonResponse);
writer.flush();
return;
} else {
streamedResponses.get(browser).clear();
}
if(command == null) {
command = browser.dequeueCommand();
browser.heartBeat();
}
logger.trace("sending command {}", command == null ? "null" : command.getCommand());
writer.print(command.getCommand());
}