mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 05:37:43 +00:00
BroadcastTcpLinkProvider now only uses NioSessionComputerLinks
This commit is contained in:
parent
158d57153a
commit
a3c9893afd
@ -7,7 +7,11 @@ import android.os.Looper;
|
|||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.util.Log;
|
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.future.ReadFuture;
|
||||||
|
import org.apache.mina.core.service.IoHandler;
|
||||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||||
import org.apache.mina.core.session.IoSession;
|
import org.apache.mina.core.session.IoSession;
|
||||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||||
@ -15,6 +19,7 @@ import org.apache.mina.filter.codec.textline.LineDelimiter;
|
|||||||
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
|
||||||
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
|
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.kde.connect.ComputerLinks.BaseComputerLink;
|
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||||
import org.kde.connect.ComputerLinks.NioSessionComputerLink;
|
import org.kde.connect.ComputerLinks.NioSessionComputerLink;
|
||||||
import org.kde.connect.ComputerLinks.TcpComputerLink;
|
import org.kde.connect.ComputerLinks.TcpComputerLink;
|
||||||
@ -30,34 +35,7 @@ import java.util.HashMap;
|
|||||||
public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
||||||
|
|
||||||
private final static int port = 1714;
|
private final static int port = 1714;
|
||||||
|
private final IoHandler tcpHandler = new IoHandlerAdapter() {
|
||||||
Context context;
|
|
||||||
HashMap<String, BaseComputerLink> visibleComputers = new HashMap<String, BaseComputerLink>();
|
|
||||||
HashMap<String, NioSessionComputerLink> nioSessions = new HashMap<String, NioSessionComputerLink>();
|
|
||||||
private boolean started = false;
|
|
||||||
|
|
||||||
NioSocketAcceptor tcpAcceptor = null;
|
|
||||||
NioDatagramAcceptor updAcceptor = null;
|
|
||||||
|
|
||||||
private void addLink(NetworkPackage identityPackage, BaseComputerLink link) {
|
|
||||||
Log.e("BroadcastTcpLinkProvider","addLink to "+identityPackage.getString("deviceName"));
|
|
||||||
String deviceId = identityPackage.getString("deviceId");
|
|
||||||
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
|
||||||
if (oldLink != null) {
|
|
||||||
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
|
||||||
connectionLost(oldLink);
|
|
||||||
}
|
|
||||||
visibleComputers.put(deviceId, link);
|
|
||||||
connectionAccepted(identityPackage, link);
|
|
||||||
}
|
|
||||||
|
|
||||||
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(new IoHandlerAdapter() {
|
|
||||||
@Override
|
@Override
|
||||||
public void sessionClosed(IoSession session) throws Exception {
|
public void sessionClosed(IoSession session) throws Exception {
|
||||||
|
|
||||||
@ -68,6 +46,7 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
|||||||
String id = brokenLink.getDeviceId();
|
String id = brokenLink.getDeviceId();
|
||||||
visibleComputers.remove(id);
|
visibleComputers.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -95,7 +74,35 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
Context context;
|
||||||
|
HashMap<String, BaseComputerLink> visibleComputers = new HashMap<String, BaseComputerLink>();
|
||||||
|
HashMap<String, NioSessionComputerLink> nioSessions = new HashMap<String, NioSessionComputerLink>();
|
||||||
|
private boolean started = false;
|
||||||
|
|
||||||
|
NioSocketAcceptor tcpAcceptor = null;
|
||||||
|
NioDatagramAcceptor updAcceptor = null;
|
||||||
|
|
||||||
|
private void addLink(NetworkPackage identityPackage, BaseComputerLink link) {
|
||||||
|
Log.e("BroadcastTcpLinkProvider","addLink to "+identityPackage.getString("deviceName"));
|
||||||
|
String deviceId = identityPackage.getString("deviceId");
|
||||||
|
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
||||||
|
if (oldLink != null) {
|
||||||
|
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
||||||
|
connectionLost(oldLink);
|
||||||
|
}
|
||||||
|
visibleComputers.put(deviceId, link);
|
||||||
|
connectionAccepted(identityPackage, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
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().setKeepAlive(true);
|
||||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||||
tcpAcceptor.getFilterChain().addLast("codec",
|
tcpAcceptor.getFilterChain().addLast("codec",
|
||||||
@ -120,12 +127,12 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
|||||||
//This handles the case when I'm the existing device in the network and receive a "hello" UDP package
|
//This handles the case when I'm the existing device in the network and receive a "hello" UDP package
|
||||||
updAcceptor = new NioDatagramAcceptor();
|
updAcceptor = new NioDatagramAcceptor();
|
||||||
updAcceptor.getSessionConfig().setReuseAddress(true); //Share port if existing
|
updAcceptor.getSessionConfig().setReuseAddress(true); //Share port if existing
|
||||||
updAcceptor.setHandler(new IoHandlerAdapter(){
|
updAcceptor.setHandler(new IoHandlerAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(IoSession session, Object message) throws Exception {
|
public void messageReceived(IoSession session, Object message) throws Exception {
|
||||||
super.messageReceived(session, message);
|
super.messageReceived(session, message);
|
||||||
|
|
||||||
Log.e("BroadcastTcpLinkProvider","Udp message received (" + message.getClass() + ") " + message.toString());
|
Log.e("BroadcastTcpLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
||||||
|
|
||||||
NetworkPackage np = null;
|
NetworkPackage np = null;
|
||||||
|
|
||||||
@ -135,74 +142,52 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
|||||||
np = NetworkPackage.unserialize(theMessage);
|
np = NetworkPackage.unserialize(theMessage);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Log.e("BroadcastTcpLinkProvider","Could not unserialize package");
|
Log.e("BroadcastTcpLinkProvider", "Could not unserialize package");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (np != null) {
|
if (np != null) {
|
||||||
|
|
||||||
final NetworkPackage identityPackage = np;
|
final NetworkPackage identityPackage = np;
|
||||||
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||||
Log.e("BroadcastTcpLinkProvider","1 Expecting an identity package");
|
Log.e("BroadcastTcpLinkProvider", "1 Expecting an identity package");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.e("BroadcastTcpLinkProvider","It is an identity package, creating link");
|
Log.e("BroadcastTcpLinkProvider", "It is an identity package, creating link");
|
||||||
|
|
||||||
final TcpComputerLink link = new TcpComputerLink(np.getString("deviceId"), BroadcastTcpLinkProvider.this);
|
|
||||||
|
|
||||||
final InetSocketAddress address = (InetSocketAddress) session.getRemoteAddress();
|
final InetSocketAddress address = (InetSocketAddress) session.getRemoteAddress();
|
||||||
|
|
||||||
//This handler inside a thread with a looper inside a handler is ultra hackish
|
final NioSocketConnector connector = new NioSocketConnector();
|
||||||
new Thread(new Runnable() {
|
connector.setHandler(tcpHandler);
|
||||||
@Override
|
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||||
public void run() {
|
connector.getFilterChain().addLast("codec",
|
||||||
try {
|
new ProtocolCodecFilter(
|
||||||
Looper l = Looper.myLooper();
|
new TextLineCodecFactory(Charset.defaultCharset(), LineDelimiter.UNIX, LineDelimiter.UNIX)
|
||||||
if (l != null) l.quit();
|
)
|
||||||
try {
|
);
|
||||||
Looper.prepare();
|
connector.getSessionConfig().setKeepAlive(true);
|
||||||
} catch(Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("BroadcastTcpLinkProvider","Looper prepare exception");
|
|
||||||
}
|
|
||||||
final Looper l2 = Looper.myLooper();
|
|
||||||
link.connect(address.getAddress(), port, new Handler() {
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
|
|
||||||
Log.e("BroadcastTcpLinkProvider","Link established, sending own identity");
|
ConnectFuture future = connector.connect(new InetSocketAddress(address.getAddress(), port));
|
||||||
|
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);
|
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context);
|
||||||
link.sendPackage(np2);
|
link.sendPackage(np2);
|
||||||
|
|
||||||
|
nioSessions.put(address.toString(),link);
|
||||||
addLink(identityPackage, link);
|
addLink(identityPackage, link);
|
||||||
|
|
||||||
l2.quit();
|
|
||||||
}
|
|
||||||
}, new Handler() {
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
connectionLost(link);
|
|
||||||
visibleComputers.remove(identityPackage.getString("deviceId"));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Looper.loop();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("BroadcastTcpLinkProvider","Exception");
|
|
||||||
Looper l = Looper.myLooper();
|
|
||||||
if (l != null) l.quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}).run();
|
|
||||||
|
|
||||||
Log.e("DONE","DONE0");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Log.e("DONE","DONE2");
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
//TextLineCodecFactory will split incoming data delimited by the given string
|
//TextLineCodecFactory will split incoming data delimited by the given string
|
||||||
updAcceptor.getFilterChain().addLast("codec",
|
updAcceptor.getFilterChain().addLast("codec",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user