Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 165948 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/tptp/platform/execution/client/core/IProcess.java (-1 / +7 lines)
Lines 200-205 Link Here
200
            * Add a Process Listener
200
            * Add a Process Listener
201
            * @return        
201
            * @return        
202
            */
202
            */
203
           public void addProcessListener(IProcessListener listener);		   
203
           public void addProcessListener(IProcessListener listener);
204
           
205
           /**
206
            * Get the exit code for this process.
207
            * @return        
208
            */
209
           public int getExitCode();	/* Bug 167208 */
204
}
210
}
205
211
(-)src/org/eclipse/tptp/platform/execution/client/core/internal/ProcessImpl.java (-126 / +196 lines)
Lines 13-19 Link Here
13
 *******************************************************************************/
13
 *******************************************************************************/
14
package org.eclipse.tptp.platform.execution.client.core.internal;
14
package org.eclipse.tptp.platform.execution.client.core.internal;
15
15
16
import java.util.Collection;
17
import java.util.HashMap;
18
import java.util.HashSet;
16
import java.util.Hashtable;
19
import java.util.Hashtable;
20
import java.util.Iterator;
21
import java.util.Map;
22
import java.util.Set;
17
import java.util.Vector;
23
import java.util.Vector;
18
import java.util.Enumeration;
24
import java.util.Enumeration;
19
import java.io.IOException;
25
import java.io.IOException;
Lines 70-75 Link Here
70
	private boolean _requireEclipseLock = false;
76
	private boolean _requireEclipseLock = false;
71
	
77
	
72
	private ProcessEventListener _processListener = null;
78
	private ProcessEventListener _processListener = null;
79
	private int exitCode;	/* Bug 167208 */
73
80
74
	public ProcessImpl(IAgentController ac)
81
	public ProcessImpl(IAgentController ac)
75
	{
82
	{
Lines 374-379 Link Here
374
	{
381
	{
375
		_processId = processId;
382
		_processId = processId;
376
		_isActive = true;
383
		_isActive = true;
384
385
		try {
386
			IProcess proc = ((AgentController)_ac).getProcess(processId);
387
			if (proc == null) {
388
				synchronized(((AgentController)_ac)._processList) {
389
					((AgentController)_ac)._processList.add(this);
390
				}
391
			}
392
		} catch (Exception e) {
393
		}
377
	}
394
	}
378
395
379
	/* (non-Javadoc)
396
	/* (non-Javadoc)
Lines 542-548 Link Here
542
			//Get the PC destID
559
			//Get the PC destID
543
		 	this._destID = getDestID();
560
		 	this._destID = getDestID();
544
		 	if(this._destID == -1) { throw new InactiveAgentException(Constants.TPTP_PLATFORM_EXEC_MSG32); }
561
		 	if(this._destID == -1) { throw new InactiveAgentException(Constants.TPTP_PLATFORM_EXEC_MSG32); }
545
		 	final GenericCommandHandler genCmdHandler = new GenericCommandHandler();
546
	 		org.eclipse.tptp.platform.execution.client.core.internal.commands.LaunchProcessCommand command= new org.eclipse.tptp.platform.execution.client.core.internal.commands.LaunchProcessCommand();
562
	 		org.eclipse.tptp.platform.execution.client.core.internal.commands.LaunchProcessCommand command= new org.eclipse.tptp.platform.execution.client.core.internal.commands.LaunchProcessCommand();
547
	 		command.setExecutable(_processInfo.getExecutable()); 
563
	 		command.setExecutable(_processInfo.getExecutable()); 
548
	 		command.setLocation(_processInfo.getWorkingDirectory()); 
564
	 		command.setLocation(_processInfo.getWorkingDirectory()); 
Lines 557-664 Link Here
557
 				_consoleDataConnectID = ((AgentController)this._ac).getConsoleDataConnection(3);
573
 				_consoleDataConnectID = ((AgentController)this._ac).getConsoleDataConnection(3);
558
 				command.setConsoleConnectID(_consoleDataConnectID); 
574
 				command.setConsoleConnectID(_consoleDataConnectID); 
559
 				this._console.setDataConnectionID(_consoleDataConnectID);
575
 				this._console.setDataConnectionID(_consoleDataConnectID);
576
				((AgentController)this._ac).addDataListener(_consoleDataConnectID, _console.getDataProcessor()); /* Bug 165948 */
560
 			}
577
 			}
