Refactoring and simplifications

This commit is contained in:
RetGal 2021-02-22 23:28:52 +01:00
parent 7fa39f3313
commit efdb1e79ba
6 changed files with 47 additions and 63 deletions

View File

@ -6,7 +6,6 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import javax.swing.*;
@ -28,7 +27,6 @@ import mpo.dayon.common.error.KeyboardErrorHandler;
import mpo.dayon.common.event.Subscriber;
import mpo.dayon.common.gui.common.DialogFactory;
import mpo.dayon.common.log.Log;
import mpo.dayon.common.network.NetworkEngine;
import mpo.dayon.common.network.message.*;
import mpo.dayon.common.utils.FileUtilities;
import mpo.dayon.common.utils.SystemUtilities;
@ -69,7 +67,8 @@ public class Assisted implements Subscriber, ClipboardOwner {
final NetworkControlMessageHandler controlHandler = new RobotNetworkControlMessageHandler();
controlHandler.subscribe(this);
networkEngine = new NetworkAssistedEngine(captureConfigurationHandler, compressorConfigurationHandler, controlHandler, clipboardRequestHandler, this);
networkEngine = new NetworkAssistedEngine(captureConfigurationHandler, compressorConfigurationHandler,
controlHandler, clipboardRequestHandler, this);
networkEngine.addListener(new MyNetworkAssistedEngineListener());
if (frame == null) {
@ -207,7 +206,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
/**
* Should not block as called from the network incoming message thread (!)
*/
private void onCaptureEngineConfigured(NetworkEngine engine, NetworkCaptureConfigurationMessage configuration) {
private void onCaptureEngineConfigured(NetworkCaptureConfigurationMessage configuration) {
final CaptureEngineConfiguration captureEngineConfiguration = configuration.getConfiguration();
if (captureEngine != null) {
@ -218,7 +217,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
// Setup the mouse engine (no need before I guess)
final MouseEngine mouseEngine = new MouseEngine();
mouseEngine.addListener((NetworkAssistedEngine) engine);
mouseEngine.addListener(networkEngine);
mouseEngine.start();
captureEngine = new CaptureEngine(new RobotCaptureFactory());
@ -232,7 +231,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
/**
* Should not block as called from the network incoming message thread (!)
*/
private void onCompressorEngineConfigured(NetworkEngine engine, NetworkCompressorConfigurationMessage configuration) {
private void onCompressorEngineConfigured(NetworkCompressorConfigurationMessage configuration) {
final CompressorEngineConfiguration compressorEngineConfiguration = configuration.getConfiguration();
if (compressorEngine != null) {
@ -243,7 +242,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
compressorEngine = new CompressorEngine();
compressorEngine.configure(compressorEngineConfiguration);
compressorEngine.addListener((NetworkAssistedEngine) engine);
compressorEngine.addListener(networkEngine);
compressorEngine.start(1);
if (captureEngine != null) {
captureEngine.addListener(compressorEngine);
@ -253,7 +252,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
/**
* Should not block as called from the network incoming message thread (!)
*/
private void onClipboardRequested(NetworkAssistedEngine engine) {
private void onClipboardRequested() {
Log.info("Clipboard transfer request received");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
@ -268,14 +267,14 @@ public class Assisted implements Subscriber, ClipboardOwner {
if (!files.isEmpty()) {
final long totalFilesSize = FileUtilities.calculateTotalFileSize(files);
Log.debug("Clipboard contains files with size: " + totalFilesSize);
engine.sendClipboardFiles(files, totalFilesSize, files.get(0).getParent());
networkEngine.sendClipboardFiles(files, totalFilesSize, files.get(0).getParent());
return;
}
} else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
// noinspection unchecked
String text = (String) clipboard.getData(DataFlavor.stringFlavor);
Log.debug("Clipboard contains text: " + text);
engine.sendClipboardText(text, text.getBytes().length);
networkEngine.sendClipboardText(text, text.getBytes().length);
return;
} else {
Log.debug("Clipboard contains no supported data");
@ -285,7 +284,7 @@ public class Assisted implements Subscriber, ClipboardOwner {
}
String text = "\uD83E\uDD84";
Log.debug("Sending a unicorn: " + text);
engine.sendClipboardText(text, text.getBytes().length);
networkEngine.sendClipboardText(text, text.getBytes().length);
}
@Override
@ -320,12 +319,12 @@ public class Assisted implements Subscriber, ClipboardOwner {
}
@Override
public void onConnected(Socket connection) {
public void onConnected() {
frame.onConnected();
}
@Override
public void onDisconnecting(Socket connection) {
public void onDisconnecting() {
frame.onDisconnecting();
}

View File

@ -21,7 +21,6 @@ import javax.net.ssl.*;
import java.awt.*;
import java.awt.datatransfer.ClipboardOwner;
import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.*;
@ -52,32 +51,25 @@ public class NetworkAssistedEngine extends NetworkEngine
private NetworkSender sender; // out
private SSLSocket connection;
private ObjectOutputStream out;
private ObjectInputStream in;
private Thread fileReceiver; // file in
private NetworkSender fileSender; // file out
private SSLSocket fileConnection;
private ObjectOutputStream fileOut;
private ObjectInputStream fileIn;
private final AtomicBoolean cancelling = new AtomicBoolean(false);
public NetworkAssistedEngine(NetworkCaptureConfigurationMessageHandler captureConfigurationHandler,
NetworkCompressorConfigurationMessageHandler compressorConfigurationHandler, NetworkControlMessageHandler controlHandler, NetworkClipboardRequestMessageHandler clipboardRequestHandler, ClipboardOwner clipboardOwner) {
NetworkCompressorConfigurationMessageHandler compressorConfigurationHandler,
NetworkControlMessageHandler controlHandler,
NetworkClipboardRequestMessageHandler clipboardRequestHandler, ClipboardOwner clipboardOwner) {
this.captureConfigurationHandler = captureConfigurationHandler;
this.compressorConfigurationHandler = compressorConfigurationHandler;
this.controlHandler = controlHandler;
this.clipboardRequestHandler = clipboardRequestHandler;
this.clipboardOwner = clipboardOwner;
}
private void runReceivers() {
@ -110,7 +102,7 @@ public class NetworkAssistedEngine extends NetworkEngine
try {
start();
sendHello();
fireOnConnected(connection);
fireOnConnected();
} catch (UnknownHostException e) {
fireOnHostNotFound(configuration);
} catch (SocketTimeoutException e) {
@ -134,8 +126,8 @@ public class NetworkAssistedEngine extends NetworkEngine
}
SSLSocketFactory ssf = initSSLContext().getSocketFactory();
connection = (SSLSocket) ssf.createSocket(configuration.getServerName(), configuration.getServerPort());
out = new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream()));
SSLSocket connection = (SSLSocket) ssf.createSocket(configuration.getServerName(), configuration.getServerPort());
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream()));
sender = new NetworkSender(out); // the active part (!)
sender.start(1);
sender.ping();
@ -143,7 +135,7 @@ public class NetworkAssistedEngine extends NetworkEngine
receiver.start();
SSLSocket fileConnection = (SSLSocket) ssf.createSocket(configuration.getServerName(), configuration.getServerPort());
fileOut = new ObjectOutputStream(new BufferedOutputStream(fileConnection.getOutputStream()));
ObjectOutputStream fileOut = new ObjectOutputStream(new BufferedOutputStream(fileConnection.getOutputStream()));
fileSender = new NetworkSender(fileOut); // the active part (!)
fileSender.start(1);
fileSender.ping();
@ -158,7 +150,7 @@ public class NetworkAssistedEngine extends NetworkEngine
Log.info("Cancelling the network assisted engine...");
cancelling.set(true);
closeConnections();
fireOnDisconnecting(connection);
fireOnDisconnecting();
}
private ObjectInputStream initInputStream(SSLSocket connection) throws IOException {
@ -182,12 +174,12 @@ public class NetworkAssistedEngine extends NetworkEngine
switch (type) {
case CAPTURE_CONFIGURATION:
final NetworkCaptureConfigurationMessage captureConfigurationMessage = NetworkCaptureConfigurationMessage.unmarshall(in);
captureConfigurationHandler.handleConfiguration(NetworkAssistedEngine.this, captureConfigurationMessage);
captureConfigurationHandler.handleConfiguration(captureConfigurationMessage);
break;
case COMPRESSOR_CONFIGURATION:
final NetworkCompressorConfigurationMessage compressorConfigurationMessage = NetworkCompressorConfigurationMessage.unmarshall(in);
compressorConfigurationHandler.handleConfiguration(NetworkAssistedEngine.this, compressorConfigurationMessage);
compressorConfigurationHandler.handleConfiguration(compressorConfigurationMessage);
break;
case MOUSE_CONTROL:
@ -201,7 +193,7 @@ public class NetworkAssistedEngine extends NetworkEngine
break;
case CLIPBOARD_REQUEST:
clipboardRequestHandler.handleClipboardRequest(this);
clipboardRequestHandler.handleClipboardRequest();
break;
case CLIPBOARD_TEXT:
@ -221,7 +213,7 @@ public class NetworkAssistedEngine extends NetworkEngine
handleIOException(ex);
} finally {
closeConnections();
fireOnDisconnecting(connection);
fireOnDisconnecting();
}
}
@ -239,13 +231,13 @@ public class NetworkAssistedEngine extends NetworkEngine
sender.cancel();
}
receiver = safeInterrupt(receiver);
safeClose(in, out, connection);
safeClose(in);
if (fileSender != null) {
fileSender.cancel();
}
fileReceiver = safeInterrupt(fileReceiver);
safeClose(fileIn, fileOut, fileConnection);
safeClose(fileIn);
cancelling.set(false);
}
@ -352,15 +344,15 @@ public class NetworkAssistedEngine extends NetworkEngine
}
}
private void fireOnConnected(Socket connection) {
private void fireOnConnected() {
for (final NetworkAssistedEngineListener xListener : listeners.getListeners()) {
xListener.onConnected(connection);
xListener.onConnected();
}
}
private void fireOnDisconnecting(Socket connection) {
private void fireOnDisconnecting() {
for (final NetworkAssistedEngineListener xListener : listeners.getListeners()) {
xListener.onDisconnecting(connection);
xListener.onDisconnecting();
}
}

View File

@ -3,7 +3,6 @@ package mpo.dayon.assisted.network;
import mpo.dayon.common.event.Listener;
import java.io.IOException;
import java.net.Socket;
public interface NetworkAssistedEngineListener extends Listener {
@ -30,12 +29,12 @@ public interface NetworkAssistedEngineListener extends Listener {
/**
* Should not block as called from the network receiving thread (!)
*/
void onConnected(Socket connection);
void onConnected();
/**
* Should not block as called from the network receiving thread (!)
*/
void onDisconnecting(Socket connection);
void onDisconnecting();
/**
* Should not block as called from the network receiving thread (!)

View File

@ -1,10 +1,8 @@
package mpo.dayon.common.network.message;
import mpo.dayon.common.network.NetworkEngine;
public interface NetworkCaptureConfigurationMessageHandler extends NetworkMessageHandler {
/**
* Should not block as called from the network incoming message thread (!)
*/
void handleConfiguration(NetworkEngine engine, NetworkCaptureConfigurationMessage configuration);
package mpo.dayon.common.network.message;
public interface NetworkCaptureConfigurationMessageHandler extends NetworkMessageHandler {
/**
* Should not block as called from the network incoming message thread (!)
*/
void handleConfiguration(NetworkCaptureConfigurationMessage configuration);
}

View File

@ -1,11 +1,9 @@
package mpo.dayon.common.network.message;
import mpo.dayon.assisted.network.NetworkAssistedEngine;
public interface NetworkClipboardRequestMessageHandler extends NetworkMessageHandler {
/**
* Should not block as called from the network incoming message thread (!)
*/
void handleClipboardRequest(NetworkAssistedEngine networkAssistedEngine);
void handleClipboardRequest();
}

View File

@ -1,10 +1,8 @@
package mpo.dayon.common.network.message;
import mpo.dayon.common.network.NetworkEngine;
public interface NetworkCompressorConfigurationMessageHandler extends NetworkMessageHandler {
/**
* Should not block as called from the network incoming message thread (!)
*/
void handleConfiguration(NetworkEngine engine, NetworkCompressorConfigurationMessage configuration);
package mpo.dayon.common.network.message;
public interface NetworkCompressorConfigurationMessageHandler extends NetworkMessageHandler {
/**
* Should not block as called from the network incoming message thread (!)
*/
void handleConfiguration(NetworkCompressorConfigurationMessage configuration);
}