[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/codecompletion/shell AbstractShell.java,1.2
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2005-12-19 13:07:22
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/shell In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2039/src/org/python/pydev/editor/codecompletion/shell Modified Files: AbstractShell.java Log Message: Index: AbstractShell.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/shell/AbstractShell.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** AbstractShell.java 4 Nov 2005 14:35:12 -0000 1.20 --- AbstractShell.java 19 Dec 2005 13:07:10 -0000 1.21 *************** *** 47,50 **** --- 47,51 ---- protected static final int DEFAULT_SLEEP_BETWEEN_ATTEMPTS = 1000; protected static final boolean DEBUG_SHELL = false; + private boolean inStart = false; *************** *** 192,196 **** throw new RuntimeException("unknown related id"); } ! pythonShell.startIt(); //first start it //then make it accessible --- 193,199 ---- throw new RuntimeException("unknown related id"); } ! synchronized(pythonShell){ ! pythonShell.startIt(); //first start it ! } //then make it accessible *************** *** 265,269 **** */ public synchronized void startIt() throws IOException, Exception { ! this.startIt(AbstractShell.DEFAULT_SLEEP_BETWEEN_ATTEMPTS); } --- 268,274 ---- */ public synchronized void startIt() throws IOException, Exception { ! synchronized(this){ ! this.startIt(AbstractShell.DEFAULT_SLEEP_BETWEEN_ATTEMPTS); ! } } *************** *** 278,383 **** */ protected synchronized void startIt(int milisSleep) throws IOException, Exception { ! if(finishedForGood){ ! throw new RuntimeException("Shells are already finished for good, so, it is an invalid state to try to restart it."); ! } ! try { ! ! int pWrite = SocketUtil.findUnusedLocalPort("127.0.0.1", 50000, 55000); ! int pRead = SocketUtil.findUnusedLocalPort("127.0.0.1", 55001, 60000); ! ! if(process != null){ ! endIt(); //end the current process ! } ! ! String execMsg = createServerProcess(pWrite, pRead); ! dbg("executing "+execMsg); ! ! sleepALittle(200); ! String osName = System.getProperty("os.name"); ! if(process == null){ ! String msg = "Error creating python process - got null process("+execMsg+") - os:"+osName; ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, msg, new Exception(msg))); ! } ! try { ! int exitVal = process.exitValue(); //should throw exception saying that it still is not terminated... ! String msg = "Error creating python process - exited before creating sockets - exitValue = ("+exitVal+")("+execMsg+") - os:"+osName; ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, msg, new Exception(msg))); ! } catch (IllegalThreadStateException e2) { //this is ok ! } ! ! dbg("afterCreateProcess "); ! //ok, process validated, so, let's get its output and store it for further use. ! afterCreateProcess(); ! ! boolean connected = false; ! int attempts = 0; ! ! dbg("connecting "); ! sleepALittle(milisSleep); ! socketToWrite = null; ! serverSocket = new ServerSocket(pRead); //read in this port ! int maxAttempts = PyCodeCompletionPreferencesPage.getNumberOfConnectionAttempts(); ! while(!connected && attempts < maxAttempts && !finishedForGood){ ! attempts += 1; ! try { ! if(socketToWrite == null || socketToWrite.isConnected() == false){ ! socketToWrite = new Socket("127.0.0.1",pWrite); //we should write in this port ! } ! ! if(socketToWrite != null || socketToWrite.isConnected()){ ! serverSocket.setSoTimeout(milisSleep*2); //let's give it a higher timeout, as we're already half - connected ! try { ! socketToRead = serverSocket.accept(); ! connected = true; ! } catch (SocketTimeoutException e) { ! //that's ok, timeout for waiting connection expired, let's check it again in the next loop ! } ! } ! } catch (IOException e1) { ! if(socketToWrite != null && socketToWrite.isConnected() == true){ ! PydevPlugin.log(IStatus.ERROR, "Attempt: "+attempts+" of "+maxAttempts+" failed, trying again...(socketToWrite already binded)", e1); ! } ! } ! ! //if not connected, let's sleep a little for another attempt ! if(!connected){ ! sleepALittle(milisSleep); ! } ! } ! ! if(!connected && !finishedForGood ){ ! dbg("NOT connected "); ! //what, after all this trouble we are still not connected????!?!?!?! ! //let's communicate this to the user... ! String isAlive; ! try { ! int exitVal = process.exitValue(); //should throw exception saying that it still is not terminated... ! isAlive = " - the process in NOT ALIVE anymore (output="+exitVal+") - "; ! } catch (IllegalThreadStateException e2) { //this is ok ! isAlive = " - the process in still alive (killing it now)- "; ! process.destroy(); ! } ! ! String output = getProcessOutput(); ! Exception exception = new Exception("Error connecting to python process (" + execMsg + ") "+isAlive+" the output of the process is: "+output); ! try { ! Status status = PydevPlugin.makeStatus(IStatus.ERROR, "Error connecting to python process (" + execMsg + ") "+isAlive+" the output of the process is: "+output, exception); ! throw new CoreException(status); ! } catch (Exception e) { ! throw exception; ! } ! } ! ! } catch (IOException e) { ! ! if(process!=null){ ! process.destroy(); ! process = null; ! } ! throw e; ! } } --- 283,401 ---- */ protected synchronized void startIt(int milisSleep) throws IOException, Exception { ! if(inStart){ ! //it is already in the process of starting, so, if we are in another thread, just forget about it. ! return; ! } ! inStart = true; try { ! if (finishedForGood) { ! throw new RuntimeException( ! "Shells are already finished for good, so, it is an invalid state to try to restart it."); ! } ! try { ! int pWrite = SocketUtil.findUnusedLocalPort("127.0.0.1", 50000, 55000); ! int pRead = SocketUtil.findUnusedLocalPort("127.0.0.1", 55001, 60000); ! ! if (process != null) { ! endIt(); //end the current process ! } ! ! String execMsg = createServerProcess(pWrite, pRead); ! dbg("executing " + execMsg); ! ! sleepALittle(200); ! String osName = System.getProperty("os.name"); ! if (process == null) { ! String msg = "Error creating python process - got null process(" + execMsg + ") - os:" + osName; ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, msg, new Exception(msg))); ! } ! try { ! int exitVal = process.exitValue(); //should throw exception saying that it still is not terminated... ! String msg = "Error creating python process - exited before creating sockets - exitValue = (" ! + exitVal + ")(" + execMsg + ") - os:" + osName; ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, msg, new Exception(msg))); ! } catch (IllegalThreadStateException e2) { //this is ok ! } ! ! dbg("afterCreateProcess "); ! //ok, process validated, so, let's get its output and store it for further use. ! afterCreateProcess(); ! ! boolean connected = false; ! int attempts = 0; ! ! dbg("connecting "); ! sleepALittle(milisSleep); ! socketToWrite = null; ! serverSocket = new ServerSocket(pRead); //read in this port ! int maxAttempts = PyCodeCompletionPreferencesPage.getNumberOfConnectionAttempts(); ! while (!connected && attempts < maxAttempts && !finishedForGood) { ! attempts += 1; ! try { ! if (socketToWrite == null || socketToWrite.isConnected() == false) { ! socketToWrite = new Socket("127.0.0.1", pWrite); //we should write in this port ! } ! ! if (socketToWrite != null || socketToWrite.isConnected()) { ! serverSocket.setSoTimeout(milisSleep * 2); //let's give it a higher timeout, as we're already half - connected ! try { ! socketToRead = serverSocket.accept(); ! connected = true; ! } catch (SocketTimeoutException e) { ! //that's ok, timeout for waiting connection expired, let's check it again in the next loop ! } ! } ! } catch (IOException e1) { ! if (socketToWrite != null && socketToWrite.isConnected() == true) { ! PydevPlugin.log(IStatus.ERROR, "Attempt: " + attempts + " of " + maxAttempts ! + " failed, trying again...(socketToWrite already binded)", e1); ! } ! } ! ! //if not connected, let's sleep a little for another attempt ! if (!connected) { ! sleepALittle(milisSleep); ! } ! } ! ! if (!connected && !finishedForGood) { ! dbg("NOT connected "); ! ! //what, after all this trouble we are still not connected????!?!?!?! ! //let's communicate this to the user... ! String isAlive; ! try { ! int exitVal = process.exitValue(); //should throw exception saying that it still is not terminated... ! isAlive = " - the process in NOT ALIVE anymore (output=" + exitVal + ") - "; ! } catch (IllegalThreadStateException e2) { //this is ok ! isAlive = " - the process in still alive (killing it now)- "; ! process.destroy(); ! } ! ! String output = getProcessOutput(); ! Exception exception = new Exception("Error connecting to python process (" + execMsg + ") " ! + isAlive + " the output of the process is: " + output); ! try { ! Status status = PydevPlugin.makeStatus(IStatus.ERROR, "Error connecting to python process (" ! + execMsg + ") " + isAlive + " the output of the process is: " + output, exception); ! throw new CoreException(status); ! } catch (Exception e) { ! throw exception; ! } ! } ! ! } catch (IOException e) { ! ! if (process != null) { ! process.destroy(); ! process = null; ! } ! throw e; ! } ! } finally { ! this.inStart = false; ! } } *************** *** 721,725 **** } try { ! this.startIt(); } catch (Exception e) { PydevPlugin.log(IStatus.ERROR, "ERROR restarting shell.", e); --- 739,745 ---- } try { ! synchronized(this){ ! this.startIt(); ! } } catch (Exception e) { PydevPlugin.log(IStatus.ERROR, "ERROR restarting shell.", e); |