561
 			
578
 			
562
 			synchronized(_launcherLock) 
579
 			ProcessCommandHandler processCmdHandler = new ProcessCommandHandler (this);
563
			{
580
		   	((AgentController)_ac).sendCommand(command.buildCommand(), this._destID, processCmdHandler);
564
 			   try 
581
		   	
565
			   { 	
582
	   		if (!processCmdHandler.waitResponce(Constants.processStarted_Cmd)) {
566
 			   	((AgentController)_ac).sendCommand(command.buildCommand(), this._destID, new ICommandHandler(){
583
        		throw new TimeoutException(Constants.TPTP_PLATFORM_EXEC_MSG33);		
567
 			   			public void incomingCommand(INode node, ICommandElement command)
584
	   		}
568
 			   			{	
585
		 } catch (IOException e) {
569
 			   				genCmdHandler.setCommandElement(command);
586
			 e.printStackTrace();
570
 			   			}
571
 			   		});
572
 			    try{_launcherLock.notifyAll();}
573
 			    catch(Exception e){e.printStackTrace();}	
574
			   }
575
 			   	catch(Exception e){e.printStackTrace();}
576
			} //synchronized
577
 			synchronized(_launcherLock)
578
		 	{
579
 				int processLauchTimeOut = 0;
580
		 	    while(genCmdHandler.getCommandElement() == null )
581
		 	    {
582
		 	        try
583
		 	        {		 	        	
584
		 	        	_launcherLock.wait(Constants.TIMEOUT_PERIOD);
585
		 	        	if (processLauchTimeOut == Constants.PROCESS_LAUNCH_TIMEOUT_TRY_COUNT)
586
		 	        	{
587
		 	        		throw new TimeoutException(Constants.TPTP_PLATFORM_EXEC_MSG33);		
588
		 	        	}
589
		 	        	processLauchTimeOut++;
590
		 	        	if (Constants.TPTP_DEBUG) System.out.println("The Process Launch Timeout count" + processLauchTimeOut);
591
		 	        }
592
		 	        catch(TimeoutException e)
593
		 	        {
594
		 	        	_launcherLock.notifyAll();
595
		 	        	//e.printStackTrace();
596
		 	        	throw e;
597
		 	        }
598
		 	        catch(Exception e)
599
		 	        {
600
		 	        	_launcherLock.notifyAll();
601
		 	        	e.printStackTrace();
602
		 	        	break;
603
		 	        }
604
		 	    }
605
		 	}
606
 		
607
 			if (genCmdHandler.getCommandElement() == null) {return;}
608
 			String commandStr = ((CommandFragment)genCmdHandler.getCommandElement()).getCommandData();
609
 			TPTPXMLParse ParseObj = new TPTPXMLParse();
610
 			ParseObj.setParser(commandStr);
611
 			if (Constants.TPTP_DEBUG)System.out.println("Process launch Response "+commandStr);
612
 			Hashtable CommandHash = ParseObj.getHashTable();
613
 			_processId	= 0;
614
 			if(CommandHash.containsKey("processID"))
615
 			{
616
 				_processId = Long.parseLong((String)ParseObj.getHashTable().get("processID"));
617
 				_isActive	= true;
618
 				//Now that you have the PID create a ConsoleData Processor
619
 				ConsoleDataProcessor consoleMap = ((AgentController)this._ac).getConsoleMapper();
620
 				if (_console != null && _console.getDataProcessor() != null)
621
 				{
622
 					((AgentController)this._ac).addDataListener(this._consoleDataConnectID, (IDataProcessor)consoleMap);
623
 					this._console.setProcessID(_processId);
624
 					consoleMap.addDataProcessor(_processId,_console.getDataProcessor(IConsole.STDOUT), _console.getDataProcessor(IConsole.STDERR));
625
 				}
626
 				if(Constants.TPTP_DEBUG) System.out.println("Process launch response "+ commandStr);
627
 				String contextID = (String)ParseObj.getHashTable().get("ctxt");
628
 				_processListener = new ProcessEventListener(new Integer(contextID).intValue(), Constants.PROC_CTLR_EVNT_IID, this);
629
 				_processListener.processLaunched(this);
630
 				
631
 				synchronized(((AgentController)_ac)._processList)
632
 				{
633
 					Enumeration procs=((AgentController)_ac).listProcesses();
634
 					boolean procexists = false;
635
 					 while(procs.hasMoreElements()) 
636
 					 {
637
 					    IProcess procvar =(IProcess)procs.nextElement();
638
 					    try
639
 						{
640
 					        if(procvar.getProcessId() == _processId)
641
 					    	{
642
 					        	procexists = true;
643
 					        	break;
644
 					        }
645
 						}
646
 					    catch(Exception proce){proce.printStackTrace();}
647
 					 }
648
 					 if(!procexists)((AgentController)_ac)._processList.add(this);
649
 				}
650
 			} //if processid
