Commit 78a04c48 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

bug fixes, tool state support

parent b418558a
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -238,6 +238,9 @@ public class LaunchCore {
            		project,
            		resource,
            		logBuildStamp);
            if (VDTLaunchUtil.getRunner().getRunningBuilds().isAlreadyOpen(tool.getName())){
            	return;
            }
            DebugUITools.launch(launchConfig, ILaunchManager.RUN_MODE);
        } catch (CoreException e) {
            IStatus status = e.getStatus();
+135 −10
Original line number Diff line number Diff line
@@ -21,12 +21,19 @@ import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsoleManager;

import com.elphel.vdt.core.tools.ToolsCore;
import com.elphel.vdt.core.tools.params.Tool;
import com.elphel.vdt.core.tools.params.Tool.TOOL_STATE;
import com.elphel.vdt.ui.MessageUI;
import com.elphel.vdt.veditor.VerilogPlugin;
import com.elphel.vdt.veditor.preference.PreferenceStrings;

@@ -58,14 +65,13 @@ public class RunningBuilds {
	}
    private Map<IConsole,MonListener> parserListeners=null; // consoles mapped to pairs of monitors and listeners
                                                            // that should be disconnected when parser is terminated

//	int nextBuildStep=0;
	private final Map<String, VDTRunnerConfiguration> unfinishedBuilds;
	
	
	public RunningBuilds(){
		parserListeners= new ConcurrentHashMap<IConsole,MonListener>();
		unfinishedBuilds = new ConcurrentHashMap<String, VDTRunnerConfiguration>();
		IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
// This is not used, just for testing
		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
			System.out.println("***Addded console listeners");
		}
@@ -75,7 +81,7 @@ public class RunningBuilds {
					if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
						System.out.println("+++ Added: "+consoles[i].getName());
					}
					// Only shows added consoles
					setConsole(consoles[i]);
				}
			}
			public void consolesRemoved(IConsole[] consoles){
@@ -89,14 +95,18 @@ public class RunningBuilds {
				}
			}
		});
		
	}
	
	public void addMonListener(IConsole parserConsole, IStreamMonitor monitor, IStreamListener listener){
		synchronized (parserListeners){
			parserListeners.put(parserConsole, new MonListener(monitor, listener));
		}
	}
	private void removeMonListener(IConsole parserConsole){
		MonListener monListener=parserListeners.remove(parserConsole);
		MonListener monListener;
		synchronized (parserListeners){
			monListener=parserListeners.remove(parserConsole);
		}
		if (monListener!=null){
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.println("--- Removing listener from the terminated parser console "+parserConsole.getName());
@@ -121,22 +131,26 @@ public class RunningBuilds {
				System.out.print("Got console name:"+consoleName);
			}
			VDTRunnerConfiguration runConfig=unfinishedBuilds.get(consoleName);
/*			
			if (runConfig.hasConsole(console)){
				if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
					System.out.println(consoleName+" -> GOT IT");
				}
				return consoleName;
			}
*/			
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.println(consoleName+" -> no luck");
			}
		}
		return null;		
	}
	public void removeConsole(IConsole console){
	
	public void removeConsoleOld(IConsole console){
		String consoleName=findConsoleParent(console);
		if (consoleName!=null){
			VDTRunnerConfiguration runConfig=unfinishedBuilds.get(consoleName);
/*			
			runConfig.removeConsole(console);
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.println("Removing console "+console.getName()+" from runConfig for "+consoleName);
@@ -147,6 +161,7 @@ public class RunningBuilds {
				}
				unfinishedBuilds.remove(consoleName);
			}
*/			
		} else {
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.println("Console "+console.getName()+" did not belong here");
@@ -158,8 +173,9 @@ public class RunningBuilds {
	}
	
	public VDTRunnerConfiguration resumeConfiguration(String consoleName){
		System.out.println("VDTRunnerConfiguration#resumeConfiguration("+consoleName+")");
		VDTRunnerConfiguration conf=unfinishedBuilds.get(consoleName);
		unfinishedBuilds.remove(consoleName);
//		unfinishedBuilds.remove(consoleName); // 
		return conf;
	}

@@ -169,11 +185,120 @@ public class RunningBuilds {
	}

	public void removeConfiguration(String consoleName){
		System.out.println("VDTRunnerConfiguration#removeConfiguration("+consoleName+")");
		unfinishedBuilds.remove(consoleName);
		System.out.println("Running consoles:");
		listConfigurations();
	}

	public void saveUnfinished(String consoleName, VDTRunnerConfiguration configuration ){
		System.out.println("VDTRunnerConfiguration#saveUnfinished("+consoleName+", configuration)");
		unfinishedBuilds.put(consoleName, configuration);
		System.out.println("Running consoles:");
		listConfigurations();
	}
	
	public void listConfigurations(){
		Iterator<String> iter=unfinishedBuilds.keySet().iterator();
		int i=0;
		while (iter.hasNext()) {
			String consoleName=iter.next();
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.print(i+": "+consoleName);
			}
		
		}		
	}
	
	public boolean findConsole(IConsole iConsole){
		String needleConsoleName=iConsole.getName();
		Iterator<String> iter=unfinishedBuilds.keySet().iterator();
		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
			System.out.println("findConsole("+needleConsoleName+")");
		}
		while (iter.hasNext()) {
			String consoleName=iter.next();
			if (needleConsoleName.equals(consoleName)){
				if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) System.out.print("Got match");
				return true;
			}
		}
		return false;
	}

	public boolean setConsole(IConsole iConsole){ // from add console;
		String needleConsoleName=iConsole.getName();
		Iterator<String> iter=unfinishedBuilds.keySet().iterator();
		while (iter.hasNext()) {
			String consoleName=iter.next();
			if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
				System.out.print("needleConsoleName="+needleConsoleName+", consoleName= "+consoleName);
			}

			if (needleConsoleName.equals(consoleName)){
				if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) System.out.print("Got match");
				VDTRunnerConfiguration runConfig=unfinishedBuilds.get(consoleName);
				runConfig.setIConsole(iConsole);
				// Add console listener here to detect change name
				final IConsole fIconsole=iConsole; 
				final String fConsoleName=fIconsole.getName();	
				final IPropertyChangeListener fListener =new IPropertyChangeListener() {
					public void propertyChange(PropertyChangeEvent event) {
						if (!fConsoleName.equals(fIconsole.getName())){
							fIconsole.removePropertyChangeListener(this);
							if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
								System.out.println(">>> "+fConsoleName+" -> "+fIconsole.getName());
							}
							removeConsole(fIconsole); // changed name means "<terminated>..."
						}
					}
				};
				fIconsole.addPropertyChangeListener(fListener);
				if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)){
					System.out.println("fiCons.getName()="+fIconsole.getName()+"addPropertyChangeListener()");
				}
				return true;
			}
		}
		return false;
	}

	// Only for closing consoles
	public boolean removeConsole(IConsole iConsole){ // from add console;
		Iterator<String> iter=unfinishedBuilds.keySet().iterator();
		while (iter.hasNext()) {
			String consoleName=iter.next();
			VDTRunnerConfiguration runConfig=unfinishedBuilds.get(consoleName);
			if (runConfig.getIConsole()==iConsole){ // same instance
				runConfig.setIConsole(null);
				Tool tool=ToolsCore.getTool(runConfig.getToolName());
				if (tool.getState()==TOOL_STATE.KEPT_OPEN) {
					tool.setState(TOOL_STATE.NEW);
					if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) System.out.print("Killed open console");
					return true;
				}
			}
		}
		return false;
	}

	public boolean isAlreadyOpen(String toolName){ // from add console;
		Iterator<String> iter=unfinishedBuilds.keySet().iterator();
		while (iter.hasNext()) {
			String consoleName=iter.next();
			VDTRunnerConfiguration runConfig=unfinishedBuilds.get(consoleName);
			if (toolName.equals(runConfig.getToolName())){
				Tool tool=ToolsCore.getTool(runConfig.getToolName());
				tool.setRunning(false);
				tool.updateViewStateIcon();
				if (tool.getState()==TOOL_STATE.KEPT_OPEN) {
					MessageUI.error("Termninal that starts by this tool ("+toolName+") is already open in console \""+consoleName+"\"");
					return true;
				}
			}
		}
		return false;
	}

	
} // class RunningBuilds
+7 −5
Original line number Diff line number Diff line
@@ -277,33 +277,35 @@ public class ToolLogFile {
			}
		} catch (IOException e) {
			System.out.println("Failed to append error log file "+
		   (singleFile?targetOutIFile:targetErrIFile).toString());
		   (singleFile?targetOutIFile:targetErrIFile).toString()+" string was:"+string);
			if (singleFile) closeOut();
			else  closeErr();
