Skip to content

Commit

Permalink
Fixes issue #13
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Mar 5, 2019
1 parent 3d0d456 commit 6923be5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,7 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,
throw new IOException("Unexpected message [HELLO]!");
}

if (https != null) {
https.cancel();
https = null;
}

final NetworkHelloMessage hello = NetworkHelloMessage.unmarshall(in);
fireOnByteReceived(1 + hello.getWireSize()); // +1 : magic number (byte)

final Version version = Version.get();
final boolean isProd = isProd(version, hello.getMajor(), hello.getMinor());

if (isProd && (version.getMajor() != hello.getMajor() || version.getMinor() != hello.getMinor())) {
throw new IOException("Version Error!");
}

introduce(in);
introduced = true;
fireOnConnected(connection);
break;
Expand All @@ -226,7 +212,6 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,

final NetworkCaptureMessage capture = NetworkCaptureMessage.unmarshall(in);
fireOnByteReceived(1 + capture.getWireSize()); // +1 : magic number (byte)

captureMessageHandler.handleCapture(capture);
break;

Expand All @@ -237,7 +222,6 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,

final NetworkMouseLocationMessage mouse = NetworkMouseLocationMessage.unmarshall(in);
fireOnByteReceived(1 + mouse.getWireSize()); // +1 : magic number (byte)

mouseMessageHandler.handleLocation(mouse);
break;

Expand All @@ -248,8 +232,8 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,

final NetworkClipboardTextMessage clipboardTextMessage = NetworkClipboardTextMessage.unmarshall(in);
fireOnByteReceived(1 + clipboardTextMessage.getWireSize()); // +1 : magic number (byte)
fireOnClipboardReceived();
setClipboardContents(clipboardTextMessage.getText(), clipboardOwner);
fireOnClipboardReceived();
break;

case CLIPBOARD_FILES:
Expand All @@ -259,18 +243,9 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,

final NetworkClipboardFilesMessage clipboardFiles = NetworkClipboardFilesMessage.unmarshall(in, filesHelper);
fireOnByteReceived(1 + clipboardFiles.getWireSize()); // +1 : magic number (byte)
filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L);

filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles, clipboardOwner);
if (filesHelper.isIdle()) {
fireOnClipboardReceived();
filesHelper = new NetworkClipboardFilesHelper();
setClipboardContents(clipboardFiles.getFiles(), clipboardOwner);
} else {
filesHelper.setFiles(clipboardFiles.getFiles());
filesHelper.setFileNames(clipboardFiles.getFileNames());
filesHelper.setFileSizes(clipboardFiles.getFileSizes());
filesHelper.setPosition(clipboardFiles.getPosition());
filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize());
}
break;

Expand All @@ -297,6 +272,23 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException,
fireOnReady();
}

private void introduce(ObjectInputStream in) throws IOException {
if (https != null) {
https.cancel();
https = null;
}

final NetworkHelloMessage hello = NetworkHelloMessage.unmarshall(in);
fireOnByteReceived(1 + hello.getWireSize()); // +1 : magic number (byte)

final Version version = Version.get();
final boolean isProd = isProd(version, hello.getMajor(), hello.getMinor());

if (isProd && (version.getMajor() != hello.getMajor() || version.getMinor() != hello.getMinor())) {
throw new IOException("Version Error!");
}
}

private void closeConnection(ObjectInputStream in, ObjectOutputStream out) {
if (sender != null) {
sender.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import mpo.dayon.common.network.message.*;
import mpo.dayon.common.security.CustomTrustManager;
import mpo.dayon.common.squeeze.CompressionMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.net.ssl.*;
Expand Down Expand Up @@ -149,13 +148,16 @@ private void receivingLoop() throws IOException {

case CLIPBOARD_TEXT:
final NetworkClipboardTextMessage clipboardTextMessage = NetworkClipboardTextMessage.unmarshall(in);
sender.ping();
setClipboardContents(clipboardTextMessage.getText(), clipboardOwner);
sender.ping();
break;

case CLIPBOARD_FILES:
final NetworkClipboardFilesMessage clipboardFiles = NetworkClipboardFilesMessage.unmarshall(in, filesHelper);
filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles);
filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles, clipboardOwner);
if (filesHelper.isIdle()) {
sender.ping();
}
break;

case PING:
Expand All @@ -167,23 +169,6 @@ private void receivingLoop() throws IOException {
}
}

@NotNull
private NetworkClipboardFilesHelper handleNetworkClipboardFilesHelper(NetworkClipboardFilesHelper filesHelper, NetworkClipboardFilesMessage clipboardFiles) {
filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L);
if (filesHelper.isIdle()) {
filesHelper = new NetworkClipboardFilesHelper();
sender.ping();
setClipboardContents(clipboardFiles.getFiles(), clipboardOwner);
} else {
filesHelper.setFiles(clipboardFiles.getFiles());
filesHelper.setFileNames(clipboardFiles.getFileNames());
filesHelper.setFileSizes(clipboardFiles.getFileSizes());
filesHelper.setPosition(clipboardFiles.getPosition());
filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize());
}
return filesHelper;
}

/**
* The first message being sent to the assistant (e.g., version
* identification).
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/mpo/dayon/common/network/NetworkEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mpo.dayon.common.network;

import mpo.dayon.common.log.Log;
import mpo.dayon.common.network.message.NetworkClipboardFilesHelper;
import mpo.dayon.common.network.message.NetworkClipboardFilesMessage;
import mpo.dayon.common.utils.TransferableFiles;

import java.awt.*;
Expand All @@ -25,10 +27,25 @@ protected void setClipboardContents(String string, ClipboardOwner clipboardOwner
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, clipboardOwner);
}

protected void setClipboardContents(List<File> files, ClipboardOwner clipboardOwner) {
private void setClipboardContents(List<File> files, ClipboardOwner clipboardOwner) {
Log.debug("setClipboardContents " + files.toString());
TransferableFiles transferableFiles = new TransferableFiles(files);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferableFiles, clipboardOwner);
}

protected NetworkClipboardFilesHelper handleNetworkClipboardFilesHelper(NetworkClipboardFilesHelper filesHelper, NetworkClipboardFilesMessage clipboardFiles, ClipboardOwner clipboardOwner) {
filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L);
if (filesHelper.isIdle()) {
filesHelper = new NetworkClipboardFilesHelper();
setClipboardContents(clipboardFiles.getFiles(), clipboardOwner);
} else {
filesHelper.setFiles(clipboardFiles.getFiles());
filesHelper.setFileNames(clipboardFiles.getFileNames());
filesHelper.setFileSizes(clipboardFiles.getFileSizes());
filesHelper.setPosition(clipboardFiles.getPosition());
filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize());
}
return filesHelper;
}

}

0 comments on commit 6923be5

Please sign in to comment.