mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-21 07:55:24 +02:00
cancel the ssh connection correctly (terminate the SshConnection thread)
This commit is contained in:
parent
b9dfb44a5c
commit
f19c85aded
2 changed files with 49 additions and 14 deletions
|
@ -30,6 +30,7 @@ import org.eclipse.tm.terminal.Logger;
|
||||||
import org.eclipse.tm.terminal.TerminalState;
|
import org.eclipse.tm.terminal.TerminalState;
|
||||||
import org.eclipse.ui.preferences.ScopedPreferenceStore;
|
import org.eclipse.ui.preferences.ScopedPreferenceStore;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.Channel;
|
||||||
import com.jcraft.jsch.ChannelShell;
|
import com.jcraft.jsch.ChannelShell;
|
||||||
import com.jcraft.jsch.JSch;
|
import com.jcraft.jsch.JSch;
|
||||||
import com.jcraft.jsch.JSchException;
|
import com.jcraft.jsch.JSchException;
|
||||||
|
@ -40,9 +41,12 @@ import com.jcraft.jsch.Session;
|
||||||
import com.jcraft.jsch.UserInfo;
|
import com.jcraft.jsch.UserInfo;
|
||||||
|
|
||||||
class SshConnection extends Thread {
|
class SshConnection extends Thread {
|
||||||
|
private static int fgNo;
|
||||||
private final ITerminalControl fControl;
|
private final ITerminalControl fControl;
|
||||||
private final SshConnector fConn;
|
private final SshConnector fConn;
|
||||||
|
private Channel fChannel;
|
||||||
protected SshConnection(SshConnector conn,ITerminalControl control) {
|
protected SshConnection(SshConnector conn,ITerminalControl control) {
|
||||||
|
super("SshConnection-"+fgNo++);
|
||||||
fControl = control;
|
fControl = control;
|
||||||
fConn = conn;
|
fConn = conn;
|
||||||
fControl.setState(TerminalState.CONNECTING);
|
fControl.setState(TerminalState.CONNECTING);
|
||||||
|
@ -238,6 +242,8 @@ class SshConnection extends Thread {
|
||||||
fControl.setState(TerminalState.CONNECTED);
|
fControl.setState(TerminalState.CONNECTED);
|
||||||
// read data until the connection gets terminated
|
// read data until the connection gets terminated
|
||||||
readDataForever(fConn.getInputStream());
|
readDataForever(fConn.getInputStream());
|
||||||
|
// when reading is done, we set the state to closed
|
||||||
|
fControl.setState(TerminalState.CLOSED);
|
||||||
} catch (JSchException e) {
|
} catch (JSchException e) {
|
||||||
connectFailed(e.getMessage(),e.getMessage());
|
connectFailed(e.getMessage(),e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -246,7 +252,21 @@ class SshConnection extends Thread {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
synchronized void setChannel(Channel channel) {
|
||||||
|
fChannel = channel;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* disconnect the ssh session
|
||||||
|
*/
|
||||||
|
void disconnect() {
|
||||||
|
interrupt();
|
||||||
|
synchronized (this) {
|
||||||
|
if(fChannel!=null) {
|
||||||
|
fChannel.disconnect();
|
||||||
|
fChannel=null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Read the data from the ssh connection and display it in the terminal.
|
* Read the data from the ssh connection and display it in the terminal.
|
||||||
* @param in
|
* @param in
|
||||||
|
@ -255,11 +275,26 @@ class SshConnection extends Thread {
|
||||||
private void readDataForever(InputStream in) throws IOException {
|
private void readDataForever(InputStream in) throws IOException {
|
||||||
// read the data
|
// read the data
|
||||||
byte bytes[]=new byte[32*1024];
|
byte bytes[]=new byte[32*1024];
|
||||||
|
// read until the thread gets interrupted....
|
||||||
|
while(!isInterrupted()) {
|
||||||
int n;
|
int n;
|
||||||
while((n=in.read(bytes))!=-1) {
|
// We have to poll. There seems no better way to cancel
|
||||||
fControl.writeToTerminal(new String(bytes,0,n));
|
// the read from ssh....
|
||||||
|
while(in.available()==0) {
|
||||||
|
try {
|
||||||
|
sleep(1);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// propagate the interrupt
|
||||||
|
interrupt();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// read some bytes
|
||||||
|
if((n=in.read(bytes))==-1)
|
||||||
|
return;
|
||||||
|
// we assume we get ASCII UTF8 bytes
|
||||||
|
fControl.writeToTerminal(new String(bytes,0,n,"UTF8"));
|
||||||
}
|
}
|
||||||
fControl.setState(TerminalState.CLOSED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Display getStandardDisplay() {
|
protected static Display getStandardDisplay() {
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class SshConnector implements ITerminalConnector {
|
||||||
private ITerminalControl fControl;
|
private ITerminalControl fControl;
|
||||||
private JSch fJsch;
|
private JSch fJsch;
|
||||||
private ChannelShell fChannel;
|
private ChannelShell fChannel;
|
||||||
|
private SshConnection fConnection;
|
||||||
private final SshSettings fSettings;
|
private final SshSettings fSettings;
|
||||||
public SshConnector() {
|
public SshConnector() {
|
||||||
this(new SshSettings());
|
this(new SshSettings());
|
||||||
|
@ -53,12 +53,12 @@ public class SshConnector implements ITerminalConnector {
|
||||||
public void connect(ITerminalControl control) {
|
public void connect(ITerminalControl control) {
|
||||||
Logger.log("entered."); //$NON-NLS-1$
|
Logger.log("entered."); //$NON-NLS-1$
|
||||||
fControl=control;
|
fControl=control;
|
||||||
SshConnection worker = new SshConnection(this,control);
|
fConnection = new SshConnection(this,control);
|
||||||
worker.start();
|
fConnection.start();
|
||||||
}
|
}
|
||||||
public void disconnect() {
|
synchronized public void disconnect() {
|
||||||
Logger.log("entered."); //$NON-NLS-1$
|
Logger.log("entered."); //$NON-NLS-1$
|
||||||
|
fConnection.disconnect();
|
||||||
if (getInputStream() != null) {
|
if (getInputStream() != null) {
|
||||||
try {
|
try {
|
||||||
getInputStream().close();
|
getInputStream().close();
|
||||||
|
|
Loading…
Add table
Reference in a new issue