//			close();
		}
	}

	public void closeOut(){
		if (logOutWriter!=null) {
	public void closeOut(){ // should be called first
		if ((logOutWriter!=null) && ((logErrWriter==null) || (logErrWriter==logOutWriter))) {
			try {
				logOutWriter.close();
				if(debugPrint) System.out.println("closeOut(), wrote "+outBytes+" bytes");
			} catch (IOException e) {
				System.out.println("Failed to close log file "+targetOutIFile.toString());
			}

			logOutWriter=null;
		}
	}

	public void closeErr(){
		if (logErrWriter!=null)
		if (logErrWriter!=null) {
			try {
				logErrWriter.close();
				if(debugPrint) System.out.println("closeErr(), wrote "+errBytes+" bytes");
			} catch (IOException e) {
				System.out.println("Failed to close error log file "+targetErrIFile.toString());
			}
			logErrWriter=null;
		}
	}
	
	public FileReader getOutReader(){
+16 −27
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@ public class VDTConsoleRunner{
		String playBackStamp=runConfig.getPlayBackStamp();
		if (playBackStamp!=null){
			System.out.println("Wrong, it should be playback, not run, as playBackStamp = "+playBackStamp+ "(not null)");
			return null;
    		VDTLaunchUtil.getRunner().abortLaunch(runConfig.getOriginalConsoleName());    		
		}
		
		
@@ -113,14 +113,7 @@ public class VDTConsoleRunner{
		}
		if (iCons==null) {
			MessageUI.error("Specified console: "+consolePrefix+" is not found (was looking for \""+consoleStartsWith+"\"");
	    	Tool tool=ToolsCore.getTool(runConfig.getToolName());
    		tool.setDirty(false);
    		tool.setState(TOOL_STATE.FAILURE);
    		tool.setRunning(false);
    		tool.setFinishTimeStamp();
    		tool.updateViewStateIcon();
    		//removeConfiguration
    		VDTLaunchUtil.getRunner().getRunningBuilds().removeConfiguration(runConfig.getOriginalConsoleName());
    		VDTLaunchUtil.getRunner().abortLaunch(runConfig.getOriginalConsoleName());    		
			return null;
		}
		// try to send 
@@ -189,8 +182,6 @@ public class VDTConsoleRunner{
        final IStreamsProxy2 fSendOutputToStreamProxy= stdoutStreamProxy;

// connect input streams of the parsers to the out from the console process         
        IStreamMonitor consoleOutStreamMonitor=null;
        IStreamMonitor consoleErrStreamMonitor=null;
        runConfig.resetConsoleText();
        String interrupt=buildParamsItem.getInterrupt(); // Not yet used
        runConfig.setConsoleFinish(buildParamsItem.getPrompt());
@@ -220,7 +211,8 @@ public class VDTConsoleRunner{
        //final ToolLogFile fToolLogFile=toolLogFile;	
        //errorListener=null;
        //        if (fSendErrorsToStreamProxy!=null){
        consoleErrStreamMonitor=consoleInStreamProxy.getErrorStreamMonitor();

        final IStreamMonitor consoleErrStreamMonitor=consoleInStreamProxy.getErrorStreamMonitor();
        errorListener=new IStreamListener(){
        	public void streamAppended(String text, IStreamMonitor monitor){
        		if (fSendErrorsToStreamProxy!=null) {
@@ -236,19 +228,19 @@ public class VDTConsoleRunner{
        		}
        		if (runConfig.addConsoleText(text)){
        			if (debugPrint)  System.out.println("Got finish sequence");
        			// TODO: launch continuation of the build process
        			consoleErrStreamMonitor.removeListener(errorListener);
        			finishConsolescript(); // got here when computer running Vivado was disconnected
        		}
        	}
        };
        VDTLaunchUtil.getRunner().getRunningBuilds().addMonListener( // to remove listener when parser is terminated
        if (processErr!=null) VDTLaunchUtil.getRunner().getRunningBuilds().addMonListener( // to remove listener when parser is terminated
        		DebugUITools.getConsole(processErr), //IConsole parserConsole,
        		consoleErrStreamMonitor,
        		errorListener);
        consoleErrStreamMonitor.addListener(errorListener);
        outputListener=null;
        //        if (fSendOutputToStreamProxy!=null){
        consoleOutStreamMonitor=consoleInStreamProxy.getOutputStreamMonitor();
        final IStreamMonitor consoleOutStreamMonitor=consoleInStreamProxy.getOutputStreamMonitor();
        outputListener=new IStreamListener(){
        	public void streamAppended(String text, IStreamMonitor monitor){
        		if (fSendOutputToStreamProxy!=null){
@@ -264,11 +256,12 @@ public class VDTConsoleRunner{
        		if (runConfig.addConsoleText(text)){
        			if (debugPrint) System.out.println("Got finish sequence");
        			// TODO: launch continuation of the build process
        			consoleOutStreamMonitor.removeListener(outputListener);
        			finishConsolescript();
        		}
        	}
        };
        VDTLaunchUtil.getRunner().getRunningBuilds().addMonListener( // to remove listener when parser is terminated
        if (processOut!=null) VDTLaunchUtil.getRunner().getRunningBuilds().addMonListener( // to remove listener when parser is terminated
        		DebugUITools.getConsole(processOut), //IConsole parserConsole,
        		consoleOutStreamMonitor,
        		outputListener);
@@ -276,11 +269,12 @@ public class VDTConsoleRunner{
        //       }
        //Problems occurred when invoking code from plug-in: "org.eclipse.ui.console".
        //Exception occurred during console property change notification.
        outStream.setColor(new Color(null, 128, 128, 255)); 
        outStream.setColor(new Color(null, 128, 128, 255)); // org.eclipse.swt.SWTException: Invalid thread access
        try {
        	for (int i=0;i<arguments.length;i++){
        		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.LOCAL_ECHO)) {
        			outStream.write(arguments[i]+"\n"); // writes to console itself
        			System.out.println("--->"+arguments[i]+"\n");
        		}
        		consoleInStreamProxy.write(arguments[i]+"\n");
        	}
@@ -294,6 +288,7 @@ public class VDTConsoleRunner{
        	if (debugPrint) System.out.println("Setting timeout "+timeout);
        	final int fTimeout = timeout;
        	timer=new Timer();
        	System.out.println("VDTConsoleRunner(): setting timer "+fTimeout*1000);
        	timer.schedule(new TimerTask() {          
        		@Override
        		public void run() {
@@ -313,11 +308,10 @@ public class VDTConsoleRunner{
    	if (debugPrint) System.out.println("finishConsolescript()");
		String playBackStamp=runConfig.getPlayBackStamp();
		if (playBackStamp!=null){
			// happened when Vivaod console was disconnected with old console listener still attached
			// happened when Vivado console was disconnected with old console listener still attached
			// they should be removed when a parser process is terminated
			
			
			System.out.println("Wrong, it should be playback, not run, as playBackStamp = "+playBackStamp+ "(not null)");
    		VDTLaunchUtil.getRunner().abortLaunch(runConfig.getOriginalConsoleName());    		
			return;
		}
		if (timer!=null){
@@ -329,6 +323,7 @@ public class VDTConsoleRunner{
        }
    	if (consoleInStreamProxy==null) {
    		System.out.println("Bug: consoleInStreamProxy == null");
    		VDTLaunchUtil.getRunner().abortLaunch(runConfig.getOriginalConsoleName());    		
    		return; // or continue other commands?
    	}
    	if (errorListener !=null) { // disconnect error stream listener
@@ -401,13 +396,7 @@ public class VDTConsoleRunner{
    					!runConfig.gotGood() &&
    					(buildParamsItem.getSuccessString()!=null) &&
    					(buildParamsItem.getFailureString()==null))){
    		tool.setDirty(false);
    		tool.setState(TOOL_STATE.FAILURE);
    		tool.setRunning(false);
    		tool.setFinishTimeStamp();
    		tool.updateViewStateIcon();
    		//removeConfiguration
    		VDTLaunchUtil.getRunner().getRunningBuilds().removeConfiguration(runConfig.getOriginalConsoleName());
    		VDTLaunchUtil.getRunner().abortLaunch(runConfig.getOriginalConsoleName());    		
    		return;
    	}
    	if (runConfig.gotGood()){
+15 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.swt.widgets.Display;

import com.elphel.vdt.Txt;
import com.elphel.vdt.core.tools.contexts.BuildParamsItem;
@@ -111,14 +112,26 @@ public class VDTLaunchConfigurationDelegate implements ILaunchConfigurationDeleg
    	runConfig.setControlFiles((String[])controlFiles.toArray(new String[controlFiles.size()]));
//        String consoleName=VDTRunner.renderProcessLabel(runConfig.getToolName());
    	
        String consoleName=runConfig.getOriginalConsoleName();
        final String consoleName=runConfig.getOriginalConsoleName();
        runner.getRunningBuilds().saveUnfinished(consoleName, runConfig );
        
        String playBackStamp=VDTLaunchUtil.getLogBuildStamp(configuration); // got null
        runConfig.setPlayBackStamp(playBackStamp); // null
        
        if (playBackStamp==null){
        	runner.resumeLaunch(consoleName); // actual run of the tools
        	// Causes "Invalid thread access" when trying to  write to console output if got there directly, not through console event
//        	runner.resumeLaunch(consoleName); // actual run of the tools
        	// try from Display thread
        	Display.getDefault().syncExec(new Runnable() {
        		public void run() {
					try {
						VDTLaunchUtil.getRunner().resumeLaunch(consoleName);
					} catch (CoreException e) {
						System.out.println("Failed to resumeLaunch");
					} //, fiCons, this); // replace with console
        		}
        	});
        	
        } else {
        	runConfig.setBuildStep(-1); // to cause errors if will try to continue
        	runner.logPlaybackLaunch(consoleName); // tool logs playback with parsing
Loading