Skip to content

Commit

Permalink
Removed JSSC connection driver and added an event to detect if a cont…
Browse files Browse the repository at this point in the history
…roller has been disconnected. (#2562)
  • Loading branch information
breiler authored Jun 28, 2024
1 parent 6c7cdc2 commit 9c7865a
Show file tree
Hide file tree
Showing 38 changed files with 369 additions and 391 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<jogl.version>2.5.0</jogl.version>
<miglayout.version>3.7.4</miglayout.version>
<guava.version>33.0.0-jre</guava.version>
<jssc.version>2.8.0</jssc.version>
<jserialcomm.version>2.10.4</jserialcomm.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-io.version>2.14.0</commons-io.version>
Expand All @@ -67,7 +66,7 @@
<swing-layout.version>1.0.3</swing-layout.version>
<ugs.surefire.version>3.0.0-M5</ugs.surefire.version>
<download-maven-plugin.version>1.3.0</download-maven-plugin.version>
<mockito.version>3.2.4</mockito.version>
<mockito.version>5.12.0</mockito.version>
<batik.version>1.17</batik.version>
<jts.version>1.19.0</jts.version>
<ugs.maven-resources-plugin.version>3.1.0</ugs.maven-resources-plugin.version>
Expand Down
5 changes: 0 additions & 5 deletions ugs-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.scream3r</groupId>
<artifactId>jssc</artifactId>
<version>${jssc.version}</version>
</dependency>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ public Boolean closeCommPort() throws Exception {
return true;
}

@Override
public void onConnectionClosed() {
try {
closeCommPort();
} catch (Exception e) {
// Ignore
}
}


@Override
public Boolean isCommOpen() {
return comm != null && comm.isConnected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.connection.Connection;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.connection.ConnectionFactory;
import com.willwinder.universalgcodesender.connection.IConnectionListener;
import com.willwinder.universalgcodesender.i18n.Localization;

import java.io.IOException;
Expand All @@ -33,7 +34,7 @@ This file is part of Universal Gcode Sender (UGS).
*
* @author wwinder
*/
public abstract class AbstractCommunicator implements ICommunicator {
public abstract class AbstractCommunicator implements ICommunicator, IConnectionListener {
private static final Logger logger = Logger.getLogger(AbstractCommunicator.class.getName());

private final ICommunicatorEventDispatcher eventDispatcher;
Expand Down Expand Up @@ -124,4 +125,9 @@ public byte[] xmodemReceive() throws IOException {
public void xmodemSend(byte[] data) throws IOException {
connection.xmodemSend(data);
}

@Override
public void onConnectionClosed() {
eventDispatcher.onConnectionClosed();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Will Winder
Copyright 2019-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.universalgcodesender.connection.Connection;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.connection.IConnectionListener;
import com.willwinder.universalgcodesender.types.GcodeCommand;
import com.willwinder.universalgcodesender.utils.IGcodeStreamReader;

Expand All @@ -36,7 +35,7 @@ This file is part of Universal Gcode Sender (UGS).
*
* @author Joacim Breiler
*/
public interface ICommunicator extends IConnectionListener {
public interface ICommunicator {

/**
* Add command to the command buffer outside file mode. These commands will be sent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2018 Will Winder
Copyright 2012-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -55,4 +55,9 @@ public interface ICommunicatorListener {
* processing of commands.
*/
void communicatorPausedOnError();

/**
* Called when the connection was closed
*/
void onConnectionClosed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public void commandSkipped(GcodeCommand command) {
public void communicatorPausedOnError() {
communicatorListeners.forEach(ICommunicatorListener::communicatorPausedOnError);
}

@Override
public void onConnectionClosed() {
communicatorListeners.forEach(ICommunicatorListener::onConnectionClosed);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2022 Will Winder
Copyright 2013-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -18,7 +18,7 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.universalgcodesender.connection;

import com.willwinder.universalgcodesender.connection.xmodem.XModemResponseMessageHandler;
import com.willwinder.universalgcodesender.connection.xmodem.XModemConnectionListenerHandler;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -31,26 +31,25 @@ This file is part of Universal Gcode Sender (UGS).
*/
public abstract class AbstractConnection implements Connection {

protected IResponseMessageHandler responseMessageHandler = new ResponseMessageHandler();
protected IConnectionListenerManager connectionListenerManager = new ConnectionListenerManager();

@Override
public void addListener(IConnectionListener connectionListener) {
responseMessageHandler.addListener(connectionListener);
connectionListenerManager.addListener(connectionListener);
}

@Override
public byte[] xmodemReceive() throws IOException {
// Switch to a special XModem response handler
IResponseMessageHandler previousResponseMessageHandler = responseMessageHandler;
XModemConnectionListenerHandler reader = new XModemConnectionListenerHandler(this, connectionListenerManager);
try {
XModemResponseMessageHandler reader = new XModemResponseMessageHandler(this);
responseMessageHandler = reader;
connectionListenerManager = reader;
byte[] result = reader.xmodemReceive();

return trimEOF(result);
} finally {
// Restore the old response message handler
responseMessageHandler = previousResponseMessageHandler;
connectionListenerManager = reader.unwrap();
}
}

Expand All @@ -67,18 +66,17 @@ protected static byte[] trimEOF(byte[] result) {
@Override
public void xmodemSend(byte[] data) throws IOException {
// Switch to a special XModem response handler
IResponseMessageHandler previousResponseMessageHandler = responseMessageHandler;
XModemConnectionListenerHandler reader = new XModemConnectionListenerHandler(this, connectionListenerManager);
try {
XModemResponseMessageHandler reader = new XModemResponseMessageHandler(this);
responseMessageHandler = reader;
connectionListenerManager = reader;
reader.xmodemSend(data);
} finally {
// Restore the old response message handler
responseMessageHandler = previousResponseMessageHandler;
connectionListenerManager = reader.unwrap();
}
}

public IResponseMessageHandler getResponseMessageHandler() {
return responseMessageHandler;
public IConnectionListenerManager getConnectionListenerManager() {
return connectionListenerManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2018 Will Winder
Copyright 2013-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -89,19 +89,12 @@ public interface Connection {
*/
boolean isOpen();

/**
* Returns a list of all port names available
*
* @return a list of available port names
*/
List<String> getPortNames();

/**
* Returns a list of all available connection devices
*
* @return a list of available connection devices
*/
List<IConnectionDevice> getDevices();
List<? extends IConnectionDevice> getDevices();

/**
* Enters a mode for receiving using the xmodem protocol and return the file stream as an byte array.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Will Winder
Copyright 2018-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -28,7 +28,6 @@ This file is part of Universal Gcode Sender (UGS).
*/
public enum ConnectionDriver {
JSERIALCOMM("JSerialComm", "jserialcomm://"),
JSSC("JSSC", "jssc://"),
TCP("TCP", "tcp://"),
WS("WebSocket", "ws://");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2015-2023 Will Winder
Copyright 2015-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -40,7 +40,7 @@ public class ConnectionFactory {
* @return a connection
* @throws ConnectionException if something went wron while creating the connection
*/
static public Connection getConnection(String uri) throws ConnectionException {
public static Connection getConnection(String uri) throws ConnectionException {
for (ConnectionDriver connectionDriver : ConnectionDriver.values()) {
if (StringUtils.startsWithIgnoreCase(uri, connectionDriver.getProtocol())) {
Connection connection = getConnection(connectionDriver).orElseThrow(() -> new ConnectionException("Couldn't load connection driver " + connectionDriver + " for uri: " + uri));
Expand All @@ -52,42 +52,24 @@ static public Connection getConnection(String uri) throws ConnectionException {
throw new ConnectionException("Couldn't find connection driver for uri: " + uri);
}

/**
* Returns available ports for this connection driver
*
* @param connectionDriver the connection driver to use for querying ports
* @return a list of port names
* @deprecated use {@link #getDevices(ConnectionDriver)} instead
*/
public static List<String> getPortNames(ConnectionDriver connectionDriver) {
return getConnection(connectionDriver)
.map(Connection::getPortNames)
.orElseGet(Collections::emptyList);
}

/**
* Lists found devices for the given connection driver
*
* @param connectionDriver the connection driver to use for querying devices
* @return a list of connection devices
*/
public static List<IConnectionDevice> getDevices(ConnectionDriver connectionDriver) {
public static List<? extends IConnectionDevice> getDevices(ConnectionDriver connectionDriver) {
return getConnection(connectionDriver)
.map(Connection::getDevices)
.orElseGet(Collections::emptyList);
}

public static Optional<Connection> getConnection(ConnectionDriver connectionDriver) {
switch (connectionDriver) {
case JSERIALCOMM:
return Optional.of(new JSerialCommConnection());
case JSSC:
return Optional.of(new JSSCConnection());
case TCP:
return Optional.of(new TCPConnection());
case WS:
return Optional.of(new WSConnection());
}
return Optional.empty();
return switch (connectionDriver) {
case JSERIALCOMM -> Optional.of(new JSerialCommConnection());
case TCP -> Optional.of(new TCPConnection());
case WS -> Optional.of(new WSConnection());
default -> Optional.empty();
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Will Winder
Copyright 2018-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -28,19 +28,19 @@ This file is part of Universal Gcode Sender (UGS).
/**
* Handles response messages from the serial connection buffering the data
* until we have a complete line. It will then attempt to dispatch that
* data to a communicator.
* data to a communicator via a {@link IConnectionListener}
*
* @author wwinder
* @author Joacim Breiler
*/
public class ResponseMessageHandler implements IResponseMessageHandler {
public class ConnectionListenerManager implements IConnectionListenerManager {

private final static Logger LOGGER = Logger.getLogger(ResponseMessageHandler.class.getSimpleName());
private static final Logger LOGGER = Logger.getLogger(ConnectionListenerManager.class.getSimpleName());

private final StringBuilder inputBuffer;
private final Set<IConnectionListener> listeners = new HashSet<>();

public ResponseMessageHandler() {
public ConnectionListenerManager() {
inputBuffer = new StringBuilder();
}

Expand Down Expand Up @@ -76,13 +76,19 @@ public void notifyListeners(String message) {
try {
listener.handleResponseMessage(message);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "The response message could not be handled: \"" + message + "\", unsafe to proceed, shutting down connection.", e);
throw e;
LOGGER.log(Level.SEVERE, e, () -> "The response message could not be handled: \"" + message + "\", unsafe to proceed, shutting down connection.");
throw new ConnectionException("The response message could not be handled: \"" + message + "\", unsafe to proceed, shutting down connection.", e);
}
});
}

@Override
public void addListener(IConnectionListener connectionListener) {
listeners.add(connectionListener);
}

@Override
public void onConnectionClosed() {
listeners.forEach(IConnectionListener::onConnectionClosed);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
Copyright 2019-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.connection;

/**
Expand All @@ -12,4 +30,9 @@ public interface IConnectionListener {
* @param response a response message
*/
void handleResponseMessage(String response);

/**
* This method will be executed if the connection is closed
*/
void onConnectionClosed();
}
Loading

0 comments on commit 9c7865a

Please sign in to comment.