@Override public void streamAppended(String text, IStreamMonitor monitor) { log.log(Level.FINER, "Text was received: {0}", text); synchronized (jsonBuffer) { String[] lines = text.split(PVSConstants.NL); for (String line : lines) { if (LCB.equals(line)) { if (jsonStarted) { log.severe("Got another {, but did not expect it"); resetJSONBuffer(); } else { jsonStarted = true; jsonBuffer.append(line).append(PVSConstants.NL); } } else if (RCB.equals(line)) { if (!jsonStarted) { log.severe("Got and }, but did not expect it"); resetJSONBuffer(); } else { jsonBuffer.append(line).append(PVSConstants.NL); String jbs = jsonBuffer.toString(); PVSExecutionManager.INST().dispatchJSONMessage(jbs); resetJSONBuffer(); } } else if (!jsonStarted) { if (pvsPromptPattern.matcher(line).matches()) { synchronized (bufferedLines) { PVSExecutionManager.INST().pvsPromptReceived(bufferedLines, line); bufferedLines.clear(); } } else { // line is unstructured data if (!"nil" .equals( line)) { // nil is the result of sending a JSON to PVS. For now let's ignore and // not display them bufferedLines.add(line); PVSExecutionManager.INST().dispatchStringMessage(line + PVSConstants.NL); } } } else { jsonBuffer.append(line).append(PVSConstants.NL); } } } }
/** * the command has been executed, so extract extract the needed information from the application * context. */ public Object execute(ExecutionEvent event) throws ExecutionException { log.fine("Message to start PVS was received"); window = HandlerUtil.getActiveWorkbenchWindowChecked(event); if (PVSExecutionManager.INST().isPVSRunning()) { MessageDialog.openInformation( window.getShell(), "PVS Running", "An instance of PVS is already running."); } else { try { final PVSConsole console = PVSConsole.getConsole(); console.activate(); console.clearConsole(); final IOConsoleOutputStream outStream = console.newOutputStream(); Map<String, String> attributes = new HashMap<String, String>(); attributes.put(IProcess.ATTR_CMDLINE, PVSExecutionManager.INST().getPVSStartingCommand()); ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null); IProcess process = DebugPlugin.newProcess( launch, PVSExecutionManager.INST().startPVS(), Activator.name, attributes); PVSExecutionManager.INST().setIProcess(process); DebugPlugin.getDefault().getLaunchManager().addLaunch(launch); DebugPlugin.getDefault().addDebugEventListener(PVSExecutionManager.INST()); PVSJsonWrapper.init(); PVSExecutionManager.INST().removeRespondListeners(); PVSExecutionManager.INST() .addListener( new PVSRespondListener() { @Override public void onMessageReceived(String message) { log.log(Level.INFO, "Message received: {0}", message); try { outStream.write(message); } catch (IOException e) { e.printStackTrace(); } } @Override public void onMessageReceived(JSONObject message) { log.log(Level.INFO, "JSON received: {0}", message); PVSJsonWrapper.INST().addToJSONQueue(message); } @Override public void onPromptReceived(List<String> previousLines, String prompt) { log.log(Level.INFO, "Prompt received: {0}", prompt); try { outStream.write(prompt); } catch (IOException e) { e.printStackTrace(); } PVSPromptProcessor.processPrompt(previousLines, prompt); } }); IStreamsProxy streamProxy = process.getStreamsProxy(); IStreamMonitor outMonitor = streamProxy.getOutputStreamMonitor(); outMonitor.addListener(new PVSStreamListener(EclipsePluginUtil.getLispType())); IOConsoleKeyboardReader.init(console); IOConsoleKeyboardReader.INST() .addListener( new IOConsoleKeyboardReader.IOConsoleKeyboardReaderListener() { public void onTextReceived(String text) { PVSExecutionManager.INST().writeToPVS(text); } }); IOConsoleKeyboardReader.INST().start(); Thread.sleep(500); restorePVSContext(); } catch (IOException e) { log.severe("Failed to start PVS"); MessageDialog.openInformation(window.getShell(), "Error", "Failed to start PVS"); } catch (InterruptedException e) { log.severe("Failed to restore PVS context"); MessageDialog.openInformation( window.getShell(), "Error", "Failed to restore the PVS context"); } catch (PVSException e) { log.severe("Failed to restore PVS context"); MessageDialog.openInformation( window.getShell(), "Error", "Failed to restore the PVS context"); } } return null; }