Added DefaultDatagramChannelConfig

This commit is contained in:
Trustin Lee 2009-03-12 12:56:41 +00:00
parent 1931ba6ceb
commit d33e38e626

View File

@ -0,0 +1,257 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.channel.socket;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.channel.DefaultChannelConfig;
import org.jboss.netty.util.ConversionUtil;
/**
* The default {@link SocketChannelConfig} implementation.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
public class DefaultDatagramChannelConfig extends DefaultChannelConfig
implements DatagramChannelConfig {
private final DatagramSocket socket;
/**
* Creates a new instance.
*/
public DefaultDatagramChannelConfig(DatagramSocket socket) {
if (socket == null) {
throw new NullPointerException("socket");
}
this.socket = socket;
}
@Override
public boolean setOption(String key, Object value) {
if (super.setOption(key, value)) {
return true;
}
if (key.equals("broadcast")) {
setBroadcast(ConversionUtil.toBoolean(value));
} else if (key.equals("receiveBufferSize")) {
setReceiveBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("sendBufferSize")) {
setSendBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("reuseAddress")) {
setReuseAddress(ConversionUtil.toBoolean(value));
} else if (key.equals("trafficClass")) {
setTrafficClass(ConversionUtil.toInt(value));
} else {
return false;
}
return true;
}
public boolean isBroadcast() {
try {
return socket.getBroadcast();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setBroadcast(boolean broadcast) {
try {
socket.setBroadcast(broadcast);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public InetAddress getInterface() {
if (socket instanceof MulticastSocket) {
try {
return ((MulticastSocket) socket).getInterface();
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public void setInterface(InetAddress interfaceAddress) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setInterface(interfaceAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public boolean isLoopbackModeDisabled() {
if (socket instanceof MulticastSocket) {
try {
return ((MulticastSocket) socket).getLoopbackMode();
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public void setLoopbackModeDisabled(boolean loopbackModeDisabled) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setLoopbackMode(loopbackModeDisabled);
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public NetworkInterface getNetworkInterface() {
if (socket instanceof MulticastSocket) {
try {
return ((MulticastSocket) socket).getNetworkInterface();
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public void setNetworkInterface(NetworkInterface networkInterface) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setNetworkInterface(networkInterface);
} catch (SocketException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public boolean isReuseAddress() {
try {
return socket.getReuseAddress();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setReuseAddress(boolean reuseAddress) {
try {
socket.setReuseAddress(reuseAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getReceiveBufferSize() {
try {
return socket.getReceiveBufferSize();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setReceiveBufferSize(int receiveBufferSize) {
try {
socket.setReceiveBufferSize(receiveBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getSendBufferSize() {
try {
return socket.getSendBufferSize();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setSendBufferSize(int sendBufferSize) {
try {
socket.setSendBufferSize(sendBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public int getTimeToLive() {
if (socket instanceof MulticastSocket) {
try {
return ((MulticastSocket) socket).getTimeToLive();
} catch (IOException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public void setTimeToLive(int ttl) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setTimeToLive(ttl);
} catch (IOException e) {
throw new ChannelException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public int getTrafficClass() {
try {
return socket.getTrafficClass();
} catch (SocketException e) {
throw new ChannelException(e);
}
}
public void setTrafficClass(int trafficClass) {
try {
socket.setTrafficClass(trafficClass);
} catch (SocketException e) {
throw new ChannelException(e);
}
}
}