diff options
author | Eugene Tarassov | 2013-10-02 20:47:06 +0000 |
---|---|---|
committer | Eugene Tarassov | 2013-10-02 20:47:06 +0000 |
commit | acbc8508618a2d1429f9dab10c487594e002423e (patch) | |
tree | 56620939ce20572f6dd146b0fcee465cf32d67ce /plugins/org.eclipse.tcf.rse | |
parent | c99b0a7a6fc02f19885be44bec92fd7625e27944 (diff) | |
download | org.eclipse.tcf-acbc8508618a2d1429f9dab10c487594e002423e.tar.gz org.eclipse.tcf-acbc8508618a2d1429f9dab10c487594e002423e.tar.xz org.eclipse.tcf-acbc8508618a2d1429f9dab10c487594e002423e.zip |
TCF RSE: Terminal implementation changed to use stream classes from TCF Core
Diffstat (limited to 'plugins/org.eclipse.tcf.rse')
3 files changed, 55 insertions, 247 deletions
diff --git a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalInputStream.java b/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalInputStream.java deleted file mode 100644 index a08811e68..000000000 --- a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2011 Intel Corporation. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Liping Ke (Intel Corp.) - initial API and implementation - ******************************************************************************/ -package org.eclipse.tcf.internal.rse.shells; - -import java.io.IOException; -import java.io.InputStream; - -import org.eclipse.tcf.protocol.IToken; -import org.eclipse.tcf.services.IStreams; -import org.eclipse.tcf.util.TCFTask; - -public class TCFTerminalInputStream extends InputStream { - private final TCFTerminalShell terminal; - private final IStreams streams; - private final String is_id; - private boolean connected = true; /* The stream is connected or not */ - private boolean bEof = false;; - - public TCFTerminalInputStream(TCFTerminalShell terminal, IStreams streams, String is_id) throws IOException{ - if (streams == null) throw new IOException("TCP streams is null");//$NON-NLS-1$ - this.terminal = terminal; - this.streams = streams; - this.is_id = is_id; - } - - /* read must be synchronized */ - @Override - public synchronized int read() throws IOException { - if (!connected) throw new IOException("istream is not connected");//$NON-NLS-1$ - if (bEof) return -1; - try { - return new TCFTask<Integer>() { - public void run() { - streams.read(is_id, 1, new IStreams.DoneRead() { - public void doneRead(IToken token, Exception error, int lostSize, - byte[] data, boolean eos) { - if (error != null) { - error(error); - return; - } - bEof = eos; - if (data != null) { - done(data[0] & 0xff); - } - else { - done(-1); - } - } - }); - } - }.getIO(); - } - catch (IOException e) { - if (!connected) return -1; - throw e; - } - } - - public synchronized int read(byte b[], final int off, final int len) throws IOException { - if (!connected) throw new IOException("istream is not connected");//$NON-NLS-1$ - if (bEof) return -1; - if (b == null) throw new NullPointerException(); - if (off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException(); - if (len == 0) return 0; - try { - byte[] data = new TCFTask<byte[]>() { - public void run() { - streams.read(is_id, len, new IStreams.DoneRead() { - public void doneRead(IToken token, Exception error, int lostSize, - byte[] data, boolean eos) { - if (error != null) { - error(error); - return; - } - bEof = eos; - done(data); - } - }); - } - }.getIO(); - - if (data != null) { - int length = data.length; - System.arraycopy(data, 0, b, off, length); - return length; - } - if (bEof) return -1; - return 0; - } - catch (IOException e) { - if (!connected) return -1; - throw e; - } - } - - public void close() throws IOException { - if (!connected) return; - connected = false; - new TCFTask<Object>() { - public void run() { - streams.disconnect(is_id, new IStreams.DoneDisconnect() { - public void doneDisconnect(IToken token, Exception error) { - if (error != null) { - error(error); - return; - } - terminal.onInputStreamClosed(); - done(this); - } - }); - } - }.getIO(); - } -} diff --git a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalOutputStream.java b/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalOutputStream.java deleted file mode 100644 index 1611b555c..000000000 --- a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalOutputStream.java +++ /dev/null @@ -1,84 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2011 Intel Corporation. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Liping Ke (Intel Corp.) - initial API and implementation - ******************************************************************************/ -package org.eclipse.tcf.internal.rse.shells; - -import java.io.IOException; - -import java.io.OutputStream; - -import org.eclipse.tcf.protocol.IToken; -import org.eclipse.tcf.services.IStreams; -import org.eclipse.tcf.util.TCFTask; - -public class TCFTerminalOutputStream extends OutputStream { - private final TCFTerminalShell terminal; - private final IStreams streams; - private final String os_id; - private boolean connected = true; - - public TCFTerminalOutputStream(TCFTerminalShell terminal, IStreams streams, String os_id) throws IOException{ - if (streams == null) throw new IOException("istream is null");//$NON-NLS-1$ - this.terminal = terminal; - this.streams = streams; - this.os_id = os_id; - } - - @Override - public synchronized void write(final byte b[], final int off, final int len) throws IOException { - /* If eof is written, we can't write anything into the stream */ - if (!connected) throw new IOException("stream is not connected!");//$NON-NLS-1$ - new TCFTask<Object>() { - public void run() { - streams.write(os_id, b, off, len, new IStreams.DoneWrite() { - public void doneWrite(IToken token, Exception error) { - if (error != null) error(error); - else done(this); - } - }); - } - }.getIO(); - } - - @Override - public synchronized void write(int b) throws IOException { - byte[] buf = new byte[1]; - buf[0] = (byte)b; - write(buf, 0, 1); - } - - /* close must be called --Need to reconsider it in the future*/ - public void close() throws IOException { - if (!connected) return; - connected = false; - new TCFTask<Object>() { - public void run() { - streams.eos(os_id, new IStreams.DoneEOS() { - public void doneEOS(IToken token, Exception error) { - if (error != null) { - error(error); - return; - } - streams.disconnect(os_id, new IStreams.DoneDisconnect() { - public void doneDisconnect(IToken token, Exception error) { - if (error != null) { - error(error); - return; - } - terminal.onOutputStreamClosed(); - done(this); - } - }); - } - }); - } - }.getIO(); - } -} diff --git a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalShell.java b/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalShell.java index 5b70e143e..29020ee92 100644 --- a/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalShell.java +++ b/plugins/org.eclipse.tcf.rse/src/org/eclipse/tcf/internal/rse/shells/TCFTerminalShell.java @@ -39,13 +39,14 @@ import org.eclipse.tcf.internal.rse.TCFConnectorService; import org.eclipse.tcf.internal.rse.TCFRSETask; import org.eclipse.tcf.protocol.IChannel; import org.eclipse.tcf.protocol.IToken; -import org.eclipse.tcf.services.IStreams; import org.eclipse.tcf.services.ITerminals; +import org.eclipse.tcf.util.TCFVirtualInputStream; +import org.eclipse.tcf.util.TCFVirtualOutputStream; public class TCFTerminalShell extends AbstractTerminalShell { - private ITCFSessionProvider fSessionProvider; - private IChannel fChannel; + private final ITCFSessionProvider fSessionProvider; + private final IChannel fChannel; private String fPtyType; private ITerminals.TerminalContext terminalContext; private String fEncoding; @@ -55,12 +56,11 @@ public class TCFTerminalShell extends AbstractTerminalShell { private int fWidth = 0; private int fHeight = 0; private String fContextID; - private String in_id; + private String inp_id; private String out_id; private boolean connected; private boolean exited; private ITerminals terminals; - private IStreams streams; private int status; private IPropertySet tcfPropertySet = null; @@ -204,12 +204,12 @@ public class TCFTerminalShell extends AbstractTerminalShell { final String encoding, final String[] environment, String initialWorkingDirectory, String commandToRun) throws SystemMessageException { + fSessionProvider = sessionProvider; + fEncoding = encoding; + fPtyType = ptyType; + fChannel = fSessionProvider.getChannel(); Exception nestedException = null; try { - fSessionProvider = sessionProvider; - fEncoding = encoding; - fPtyType = ptyType; - fChannel = fSessionProvider.getChannel(); if (fChannel == null || fChannel.getState() != IChannel.STATE_OPEN) throw new Exception("TCP channel is not connected!");//$NON-NLS-1$ @@ -217,7 +217,6 @@ public class TCFTerminalShell extends AbstractTerminalShell { new TCFRSETask<ITerminals.TerminalContext>() { public void run() { terminals = ((TCFConnectorService)sessionProvider).getService(ITerminals.class); - streams = ((TCFConnectorService)sessionProvider).getService(IStreams.class); fSessionProvider.onStreamsConnecting(); terminals.launch(ptyType, encoding, environment, new ITerminals.DoneLaunch() { @Override @@ -225,8 +224,27 @@ public class TCFTerminalShell extends AbstractTerminalShell { if (ctx != null) { terminalContext = ctx; terminals.addListener(listeners); - fSessionProvider.onStreamsID(ctx.getStdInID()); - fSessionProvider.onStreamsID(ctx.getStdOutID()); + inp_id = ctx.getStdOutID(); + out_id = ctx.getStdInID(); + fSessionProvider.onStreamsID(inp_id); + fSessionProvider.onStreamsID(out_id); + try { + fInputStream = new TCFVirtualInputStream(fChannel, inp_id, new Runnable() { + @Override + public void run() { + onInputStreamClosed(); + } + }); + fOutputStream = new TCFVirtualOutputStream(fChannel, out_id, new Runnable() { + @Override + public void run() { + onOutputStreamClosed(); + } + }); + } + catch (Exception x) { + error = x; + } } fSessionProvider.onStreamsConnected(); if (error != null) error(error); @@ -242,15 +260,11 @@ public class TCFTerminalShell extends AbstractTerminalShell { fContextID = terminalContext.getID(); fWidth = terminalContext.getWidth(); fHeight = terminalContext.getHeight(); - in_id = terminalContext.getStdOutID(); - out_id = terminalContext.getStdInID(); String user = fSessionProvider.getSessionUserId(); String password = fSessionProvider.getSessionPassword(); status = ITCFSessionProvider.ERROR_CODE; - fOutputStream = new TCFTerminalOutputStream(this, streams, out_id); - fInputStream = new TCFTerminalInputStream(this, streams, in_id); if (fEncoding != null) { fOutputStreamWriter = new BufferedWriter(new OutputStreamWriter(fOutputStream, encoding)); } @@ -264,41 +278,39 @@ public class TCFTerminalShell extends AbstractTerminalShell { status = login(user, password); } finally { - if ((status == ITCFSessionProvider.CONNECT_CLOSED)) { - //Give one time chance of retrying.... + if (status == ITCFSessionProvider.CONNECT_CLOSED) { + // Give one time chance of retrying.... } } - //give another chance of retrying - if ((status == ITCFSessionProvider.CONNECT_CLOSED)) - { + // give another chance of retrying + if (status == ITCFSessionProvider.CONNECT_CLOSED) { status = login(user, password); } - connected = true; - if (initialWorkingDirectory!=null && initialWorkingDirectory.length()>0 - && !initialWorkingDirectory.equals(".") //$NON-NLS-1$ - && !initialWorkingDirectory.equals("Command Shell") //$NON-NLS-1$ //FIXME workaround for bug 153047 - ) { - writeToShell("cd " + PathUtility.enQuoteUnix(initialWorkingDirectory)); //$NON-NLS-1$ - } + if (status == ITCFSessionProvider.SUCCESS_CODE) { + connected = true; + if (initialWorkingDirectory != null && initialWorkingDirectory.length() > 0 + && !initialWorkingDirectory.equals(".") //$NON-NLS-1$ + && !initialWorkingDirectory.equals("Command Shell")) //$NON-NLS-1$ //FIXME workaround for bug 153047 + { + writeToShell("cd " + PathUtility.enQuoteUnix(initialWorkingDirectory)); //$NON-NLS-1$ + } - if (commandToRun != null && commandToRun.length() > 0) { - writeToShell(commandToRun); + if (commandToRun != null && commandToRun.length() > 0) { + writeToShell(commandToRun); + } } - } catch (Exception e) { e.printStackTrace(); nestedException = e; } finally { - if (status == ITCFSessionProvider.SUCCESS_CODE) { - } - else { + if (status != ITCFSessionProvider.SUCCESS_CODE) { SystemMessage msg; - if (nestedException!=null) { + if (nestedException != null) { msg = new SimpleSystemMessage(org.eclipse.tcf.internal.rse.Activator.PLUGIN_ID, ICommonMessageIds.MSG_EXCEPTION_OCCURRED, IStatus.ERROR, @@ -317,7 +329,7 @@ public class TCFTerminalShell extends AbstractTerminalShell { ICommonMessageIds.MSG_COMM_AUTH_FAILED, IStatus.ERROR, strErr, - "Meet error when trying to login in!");//$NON-NLS-1$ + "Meet error when trying to login in!");//$NON-NLS-1$ msg.makeSubstitution(((TCFConnectorService)fSessionProvider).getHost().getAliasName()); } throw new SystemMessageException(msg);//$NON-NLS-1$ @@ -346,7 +358,6 @@ public class TCFTerminalShell extends AbstractTerminalShell { try { getOutputStream().close(); getInputStream().close(); - } catch (IOException ioe) { ioe.printStackTrace(); @@ -370,10 +381,12 @@ public class TCFTerminalShell extends AbstractTerminalShell { } public InputStream getInputStream() { + if (!connected) throw new Error("Not connected"); return fInputStream; } public OutputStream getOutputStream() { + if (!connected) throw new Error("Not connected"); return fOutputStream; } @@ -391,6 +404,7 @@ public class TCFTerminalShell extends AbstractTerminalShell { } public void setTerminalSize(int newWidth, int newHeight) { + if (fWidth == newWidth && fHeight == newHeight) return; if (fChannel == null || (fChannel.getState() == IChannel.STATE_CLOSED) || !connected) { // do nothing return; @@ -424,8 +438,8 @@ public class TCFTerminalShell extends AbstractTerminalShell { return defaultEncoding; } - void onInputStreamClosed() { - in_id = null; + private void onInputStreamClosed() { + inp_id = null; if (out_id == null && !exited) { terminalContext.exit(new ITerminals.DoneCommand(){ public void doneCommand(IToken token, Exception error) { @@ -434,9 +448,9 @@ public class TCFTerminalShell extends AbstractTerminalShell { } } - void onOutputStreamClosed() { + private void onOutputStreamClosed() { out_id = null; - if (in_id == null && !exited) { + if (inp_id == null && !exited) { terminalContext.exit(new ITerminals.DoneCommand(){ public void doneCommand(IToken token, Exception error) { } |