/*
* Copyright 2009 Red Hat, Inc.
*
* Red Hat 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 org.jboss.netty.channel.socket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Map;
import java.util.Map.Entry;
import org.jboss.netty.buffer.ChannelBufferFactory;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.buffer.HeapChannelBufferFactory;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.util.internal.ConversionUtil;
/**
* The default {@link SocketChannelConfig} implementation.
*
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
*
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
*
*/
public class DefaultSocketChannelConfig implements SocketChannelConfig {
private final Socket socket;
/**
* Creates a new instance.
*/
public DefaultSocketChannelConfig(Socket socket) {
if (socket == null) {
throw new NullPointerException("socket");
}
this.socket = socket;
}
@Override
public boolean setOption(String key, Object value) {
if (key.equals("connectTimeoutMillis")) {
setConnectTimeoutMillis(ConversionUtil.toInt(value));
} else if (key.equals("bufferFactory")) {
setBufferFactory((ChannelBufferFactory) value);
} else if (key.equals("receiveBufferSize")) {
setReceiveBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("sendBufferSize")) {
setSendBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("tcpNoDelay")) {
setTcpNoDelay(ConversionUtil.toBoolean(value));
} else if (key.equals("keepAlive")) {
setKeepAlive(ConversionUtil.toBoolean(value));
} else if (key.equals("reuseAddress")) {
setReuseAddress(ConversionUtil.toBoolean(value));
} else if (key.equals("soLinger")) {
setSoLinger(ConversionUtil.toInt(value));
} else if (key.equals("trafficClass")) {
setTrafficClass(ConversionUtil.toInt(value));
} else {
return false;
}
return true;
}
public int getReceiveBufferSize() {
try {
return socket.getReceiveBufferSize();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getSendBufferSize() {
try {
return socket.getSendBufferSize();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getSoLinger() {
try {
return socket.getSoLinger();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getTrafficClass() {
try {
return socket.getTrafficClass();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public boolean isKeepAlive() {
try {
return socket.getKeepAlive();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public boolean isReuseAddress() {
try {
return socket.getReuseAddress();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public boolean isTcpNoDelay() {
try {
return socket.getTcpNoDelay();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setKeepAlive(boolean keepAlive) {
try {
socket.setKeepAlive(keepAlive);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setPerformancePreferences(
int connectionTime, int latency, int bandwidth) {
socket.setPerformancePreferences(connectionTime, latency, bandwidth);
}
public void setReceiveBufferSize(int receiveBufferSize) {
try {
socket.setReceiveBufferSize(receiveBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setReuseAddress(boolean reuseAddress) {
try {
socket.setReuseAddress(reuseAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setSendBufferSize(int sendBufferSize) {
try {
socket.setSendBufferSize(sendBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setSoLinger(int soLinger) {
try {
if (soLinger < 0) {
socket.setSoLinger(false, 0);
} else {
socket.setSoLinger(true, soLinger);
}
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setTcpNoDelay(boolean tcpNoDelay) {
try {
socket.setTcpNoDelay(tcpNoDelay);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setTrafficClass(int trafficClass) {
try {
socket.setTrafficClass(trafficClass);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
//DefaultChannelConfigと統合
private volatile ChannelBufferFactory bufferFactory = HeapChannelBufferFactory.getInstance(ChannelBuffers.BIG_ENDIAN);
private volatile int connectTimeoutMillis = 10000; // 10 seconds
public void setOptions(Map<String, Object> options) {
for (Entry<String, Object> e: options.entrySet()) {
setOption(e.getKey(), e.getValue());
}
}
public int getConnectTimeoutMillis() {
return connectTimeoutMillis;
}
public ChannelBufferFactory getBufferFactory() {
return bufferFactory;
}
public void setBufferFactory(ChannelBufferFactory bufferFactory) {
if (bufferFactory == null) {
throw new NullPointerException("bufferFactory");
}
this.bufferFactory = bufferFactory;
}
public void setConnectTimeoutMillis(int connectTimeoutMillis) {
if (connectTimeoutMillis < 0) {
throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
}
this.connectTimeoutMillis = connectTimeoutMillis;
}
}