1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

[221184] Redesign Serial Terminal Ownership Handling

This commit is contained in:
Martin Oberhuber 2008-05-07 10:58:39 +00:00
parent 49300482ea
commit c6048ad0dd
4 changed files with 91 additions and 34 deletions

View file

@ -16,6 +16,7 @@
* Martin Oberhuber (Wind River) - [207158] improve error message when port not available
* Martin Oberhuber (Wind River) - [208029] COM port not released after quick disconnect/reconnect
* Martin Oberhuber (Wind River) - [206884] Update Terminal Ownership ID to "org.eclipse.tm.terminal.serial"
* Martin Oberhuber (Wind River) - [221184] Redesign Serial Terminal Ownership Handling
*******************************************************************************/
package org.eclipse.tm.internal.terminal.serial;
@ -27,6 +28,10 @@ import gnu.io.SerialPort;
import java.util.Arrays;
import java.util.Enumeration;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl;
import org.eclipse.tm.internal.terminal.provisional.api.Logger;
import org.eclipse.tm.internal.terminal.provisional.api.TerminalState;
@ -129,29 +134,61 @@ public class SerialConnectWorker extends Thread {
}
fConn.setSerialPortHandler(new SerialPortHandler(fConn,fControl));
fConn.setSerialPortIdentifier(CommPortIdentifier.getPortIdentifier(portName));
int timeoutInMs = s.getTimeout() * 1000;
serialPort=(SerialPort) fConn.getSerialPortIdentifier().open(strID,timeoutInMs);
serialPort.setSerialPortParams(s.getBaudRate(), s.getDataBits(), s.getStopBits(), s.getParity());
serialPort.setFlowControlMode(s.getFlowControl());
serialPort.addEventListener(fConn.getSerialPortHandler());
serialPort.notifyOnDataAvailable(true);
fConn.getSerialPortIdentifier().addPortOwnershipListener(fConn.getSerialPortHandler());
fConn.setSerialPort(serialPort);
fControl.setState(TerminalState.CONNECTED);
//Bug 221184: Warn about serial port already in use
String currentOwner = fConn.getSerialPortIdentifier().getCurrentOwner();
if (strID.equals(currentOwner)) {
currentOwner = SerialMessages.ANOTHER_TERMINAL;
}
final int[] answer = { SWT.YES };
final String fPortName = portName;
final String fCurrentOwner = currentOwner;
if (currentOwner != null) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
mb.setText(SerialMessages.PROP_TITLE);
mb.setMessage(NLS.bind(SerialMessages.PORT_IN_USE, fPortName, fCurrentOwner));
answer[0] = mb.open();
}
});
}
if (answer[0] != SWT.YES) {
// don't try to steal the port
fControl.setState(TerminalState.CLOSED);
fConn.setSerialPortHandler(null);
return;
} else {
// Try to steal the port -- may throw PortInUseException
int timeoutInMs = s.getTimeout() * 1000;
serialPort = (SerialPort) fConn.getSerialPortIdentifier().open(strID, timeoutInMs);
serialPort.setSerialPortParams(s.getBaudRate(), s.getDataBits(), s.getStopBits(), s.getParity());
serialPort.setFlowControlMode(s.getFlowControl());
serialPort.addEventListener(fConn.getSerialPortHandler());
serialPort.notifyOnDataAvailable(true);
fConn.getSerialPortIdentifier().addPortOwnershipListener(fConn.getSerialPortHandler());
fConn.setSerialPort(serialPort);
if (fCurrentOwner != null) {
fControl.displayTextInTerminal(NLS.bind(SerialMessages.PORT_STOLEN, fPortName, fCurrentOwner));
}
fControl.setState(TerminalState.CONNECTED);
}
} catch (PortInUseException portInUseException) {
fControl.setState(TerminalState.CLOSED);
String theOwner = portInUseException.currentOwner;
if (strID.equals(theOwner)) {
theOwner = "another Terminal View";
theOwner = SerialMessages.ANOTHER_TERMINAL;
}
fControl.displayTextInTerminal("Connection Error!\r\n" +portName+" is already in use by "+ theOwner);
fControl.displayTextInTerminal(NLS.bind(SerialMessages.PORT_NOT_STOLEN, portName, theOwner));
} catch (NoSuchPortException e) {
fControl.setState(TerminalState.CLOSED);
String msg=e.getMessage();
if(msg==null)
msg=portName;
fControl.displayTextInTerminal("No such port: \"" + msg+"\"\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
fControl.displayTextInTerminal(NLS.bind(SerialMessages.NO_SUCH_PORT, msg));
} catch (Exception exception) {
Logger.logException(exception);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2007 Wind River Systems, Inc. and others.
* Copyright (c) 2003, 2008 Wind River Systems, Inc. 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
@ -11,8 +11,9 @@
* Helmut Haigermoser and Ted Williams.
*
* Contributors:
* Michael Scharf (Wind River) - split into core, view and connector plugins
* Michael Scharf (Wind River) - split into core, view and connector plugins
* Martin Oberhuber (Wind River) - fixed copyright headers and beautified
* Martin Oberhuber (Wind River) - [221184] Redesign Serial Terminal Ownership Handling
*******************************************************************************/
package org.eclipse.tm.internal.terminal.serial;
@ -28,9 +29,16 @@ public class SerialMessages extends NLS {
public static String STOPBITS;
public static String PARITY;
public static String FLOWCONTROL;
public static String PORT_IN_USE;
public static String TIMEOUT;
public static String ERROR_LIBRARY_NOT_INSTALLED;
public static String PORT_IN_USE;
public static String ANOTHER_TERMINAL;
public static String PORT_STOLEN;
public static String PORT_NOT_STOLEN;
public static String NO_SUCH_PORT;
public static String OWNERSHIP_GRANTED;
}

View file

@ -14,8 +14,8 @@
# Michael Scharf (Wind River) - split into core, view and connector plugins
# Martin Oberhuber (Wind River) - fixed copyright headers and beautified
# Martin Oberhuber (Wind River) - [168197] Replace JFace MessagDialog by SWT MessageBox
# Martin Oberhuber (Wind River) - [221184] Redesign Serial Terminal Ownership Handling
###############################################################################
PORT_IN_USE = Serial port \''{0}\'' is currently in use\!\nDo you want to close the port?
PROP_TITLE = Terminal
PORT = Port
BAUDRATE = Baud Rate
@ -37,3 +37,12 @@ Installation:\n\
* More downloads for other platforms (currently about 30)\n\
are available from the "ToyBox" link on\n\
http://users.frii.com/jarvi/rxtx/download.html
# Port Ownership Handling
PORT_IN_USE = Serial port \''{0}\'' is currently in use by {1}\!\nDo you want to try and steal the port?
ANOTHER_TERMINAL = another Terminal View
PORT_STOLEN = Port \''{0}\'' successfully obtained from {1}\r\n
PORT_NOT_STOLEN = Connection Error!\r\n \''{0}\'' is already in use by {1}.\r\n
NO_SUCH_PORT = No such port: \''{0}\''\r\n
OWNERSHIP_GRANTED = Connection canceled due to ownership request from {0}.\r\n

View file

@ -14,6 +14,7 @@
* Michael Scharf (Wind River) - extracted from TerminalControl
* Martin Oberhuber (Wind River) - fixed copyright headers and beautified
* Martin Oberhuber (Wind River) - [168197] Replace JFace MessagDialog by SWT MessageBox
* Martin Oberhuber (Wind River) - [221184] Redesign Serial Terminal Ownership Handling
*******************************************************************************/
package org.eclipse.tm.internal.terminal.serial;
@ -22,11 +23,9 @@ import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.text.MessageFormat;
import org.eclipse.swt.SWT;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl;
import org.eclipse.tm.internal.terminal.provisional.api.Logger;
@ -69,21 +68,25 @@ public class SerialPortHandler implements
}
public void onSerialOwnershipRequested(Object data) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
String[] args = new String[] { fConn.getSerialSettings().getSerialPort() };
String strMsg = MessageFormat.format(SerialMessages.PORT_IN_USE, args);
// [168197] Replace JFace MessagDialog by SWT MessageBox
//if (!MessageDialog.openQuestion(fControl.getShell(), SerialMessages.PROP_TITLE, strMsg))
MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
mb.setText(SerialMessages.PROP_TITLE);
mb.setMessage(strMsg);
if (mb.open() != SWT.YES)
return;
fConn.disconnect();
}
});
//Bug 221184: We immediately release the port on any ownership request
try {
throw new Exception();
} catch (Exception e) {
StackTraceElement[] elems = e.getStackTrace();
final String requester = elems[elems.length - 4].getClassName();
Display.getDefault().asyncExec(new Runnable() {
public void run() {
fConn.disconnect();
String req = requester;
String myPackage = this.getClass().getPackage().getName();
if (req.startsWith(myPackage)) {
req = SerialMessages.ANOTHER_TERMINAL;
}
fControl.displayTextInTerminal(NLS.bind(SerialMessages.OWNERSHIP_GRANTED, req));
}
});
fConn.disconnect();
}
}
// SerialPortEventListener interface