Add ClientChannelConfig and move connectTimeoutMillis there

This commit is contained in:
Trustin Lee 2012-05-01 18:17:12 +09:00
parent f4423ac555
commit 1b5960a1a8
6 changed files with 58 additions and 182 deletions

View File

@ -35,6 +35,17 @@ public final class ConversionUtil {
}
}
/**
* Converts the specified object into a long integer.
*/
public static long toLong(Object value) {
if (value instanceof Number) {
return ((Number) value).longValue();
} else {
return Long.parseLong(String.valueOf(value));
}
}
/**
* Converts the specified object into a boolean.
*/

View File

@ -15,7 +15,6 @@
*/
package io.netty.channel;
import io.netty.buffer.ChannelBufferFactory;
import io.netty.channel.socket.SocketChannelConfig;
import io.netty.channel.socket.nio.NioSocketChannelConfig;
@ -39,23 +38,10 @@ import java.util.Map;
* the configuration of a {@link Channel} without down-casting its associated
* {@link ChannelConfig}. To update an option map, please call {@link #setOptions(Map)}.
* <p>
* All {@link ChannelConfig} has the following options:
*
* <table border="1" cellspacing="0" cellpadding="6">
* <tr>
* <th>Name</th><th>Associated setter method</th>
* </tr><tr>
* <td>{@code "bufferFactory"}</td><td>{@link #setBufferFactory(ChannelBufferFactory)}</td>
* </tr><tr>
* <td>{@code "connectTimeoutMillis"}</td><td>{@link #setConnectTimeoutMillis(int)}</td>
* </tr><tr>
* <td>{@code "pipelineFactory"}</td><td>{@link #setPipelineFactory(ChannelPipelineFactory)}</td>
* </tr>
* </table>
* <p>
* More options are available in the sub-types of {@link ChannelConfig}. For
* Options are available in the sub-types of {@link ChannelConfig}. For
* example, you can configure the parameters which are specific to a TCP/IP
* socket as explained in {@link SocketChannelConfig} or {@link NioSocketChannelConfig}.
*
* @apiviz.has io.netty.channel.ChannelPipelineFactory
* @apiviz.composedOf io.netty.channel.ReceiveBufferSizePredictor
*
@ -89,23 +75,4 @@ public interface ChannelConfig {
* @return {@code true} if and only if the property has been set
*/
boolean setOption(String name, Object value);
/**
* Returns the connect timeout of the channel in milliseconds. If the
* {@link Channel} does not support connect operation, this property is not
* used at all, and therefore will be ignored.
*
* @return the connect timeout in milliseconds. {@code 0} if disabled.
*/
int getConnectTimeoutMillis();
/**
* Sets the connect timeout of the channel in milliseconds. If the
* {@link Channel} does not support connect operation, this property is not
* used at all, and therefore will be ignored.
*
* @param connectTimeoutMillis the connect timeout in milliseconds.
* {@code 0} to disable.
*/
void setConnectTimeoutMillis(int connectTimeoutMillis);
}

View File

@ -0,0 +1,6 @@
package io.netty.channel;
public interface ClientChannelConfig extends ChannelConfig {
long getConnectTimeoutMillis();
void setConnectTimeoutMillis(long connectTimeoutMillis);
}

View File

@ -15,22 +15,15 @@
*/
package io.netty.channel;
import io.netty.channel.socket.SocketChannelConfig;
import java.util.Map;
import java.util.Map.Entry;
import io.netty.buffer.ChannelBufferFactory;
import io.netty.buffer.HeapChannelBufferFactory;
import io.netty.channel.socket.SocketChannelConfig;
import io.netty.util.internal.ConversionUtil;
/**
* The default {@link SocketChannelConfig} implementation.
*/
public class DefaultChannelConfig implements ChannelConfig {
private volatile ChannelBufferFactory bufferFactory = HeapChannelBufferFactory.getInstance();
private volatile int connectTimeoutMillis = 10000; // 10 seconds
@Override
public void setOptions(Map<String, Object> options) {
for (Entry<String, Object> e: options.entrySet()) {
@ -40,51 +33,6 @@ public class DefaultChannelConfig implements ChannelConfig {
@Override
public boolean setOption(String key, Object value) {
if (key.equals("pipelineFactory")) {
setPipelineFactory((ChannelPipelineFactory) value);
} else if (key.equals("connectTimeoutMillis")) {
setConnectTimeoutMillis(ConversionUtil.toInt(value));
} else if (key.equals("bufferFactory")) {
setBufferFactory((ChannelBufferFactory) value);
} else {
return false;
}
return true;
}
@Override
public int getConnectTimeoutMillis() {
return connectTimeoutMillis;
}
@Override
public ChannelBufferFactory getBufferFactory() {
return bufferFactory;
}
@Override
public void setBufferFactory(ChannelBufferFactory bufferFactory) {
if (bufferFactory == null) {
throw new NullPointerException("bufferFactory");
}
this.bufferFactory = bufferFactory;
}
@Override
public ChannelPipelineFactory getPipelineFactory() {
return null;
}
@Override
public void setConnectTimeoutMillis(int connectTimeoutMillis) {
if (connectTimeoutMillis < 0) {
throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
}
this.connectTimeoutMillis = connectTimeoutMillis;
}
@Override
public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
// Unused
}
}

View File

@ -0,0 +1,36 @@
package io.netty.channel;
import io.netty.util.internal.ConversionUtil;
import java.util.concurrent.TimeUnit;
public class DefaultClientChannelConfig extends DefaultChannelConfig implements
ClientChannelConfig {
private static final long DEFAULT_CONNECT_TIMEOUT = TimeUnit.SECONDS.toMillis(30);
private volatile long connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT;
@Override
public boolean setOption(String key, Object value) {
if ("connectTimeoutMillis".equals(key)) {
setConnectTimeoutMillis(ConversionUtil.toLong(value));
} else {
return false;
}
return true;
}
@Override
public long getConnectTimeoutMillis() {
return connectTimeoutMillis;
}
@Override
public void setConnectTimeoutMillis(long connectTimeoutMillis) {
if (connectTimeoutMillis < 0) {
throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
}
this.connectTimeoutMillis = connectTimeoutMillis;
}
}

View File

@ -1,92 +0,0 @@
/*
* Copyright 2011 The Netty Project
*
* 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;
import java.util.Map;
import java.util.Map.Entry;
import io.netty.buffer.ChannelBufferFactory;
import io.netty.buffer.HeapChannelBufferFactory;
import io.netty.channel.socket.ServerSocketChannelConfig;
/**
* The default {@link ServerSocketChannelConfig} implementation.
*/
public class DefaultServerChannelConfig implements ChannelConfig {
private volatile ChannelPipelineFactory pipelineFactory;
private volatile ChannelBufferFactory bufferFactory = HeapChannelBufferFactory.getInstance();
@Override
public void setOptions(Map<String, Object> options) {
for (Entry<String, Object> e: options.entrySet()) {
setOption(e.getKey(), e.getValue());
}
}
/**
* Sets an individual option. You can override this method to support
* additional configuration parameters.
*/
@Override
public boolean setOption(String key, Object value) {
if (key.equals("pipelineFactory")) {
setPipelineFactory((ChannelPipelineFactory) value);
} else if (key.equals("bufferFactory")) {
setBufferFactory((ChannelBufferFactory) value);
} else {
return false;
}
return true;
}
@Override
public ChannelPipelineFactory getPipelineFactory() {
return pipelineFactory;
}
@Override
public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
if (pipelineFactory == null) {
throw new NullPointerException("pipelineFactory");
}
this.pipelineFactory = pipelineFactory;
}
@Override
public ChannelBufferFactory getBufferFactory() {
return bufferFactory;
}
@Override
public void setBufferFactory(ChannelBufferFactory bufferFactory) {
if (bufferFactory == null) {
throw new NullPointerException("bufferFactory");
}
this.bufferFactory = bufferFactory;
}
@Override
public int getConnectTimeoutMillis() {
return 0;
}
@Override
public void setConnectTimeoutMillis(int connectTimeoutMillis) {
// Unused
}
}