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

Server.java « model « humla « lublin « se « java « main « src - gitlab.com/quite/humla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: adde92a9b1a94f2e24e7cfe480647c3f6dc8af80 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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.humla.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.google.common.net.InetAddresses;

import org.minidns.hla.ResolverApi;
import org.minidns.hla.SrvResolverResult;
import org.minidns.record.SRV;
import org.minidns.util.SrvUtil;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import se.lublin.humla.Constants;

public class Server implements Parcelable {
    private long mId;
    private String mName;
    private String mHost;
    private int mPort;
    private String mUsername;
    private String mPassword;

    private String mResolvedHost = null;
    private int mResolvedPort;

    public static final Parcelable.Creator<Server> CREATOR = new Parcelable.Creator<Server>() {

        @Override
        public Server createFromParcel(Parcel parcel) {
            return new Server(parcel);
        }

        @Override
        public Server[] newArray(int i) {
            return new Server[i];
        }
    };

    public Server(long id, String name, String host, int port, String username, String password) {
        mId = id;
        mName = name;
        mHost = host;
        mPort = port;
        mUsername = username;
        mPassword = password;
        mResolvedHost = null;
    }

    private Server(Parcel in) {
        readFromParcel(in);
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(mId);
        parcel.writeString(mName);
        parcel.writeString(mHost);
        parcel.writeInt(mPort);
        parcel.writeString(mUsername);
        parcel.writeString(mPassword);
    }

    private void readFromParcel(Parcel in) {
        mId = in.readLong();
        mName = in.readString();
        mHost = in.readString();
        mPort = in.readInt();
        mUsername = in.readString();
        mPassword = in.readString();
        mResolvedHost = null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        mId = id;
    }

    /**
     * Returns a user-defined name for the server, or the host if the user-defined name is not set.
     * @return A user readable name for the server.
     */
    public String getName() {
        return (mName != null && mName.length() > 0) ? mName : mHost;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public String getHost() {
        return mHost;
    }

    public void setHost(String mHost) {
        this.mHost = mHost;
        this.mResolvedHost = null;
    }

    public int getPort() {
        return mPort;
    }

    public void setPort(int mPort) {
        this.mPort = mPort;
        this.mResolvedHost = null;
    }

    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String mUsername) {
        this.mUsername = mUsername;
    }

    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String mPassword) {
        this.mPassword = mPassword;
    }

    /**
     * Returns whether or not the server is stored in a database.
     * @return true if the server's ID is in the database.
     */
    public boolean isSaved() {
        return mId != -1;
    }

    public String getSrvHost() {
        srvResolve();
        return mResolvedHost;
    }

    public int getSrvPort() {
        srvResolve();
        return mResolvedPort;
    }

    private synchronized void srvResolve() {
        if (mResolvedHost != null) {
            return;
        }
        // if we have a port then don't bother with SRV
        if (mPort != 0) {
            mResolvedHost = mHost;
            mResolvedPort = mPort;
            return;
        }
        if (InetAddresses.isInetAddress(mHost)) {
            mResolvedHost = mHost;
            mResolvedPort = Constants.DEFAULT_PORT;
            return;
        }
        // set to our fallback values in case of no SRV or resolve fail
        final AtomicReference<String> srvHost = new AtomicReference<>(mHost);
        final AtomicInteger srvPort = new AtomicInteger(Constants.DEFAULT_PORT);
        try {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        final String lookup = "_mumble._tcp." + srvHost.get();
                        SrvResolverResult res = ResolverApi.INSTANCE.resolveSrv(lookup);
                        if (!res.wasSuccessful()) {
                            Log.d(Constants.TAG, "resolveSrv " + lookup + ": " + res.getResponseCode());
                            return;
                        }
                        Set<SRV> answers = res.getAnswersOrEmptySet();
                        if (answers.isEmpty()) {
                            Log.d(Constants.TAG, "resolveSrv " + lookup + ": empty answer");
                            return;
                        }
                        List<SRV> srvs = SrvUtil.sortSrvRecords(answers);
                        for (SRV srv : srvs) {
                            Log.d(Constants.TAG, "resolved " + lookup + " SRV: " + srv.toString());
                            srvHost.set(srv.target.toString());
                            srvPort.set(srv.port);
                            // TODO SRV just picking the first record.
                            return;
                        }
                    } catch (IOException e) {
                        Log.d(Constants.TAG, "resolveSRV() run() " + e);
                    }
                }
            });
            t.start();
            t.join();
        }
        catch (Exception e) {
            Log.d(Constants.TAG, "resolveSRV() " + e);
        }
        mResolvedHost = srvHost.get();
        mResolvedPort = srvPort.get();
    }
}