651
 			if(ParseObj.getHashTable().containsKey("TPTP_Error"))
652
 			{
653
 				String srciid = (String)ParseObj.getHashTable().get("iid");
654
 				String errCode= (String)ParseObj.getHashTable().get("ErrCode");
655
 				String errInfo= (String)ParseObj.getHashTable().get("ErrInfo");
656
 				if (Constants.TPTP_DEBUG) System.out.println("Failed to launch Process : reported by "+srciid+" Err Code: "+errCode+" Message: "+errInfo);
657
 				throw new NoSuchApplicationException(Constants.TPTP_PLATFORM_EXEC_MSG34 + errCode + " and " + errInfo); 
658
 			}
659
		 }
587
		 }
660
		 catch(NoSuchApplicationException e){throw e;}
661
	 	 //catch(Exception e){e.printStackTrace();}
662
	}
588
	}
663
	//To Do 
589
	//To Do 
664
	public void handleCommand(ICommandElement command) 
590
	public void handleCommand(ICommandElement command) 
Lines 863-896 Link Here
863
		}
789
		}
864
	}
790
	}
865
	}
791
	}
866
}
792
867
class ProcessCommandHandler implements ICommandHandler
793
	public void setExitCode (int exitCode) {	/* Bug 167208 */
868
{
794
		this.exitCode = exitCode;
869
	protected	ICommandElement _commandelement = null;
870
	protected	INode			_node			= null;
871
	
872
	public ProcessCommandHandler(){}
873
	public void setCommandElement(ICommandElement commandelement)
874
	{
875
		this._commandelement = commandelement;
876
	}
877
	public ICommandElement getCommandElement()
878
	{
879
		return this._commandelement;
880
	}
881
	public void setNode(INode node)
882
	{
883
		this._node = node;
884
	}
795
	}
885
	public INode getNode()
796
	
886
	{
797
	public int getExitCode () {	/* Bug 167208 */
887
		return this._node;
798
		return exitCode;
888
	}
799
	}
889
	public void incomingCommand(INode node, ICommandElement commandelement)
800
890
	{
801
	class Request {	/* Bug 166831, 166834 */
891
		this._node = node;
802
		public final static int WAIT = 0;
892
		this._commandelement = commandelement;
803
		public final static int PROCESSED = 1;
804
		public final static int ERROR = 2;
805
		 
806
		public String command;
807
		public int status;
808
		public String errCode;
809
		public String errInfo;
810
		
811
		public Request (String command) {
812
			this.command = command;
813
			status = WAIT;
814
		}
893
	}
815
	}
894
}
816
	
