Search in sources :

Example 6 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanLinkProvider method udpPacketReceived.

// I've received their broadcast and should connect to their TCP socket and send my identity.
private void udpPacketReceived(DatagramPacket packet) {
    final InetAddress address = packet.getAddress();
    try {
        String message = new String(packet.getData(), Charsets.UTF_8);
        final NetworkPacket identityPacket = NetworkPacket.unserialize(message);
        final String deviceId = identityPacket.getString("deviceId");
        if (!identityPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
            Log.e("KDE/LanLinkProvider", "Expecting an UDP identity packet");
            return;
        } else {
            String myId = DeviceHelper.getDeviceId(context);
            if (deviceId.equals(myId)) {
                // Ignore my own broadcast
                return;
            }
        }
        Log.i("KDE/LanLinkProvider", "Broadcast identity packet received from " + identityPacket.getString("deviceName"));
        int tcpPort = identityPacket.getInt("tcpPort", MIN_PORT);
        SocketFactory socketFactory = SocketFactory.getDefault();
        Socket socket = socketFactory.createSocket(address, tcpPort);
        configureSocket(socket);
        OutputStream out = socket.getOutputStream();
        NetworkPacket myIdentity = NetworkPacket.createIdentityPacket(context);
        out.write(myIdentity.serialize().getBytes());
        out.flush();
        identityPacketReceived(identityPacket, socket, LanLink.ConnectionStarted.Remotely);
    } catch (Exception e) {
        Log.e("KDE/LanLinkProvider", "Cannot connect to " + address, e);
        if (!reverseConnectionBlackList.contains(address)) {
            Log.w("KDE/LanLinkProvider", "Blacklisting " + address);
            reverseConnectionBlackList.add(address);
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    reverseConnectionBlackList.remove(address);
                }
            }, 5 * 1000);
            // Try to cause a reverse connection
            onNetworkChange();
        }
    }
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) NetworkPacket(org.kde.kdeconnect.NetworkPacket) InetAddress(java.net.InetAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 7 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanLinkProvider method tcpPacketReceived.

// They received my UDP broadcast and are connecting to me. The first thing they sned should be their identity.
private void tcpPacketReceived(Socket socket) {
    NetworkPacket networkPacket;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = reader.readLine();
        networkPacket = NetworkPacket.unserialize(message);
    // Log.e("TcpListener","Received TCP package: "+networkPacket.serialize());
    } catch (Exception e) {
        Log.e("KDE/LanLinkProvider", "Exception while receiving TCP packet", e);
        return;
    }
    if (!networkPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
        Log.e("KDE/LanLinkProvider", "Expecting an identity packet instead of " + networkPacket.getType());
        return;
    }
    Log.i("KDE/LanLinkProvider", "identity packet received from a TCP connection from " + networkPacket.getString("deviceName"));
    identityPacketReceived(networkPacket, socket, LanLink.ConnectionStarted.Locally);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) NetworkPacket(org.kde.kdeconnect.NetworkPacket) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 8 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class ClipboardPlugin method propagateClipboard.

void propagateClipboard(String content) {
    NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD);
    np.set("content", content);
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 9 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanPairingHandler method unpair.

@Override
public void unpair() {
    mPairStatus = PairStatus.NotPaired;
    NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
    np.set("pair", false);
    mDevice.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 10 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanPairingHandler method rejectPairing.

@Override
public void rejectPairing() {
    hidePairingNotification();
    mPairStatus = PairStatus.NotPaired;
    NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
    np.set("pair", false);
    mDevice.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Aggregations

NetworkPacket (org.kde.kdeconnect.NetworkPacket)77 IOException (java.io.IOException)6 SSLSocket (javax.net.ssl.SSLSocket)4 SMSHelper (org.kde.kdeconnect.Helpers.SMSHelper)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 SocketException (java.net.SocketException)3 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 SmsMessage (android.telephony.SmsMessage)2 SpannableString (android.text.SpannableString)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2