forked from TheHolyWaffle/TeamSpeak-3-Java-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TS3Query.java
314 lines (271 loc) · 9.51 KB
/
TS3Query.java
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.github.theholywaffle.teamspeak3;
/*
* #%L
* TeamSpeak 3 Java API
* %%
* Copyright (C) 2014 Bert De Geyter
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import com.github.theholywaffle.teamspeak3.api.exception.TS3ConnectionFailedException;
import com.github.theholywaffle.teamspeak3.api.exception.TS3QueryShutDownException;
import com.github.theholywaffle.teamspeak3.api.reconnect.ConnectionHandler;
import com.github.theholywaffle.teamspeak3.api.reconnect.DisconnectingConnectionHandler;
import com.github.theholywaffle.teamspeak3.api.reconnect.ReconnectStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
public class TS3Query {
private static final Logger log = LoggerFactory.getLogger(TS3Query.class);
/**
* Artificial delay between sending commands, measured in milliseconds.
* <p>
* If the query's hostname / IP has not been added to the server's {@code query_ip_whitelist.txt},
* you need to use {@link FloodRate#DEFAULT} to prevent the query from being flood-banned.
* </p><p>
* Calling {@link FloodRate#custom} allows you to use a custom command delay if neither
* {@link FloodRate#UNLIMITED} nor {@link FloodRate#DEFAULT} fit your needs.
* </p>
*/
public static class FloodRate {
/**
* Default delay of 350 milliseconds between commands for queries that are not whitelisted.
*/
public static final FloodRate DEFAULT = new FloodRate(350);
/**
* No delay between commands. If a query uses this without being whitelisted, it will likely be flood-banned.
*/
public static final FloodRate UNLIMITED = new FloodRate(0);
/**
* Creates a FloodRate object that represents a custom command delay.
*
* @param milliseconds
* the delay between sending commands in milliseconds
*
* @return a new {@code FloodRate} object representing a custom delay
*/
public static FloodRate custom(int milliseconds) {
if (milliseconds < 0) throw new IllegalArgumentException("Timeout must be positive");
return new FloodRate(milliseconds);
}
private final int ms;
private FloodRate(int ms) {
this.ms = ms;
}
public int getMs() {
return ms;
}
}
/**
* The protocol used to communicate with the TeamSpeak3 server.
*/
public enum Protocol {
RAW, SSH
}
private final ConnectionHandler connectionHandler;
private final EventManager eventManager;
private final ExecutorService userThreadPool;
private final FileTransferHelper fileTransferHelper;
private final CommandQueue globalQueue;
private final TS3Config config;
private final AtomicBoolean connected = new AtomicBoolean(false);
private Connection connection;
/**
* Creates a TS3Query that connects to a TS3 server at
* {@code localhost:10011} using default settings.
*/
public TS3Query() {
this(new TS3Config());
}
/**
* Creates a customized TS3Query that connects to a server
* specified by {@code config}.
*
* @param config
* configuration for this TS3Query
*/
public TS3Query(TS3Config config) {
this.config = config.freeze();
this.eventManager = new EventManager(this);
this.userThreadPool = Executors.newCachedThreadPool();
this.fileTransferHelper = new FileTransferHelper(config.getHost());
this.connectionHandler = config.getReconnectStrategy().create(config.getConnectionHandler());
this.globalQueue = CommandQueue.newGlobalQueue(this, connectionHandler instanceof DisconnectingConnectionHandler);
}
// PUBLIC
/**
* Tries to establish a connection to the TeamSpeak3 server.
*
* @throws IllegalStateException
* if this method was called from {@link ConnectionHandler#onConnect}
* @throws TS3ConnectionFailedException
* if the query can't connect to the server or the {@link ConnectionHandler} throws an exception
*/
public void connect() {
if (Thread.holdsLock(this)) {
// Check that connect is not called from onConnect
throw new IllegalStateException("Cannot call connect from onConnect handler");
}
doConnect();
}
private synchronized void doConnect() {
if (userThreadPool.isShutdown()) {
throw new IllegalStateException("The query has already been shut down");
}
disconnect(); // If we're already connected
try {
CommandQueue queue = CommandQueue.newConnectQueue(this);
Connection con = new Connection(this, config, queue);
try {
TS3Api api = queue.getApi();
if (config.getProtocol() == Protocol.RAW && config.hasLoginCredentials()) {
api.login(config.getUsername(), config.getPassword());
}
connectionHandler.onConnect(api);
} catch (TS3QueryShutDownException e) {
// Disconnected during onConnect, re-throw as a TS3ConnectionFailedException
queue.failRemainingCommands();
throw new TS3ConnectionFailedException(e);
} catch (Exception e) {
con.disconnect();
queue.failRemainingCommands();
throw new TS3ConnectionFailedException("ConnectionHandler threw exception in connect handler", e);
}
// Reject new commands and wait until the onConnect queue is empty
queue.shutDown();
connection = con;
con.setCommandQueue(globalQueue);
connected.set(true);
} catch (TS3ConnectionFailedException conFailed) {
// If this is the first connection attempt, we won't run the handleDisconnect method,
// so we need to call shutDown from this method instead.
if (connection == null) shutDown();
throw conFailed;
}
}
/**
* Disconnects the query and closes all open resources.
* <p>
* If the command queue still contains commands when this method is called,
* the query will first process these commands, causing this method to block.
* However, the query will reject any new commands as soon as this method is called.
* </p>
*
* @throws IllegalStateException
* if this method was called from {@link ConnectionHandler#onConnect}
*/
public void exit() {
if (Thread.holdsLock(this)) {
// Check that exit is not called from onConnect
throw new IllegalStateException("Cannot call exit from onConnect handler");
}
try {
globalQueue.quit();
} finally {
shutDown();
}
}
private synchronized void shutDown() {
if (userThreadPool.isShutdown()) return;
disconnect();
globalQueue.failRemainingCommands();
userThreadPool.shutdown();
}
private synchronized void disconnect() {
if (connection == null) return;
connection.disconnect();
connected.set(false);
}
/**
* Returns {@code true} if the query is likely connected,
* {@code false} if the query is disconnected or currently trying to reconnect.
* <p>
* Note that the only way to really determine whether the query is connected or not
* is to send a command and check whether it succeeds.
* Thus this method could return {@code true} almost a minute after the connection
* has been lost, when the last keep-alive command was sent.
* </p><p>
* Please do not use this method to write your own connection handler.
* Instead, use the built-in classes in the {@code api.reconnect} package.
* </p>
*
* @return whether the query is connected or not
*
* @see TS3Config#setReconnectStrategy(ReconnectStrategy)
* @see TS3Config#setConnectionHandler(ConnectionHandler)
*/
public boolean isConnected() {
return connected.get();
}
/**
* Gets the API object that can be used to send commands to the TS3 server.
*
* @return a {@code TS3Api} object
*/
public TS3Api getApi() {
return globalQueue.getApi();
}
/**
* Gets the asynchronous API object that can be used to send commands to the TS3 server
* in a non-blocking manner.
* <p>
* Please only use the asynchronous API if it is really necessary and if you understand
* the implications of having multiple threads interact with your program.
* </p>
*
* @return a {@code TS3ApiAsync} object
*/
public TS3ApiAsync getAsyncApi() {
return globalQueue.getAsyncApi();
}
// INTERNAL
void submitUserTask(final String name, final Runnable task) {
userThreadPool.submit(() -> {
try {
task.run();
} catch (Throwable throwable) {
log.error(name + " threw an exception", throwable);
}
});
}
EventManager getEventManager() {
return eventManager;
}
FileTransferHelper getFileTransferHelper() {
return fileTransferHelper;
}
void fireDisconnect() {
connected.set(false);
submitUserTask("ConnectionHandler disconnect task", this::handleDisconnect);
}
private void handleDisconnect() {
try {
connectionHandler.onDisconnect(this);
} finally {
synchronized (this) {
if (!connected.get()) {
shutDown();
}
}
}
}
}