From 3d415c5959d84c8ac178de73f57678f9d9cb9f98 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sat, 31 Jan 2015 00:28:27 -0800 Subject: [PATCH] Fixes to the new sendPackage logic --- .../Backends/LanBackend/LanLink.java | 8 ++++- .../Backends/LanBackend/LanLinkProvider.java | 31 ++++++++++++++++--- .../LoopbackBackend/LoopbackLink.java | 2 +- src/org/kde/kdeconnect/Device.java | 2 +- src/org/kde/kdeconnect/NetworkPackage.java | 5 ++- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java index fb90f39e..2da5922d 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java @@ -60,6 +60,7 @@ public class LanLink extends BaseLink { //Blocking, do not call from main thread private void sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) { if (session == null) { + Log.e("sendPackage", "Not yet connected"); callback.sendFailure(new NotYetConnectedException()); return; } @@ -86,6 +87,7 @@ public class LanLink extends BaseLink { WriteFuture future = session.write(np.serialize()); future.awaitUninterruptibly(); if (!future.isWritten()) { + Log.e("sendPackage", "!future.isWritten()"); callback.sendFailure(future.getException()); return; } @@ -99,6 +101,7 @@ public class LanLink extends BaseLink { timeout.schedule(new TimerTask() { @Override public void run() { + Log.e("sendPackage","Timeout"); try { server.close(); } catch (Exception e) { } callback.sendFailure(new TimeoutException("Timed out waiting for other end to establish a connection to receive the payload.")); } @@ -122,6 +125,7 @@ public class LanLink extends BaseLink { } Log.i("LanLink", "Finished sending payload"); } catch (Exception e) { + Log.e("sendPackage", "Exception: "+e); callback.sendFailure(e); return; } finally { @@ -135,7 +139,9 @@ public class LanLink extends BaseLink { callback.sendSuccess(); } catch (Exception e) { - callback.sendFailure(e); + if (callback != null) { + callback.sendFailure(e); + } } } diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 35850eef..6295f41c 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -39,6 +39,7 @@ import org.apache.mina.transport.socket.nio.NioDatagramAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import org.apache.mina.transport.socket.nio.NioSocketConnector; import org.kde.kdeconnect.Backends.BaseLinkProvider; +import org.kde.kdeconnect.Device; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.UserInterface.CustomDevicesActivity; @@ -66,15 +67,26 @@ public class LanLinkProvider extends BaseLinkProvider { @Override public void sessionClosed(IoSession session) throws Exception { - LanLink brokenLink = nioSessions.get(session.getId()); + final LanLink brokenLink = nioSessions.get(session.getId()); if (brokenLink != null) { nioSessions.remove(session.getId()); - connectionLost(brokenLink); brokenLink.disconnect(); String deviceId = brokenLink.getDeviceId(); if (visibleComputers.get(deviceId) == brokenLink) { visibleComputers.remove(deviceId); } + new Thread(new Runnable() { + @Override + public void run() { + //Wait a bit before emiting connectionLost, in case the same device re-appears + try { + Thread.sleep(200); + } catch (InterruptedException e) { } + connectionLost(brokenLink); + + } + }).start(); + } } @@ -169,10 +181,19 @@ public class LanLinkProvider extends BaseLinkProvider { @Override public void run() { NetworkPackage np2 = NetworkPackage.createIdentityPackage(context); - link.sendPackage(np2,null); + link.sendPackage(np2,new Device.SendPackageStatusCallback() { + @Override + protected void onSuccess() { + nioSessions.put(session.getId(), link); + addLink(identityPackage, link); + } + + @Override + protected void onFailure(Throwable e) { + + } + }); - nioSessions.put(session.getId(), link); - addLink(identityPackage, link); } }).start(); diff --git a/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java b/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java index 0c5d9aac..add92f85 100644 --- a/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java +++ b/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java @@ -47,7 +47,7 @@ public class LoopbackLink extends BaseLink { String s = in.serialize(); NetworkPackage out= NetworkPackage.unserialize(s); if (key != null) { - out.decrypt(privateKey); + out = out.decrypt(privateKey); } packageReceived(out); if (in.hasPayload()) { diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index f017a9c5..4c49af83 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -544,7 +544,7 @@ public class Device implements BaseLink.PackageReceiver { } if (!callback.success) { - Log.e("sendPackage", "No device link could send the package. package lost!"); + Log.e("sendPackage", "No device link (of "+mLinks.size()+" available) could send the package. Package lost!"); backtrace.printStackTrace(); } diff --git a/src/org/kde/kdeconnect/NetworkPackage.java b/src/org/kde/kdeconnect/NetworkPackage.java index 440e3bde..e5a777e0 100644 --- a/src/org/kde/kdeconnect/NetworkPackage.java +++ b/src/org/kde/kdeconnect/NetworkPackage.java @@ -215,6 +215,7 @@ public class NetworkPackage { NetworkPackage encrypted = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_ENCRYPTED); encrypted.set("data", chunks); + encrypted.setPayload(mPayload, mPayloadSize); return encrypted; } @@ -233,7 +234,9 @@ public class NetworkPackage { decryptedJson += decryptedChunk; } - return unserialize(decryptedJson); + NetworkPackage decrypted = unserialize(decryptedJson); + decrypted.setPayload(mPayload, mPayloadSize); + return decrypted; } static public NetworkPackage createIdentityPackage(Context context) {