001package com.github.theholywaffle.teamspeak3;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2014 Bert De Geyter
008 * %%
009 * Permission is hereby granted, free of charge, to any person obtaining a copy
010 * of this software and associated documentation files (the "Software"), to deal
011 * in the Software without restriction, including without limitation the rights
012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
013 * copies of the Software, and to permit persons to whom the Software is
014 * furnished to do so, subject to the following conditions:
015 * 
016 * The above copyright notice and this permission notice shall be included in
017 * all copies or substantial portions of the Software.
018 * 
019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
025 * THE SOFTWARE.
026 * #L%
027 */
028
029import com.github.theholywaffle.teamspeak3.TS3Query.FloodRate;
030import com.github.theholywaffle.teamspeak3.api.reconnect.ConnectionHandler;
031import com.github.theholywaffle.teamspeak3.api.reconnect.ReconnectStrategy;
032
033public class TS3Config {
034
035        private String host = null;
036        private int queryPort = 10011;
037        private FloodRate floodRate = FloodRate.DEFAULT;
038        private boolean enableCommunicationsLogging = false;
039        private int commandTimeout = 4000;
040        private ReconnectStrategy reconnectStrategy = ReconnectStrategy.disconnect();
041        private ConnectionHandler connectionHandler = null;
042
043        public TS3Config setHost(String host) {
044                this.host = host;
045                return this;
046        }
047
048        String getHost() {
049                return host;
050        }
051
052        public TS3Config setQueryPort(int queryPort) {
053                if (queryPort <= 0 || queryPort > 65535) {
054                        throw new IllegalArgumentException("Port out of range: " + queryPort);
055                }
056                this.queryPort = queryPort;
057                return this;
058        }
059
060        int getQueryPort() {
061                return queryPort;
062        }
063
064        public TS3Config setFloodRate(FloodRate rate) {
065                if (rate == null) throw new IllegalArgumentException("rate cannot be null!");
066                this.floodRate = rate;
067                return this;
068        }
069
070        FloodRate getFloodRate() {
071                return floodRate;
072        }
073
074        /**
075         * Setting this value to {@code true} will log the communication between the
076         * query client and the TS3 server at the {@code DEBUG} level.
077         * <p>
078         * By default, this is turned off to prevent leaking IPs, tokens, passwords, etc.
079         * into the console and / or log files.
080         * </p>
081         *
082         * @param enable
083         *              whether to log query commands
084         *
085         * @return this TS3Config object for chaining
086         */
087        public TS3Config setEnableCommunicationsLogging(boolean enable) {
088                enableCommunicationsLogging = enable;
089                return this;
090        }
091
092        boolean getEnableCommunicationsLogging() {
093                return enableCommunicationsLogging;
094        }
095
096        /**
097         * Sets how many milliseconds a call in {@link TS3Api} should block at most until a command
098         * without response fails. By default, this timeout is 4000 milliseconds.
099         *
100         * @param commandTimeout
101         *              the maximum time to wait for a response until a synchronous command call fails
102         *
103         * @return this TS3Config object for chaining
104         *
105         * @throws IllegalArgumentException
106         *              if the timeout value is smaller than or equal to {@code 0}
107         */
108        public TS3Config setCommandTimeout(int commandTimeout) {
109                if (commandTimeout <= 0) {
110                        throw new IllegalArgumentException("Timeout value must be greater than 0");
111                }
112
113                this.commandTimeout = commandTimeout;
114                return this;
115        }
116
117        int getCommandTimeout() {
118                return commandTimeout;
119        }
120
121        public TS3Config setReconnectStrategy(ReconnectStrategy reconnectStrategy) {
122                if (reconnectStrategy == null) throw new IllegalArgumentException("reconnectStrategy cannot be null!");
123                this.reconnectStrategy = reconnectStrategy;
124                return this;
125        }
126
127        ReconnectStrategy getReconnectStrategy() {
128                return reconnectStrategy;
129        }
130
131        public TS3Config setConnectionHandler(ConnectionHandler connectionHandler) {
132                this.connectionHandler = connectionHandler;
133                return this;
134        }
135
136        ConnectionHandler getConnectionHandler() {
137                return connectionHandler;
138        }
139}