use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project baseio by generallycloud.
the class NettyClientThread method main.
public static void main(String[] args) throws Exception {
EventLoopGroup group = NettyUtil.newEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new HelloClient());
}
});
System.out.println("################## Test start ####################");
ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
System.out.println(f.isSuccess());
Channel channel = f.channel();
System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
int len = 1024 * 64;
StringBuilder s = new StringBuilder(len);
for (int i = 0; i < len; i++) {
s.append(len % 10);
}
final String msg = s.toString();
Util.exec(() -> {
int i = 0;
for (; ; ) {
// String s = "hello Service! ---> :" + i;
ChannelFuture f1 = channel.writeAndFlush(msg);
Util.sleep(1);
System.out.println(f1.isDone() + "--------" + i);
i++;
}
});
Util.sleep(Integer.MAX_VALUE);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project riposte by Nike-Inc.
the class RequestStateCleanerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
// New request incoming - setup/clear *all* state objects for new requests
for (ProcessingStateClassAndKeyPair<? extends ProcessingState> stateClassAndKeyPair : PROCESSING_STATE_ATTRIBUTE_KEYS) {
// See if we have an existing state object for this channel for the given state type.
@SuppressWarnings("unchecked") AttributeKey<ProcessingState> attrKey = (AttributeKey<ProcessingState>) stateClassAndKeyPair.getRight();
Attribute<ProcessingState> processingStateAttr = ctx.channel().attr(attrKey);
ProcessingState processingState = processingStateAttr.get();
if (processingState == null) {
// We don't already have one for this channel, so create one and register it.
processingState = stateClassAndKeyPair.getLeft().newInstance();
processingStateAttr.set(processingState);
}
// Clean the state for the new request.
processingState.cleanStateForNewRequest();
}
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
// Set the DistributedTracingConfig on the HttpProcessingState.
// noinspection deprecation - This is the only place that should actually be calling this method.
httpProcessingState.setDistributedTracingConfig(distributedTracingConfig);
// Send a request received event to the metricsListener.
if (metricsListener != null) {
metricsListener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, httpProcessingState);
}
// Remove the idle channel timeout handler (if there is one) so that it doesn't kill this new request if the
// endpoint takes longer to complete than the idle timeout value - the idle channel timeout is only for
// timing out channels that are idle *in-between* requests.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler idleChannelTimeoutHandler = pipeline.get(HttpChannelInitializer.IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
if (idleChannelTimeoutHandler != null)
pipeline.remove(idleChannelTimeoutHandler);
// last chunk when the timeout hits.
if (incompleteHttpCallTimeoutMillis > 0 && !(msg instanceof LastHttpContent)) {
IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler(incompleteHttpCallTimeoutMillis);
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler == null) {
pipeline.addFirst(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
} else {
logger.error("Handling HttpRequest for new request and found an IncompleteHttpCallTimeoutHandler " + "already in the pipeline. This should not be possible. A new " + "IncompleteHttpCallTimeoutHandler will replace the old one. worker_channel_id={}", ctx.channel().toString());
pipeline.replace(existingHandler, INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
}
}
ProxyRouterProcessingState proxyRouterProcessingState = ChannelAttributes.getProxyRouterProcessingStateForChannel(ctx).get();
// Set the DistributedTracingConfig on the ProxyRouterProcessingState.
// noinspection deprecation - This is the only place that should actually be calling this method.
proxyRouterProcessingState.setDistributedTracingConfig(distributedTracingConfig);
} else if (msg instanceof LastHttpContent) {
// The HTTP call is complete, so we can remove the IncompleteHttpCallTimeoutHandler.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler != null)
pipeline.remove(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
}
// Continue on the pipeline processing.
super.channelRead(ctx, msg);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project Terasology by MovingBlocks.
the class TerasologyClientPipelineFactory method initChannel.
@Override
protected void initChannel(Channel ch) throws Exception {
JoinStatusImpl joinStatus = new JoinStatusImpl();
ChannelPipeline p = ch.pipeline();
p.addLast(MetricRecordingHandler.NAME, new MetricRecordingHandler());
p.addLast("inflateDecoder", new Lz4FrameDecoder());
p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(8388608, 0, 3, 0, 3));
p.addLast("protobufDecoder", new ProtobufDecoder(NetData.NetMessage.getDefaultInstance()));
p.addLast("deflateEncoder", new Lz4FrameEncoder(true));
p.addLast("frameLengthEncoder", new LengthFieldPrepender(3));
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("authenticationHandler", new ClientHandshakeHandler(joinStatus));
p.addLast("connectionHandler", new ClientConnectionHandler(joinStatus, networkSystem));
p.addLast("handler", new ClientHandler(networkSystem));
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project Terasology by MovingBlocks.
the class InfoRequestPipelineFactory method initChannel.
@Override
protected void initChannel(Channel ch) throws Exception {
JoinStatusImpl joinStatus = new JoinStatusImpl();
ChannelPipeline p = ch.pipeline();
p.addLast(MetricRecordingHandler.NAME, new MetricRecordingHandler());
p.addLast("inflateDecoder", new Lz4FrameDecoder());
p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(8388608, 0, 3, 0, 3));
p.addLast("protobufDecoder", new ProtobufDecoder(NetData.NetMessage.getDefaultInstance()));
p.addLast("deflateEncoder", new Lz4FrameEncoder(true));
p.addLast("frameLengthEncoder", new LengthFieldPrepender(3));
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("authenticationHandler", new ClientHandshakeHandler(joinStatus));
p.addLast("connectionHandler", new ServerInfoRequestHandler());
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project Terasology by MovingBlocks.
the class TerasologyServerPipelineFactory method initChannel.
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(MetricRecordingHandler.NAME, new MetricRecordingHandler());
p.addLast("inflateDecoder", new Lz4FrameDecoder());
p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(8388608, 0, 3, 0, 3));
p.addLast("protobufDecoder", new ProtobufDecoder(NetData.NetMessage.getDefaultInstance()));
p.addLast("deflateEncoder", new Lz4FrameEncoder(true));
p.addLast("frameLengthEncoder", new LengthFieldPrepender(3));
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("authenticationHandler", new ServerHandshakeHandler());
p.addLast("connectionHandler", new ServerConnectionHandler(networkSystem));
p.addLast("handler", new ServerHandler(networkSystem));
}
Aggregations