Retrofit the socket channel API with the new API

This commit is contained in:
Trustin Lee 2012-05-02 14:07:50 +09:00
parent e65e496fc0
commit 5dda9d1840
7 changed files with 31 additions and 181 deletions

View File

@ -1,58 +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.socket;
import io.netty.channel.Channel;
import io.netty.channel.DefaultChannelFuture;
public class ChannelRunnableWrapper extends DefaultChannelFuture implements Runnable {
private final Runnable task;
private boolean started;
public ChannelRunnableWrapper(Channel channel, Runnable task) {
super(channel, true);
this.task = task;
}
@Override
public void run() {
synchronized (this) {
if (!isCancelled()) {
started = true;
} else {
return;
}
}
try {
task.run();
setSuccess();
} catch (Throwable t) {
setFailure(t);
}
}
@Override
public synchronized boolean cancel() {
if (started) {
return false;
}
return super.cancel();
}
}

View File

@ -15,13 +15,13 @@
*/
package io.netty.channel.socket;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
/**
* A UDP/IP {@link Channel} which is created by {@link DatagramChannelFactory}.
* @apiviz.landmark
@ -29,29 +29,29 @@ import io.netty.channel.ChannelFuture;
*/
public interface DatagramChannel extends Channel {
@Override
DatagramChannelConfig getConfig();
DatagramChannelConfig config();
@Override
InetSocketAddress getLocalAddress();
InetSocketAddress localAddress();
@Override
InetSocketAddress getRemoteAddress();
InetSocketAddress remoteAddress();
/**
* Joins a multicast group.
*/
ChannelFuture joinGroup(InetAddress multicastAddress);
void joinGroup(InetAddress multicastAddress, ChannelFuture future);
/**
* Joins the specified multicast group at the specified interface.
*/
ChannelFuture joinGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface);
void joinGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelFuture future);
/**
* Leaves a multicast group.
*/
ChannelFuture leaveGroup(InetAddress multicastAddress);
void leaveGroup(InetAddress multicastAddress, ChannelFuture future);
/**
* Leaves a multicast group on a specified local interface.
*/
ChannelFuture leaveGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface);
void leaveGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface, ChannelFuture future);
}

View File

@ -15,16 +15,12 @@
*/
package io.netty.channel.socket;
import io.netty.channel.ChannelConfig;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import io.netty.channel.ChannelConfig;
import io.netty.channel.FixedReceiveBufferSizePredictor;
import io.netty.channel.FixedReceiveBufferSizePredictorFactory;
import io.netty.channel.ReceiveBufferSizePredictor;
import io.netty.channel.ReceiveBufferSizePredictorFactory;
/**
* A {@link ChannelConfig} for a {@link DatagramChannel}.
*
@ -49,10 +45,6 @@ import io.netty.channel.ReceiveBufferSizePredictorFactory;
* </tr><tr>
* <td>{@code "receiveBufferSize"}</td><td>{@link #setReceiveBufferSize(int)}</td>
* </tr><tr>
* <td>{@code "receiveBufferSizePredictor"}</td><td>{@link #setReceiveBufferSizePredictor(ReceiveBufferSizePredictor)}</td>
* </tr><tr>
* <td>{@code "receiveBufferSizePredictorFactory"}</td><td>{@link #setReceiveBufferSizePredictorFactory(ReceiveBufferSizePredictorFactory)}</td>
* </tr><tr>
* <td>{@code "sendBufferSize"}</td><td>{@link #setSendBufferSize(int)}</td>
* </tr><tr>
* <td>{@code "timeToLive"}</td><td>{@link #setTimeToLive(int)}</td>
@ -161,38 +153,4 @@ public interface DatagramChannelConfig extends ChannelConfig {
* the {@link DatagramChannel}.
*/
void setNetworkInterface(NetworkInterface networkInterface);
/**
* Returns the {@link ReceiveBufferSizePredictor} which predicts the
* number of readable bytes in the socket receive buffer. The default
* predictor is <tt>{@link FixedReceiveBufferSizePredictor}(768)</tt>.
*/
ReceiveBufferSizePredictor getReceiveBufferSizePredictor();
/**
* Sets the {@link ReceiveBufferSizePredictor} which predicts the
* number of readable bytes in the socket receive buffer. The default
* predictor is <tt>{@link FixedReceiveBufferSizePredictor}(768)</tt>.
*/
void setReceiveBufferSizePredictor(ReceiveBufferSizePredictor predictor);
/**
* Returns the {@link ReceiveBufferSizePredictorFactory} which creates a new
* {@link ReceiveBufferSizePredictor} when a new channel is created and
* no {@link ReceiveBufferSizePredictor} was set. If no predictor was set
* for the channel, {@link #setReceiveBufferSizePredictor(ReceiveBufferSizePredictor)}
* will be called with the new predictor. The default factory is
* <tt>{@link FixedReceiveBufferSizePredictorFactory}(768)</tt>.
*/
ReceiveBufferSizePredictorFactory getReceiveBufferSizePredictorFactory();
/**
* Sets the {@link ReceiveBufferSizePredictor} which creates a new
* {@link ReceiveBufferSizePredictor} when a new channel is created and
* no {@link ReceiveBufferSizePredictor} was set. If no predictor was set
* for the channel, {@link #setReceiveBufferSizePredictor(ReceiveBufferSizePredictor)}
* will be called with the new predictor. The default factory is
* <tt>{@link FixedReceiveBufferSizePredictorFactory}(768)</tt>.
*/
void setReceiveBufferSizePredictorFactory(ReceiveBufferSizePredictorFactory predictorFactory);
}

