1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Reuse Anrduino remote connections when fetching.

Change-Id: I776cf031f34b51ed504709d367b6b0352c868815
This commit is contained in:
Doug Schaefer 2015-05-23 22:21:58 -04:00
parent 49bf118c8f
commit 409b39b10a

View file

@ -11,6 +11,8 @@
package org.eclipse.cdt.arduino.core.internal.remote;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.arduino.core.Board;
import org.eclipse.cdt.arduino.core.IArduinoBoardManager;
@ -19,27 +21,50 @@ import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.serial.SerialPort;
import org.eclipse.remote.core.IRemoteCommandShellService;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionChangeListener;
import org.eclipse.remote.core.IRemoteConnectionPropertyService;
import org.eclipse.remote.core.IRemoteProcess;
import org.eclipse.remote.core.RemoteConnectionChangeEvent;
import org.eclipse.remote.serial.core.SerialPortCommandShell;
public class ArduinoRemoteConnection implements IRemoteConnectionPropertyService, IRemoteCommandShellService, IArduinoRemoteConnection {
public class ArduinoRemoteConnection implements IRemoteConnectionPropertyService, IRemoteCommandShellService,
IArduinoRemoteConnection, IRemoteConnectionChangeListener {
private final IArduinoBoardManager boardManager = Activator.getService(IArduinoBoardManager.class);
private final IRemoteConnection remoteConnection;
private SerialPort serialPort;
private SerialPortCommandShell commandShell;
private static final Map<IRemoteConnection, ArduinoRemoteConnection> connectionMap = new HashMap<>();
public ArduinoRemoteConnection(IRemoteConnection remoteConnection) {
this.remoteConnection = remoteConnection;
remoteConnection.addConnectionChangeListener(this);
}
@Override
public void connectionChanged(RemoteConnectionChangeEvent event) {
if (event.getType() == RemoteConnectionChangeEvent.CONNECTION_REMOVED) {
synchronized (connectionMap) {
connectionMap.remove(event.getConnection());
}
}
}
public static class Factory implements IRemoteConnection.Service.Factory {
@SuppressWarnings("unchecked")
@Override
public <T extends IRemoteConnection.Service> T getService(IRemoteConnection remoteConnection, Class<T> service) {
public <T extends IRemoteConnection.Service> T getService(IRemoteConnection remoteConnection,
Class<T> service) {
if (IArduinoRemoteConnection.class.equals(service)) {
return (T) new ArduinoRemoteConnection(remoteConnection);
synchronized (connectionMap) {
ArduinoRemoteConnection connection = connectionMap.get(remoteConnection);
if (connection == null) {
connection = new ArduinoRemoteConnection(remoteConnection);
connectionMap.put(remoteConnection, connection);
}
return (T) connection;
}
} else if (IRemoteConnectionPropertyService.class.equals(service)
|| IRemoteCommandShellService.class.equals(service)) {
return (T) remoteConnection.getService(IArduinoRemoteConnection.class);
@ -78,7 +103,6 @@ public class ArduinoRemoteConnection implements IRemoteConnectionPropertyService
return remoteConnection.getAttribute(PORT_NAME);
}
@Override
public IRemoteProcess getCommandShell(int flags) throws IOException {
if (serialPort != null && serialPort.isOpen()) {