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

View File

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

View File

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

View File

@ -1,11 +1,9 @@
package mpo.dayon.common.network.message; package mpo.dayon.common.network.message;
import mpo.dayon.assisted.network.NetworkAssistedEngine;
public interface NetworkClipboardRequestMessageHandler extends NetworkMessageHandler { public interface NetworkClipboardRequestMessageHandler extends NetworkMessageHandler {
/** /**
* Should not block as called from the network incoming message thread (!) * 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; package mpo.dayon.common.network.message;
import mpo.dayon.common.network.NetworkEngine;
public interface NetworkCompressorConfigurationMessageHandler extends NetworkMessageHandler { public interface NetworkCompressorConfigurationMessageHandler extends NetworkMessageHandler {
/** /**
* Should not block as called from the network incoming message thread (!) * Should not block as called from the network incoming message thread (!)
*/ */
void handleConfiguration(NetworkEngine engine, NetworkCompressorConfigurationMessage configuration); void handleConfiguration(NetworkCompressorConfigurationMessage configuration);
} }