1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*******************************************************************************
* Copyright (c) 2014 Elphel, Inc.
* This file is a part of VDT plug-in.
* VDT plug-in is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VDT plug-in is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7:
* If you modify this Program, or any covered work, by linking or combining it
* with Eclipse or Eclipse plugins (or a modified version of those libraries),
* containing parts covered by the terms of EPL/CPL, the licensors of this
* Program grant you additional permission to convey the resulting work.
* {Corresponding Source for a non-source form of such a combination shall
* include the source code for the parts of Eclipse or Eclipse plugins used
* as well as that of the covered work.}
*******************************************************************************/
package com.elphel.vdt.core.launching;
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_MODE;
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;
import org.eclipse.ui.console.IConsoleListener;
public class RunningBuilds {
public class MonListener{
private IStreamMonitor monitor;
private IStreamListener listener;
public IStreamMonitor getMonitor() {
return monitor;
}
public IStreamListener getListener() {
return listener;
}
public MonListener(IStreamMonitor monitor, IStreamListener listener) {
super();
this.monitor = monitor;
this.listener = listener;
}
public void finalize() throws Throwable{
if ((monitor!=null) && (listener!=null)){
monitor.removeListener(listener);
}
monitor=null;
listener=null;
super.finalize();
}
}
private Map<IConsole,MonListener> parserListeners=null; // consoles mapped to pairs of monitors and listeners
// that should be disconnected when parser is terminated
private final Map<String, VDTRunnerConfiguration> unfinishedBuilds;
public RunningBuilds(){
parserListeners= new ConcurrentHashMap<IConsole,MonListener>();
unfinishedBuilds = new ConcurrentHashMap<String, VDTRunnerConfiguration>();
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("***Addded console listeners");
}
manager.addConsoleListener(new IConsoleListener(){
public void consolesAdded(IConsole[] consoles){
for (int i=0;i<consoles.length;i++){
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("+++ Added: "+consoles[i].getName());
}
setConsole(consoles[i]);
}
}
public void consolesRemoved(IConsole[] consoles){
for (int i=0;i<consoles.length;i++){
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("--- Removed: "+consoles[i].getName());
}
// unfinishedBuilds.remove(consoles[i]);
removeMonListener(consoles[i]); // remove listeners that provided input data for parsers
removeConsole(consoles[i]);
}
}
});
}
public void addMonListener(IConsole parserConsole, IStreamMonitor monitor, IStreamListener listener){
if (parserListeners==null){
MessageUI.error("BUG in addMonListener(): parserListeners=null - enable breakpoints");
System.out.println("BUG in addMonListener(): parserListeners=null");
return;
}
// synchronized (parserListeners){
parserListeners.put(parserConsole, new MonListener(monitor, listener)); // java.lang.NullPointerException
// }
}
private void removeMonListener(IConsole 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());
}
try {
monListener.finalize();
} catch (Throwable e) {
System.out.println("Failed to finalize monListener for console "+parserConsole.getName());
e.printStackTrace();
}
}
}
public String findConsoleParent(IConsole console){
Iterator<String> iter=unfinishedBuilds.keySet().iterator();
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("findConsoleParent("+console.getName()+")");
}
while (iter.hasNext()) {
String consoleName=iter.next();
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
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 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);
}
if (runConfig.noConsoles()){
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("No consoles left in unfinished "+consoleName+" - removing it too");
}
unfinishedBuilds.remove(consoleName);
}
*/
} else {
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("Console "+console.getName()+" did not belong here");
}
}
}
public boolean isUnfinished(IConsole console){
return unfinishedBuilds.containsKey(console);
}
public VDTRunnerConfiguration resumeConfiguration(String consoleName){
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("VDTRunnerConfiguration#resumeConfiguration("+consoleName+")");
}
VDTRunnerConfiguration conf=unfinishedBuilds.get(consoleName);
// unfinishedBuilds.remove(consoleName); //
return conf;
}
public VDTRunnerConfiguration getConfiguration(String consoleName){
VDTRunnerConfiguration conf=unfinishedBuilds.get(consoleName);
return conf;
}
public void removeConfiguration(String consoleName){
unfinishedBuilds.remove(consoleName);
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("VDTRunnerConfiguration#removeConfiguration("+consoleName+"), running consoles:");
listConfigurations();
}
}
public void saveUnfinished(String consoleName, VDTRunnerConfiguration configuration ){
unfinishedBuilds.put(consoleName, configuration);
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("VDTRunnerConfiguration#saveUnfinished("+consoleName+", configuration), 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.println((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);
tool. setOpenTool(null);
removeConfiguration(consoleName);
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);
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("RunningBuilds#isAlreadyOpen("+toolName+"), state="+tool.getState()+" consoleName="+
consoleName+" threadID="+Thread.currentThread().getId()); // Reused old VDTRunner()
for (String key:unfinishedBuilds.keySet()){
System.out.println("Unfinished build: "+key);
}
}
// got here with no actual console, but unfinishedBuilds had "Vivado ..." in state "New"
if (actualConsoleExists(consoleName)) {
tool.setMode(TOOL_MODE.STOP);
tool.toolFinished();
if (tool.getState()==TOOL_STATE.KEPT_OPEN) {
MessageUI.error("Termninal that starts by this tool ("+toolName+") is already open in console \""+consoleName+"\". You may close it manually.");
tool.toolFinished();
return true;
}
MessageUI.error("Something is wrong in: RunningBuilds#isAlreadyOpen as the console for the specified tool "+tool.getName()+" is already open");
tool.setState(TOOL_STATE.FAILURE);
tool.toolFinished();
return true;
} else {
String msg="BUG: there is unfinishedBuild for cosole="+consoleName+ "( tool="+toolName+
"), but no actual is console open.";
System.out.println(msg);
MessageUI.error(msg);
removeConfiguration(consoleName);
}
}
}
if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_LAUNCHING)) {
System.out.println("RunningBuilds#isAlreadyOpen("+toolName+"), no match threadID="+Thread.currentThread().getId());
for (String key:unfinishedBuilds.keySet()){
System.out.println("Unfinished build: "+key+" tool "+unfinishedBuilds.get(key).getToolName());
}
}
return false;
}
/**
* Checks if there is actual console starting with this name (plus " (" actually exists
* Used to recover if for some reasons unfinishedBuild for this console was not removed (a BUG)
* @param consolePrefix console name without timestamp data
* @return true if such console actually exusts
*/
public boolean actualConsoleExists(String consolePrefix){
IConsoleManager man = ConsolePlugin.getDefault().getConsoleManager(); // debugging
IConsole[] consoles=(IConsole[]) man.getConsoles();
String consoleStartsWith=consolePrefix+" ("; // space and start of date
for (int i=0;i<consoles.length;i++){
if (consoles[i].getName().startsWith(consoleStartsWith)){
return true;
}
}
return false;
}
} // class RunningBuilds