2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

BroadcastTcpLinkProvider now only uses NioSessionComputerLinks

This commit is contained in:
Albert Vaca 2013-08-07 12:23:52 +02:00
parent 158d57153a
commit a3c9893afd

View File

@ -7,7 +7,11 @@ import android.os.Looper;
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.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
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.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.TcpComputerLink;
@ -30,34 +35,7 @@ import java.util.HashMap;
public class BroadcastTcpLinkProvider extends BaseLinkProvider {
private final static int port = 1714;
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() {
private final IoHandler tcpHandler = new IoHandlerAdapter() {
@Override
public void sessionClosed(IoSession session) throws Exception {
@ -68,6 +46,7 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
String id = brokenLink.getDeviceId();
visibleComputers.remove(id);
}
}
@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);
//TextLineCodecFactory will split incoming data delimited by the given string
tcpAcceptor.getFilterChain().addLast("codec",
@ -148,61 +155,39 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
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();
//This handler inside a thread with a looper inside a handler is ultra hackish
new Thread(new Runnable() {
@Override
public void run() {
try {
Looper l = Looper.myLooper();
if (l != null) l.quit();
try {
Looper.prepare();
} 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) {
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);
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);
link.sendPackage(np2);
nioSessions.put(address.toString(),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
updAcceptor.getFilterChain().addLast("codec",