Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerInfoTask.java « servers « mumla « lublin « se « java « main « src « app - gitlab.com/quite/mumla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80cccb9730bb5253c6bbc12c7a6e38312d8687cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2014 Andrew Comminos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.lublin.mumla.servers;

import android.os.AsyncTask;
import android.util.Log;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

import se.lublin.humla.model.Server;
import se.lublin.mumla.Constants;

/**
 * Pings the requested server and returns a ServerInfoResponse.
 * Will return a 'dummy' ServerInfoResponse in the case of failure.
 * @author morlunk
 *
 */
public class ServerInfoTask extends AsyncTask<Server, Void, ServerInfoResponse> {
    private Server server;

    @Override
    protected ServerInfoResponse doInBackground(Server... params) {
        server = params[0];
        try {
            // Create ping message
            ByteBuffer buffer = ByteBuffer.allocate(12);
            buffer.putInt(0); // Request type
            buffer.putLong(server.getId()); // Identifier
            DatagramPacket requestPacket = new DatagramPacket(buffer.array(), 12,
                    InetAddress.getByName(server.getSrvHost()), server.getSrvPort());

            // Send packet and wait for response
            DatagramSocket socket = new DatagramSocket();
            socket.setSoTimeout(1000);
            socket.setReceiveBufferSize(1024);

            long startTime = System.nanoTime();

            socket.send(requestPacket);

            byte[] responseBuffer = new byte[24];
            DatagramPacket responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length);
            socket.receive(responsePacket);

            int latencyInMs = (int) ((System.nanoTime()-startTime)/1000000);

            ServerInfoResponse response = new ServerInfoResponse(server, responseBuffer, latencyInMs);

            Log.d(Constants.TAG, "Server version: " + response.getVersionString()
                    + " Users: " + response.getCurrentUsers() + "/" + response.getMaximumUsers());

            return response;

        } catch (Exception e) {
//            e.printStackTrace();
        }

        return new ServerInfoResponse(); // Return dummy in case of failure
    }

}