2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 21:27:40 +00:00

Fixes to the new sendPackage logic

This commit is contained in:
Albert Vaca 2015-01-31 00:28:27 -08:00
parent bda6e3cb7f
commit 3d415c5959
5 changed files with 39 additions and 9 deletions

View File

@ -60,6 +60,7 @@ public class LanLink extends BaseLink {
//Blocking, do not call from main thread //Blocking, do not call from main thread
private void sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) { private void sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) {
if (session == null) { if (session == null) {
Log.e("sendPackage", "Not yet connected");
callback.sendFailure(new NotYetConnectedException()); callback.sendFailure(new NotYetConnectedException());
return; return;
} }
@ -86,6 +87,7 @@ public class LanLink extends BaseLink {
WriteFuture future = session.write(np.serialize()); WriteFuture future = session.write(np.serialize());
future.awaitUninterruptibly(); future.awaitUninterruptibly();
if (!future.isWritten()) { if (!future.isWritten()) {
Log.e("sendPackage", "!future.isWritten()");
callback.sendFailure(future.getException()); callback.sendFailure(future.getException());
return; return;
} }
@ -99,6 +101,7 @@ public class LanLink extends BaseLink {
timeout.schedule(new TimerTask() { timeout.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
Log.e("sendPackage","Timeout");
try { server.close(); } catch (Exception e) { } try { server.close(); } catch (Exception e) { }
callback.sendFailure(new TimeoutException("Timed out waiting for other end to establish a connection to receive the payload.")); 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"); Log.i("LanLink", "Finished sending payload");
} catch (Exception e) { } catch (Exception e) {
Log.e("sendPackage", "Exception: "+e);
callback.sendFailure(e); callback.sendFailure(e);
return; return;
} finally { } finally {
@ -135,7 +139,9 @@ public class LanLink extends BaseLink {
callback.sendSuccess(); callback.sendSuccess();
} catch (Exception e) { } catch (Exception e) {
callback.sendFailure(e); if (callback != null) {
callback.sendFailure(e);
}
} }
} }

View File

@ -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.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector; import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.Backends.BaseLinkProvider;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.NetworkPackage;
import org.kde.kdeconnect.UserInterface.CustomDevicesActivity; import org.kde.kdeconnect.UserInterface.CustomDevicesActivity;
@ -66,15 +67,26 @@ public class LanLinkProvider extends BaseLinkProvider {
@Override @Override
public void sessionClosed(IoSession session) throws Exception { public void sessionClosed(IoSession session) throws Exception {
LanLink brokenLink = nioSessions.get(session.getId()); final LanLink brokenLink = nioSessions.get(session.getId());
if (brokenLink != null) { if (brokenLink != null) {
nioSessions.remove(session.getId()); nioSessions.remove(session.getId());
connectionLost(brokenLink);
brokenLink.disconnect(); brokenLink.disconnect();
String deviceId = brokenLink.getDeviceId(); String deviceId = brokenLink.getDeviceId();
if (visibleComputers.get(deviceId) == brokenLink) { if (visibleComputers.get(deviceId) == brokenLink) {
visibleComputers.remove(deviceId); 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 @Override
public void run() { public void run() {
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context); 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(); }).start();

View File

@ -47,7 +47,7 @@ public class LoopbackLink extends BaseLink {
String s = in.serialize(); String s = in.serialize();
NetworkPackage out= NetworkPackage.unserialize(s); NetworkPackage out= NetworkPackage.unserialize(s);
if (key != null) { if (key != null) {
out.decrypt(privateKey); out = out.decrypt(privateKey);
} }
packageReceived(out); packageReceived(out);
if (in.hasPayload()) { if (in.hasPayload()) {

View File

@ -544,7 +544,7 @@ public class Device implements BaseLink.PackageReceiver {
} }
if (!callback.success) { 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(); backtrace.printStackTrace();
} }

View File

@ -215,6 +215,7 @@ public class NetworkPackage {
NetworkPackage encrypted = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_ENCRYPTED); NetworkPackage encrypted = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_ENCRYPTED);
encrypted.set("data", chunks); encrypted.set("data", chunks);
encrypted.setPayload(mPayload, mPayloadSize);
return encrypted; return encrypted;
} }
@ -233,7 +234,9 @@ public class NetworkPackage {
decryptedJson += decryptedChunk; decryptedJson += decryptedChunk;
} }
return unserialize(decryptedJson); NetworkPackage decrypted = unserialize(decryptedJson);
decrypted.setPayload(mPayload, mPayloadSize);
return decrypted;
} }
static public NetworkPackage createIdentityPackage(Context context) { static public NetworkPackage createIdentityPackage(Context context) {