View File

@ -15,6 +15,10 @@
*/
package io.netty.channel.socket;
import io.netty.channel.ChannelException;
import io.netty.channel.DefaultChannelConfig;
import io.netty.util.internal.ConversionUtil;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
@ -22,25 +26,13 @@ import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import io.netty.channel.ChannelException;
import io.netty.channel.DefaultChannelConfig;
import io.netty.channel.FixedReceiveBufferSizePredictorFactory;
import io.netty.channel.ReceiveBufferSizePredictor;
import io.netty.channel.ReceiveBufferSizePredictorFactory;
import io.netty.util.internal.ConversionUtil;
/**
* The default {@link DatagramChannelConfig} implementation.
*/
public class DefaultDatagramChannelConfig extends DefaultChannelConfig
implements DatagramChannelConfig {
private static final ReceiveBufferSizePredictorFactory DEFAULT_PREDICTOR_FACTORY =
new FixedReceiveBufferSizePredictorFactory(768);
private final DatagramSocket socket;
private volatile ReceiveBufferSizePredictor predictor;
private volatile ReceiveBufferSizePredictorFactory predictorFactory = DEFAULT_PREDICTOR_FACTORY;
/**
* Creates a new instance.
@ -64,10 +56,6 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig
setReceiveBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("sendBufferSize")) {
setSendBufferSize(ConversionUtil.toInt(value));
} else if (key.equals("receiveBufferSizePredictorFactory")) {
setReceiveBufferSizePredictorFactory((ReceiveBufferSizePredictorFactory) value);
} else if (key.equals("receiveBufferSizePredictor")) {
setReceiveBufferSizePredictor((ReceiveBufferSizePredictor) value);
} else if (key.equals("reuseAddress")) {
setReuseAddress(ConversionUtil.toBoolean(value));
} else if (key.equals("loopbackModeDisabled")) {
@ -279,42 +267,4 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig
throw new ChannelException(e);
}
}
@Override
public ReceiveBufferSizePredictor getReceiveBufferSizePredictor() {
ReceiveBufferSizePredictor predictor = this.predictor;
if (predictor == null) {
try {
this.predictor = predictor = getReceiveBufferSizePredictorFactory().getPredictor();
} catch (Exception e) {
throw new ChannelException(
"Failed to create a new " +
ReceiveBufferSizePredictor.class.getSimpleName() + '.',
e);
}
}
return predictor;
}
@Override
public void setReceiveBufferSizePredictor(
ReceiveBufferSizePredictor predictor) {
if (predictor == null) {
throw new NullPointerException("predictor");
}
this.predictor = predictor;
}
@Override
public ReceiveBufferSizePredictorFactory getReceiveBufferSizePredictorFactory() {
return predictorFactory;
}
@Override
public void setReceiveBufferSizePredictorFactory(ReceiveBufferSizePredictorFactory predictorFactory) {
if (predictorFactory == null) {
throw new NullPointerException("predictorFactory");
}
this.predictorFactory = predictorFactory;
}
}

View File

@ -15,17 +15,17 @@
*/
package io.netty.channel.socket;
import io.netty.channel.ChannelException;
import io.netty.channel.DefaultChannelConfig;
import io.netty.util.internal.ConversionUtil;
import java.net.ServerSocket;
import java.net.SocketException;
import io.netty.channel.ChannelException;
import io.netty.channel.DefaultServerChannelConfig;
import io.netty.util.internal.ConversionUtil;
/**
* The default {@link ServerSocketChannelConfig} implementation.
*/
public class DefaultServerSocketChannelConfig extends DefaultServerChannelConfig
public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
implements ServerSocketChannelConfig {
private final ServerSocket socket;

View File

@ -15,10 +15,10 @@
*/
package io.netty.channel.socket;
import java.net.InetSocketAddress;
import io.netty.channel.ServerChannel;
import java.net.InetSocketAddress;
/**
* A TCP/IP {@link ServerChannel} which accepts incoming TCP/IP connections.
* @apiviz.landmark
@ -26,9 +26,9 @@ import io.netty.channel.ServerChannel;
*/
public interface ServerSocketChannel extends ServerChannel {
@Override
ServerSocketChannelConfig getConfig();
ServerSocketChannelConfig config();
@Override
InetSocketAddress getLocalAddress();
InetSocketAddress localAddress();
@Override
InetSocketAddress getRemoteAddress();
InetSocketAddress remoteAddress();
}

View File

@ -15,10 +15,10 @@
*/
package io.netty.channel.socket;
import java.net.InetSocketAddress;
import io.netty.channel.Channel;
import java.net.InetSocketAddress;
/**
* A TCP/IP socket {@link Channel} which was either accepted by
* {@link ServerSocketChannel} or created by {@link ClientSocketChannelFactory}.
@ -27,9 +27,9 @@ import io.netty.channel.Channel;
*/
public interface SocketChannel extends Channel {
@Override
SocketChannelConfig getConfig();
SocketChannelConfig config();
@Override
InetSocketAddress getLocalAddress();
InetSocketAddress localAddress();
@Override
InetSocketAddress getRemoteAddress();
InetSocketAddress remoteAddress();
}