Rename IOStream example / Code cleanup
This commit is contained in:
parent
4cff6b56ff
commit
d40bd5e7f2
@ -13,7 +13,7 @@
|
|||||||
* License for the specific language governing permissions and limitations
|
* License for the specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package io.netty.example.iostream;
|
package io.netty.example.stdio;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -27,24 +27,24 @@ import io.netty.channel.ChannelPipelineFactory;
|
|||||||
import io.netty.channel.DefaultChannelPipeline;
|
import io.netty.channel.DefaultChannelPipeline;
|
||||||
import io.netty.channel.MessageEvent;
|
import io.netty.channel.MessageEvent;
|
||||||
import io.netty.channel.SimpleChannelHandler;
|
import io.netty.channel.SimpleChannelHandler;
|
||||||
import io.netty.channel.iostream.IOStreamAddress;
|
import io.netty.channel.iostream.IoStreamAddress;
|
||||||
import io.netty.channel.iostream.IOStreamChannelFactory;
|
import io.netty.channel.iostream.IoStreamChannelFactory;
|
||||||
import io.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
|
import io.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.frame.Delimiters;
|
import io.netty.handler.codec.frame.Delimiters;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example demonstrating the use of the {@link io.netty.channel.iostream.IOStreamChannel}.
|
* An example demonstrating the use of the {@link io.netty.channel.iostream.IoStreamChannel}.
|
||||||
*/
|
*/
|
||||||
public class IOStream {
|
public class StdioLogger {
|
||||||
|
|
||||||
private static volatile boolean running = true;
|
private static volatile boolean running = true;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
final ExecutorService executorService = Executors.newCachedThreadPool();
|
final ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
final ClientBootstrap bootstrap = new ClientBootstrap(new IOStreamChannelFactory(executorService));
|
final ClientBootstrap bootstrap = new ClientBootstrap(new IoStreamChannelFactory(executorService));
|
||||||
|
|
||||||
// Configure the event pipeline factory.
|
// Configure the event pipeline factory.
|
||||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||||
@ -64,7 +64,7 @@ public class IOStream {
|
|||||||
e.getChannel().write("Message received: " + message);
|
e.getChannel().write("Message received: " + message);
|
||||||
}
|
}
|
||||||
if ("exit".equals(message)) {
|
if ("exit".equals(message)) {
|
||||||
IOStream.running = false;
|
StdioLogger.running = false;
|
||||||
}
|
}
|
||||||
super.messageReceived(ctx, e);
|
super.messageReceived(ctx, e);
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ public class IOStream {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Make a new connection.
|
// Make a new connection.
|
||||||
ChannelFuture connectFuture = bootstrap.connect(new IOStreamAddress(System.in, System.out));
|
ChannelFuture connectFuture = bootstrap.connect(new IoStreamAddress(System.in, System.out));
|
||||||
|
|
||||||
// Wait until the connection is made successfully.
|
// Wait until the connection is made successfully.
|
||||||
Channel channel = connectFuture.awaitUninterruptibly().getChannel();
|
Channel channel = connectFuture.awaitUninterruptibly().getChannel();
|
@ -88,9 +88,6 @@ public class CIDR4 extends CIDR {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.CIDR#contains(java.net.InetAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(InetAddress inetAddress) {
|
public boolean contains(InetAddress inetAddress) {
|
||||||
int search = ipv4AddressToInt(inetAddress);
|
int search = ipv4AddressToInt(inetAddress);
|
||||||
|
@ -93,9 +93,6 @@ public class CIDR6 extends CIDR {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.CIDR#contains(java.net.InetAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(InetAddress inetAddress) {
|
public boolean contains(InetAddress inetAddress) {
|
||||||
BigInteger search = ipv6AddressToBigInteger(inetAddress);
|
BigInteger search = ipv6AddressToBigInteger(inetAddress);
|
||||||
|
@ -235,9 +235,6 @@ public class IpFilterRuleHandler extends IpFilteringHandlerImpl {
|
|||||||
return ipFilterRuleList.size();
|
return ipFilterRuleList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilteringHandler#accept(io.netty.channel.ChannelHandlerContext, io.netty.channel.ChannelEvent, java.net.InetSocketAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean accept(ChannelHandlerContext ctx, ChannelEvent e, InetSocketAddress inetSocketAddress)
|
protected boolean accept(ChannelHandlerContext ctx, ChannelEvent e, InetSocketAddress inetSocketAddress)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
@ -95,9 +95,6 @@ public abstract class IpFilteringHandlerImpl implements ChannelUpstreamHandler,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.channel.ChannelUpstreamHandler#handleUpstream(io.netty.channel.ChannelHandlerContext, io.netty.channel.ChannelEvent)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
|
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
|
||||||
if (e instanceof ChannelStateEvent) {
|
if (e instanceof ChannelStateEvent) {
|
||||||
@ -153,17 +150,11 @@ public abstract class IpFilteringHandlerImpl implements ChannelUpstreamHandler,
|
|||||||
ctx.sendUpstream(e);
|
ctx.sendUpstream(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilteringHandler#setIpFilterListener(io.netty.handler.ipfilter.IpFilterListener)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void setIpFilterListener(IpFilterListener listener) {
|
public void setIpFilterListener(IpFilterListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilteringHandler#removeIpFilterListener()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void removeIpFilterListener() {
|
public void removeIpFilterListener() {
|
||||||
this.listener = null;
|
this.listener = null;
|
||||||
|
@ -40,9 +40,6 @@ public class OneIpFilterHandler extends IpFilteringHandlerImpl {
|
|||||||
/** HashMap of current remote connected InetAddress */
|
/** HashMap of current remote connected InetAddress */
|
||||||
private final ConcurrentMap<InetAddress, Boolean> connectedSet = new ConcurrentHashMap<InetAddress, Boolean>();
|
private final ConcurrentMap<InetAddress, Boolean> connectedSet = new ConcurrentHashMap<InetAddress, Boolean>();
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilteringHandler#accept(io.netty.channel.ChannelHandlerContext, io.netty.channel.ChannelEvent, java.net.InetSocketAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean accept(ChannelHandlerContext ctx, ChannelEvent e, InetSocketAddress inetSocketAddress)
|
protected boolean accept(ChannelHandlerContext ctx, ChannelEvent e, InetSocketAddress inetSocketAddress)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
@ -54,9 +51,6 @@ public class OneIpFilterHandler extends IpFilteringHandlerImpl {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilteringHandler#handleUpstream(io.netty.channel.ChannelHandlerContext, io.netty.channel.ChannelEvent)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
|
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
|
||||||
super.handleUpstream(ctx, e);
|
super.handleUpstream(ctx, e);
|
||||||
|
@ -81,25 +81,16 @@ public class PatternRule implements IpFilterRule, Comparable<Object> {
|
|||||||
return this.pattern;
|
return this.pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilterRule#isAllowRule()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAllowRule() {
|
public boolean isAllowRule() {
|
||||||
return isAllowRule;
|
return isAllowRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpFilterRule#isDenyRule()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDenyRule() {
|
public boolean isDenyRule() {
|
||||||
return !isAllowRule;
|
return !isAllowRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.handler.ipfilter.IpSet#contains(java.net.InetAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(InetAddress inetAddress) {
|
public boolean contains(InetAddress inetAddress) {
|
||||||
if (localhost) {
|
if (localhost) {
|
||||||
|
@ -369,9 +369,6 @@ public abstract class AbstractTrafficShapingHandler extends
|
|||||||
return trafficCounter;
|
return trafficCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see io.netty.util.ExternalResourceReleasable#releaseExternalResources()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void releaseExternalResources() {
|
public void releaseExternalResources() {
|
||||||
if (trafficCounter != null) {
|
if (trafficCounter != null) {
|
||||||
|
@ -22,9 +22,9 @@ import java.net.SocketAddress;
|
|||||||
/**
|
/**
|
||||||
* A {@link java.net.SocketAddress} implementation holding an
|
* A {@link java.net.SocketAddress} implementation holding an
|
||||||
* {@link java.io.InputStream} and an {@link java.io.OutputStream} instance used
|
* {@link java.io.InputStream} and an {@link java.io.OutputStream} instance used
|
||||||
* as "remote" address to connect to with a {@link IOStreamChannel}.
|
* as "remote" address to connect to with a {@link IoStreamChannel}.
|
||||||
*/
|
*/
|
||||||
public class IOStreamAddress extends SocketAddress {
|
public class IoStreamAddress extends SocketAddress {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4382415449059935960L;
|
private static final long serialVersionUID = -4382415449059935960L;
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ public class IOStreamAddress extends SocketAddress {
|
|||||||
|
|
||||||
private final OutputStream outputStream;
|
private final OutputStream outputStream;
|
||||||
|
|
||||||
public IOStreamAddress(final InputStream inputStream, final OutputStream outputStream) {
|
public IoStreamAddress(final InputStream inputStream, final OutputStream outputStream) {
|
||||||
|
|
||||||
this.inputStream = inputStream;
|
this.inputStream = inputStream;
|
||||||
this.outputStream = outputStream;
|
this.outputStream = outputStream;
|
||||||
|
@ -28,25 +28,25 @@ import io.netty.channel.ChannelSink;
|
|||||||
* A channel to an {@link java.io.InputStream} and an
|
* A channel to an {@link java.io.InputStream} and an
|
||||||
* {@link java.io.OutputStream}.
|
* {@link java.io.OutputStream}.
|
||||||
*/
|
*/
|
||||||
public class IOStreamChannel extends AbstractChannel {
|
public class IoStreamChannel extends AbstractChannel {
|
||||||
|
|
||||||
IOStreamChannel(final ChannelFactory factory, final ChannelPipeline pipeline, final ChannelSink sink) {
|
IoStreamChannel(final ChannelFactory factory, final ChannelPipeline pipeline, final ChannelSink sink) {
|
||||||
super(null, factory, pipeline, sink);
|
super(null, factory, pipeline, sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelConfig getConfig() {
|
public ChannelConfig getConfig() {
|
||||||
return ((IOStreamChannelSink) getPipeline().getSink()).getConfig();
|
return ((IoStreamChannelSink) getPipeline().getSink()).getConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
return ((IOStreamChannelSink) getPipeline().getSink()).isBound();
|
return ((IoStreamChannelSink) getPipeline().getSink()).isBound();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return ((IOStreamChannelSink) getPipeline().getSink()).isConnected();
|
return ((IoStreamChannelSink) getPipeline().getSink()).isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,7 +56,7 @@ public class IOStreamChannel extends AbstractChannel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SocketAddress getRemoteAddress() {
|
public SocketAddress getRemoteAddress() {
|
||||||
return ((IOStreamChannelSink) getPipeline().getSink()).getRemoteAddress();
|
return ((IoStreamChannelSink) getPipeline().getSink()).getRemoteAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -27,22 +27,22 @@ import io.netty.util.internal.ExecutorUtil;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link io.netty.channel.ChannelFactory} for creating {@link IOStreamChannel} instances.
|
* A {@link io.netty.channel.ChannelFactory} for creating {@link IoStreamChannel} instances.
|
||||||
*/
|
*/
|
||||||
public class IOStreamChannelFactory implements ChannelFactory {
|
public class IoStreamChannelFactory implements ChannelFactory {
|
||||||
|
|
||||||
private final ChannelGroup channels = new DefaultChannelGroup("IOStreamChannelFactory-ChannelGroup");
|
private final ChannelGroup channels = new DefaultChannelGroup("IOStreamChannelFactory-ChannelGroup");
|
||||||
|
|
||||||
private final ExecutorService executorService;
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
public IOStreamChannelFactory(ExecutorService executorService) {
|
public IoStreamChannelFactory(ExecutorService executorService) {
|
||||||
this.executorService = executorService;
|
this.executorService = executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Channel newChannel(final ChannelPipeline pipeline) {
|
public Channel newChannel(final ChannelPipeline pipeline) {
|
||||||
IOStreamChannelSink sink = new IOStreamChannelSink(executorService);
|
IoStreamChannelSink sink = new IoStreamChannelSink(executorService);
|
||||||
IOStreamChannel channel = new IOStreamChannel(this, pipeline, sink);
|
IoStreamChannel channel = new IoStreamChannel(this, pipeline, sink);
|
||||||
sink.setChannel(channel);
|
sink.setChannel(channel);
|
||||||
channels.add(channel);
|
channels.add(channel);
|
||||||
return channel;
|
return channel;
|
||||||
|
@ -37,13 +37,13 @@ import io.netty.channel.MessageEvent;
|
|||||||
* A {@link io.netty.channel.ChannelSink} implementation which reads from
|
* A {@link io.netty.channel.ChannelSink} implementation which reads from
|
||||||
* an {@link java.io.InputStream} and writes to an {@link java.io.OutputStream}.
|
* an {@link java.io.InputStream} and writes to an {@link java.io.OutputStream}.
|
||||||
*/
|
*/
|
||||||
public class IOStreamChannelSink extends AbstractChannelSink {
|
public class IoStreamChannelSink extends AbstractChannelSink {
|
||||||
|
|
||||||
private static class ReadRunnable implements Runnable {
|
private static class ReadRunnable implements Runnable {
|
||||||
|
|
||||||
private final IOStreamChannelSink channelSink;
|
private final IoStreamChannelSink channelSink;
|
||||||
|
|
||||||
public ReadRunnable(final IOStreamChannelSink channelSink) {
|
public ReadRunnable(final IoStreamChannelSink channelSink) {
|
||||||
this.channelSink = channelSink;
|
this.channelSink = channelSink;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,9 +88,9 @@ public class IOStreamChannelSink extends AbstractChannelSink {
|
|||||||
|
|
||||||
private final ExecutorService executorService;
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
private IOStreamChannel channel;
|
private IoStreamChannel channel;
|
||||||
|
|
||||||
public IOStreamChannelSink(final ExecutorService executorService) {
|
public IoStreamChannelSink(final ExecutorService executorService) {
|
||||||
this.executorService = executorService;
|
this.executorService = executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ public class IOStreamChannelSink extends AbstractChannelSink {
|
|||||||
return inputStream != null && outputStream != null;
|
return inputStream != null && outputStream != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IOStreamAddress getRemoteAddress() {
|
public IoStreamAddress getRemoteAddress() {
|
||||||
return remoteAddress;
|
return remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,11 +110,11 @@ public class IOStreamChannelSink extends AbstractChannelSink {
|
|||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setChannel(final IOStreamChannel channel) {
|
public void setChannel(final IoStreamChannel channel) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IOStreamAddress remoteAddress;
|
private IoStreamAddress remoteAddress;
|
||||||
|
|
||||||
private OutputStream outputStream;
|
private OutputStream outputStream;
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ public class IOStreamChannelSink extends AbstractChannelSink {
|
|||||||
if (Boolean.FALSE.equals(value)) {
|
if (Boolean.FALSE.equals(value)) {
|
||||||
outputStream = null;
|
outputStream = null;
|
||||||
inputStream = null;
|
inputStream = null;
|
||||||
((IOStreamChannel) e.getChannel()).doSetClosed();
|
((IoStreamChannel) e.getChannel()).doSetClosed();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ public class IOStreamChannelSink extends AbstractChannelSink {
|
|||||||
|
|
||||||
case CONNECTED:
|
case CONNECTED:
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
remoteAddress = (IOStreamAddress) value;
|
remoteAddress = (IoStreamAddress) value;
|
||||||
outputStream = remoteAddress.getOutputStream();
|
outputStream = remoteAddress.getOutputStream();
|
||||||
inputStream = new PushbackInputStream(remoteAddress.getInputStream());
|
inputStream = new PushbackInputStream(remoteAddress.getInputStream());
|
||||||
executorService.execute(new ReadRunnable(this));
|
executorService.execute(new ReadRunnable(this));
|
||||||
|
Loading…
Reference in New Issue
Block a user