mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 05:07:40 +00:00
Removed Avahi classes
This commit is contained in:
parent
d5bc6ebcfa
commit
858bc30a4a
@ -13,16 +13,10 @@ import android.util.Log;
|
||||
|
||||
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.LinkProviders.BaseLinkProvider;
|
||||
import org.kde.connect.LinkProviders.BroadcastTcpLinkProvider;
|
||||
import org.kde.connect.LinkProviders.LanLinkProvider;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
@ -70,12 +64,8 @@ public class BackgroundService extends Service {
|
||||
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
if (settings.getBoolean("avahitcp_link", true)) {
|
||||
//linkProviders.add(new AvahiTcpLinkProvider(this));
|
||||
}
|
||||
|
||||
if (settings.getBoolean("broadcasttcp_link", true)) {
|
||||
linkProviders.add(new BroadcastTcpLinkProvider(this));
|
||||
linkProviders.add(new LanLinkProvider(this));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,32 @@
|
||||
package org.kde.connect.ComputerLinks;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.mina.core.future.ConnectFuture;
|
||||
import org.apache.mina.core.future.IoFuture;
|
||||
import org.apache.mina.core.future.IoFutureListener;
|
||||
import org.apache.mina.core.future.ReadFuture;
|
||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.filter.codec.textline.LineDelimiter;
|
||||
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
||||
import org.apache.mina.transport.socket.nio.NioSocketConnector;
|
||||
import org.kde.connect.LinkProviders.BaseLinkProvider;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class LanComputerLink extends BaseComputerLink {
|
||||
|
||||
private IoSession session = null;
|
||||
|
||||
public void disconnect() {
|
||||
Log.e("NioSessionComputerLink","Disconnect: "+session.getRemoteAddress().toString());
|
||||
Log.e("LanComputerLink","Disconnect: "+session.getRemoteAddress().toString());
|
||||
session.close(true);
|
||||
}
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
package org.kde.connect.ComputerLinks;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.mina.core.future.ConnectFuture;
|
||||
import org.apache.mina.core.future.IoFuture;
|
||||
import org.apache.mina.core.future.IoFutureListener;
|
||||
import org.apache.mina.core.future.ReadFuture;
|
||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.filter.codec.textline.LineDelimiter;
|
||||
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
||||
import org.apache.mina.transport.socket.nio.NioSocketConnector;
|
||||
import org.kde.connect.LinkProviders.BaseLinkProvider;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class NioSessionComputerLink extends BaseComputerLink {
|
||||
|
||||
private IoSession session = null;
|
||||
|
||||
public void disconnect() {
|
||||
Log.e("NioSessionComputerLink","Disconnect: "+session.getRemoteAddress().toString());
|
||||
session.close(true);
|
||||
}
|
||||
|
||||
public NioSessionComputerLink(IoSession session, String deviceId, BaseLinkProvider linkProvider) {
|
||||
super(deviceId, linkProvider);
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendPackage(NetworkPackage np) {
|
||||
Log.e("TcpComputerLink", "sendPackage");
|
||||
if (session == null) {
|
||||
Log.e("TcpComputerLink","not yet connected");
|
||||
return false;
|
||||
} else {
|
||||
session.write(np.serialize());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void injectNetworkPackage(NetworkPackage np) {
|
||||
packageReceived(np);
|
||||
}
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
package org.kde.connect.ComputerLinks;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.mina.core.future.ConnectFuture;
|
||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.filter.codec.textline.LineDelimiter;
|
||||
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
||||
import org.apache.mina.transport.socket.nio.NioSocketConnector;
|
||||
import org.kde.connect.LinkProviders.BaseLinkProvider;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class TcpComputerLink extends BaseComputerLink {
|
||||
|
||||
private IoSession session = null;
|
||||
|
||||
public TcpComputerLink(String deviceId, BaseLinkProvider linkProvider) {
|
||||
super(deviceId, linkProvider);
|
||||
}
|
||||
|
||||
public void connect(final InetAddress ip, final int port) {
|
||||
connect(ip,port,null,null);
|
||||
}
|
||||
|
||||
public void connect(final InetAddress ip, final int port, final Handler callback, final Handler brokenHandler) {
|
||||
|
||||
Log.e("TcpComputerLink","connect: "+ip.toString()+":"+port);
|
||||
|
||||
final NioSocketConnector connector = new NioSocketConnector();
|
||||
connector.setHandler(new IoHandlerAdapter() {
|
||||
@Override
|
||||
public void messageReceived(IoSession session, Object message) throws Exception {
|
||||
super.messageReceived(session, message);
|
||||
Log.e("TcpComputerLink","messageReceived (" + message.getClass() + ") " + message.toString());
|
||||
NetworkPackage np = null;
|
||||
try {
|
||||
//We should receive a string thanks to the TextLineCodecFactory filter
|
||||
String theMessage = (String) message;
|
||||
np = NetworkPackage.unserialize(theMessage);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("TcpComputerLink","Could not unserialize package");
|
||||
}
|
||||
if (np != null) packageReceived(np);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sessionClosed(IoSession session) throws Exception {
|
||||
super.sessionClosed(session);
|
||||
if (brokenHandler != null) brokenHandler.dispatchMessage(new Message());
|
||||
}
|
||||
});
|
||||
|
||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||
connector.getFilterChain().addLast("codec",
|
||||
new ProtocolCodecFilter(
|
||||
new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.UNIX, LineDelimiter.UNIX)
|
||||
)
|
||||
);
|
||||
connector.getSessionConfig().setKeepAlive(true);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ConnectFuture future = connector.connect(new InetSocketAddress(ip, port));
|
||||
//Wait unit it is connected (this call makes it blocking, but we are on a thread anyway)
|
||||
future.awaitUninterruptibly();
|
||||
if (!future.isConnected()) Log.e("TcpComputerLink","Could not connect");
|
||||
else Log.e("TcpComputerLink","connected");
|
||||
session = future.getSession();
|
||||
if (callback != null) callback.dispatchMessage(new Message());
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendPackage(NetworkPackage np) {
|
||||
Log.e("TcpComputerLink", "sendPackage");
|
||||
if (session == null) {
|
||||
Log.e("TcpComputerLink","not yet connected");
|
||||
return false;
|
||||
} else {
|
||||
session.write(np.serialize());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,160 +0,0 @@
|
||||
package org.kde.connect.LinkProviders;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.nsd.NsdManager;
|
||||
import android.net.nsd.NsdServiceInfo;
|
||||
import android.util.Log;
|
||||
|
||||
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.ComputerLinks.TcpComputerLink;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class AvahiTcpLinkProvider extends BaseLinkProvider {
|
||||
|
||||
private final String serviceType = "_kdeconnect._tcp";
|
||||
|
||||
private Context context;
|
||||
private NsdManager mNsdManager;
|
||||
private NsdManager.DiscoveryListener oldListener = null;
|
||||
|
||||
private HashMap<String, TcpComputerLink> visibleComputers = new HashMap<String, TcpComputerLink>();
|
||||
|
||||
public AvahiTcpLinkProvider(Context context) {
|
||||
this.context = context;
|
||||
mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
|
||||
if (oldListener != null) return;
|
||||
|
||||
Log.e("AvahiTcpLinkProvider", "Discovering computers...");
|
||||
|
||||
final NsdManager.ResolveListener mResolveListener = new NsdManager.ResolveListener() {
|
||||
|
||||
@Override
|
||||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||
Log.e("AvahiTcpLinkProvider", "Resolve failed" + errorCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
|
||||
Log.e("AvahiTcpLinkProvider", "Resolve Succeeded. " + serviceInfo);
|
||||
|
||||
try {
|
||||
Log.e("AvahiTcpLinkProvider", "Connecting and waiting identity package");
|
||||
final InetAddress host = serviceInfo.getHost();
|
||||
final int port = serviceInfo.getPort();
|
||||
final TcpComputerLink link = new TcpComputerLink("NO_DEVICE_ID_YET", AvahiTcpLinkProvider.this);
|
||||
link.addPackageReceiver(new BaseComputerLink.PackageReceiver() {
|
||||
@Override
|
||||
public void onPackageReceived(NetworkPackage np) {
|
||||
|
||||
Log.e("AvahiTcpLinkProvider", "Received package: " + np.getType());
|
||||
|
||||
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||
String id = np.getString("deviceId");
|
||||
|
||||
link.setDeviceId(id);
|
||||
link.sendPackage(NetworkPackage.createIdentityPackage(context));
|
||||
if (visibleComputers.containsKey(serviceInfo.getServiceName())) {
|
||||
Log.e("AvahiTcpLinkProvider","Removing old connection to same device");
|
||||
//Remove old connection to same host, probably down
|
||||
connectionLost(visibleComputers.get(serviceInfo.getServiceName()));
|
||||
}
|
||||
visibleComputers.put(serviceInfo.getServiceName(),link);
|
||||
connectionAccepted(np,link);
|
||||
link.removePackageReceiver(this);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
link.connect(host,port);
|
||||
} catch (Exception e) {
|
||||
Log.e("AvahiTcpLinkProvider","Exception");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
NsdManager.DiscoveryListener mDiscoveryListener = new NsdManager.DiscoveryListener() {
|
||||
|
||||
@Override
|
||||
public void onDiscoveryStarted(String regType) {
|
||||
Log.e("AvahiTcpLinkProvider", "Service discovery started");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceFound(NsdServiceInfo service) {
|
||||
Log.e("AvahiTcpLinkProvider", "Service discovery success" + service);
|
||||
|
||||
if (!service.getServiceType().startsWith(serviceType)) {
|
||||
Log.e("AvahiTcpLinkProvider", "Unknown Service Type: " + service.getServiceType());
|
||||
} else {
|
||||
Log.e("AvahiTcpLinkProvider", "Computer found, resolving...");
|
||||
mNsdManager.resolveService(service, mResolveListener);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceLost(NsdServiceInfo serviceInfo) {
|
||||
Log.e("AvahiTcpLinkProvider", "Service lost: " + serviceInfo.getServiceName());
|
||||
TcpComputerLink link = visibleComputers.remove(serviceInfo.getServiceName());
|
||||
if (link != null) connectionLost(link);
|
||||
else Log.e("AvahiTcpLinkProvider","Host unknown! (?)");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDiscoveryStopped(String serviceType) {
|
||||
Log.e("AvahiTcpLinkProvider", "Discovery stopped: " + serviceType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
||||
Log.e("AvahiTcpLinkProvider", "Discovery failed: Error code:" + errorCode);
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
||||
Log.e("AvahiTcpLinkProvider", "Discovery failed: Error code:" + errorCode);
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
}
|
||||
};
|
||||
|
||||
oldListener = mDiscoveryListener;
|
||||
mNsdManager.discoverServices(serviceType, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
|
||||
if (oldListener != null) mNsdManager.stopServiceDiscovery(oldListener);
|
||||
oldListener = null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkChange() {
|
||||
//Nothing to do, Avahi will handle it for us
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 101;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "AvahiTcpLinkProvider";
|
||||
}
|
||||
}
|
@ -1,285 +0,0 @@
|
||||
package org.kde.connect.LinkProviders;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.mina.core.future.ConnectFuture;
|
||||
import org.apache.mina.core.future.IoFuture;
|
||||
import org.apache.mina.core.future.IoFutureListener;
|
||||
import org.apache.mina.core.service.IoHandler;
|
||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.filter.codec.textline.LineDelimiter;
|
||||
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
||||
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.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.ComputerLinks.NioSessionComputerLink;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
||||
|
||||
private final static int port = 1714;
|
||||
|
||||
private Context context;
|
||||
private HashMap<String, NioSessionComputerLink> visibleComputers = new HashMap<String, NioSessionComputerLink>();
|
||||
private HashMap<Long, NioSessionComputerLink> nioSessions = new HashMap<Long, NioSessionComputerLink>();
|
||||
|
||||
private NioSocketAcceptor tcpAcceptor = null;
|
||||
private NioDatagramAcceptor udpAcceptor = null;
|
||||
|
||||
private final IoHandler tcpHandler = new IoHandlerAdapter() {
|
||||
@Override
|
||||
public void sessionClosed(IoSession session) throws Exception {
|
||||
|
||||
NioSessionComputerLink brokenLink = nioSessions.remove(session.getId());
|
||||
if (brokenLink != null) {
|
||||
connectionLost(brokenLink);
|
||||
String deviceId = brokenLink.getDeviceId();
|
||||
if (visibleComputers.get(deviceId) == brokenLink) {
|
||||
visibleComputers.remove(deviceId);
|
||||
connectionLost(brokenLink);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(IoSession session, Object message) throws Exception {
|
||||
super.messageReceived(session, message);
|
||||
|
||||
//Log.e("BroadcastTcpLinkProvider","Incoming package, address: "+session.getRemoteAddress()).toString());
|
||||
|
||||
String theMessage = (String) message;
|
||||
NetworkPackage np = NetworkPackage.unserialize(theMessage);
|
||||
|
||||
NioSessionComputerLink prevLink = nioSessions.get(session.getId());
|
||||
|
||||
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||
if (np.getString("deviceId").equals(myId)) {
|
||||
return;
|
||||
}
|
||||
NioSessionComputerLink link = new NioSessionComputerLink(session, np.getString("deviceId"), BroadcastTcpLinkProvider.this);
|
||||
nioSessions.put(session.getId(),link);
|
||||
addLink(np, link);
|
||||
} else {
|
||||
if (prevLink == null) {
|
||||
Log.e("BroadcastTcpLinkProvider","2 Expecting an identity package");
|
||||
} else {
|
||||
prevLink.injectNetworkPackage(np);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private IoHandler udpHandler = new IoHandlerAdapter() {
|
||||
@Override
|
||||
public void messageReceived(IoSession udpSession, Object message) throws Exception {
|
||||
super.messageReceived(udpSession, message);
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
||||
|
||||
NetworkPackage np = null;
|
||||
|
||||
try {
|
||||
//We should receive a string thanks to the TextLineCodecFactory filter
|
||||
String theMessage = (String) message;
|
||||
np = NetworkPackage.unserialize(theMessage);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("BroadcastTcpLinkProvider", "Could not unserialize package");
|
||||
}
|
||||
|
||||
if (np != null) {
|
||||
|
||||
final NetworkPackage identityPackage = np;
|
||||
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||
Log.e("BroadcastTcpLinkProvider", "1 Expecting an identity package");
|
||||
return;
|
||||
} else {
|
||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||
if (np.getString("deviceId").equals(myId)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "It is an identity package, creating link");
|
||||
|
||||
try {
|
||||
final InetSocketAddress address = (InetSocketAddress) udpSession.getRemoteAddress();
|
||||
|
||||
final NioSocketConnector connector = new NioSocketConnector();
|
||||
connector.setHandler(tcpHandler);
|
||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||
connector.getFilterChain().addLast("codec",
|
||||
new ProtocolCodecFilter(
|
||||
new TextLineCodecFactory(Charset.defaultCharset(), LineDelimiter.UNIX, LineDelimiter.UNIX)
|
||||
)
|
||||
);
|
||||
connector.getSessionConfig().setKeepAlive(true);
|
||||
|
||||
int tcpPort = np.getInt("tcpPort",port);
|
||||
ConnectFuture future = connector.connect(new InetSocketAddress(address.getAddress(), tcpPort));
|
||||
future.addListener(new IoFutureListener<IoFuture>() {
|
||||
@Override
|
||||
public void operationComplete(IoFuture ioFuture) {
|
||||
IoSession session = ioFuture.getSession();
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "Connection successful: " + session.isConnected());
|
||||
|
||||
NioSessionComputerLink link = new NioSessionComputerLink(session, identityPackage.getString("deviceId"), BroadcastTcpLinkProvider.this);
|
||||
|
||||
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context);
|
||||
link.sendPackage(np2);
|
||||
|
||||
nioSessions.put(session.getId(), link);
|
||||
addLink(identityPackage, link);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("BroadcastTcpLinkProvider","Exception!!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void addLink(NetworkPackage identityPackage, NioSessionComputerLink link) {
|
||||
String deviceId = identityPackage.getString("deviceId");
|
||||
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
|
||||
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
||||
visibleComputers.put(deviceId, link);
|
||||
connectionAccepted(identityPackage, link);
|
||||
if (oldLink != null) {
|
||||
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
||||
connectionLost(oldLink);
|
||||
}
|
||||
}
|
||||
|
||||
public BroadcastTcpLinkProvider(Context context) {
|
||||
|
||||
this.context = context;
|
||||
|
||||
//This handles the case when I'm the new device in the network and somebody answers my introduction package
|
||||
tcpAcceptor = new NioSocketAcceptor();
|
||||
tcpAcceptor.setHandler(tcpHandler);
|
||||
tcpAcceptor.getSessionConfig().setKeepAlive(true);
|
||||
tcpAcceptor.getSessionConfig().setReuseAddress(true);
|
||||
tcpAcceptor.setCloseOnDeactivation(false);
|
||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||
tcpAcceptor.getFilterChain().addLast("codec",
|
||||
new ProtocolCodecFilter(
|
||||
new TextLineCodecFactory(Charset.defaultCharset(), LineDelimiter.UNIX, LineDelimiter.UNIX)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
udpAcceptor = new NioDatagramAcceptor();
|
||||
udpAcceptor.getSessionConfig().setReuseAddress(true); //Share port if existing
|
||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||
udpAcceptor.getFilterChain().addLast("codec",
|
||||
new ProtocolCodecFilter(
|
||||
new TextLineCodecFactory(Charset.defaultCharset(), LineDelimiter.UNIX, LineDelimiter.UNIX)
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
|
||||
//This handles the case when I'm the existing device in the network and receive a "hello" UDP package
|
||||
|
||||
udpAcceptor.setHandler(udpHandler);
|
||||
|
||||
try {
|
||||
udpAcceptor.bind(new InetSocketAddress(port));
|
||||
} catch(Exception e) {
|
||||
Log.e("BroadcastTcpLinkProvider", "Error: Could not bind udp socket");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
boolean success = false;
|
||||
int tcpPort = port;
|
||||
while(!success) {
|
||||
try {
|
||||
tcpAcceptor.bind(new InetSocketAddress(tcpPort));
|
||||
success = true;
|
||||
} catch(Exception e) {
|
||||
tcpPort++;
|
||||
}
|
||||
}
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider","Using tcpPort "+tcpPort);
|
||||
|
||||
//I'm on a new network, let's be polite and introduce myself
|
||||
final int finalTcpPort = tcpPort;
|
||||
new AsyncTask<Void,Void,Void>() {
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
|
||||
try {
|
||||
NetworkPackage identity = NetworkPackage.createIdentityPackage(context);
|
||||
identity.set("tcpPort",finalTcpPort);
|
||||
byte[] b = identity.serialize().getBytes("UTF-8");
|
||||
DatagramPacket packet = new DatagramPacket(b, b.length, InetAddress.getByAddress(new byte[]{-1,-1,-1,-1}), port);
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setReuseAddress(true);
|
||||
socket.setBroadcast(true);
|
||||
socket.send(packet);
|
||||
Log.e("BroadcastTcpLinkProvider","Udp identity package sent");
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("BroadcastTcpLinkProvider","Sending udp identity package failed");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}.execute();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkChange() {
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider","OnNetworkChange: " + (udpAcceptor != null));
|
||||
|
||||
onStop();
|
||||
onStart();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
|
||||
udpAcceptor.unbind();
|
||||
tcpAcceptor.unbind();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "BroadcastTcpLinkProvider";
|
||||
}
|
||||
}
|
@ -17,7 +17,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.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.ComputerLinks.NioSessionComputerLink;
|
||||
import org.kde.connect.ComputerLinks.LanComputerLink;
|
||||
import org.kde.connect.NetworkPackage;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
@ -32,8 +32,8 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
private final static int port = 1714;
|
||||
|
||||
private Context context;
|
||||
private HashMap<String, NioSessionComputerLink> visibleComputers = new HashMap<String, NioSessionComputerLink>();
|
||||
private HashMap<Long, NioSessionComputerLink> nioSessions = new HashMap<Long, NioSessionComputerLink>();
|
||||
private HashMap<String, LanComputerLink> visibleComputers = new HashMap<String, LanComputerLink>();
|
||||
private HashMap<Long, LanComputerLink> nioSessions = new HashMap<Long, LanComputerLink>();
|
||||
|
||||
private NioSocketAcceptor tcpAcceptor = null;
|
||||
private NioDatagramAcceptor udpAcceptor = null;
|
||||
@ -42,7 +42,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
@Override
|
||||
public void sessionClosed(IoSession session) throws Exception {
|
||||
|
||||
NioSessionComputerLink brokenLink = nioSessions.remove(session.getId());
|
||||
LanComputerLink brokenLink = nioSessions.remove(session.getId());
|
||||
if (brokenLink != null) {
|
||||
connectionLost(brokenLink);
|
||||
String deviceId = brokenLink.getDeviceId();
|
||||
@ -58,24 +58,24 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
public void messageReceived(IoSession session, Object message) throws Exception {
|
||||
super.messageReceived(session, message);
|
||||
|
||||
//Log.e("BroadcastTcpLinkProvider","Incoming package, address: "+session.getRemoteAddress()).toString());
|
||||
//Log.e("LanLinkProvider","Incoming package, address: "+session.getRemoteAddress()).toString());
|
||||
|
||||
String theMessage = (String) message;
|
||||
NetworkPackage np = NetworkPackage.unserialize(theMessage);
|
||||
|
||||
NioSessionComputerLink prevLink = nioSessions.get(session.getId());
|
||||
LanComputerLink prevLink = nioSessions.get(session.getId());
|
||||
|
||||
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||
if (np.getString("deviceId").equals(myId)) {
|
||||
return;
|
||||
}
|
||||
NioSessionComputerLink link = new NioSessionComputerLink(session, np.getString("deviceId"), LanLinkProvider.this);
|
||||
LanComputerLink link = new LanComputerLink(session, np.getString("deviceId"), LanLinkProvider.this);
|
||||
nioSessions.put(session.getId(),link);
|
||||
addLink(np, link);
|
||||
} else {
|
||||
if (prevLink == null) {
|
||||
Log.e("BroadcastTcpLinkProvider","2 Expecting an identity package");
|
||||
Log.e("LanLinkProvider","2 Expecting an identity package");
|
||||
} else {
|
||||
prevLink.injectNetworkPackage(np);
|
||||
}
|
||||
@ -89,7 +89,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
public void messageReceived(IoSession udpSession, Object message) throws Exception {
|
||||
super.messageReceived(udpSession, message);
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
||||
Log.e("LanLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
||||
|
||||
NetworkPackage np = null;
|
||||
|
||||
@ -99,14 +99,14 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
np = NetworkPackage.unserialize(theMessage);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("BroadcastTcpLinkProvider", "Could not unserialize package");
|
||||
Log.e("LanLinkProvider", "Could not unserialize package");
|
||||
}
|
||||
|
||||
if (np != null) {
|
||||
|
||||
final NetworkPackage identityPackage = np;
|
||||
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||
Log.e("BroadcastTcpLinkProvider", "1 Expecting an identity package");
|
||||
Log.e("LanLinkProvider", "1 Expecting an identity package");
|
||||
return;
|
||||
} else {
|
||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||
@ -115,7 +115,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
}
|
||||
}
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "It is an identity package, creating link");
|
||||
Log.e("LanLinkProvider", "It is an identity package, creating link");
|
||||
|
||||
try {
|
||||
final InetSocketAddress address = (InetSocketAddress) udpSession.getRemoteAddress();
|
||||
@ -137,9 +137,9 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
public void operationComplete(IoFuture ioFuture) {
|
||||
IoSession session = ioFuture.getSession();
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider", "Connection successful: " + session.isConnected());
|
||||
Log.e("LanLinkProvider", "Connection successful: " + session.isConnected());
|
||||
|
||||
NioSessionComputerLink link = new NioSessionComputerLink(session, identityPackage.getString("deviceId"), LanLinkProvider.this);
|
||||
LanComputerLink link = new LanComputerLink(session, identityPackage.getString("deviceId"), LanLinkProvider.this);
|
||||
|
||||
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context);
|
||||
link.sendPackage(np2);
|
||||
@ -150,7 +150,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("BroadcastTcpLinkProvider","Exception!!");
|
||||
Log.e("LanLinkProvider","Exception!!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -158,14 +158,14 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
}
|
||||
};
|
||||
|
||||
private void addLink(NetworkPackage identityPackage, NioSessionComputerLink link) {
|
||||
private void addLink(NetworkPackage identityPackage, LanComputerLink link) {
|
||||
String deviceId = identityPackage.getString("deviceId");
|
||||
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
|
||||
Log.e("LanLinkProvider","addLink to "+deviceId);
|
||||
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
||||
visibleComputers.put(deviceId, link);
|
||||
connectionAccepted(identityPackage, link);
|
||||
if (oldLink != null) {
|
||||
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
||||
Log.e("LanLinkProvider","Removing old connection to same device");
|
||||
connectionLost(oldLink);
|
||||
}
|
||||
}
|
||||
@ -209,7 +209,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
try {
|
||||
udpAcceptor.bind(new InetSocketAddress(port));
|
||||
} catch(Exception e) {
|
||||
Log.e("BroadcastTcpLinkProvider", "Error: Could not bind udp socket");
|
||||
Log.e("LanLinkProvider", "Error: Could not bind udp socket");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -224,7 +224,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
}
|
||||
}
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider","Using tcpPort "+tcpPort);
|
||||
Log.e("LanLinkProvider","Using tcpPort "+tcpPort);
|
||||
|
||||
//I'm on a new network, let's be polite and introduce myself
|
||||
final int finalTcpPort = tcpPort;
|
||||
@ -241,10 +241,10 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
socket.setReuseAddress(true);
|
||||
socket.setBroadcast(true);
|
||||
socket.send(packet);
|
||||
Log.e("BroadcastTcpLinkProvider","Udp identity package sent");
|
||||
Log.e("LanLinkProvider","Udp identity package sent");
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("BroadcastTcpLinkProvider","Sending udp identity package failed");
|
||||
Log.e("LanLinkProvider","Sending udp identity package failed");
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -258,7 +258,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
@Override
|
||||
public void onNetworkChange() {
|
||||
|
||||
Log.e("BroadcastTcpLinkProvider","OnNetworkChange: " + (udpAcceptor != null));
|
||||
Log.e("LanLinkProvider","OnNetworkChange: " + (udpAcceptor != null));
|
||||
|
||||
onStop();
|
||||
onStart();
|
||||
@ -280,6 +280,6 @@ public class LanLinkProvider extends BaseLinkProvider {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "BroadcastTcpLinkProvider";
|
||||
return "LanLinkProvider";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user