2009-02-09 01:31:50 +00:00
|
|
|
/*
|
2012-06-04 13:31:44 -07:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-02-21 19:08:08 +00:00
|
|
|
*
|
2011-12-09 14:18:34 +09:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2009-02-09 01:31:50 +00:00
|
|
|
*
|
2012-06-04 13:31:44 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-02-09 01:31:50 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 14:18:34 +09:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 07:15:49 +00:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-02-09 01:31:50 +00:00
|
|
|
*/
|
2011-12-09 12:38:59 +09:00
|
|
|
package io.netty.channel;
|
2009-02-09 01:31:50 +00:00
|
|
|
|
2012-05-01 18:17:12 +09:00
|
|
|
import io.netty.channel.socket.SocketChannelConfig;
|
|
|
|
|
2012-05-13 00:40:28 +09:00
|
|
|
import java.util.IdentityHashMap;
|
2009-02-09 01:31:50 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2012-11-12 09:31:40 +09:00
|
|
|
import static io.netty.channel.ChannelOption.*;
|
|
|
|
|
2009-02-09 01:31:50 +00:00
|
|
|
/**
|
2009-02-21 19:08:08 +00:00
|
|
|
* The default {@link SocketChannelConfig} implementation.
|
2009-02-09 01:31:50 +00:00
|
|
|
*/
|
2009-02-21 19:08:08 +00:00
|
|
|
public class DefaultChannelConfig implements ChannelConfig {
|
2012-05-11 20:44:00 +09:00
|
|
|
|
|
|
|
private static final int DEFAULT_CONNECT_TIMEOUT = 30000;
|
|
|
|
|
|
|
|
private volatile int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT;
|
2012-05-13 00:40:28 +09:00
|
|
|
private volatile int writeSpinCount = 16;
|
2012-05-11 20:44:00 +09:00
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-13 00:40:28 +09:00
|
|
|
public Map<ChannelOption<?>, Object> getOptions() {
|
|
|
|
return getOptions(null, CONNECT_TIMEOUT_MILLIS, WRITE_SPIN_COUNT);
|
|
|
|
}
|
|
|
|
|
2012-06-08 19:28:12 +09:00
|
|
|
protected Map<ChannelOption<?>, Object> getOptions(
|
|
|
|
Map<ChannelOption<?>, Object> result, ChannelOption<?>... options) {
|
2012-05-13 00:40:28 +09:00
|
|
|
if (result == null) {
|
|
|
|
result = new IdentityHashMap<ChannelOption<?>, Object>();
|
2009-02-09 01:31:50 +00:00
|
|
|
}
|
2012-05-13 00:40:28 +09:00
|
|
|
for (ChannelOption<?> o: options) {
|
|
|
|
result.put(o, getOption(o));
|
|
|
|
}
|
|
|
|
return result;
|
2009-02-09 01:31:50 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:33:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-13 00:40:28 +09:00
|
|
|
public boolean setOptions(Map<ChannelOption<?>, ?> options) {
|
|
|
|
if (options == null) {
|
|
|
|
throw new NullPointerException("options");
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean setAllOptions = true;
|
|
|
|
for (Entry<ChannelOption<?>, ?> e: options.entrySet()) {
|
|
|
|
if (!setOption((ChannelOption<Object>) e.getKey(), e.getValue())) {
|
|
|
|
setAllOptions = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return setAllOptions;
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:33:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2012-05-13 00:40:28 +09:00
|
|
|
@Override
|
|
|
|
public <T> T getOption(ChannelOption<T> option) {
|
|
|
|
if (option == null) {
|
|
|
|
throw new NullPointerException("option");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option == CONNECT_TIMEOUT_MILLIS) {
|
|
|
|
return (T) Integer.valueOf(getConnectTimeoutMillis());
|
2012-11-12 09:31:40 +09:00
|
|
|
}
|
|
|
|
if (option == WRITE_SPIN_COUNT) {
|
2012-05-13 00:40:28 +09:00
|
|
|
return (T) Integer.valueOf(getWriteSpinCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> boolean setOption(ChannelOption<T> option, T value) {
|
|
|
|
validate(option, value);
|
|
|
|
|
|
|
|
if (option == CONNECT_TIMEOUT_MILLIS) {
|
|
|
|
setConnectTimeoutMillis((Integer) value);
|
|
|
|
} else if (option == WRITE_SPIN_COUNT) {
|
|
|
|
setWriteSpinCount((Integer) value);
|
2012-05-11 20:44:00 +09:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-13 00:40:28 +09:00
|
|
|
|
2012-05-11 20:44:00 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:40:28 +09:00
|
|
|
protected <T> void validate(ChannelOption<T> option, T value) {
|
|
|
|
if (option == null) {
|
|
|
|
throw new NullPointerException("option");
|
|
|
|
}
|
|
|
|
option.validate(value);
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:44:00 +09:00
|
|
|
@Override
|
|
|
|
public int getConnectTimeoutMillis() {
|
|
|
|
return connectTimeoutMillis;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setConnectTimeoutMillis(int connectTimeoutMillis) {
|
|
|
|
if (connectTimeoutMillis < 0) {
|
|
|
|
throw new IllegalArgumentException(String.format(
|
|
|
|
"connectTimeoutMillis: %d (expected: >= 0)", connectTimeoutMillis));
|
|
|
|
}
|
|
|
|
this.connectTimeoutMillis = connectTimeoutMillis;
|
2009-02-09 01:31:50 +00:00
|
|
|
}
|
2012-05-13 00:40:28 +09:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWriteSpinCount() {
|
|
|
|
return writeSpinCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setWriteSpinCount(int writeSpinCount) {
|
|
|
|
if (writeSpinCount <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"writeSpinCount must be a positive integer.");
|
|
|
|
}
|
|
|
|
this.writeSpinCount = writeSpinCount;
|
|
|
|
}
|
2009-02-09 01:31:50 +00:00
|
|
|
}
|