817
	class ProcessCommandHandler implements ICommandHandler {	/* Bug 166831, 166834 */
818
		private ProcessImpl process;
819
		private Map waitingRequests;
820
821
		public ProcessCommandHandler(ProcessImpl process) {
822
			this.process = process;
823
			waitingRequests = new HashMap ();
824
		}
895
825
826
		public synchronized void incomingCommand(INode node, ICommandElement commandElement) {
827
			TPTPXMLParse parseObj = new TPTPXMLParse();
828
			String commandStr = ((CommandFragment) commandElement).getCommandData();
829
			parseObj.setParser(commandStr);
830
			Hashtable commandHash = parseObj.getHashTable();
831
				
832
			boolean error = false;
833
			String errCode = null;
834
			String errInfo = null;
835
			String command = null;
836
			
837
			if(commandHash.containsKey("TPTP_Error")) {
838
				error = true;
839
 				errCode= (String) commandHash.get("ErrCode");
840
 				errInfo= (String) commandHash.get("ErrInfo");
841
			} else if (commandHash.containsKey (Constants.processStarted_Cmd)) {
842
				command = Constants.processStarted_Cmd;
843
				processStarted(commandHash);
844
			} else if(commandHash.containsKey (Constants.processExited_Cmd)) {
845
				command = Constants.processExited_Cmd;
846
				processExited(commandHash);
847
			} else if(commandHash.containsKey (Constants.processStopped_Cmd)) {
848
				command = Constants.processStopped_Cmd;
849
				processExited(commandHash);
850
			}
851
		
852
			if (error) {
853
				if (waitingRequests.size() > 0) {
854
					Iterator itr = waitingRequests.values().iterator();
855
					while (itr.hasNext()) {
856
						Collection requests = (Collection) itr.next();
857
						if (requests.size() > 0) {
858
							Iterator it = requests.iterator();
859
							while (it.hasNext()) {
860
								Request req = (Request) it.next();
861
								req.status = Request.ERROR;
862
								req.errCode = errCode;
863
								req.errInfo = errInfo;
864
							}
865
						}
866
					}
867
				
868
					notifyAll();
869
				}
870
			} else if (command != null) {
871
				Collection requests = (Collection) waitingRequests.get(command);
872
				if (requests != null && requests.size() > 0) {
873
					Iterator it = requests.iterator();
874
					while (it.hasNext()) {
875
						Request req = (Request) it.next();
876
						req.status = Request.PROCESSED;
877
					}
878
				
879
					requests.clear();
880
					notifyAll();
881
				}
882
			}			
883
		}
884
885
		private void processStarted(Hashtable commandHash) {	
886
			_isActive	= true;
896
887
888
			String s = (String) commandHash.get("processID");
889
			if(s != null) {
890
				long processId = Long.parseLong(s);
891
				process.setProcessId(processId);
892
			}
893
894
			Enumeration listenerList=_listeners.elements();
895
			while(listenerList.hasMoreElements()) {
896
				((IProcessListener)listenerList.nextElement()).processLaunched(process);
897
			}
898
		}
899
	
900
		private void processExited(Hashtable commandHash) {
901
			_isActive	= false;
902
903
			String s = (String) commandHash.get("exitCode");
904
			if(s != null) {
905
				int exitCode = Integer.parseInt(s);
906
				process.setExitCode(exitCode);
907
			}
908
909
			Enumeration listenerList = _listeners.elements();
910
			while(listenerList.hasMoreElements()) {
911
				((IProcessListener)listenerList.nextElement()).processExited(process);
912
			}
913
		}		
914
	
915
		public synchronized boolean waitResponce (String waitCommand) throws NoSuchApplicationException {
916
			String cmd;
917
			if (Constants.processStarted_Cmd.equals(waitCommand)) {
918
				if (process.isActive()) return true;
919
				cmd = Constants.processStarted_Cmd;
920
			} else if (Constants.processExited_Cmd.equals(waitCommand)) {
921
				cmd =Constants.processExited_Cmd;
922
			} else if (Constants.processStopped_Cmd.equals(waitCommand)) {
923
				cmd = Constants.processStopped_Cmd;
924
			} else
925
				cmd = null;
926
		
927
			if (cmd == null) return false;
928
929
			Request req = new Request (cmd);
930
			addRequest (req);
931
					
932
			boolean result = false;
933
			long dtmax = System.currentTimeMillis() + Constants.TIMEOUT_PERIOD*Constants.PROCESS_LAUNCH_TIMEOUT_TRY_COUNT;
934
		
935
			while (true) {
936
				long interval = dtmax - System.currentTimeMillis();
937
				if (interval <= 0) break;
938
			
939
				try {
940
					wait (interval);
941
				} catch (Exception e) {
942
				}
943
944
				if (req.status == Request.ERROR) {
945
					throw new NoSuchApplicationException(Constants.TPTP_PLATFORM_EXEC_MSG34 + req.errCode + " and " + req.errInfo);
946
				}
947
				
948
				if (req.status == Request.PROCESSED) {
949
					result = true;
950
					break;
951
				}				 
952
			}
953
954
			return result;
955
		}		
956
	
957
		private void addRequest (Request req) {
958
			Set requests = (Set) waitingRequests.get(req.command);
959
			if (requests == null) {
960
				requests = new HashSet ();
961
				waitingRequests.put(req.command, requests);
962
			}
963
			requests.add (req);
964
		} 
965
	}
966
}

Return to bug 165948