2016-08-16 15:22:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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.handler.codec.http2;
|
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelDuplexHandler;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.embedded.EmbeddedChannel;
|
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
|
import io.netty.util.internal.PlatformDependent;
|
|
|
|
|
2016-11-18 12:39:21 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-08-16 15:22:39 +02:00
|
|
|
import java.util.concurrent.locks.LockSupport;
|
|
|
|
|
|
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Channel handler that allows to easily access inbound messages.
|
|
|
|
*/
|
|
|
|
public class LastInboundHandler extends ChannelDuplexHandler {
|
2016-11-18 12:39:21 +01:00
|
|
|
private final List<Object> queue = new ArrayList<Object>();
|
2016-08-16 15:22:39 +02:00
|
|
|
private Throwable lastException;
|
|
|
|
private ChannelHandlerContext ctx;
|
|
|
|
private boolean channelActive;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handlerAdded(ChannelHandlerContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
if (channelActive) {
|
|
|
|
throw new IllegalStateException("channelActive may only be fired once.");
|
|
|
|
}
|
|
|
|
channelActive = true;
|
|
|
|
super.channelActive(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isChannelActive() {
|
|
|
|
return channelActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
if (!channelActive) {
|
|
|
|
throw new IllegalStateException("channelInactive may only be fired once after channelActive.");
|
|
|
|
}
|
|
|
|
channelActive = false;
|
|
|
|
super.channelInactive(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
2016-11-18 12:39:21 +01:00
|
|
|
queue.add(msg);
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
2016-11-18 12:39:21 +01:00
|
|
|
queue.add(new UserEvent(evt));
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
if (lastException != null) {
|
|
|
|
cause.printStackTrace();
|
|
|
|
} else {
|
|
|
|
lastException = cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkException() throws Exception {
|
|
|
|
if (lastException == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Throwable t = lastException;
|
|
|
|
lastException = null;
|
|
|
|
PlatformDependent.throwException(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> T readInbound() {
|
2016-11-18 12:39:21 +01:00
|
|
|
for (int i = 0; i < queue.size(); i++) {
|
|
|
|
Object o = queue.get(i);
|
|
|
|
if (!(o instanceof UserEvent)) {
|
|
|
|
queue.remove(i);
|
|
|
|
return (T) o;
|
|
|
|
}
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
2016-11-18 12:39:21 +01:00
|
|
|
|
|
|
|
return null;
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public <T> T blockingReadInbound() {
|
|
|
|
T msg;
|
|
|
|
while ((msg = readInbound()) == null) {
|
|
|
|
LockSupport.parkNanos(MILLISECONDS.toNanos(10));
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> T readUserEvent() {
|
2016-11-18 12:39:21 +01:00
|
|
|
for (int i = 0; i < queue.size(); i++) {
|
|
|
|
Object o = queue.get(i);
|
|
|
|
if (o instanceof UserEvent) {
|
|
|
|
queue.remove(i);
|
|
|
|
return (T) ((UserEvent) o).evt;
|
|
|
|
}
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
2016-11-18 12:39:21 +01:00
|
|
|
|
|
|
|
return null;
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Useful to test order of events and messages.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2016-11-18 12:39:21 +01:00
|
|
|
public <T> T readInboundMessageOrUserEvent() {
|
|
|
|
if (queue.isEmpty()) {
|
|
|
|
return null;
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
2016-11-18 12:39:21 +01:00
|
|
|
Object o = queue.remove(0);
|
|
|
|
if (o instanceof UserEvent) {
|
|
|
|
return (T) ((UserEvent) o).evt;
|
|
|
|
}
|
|
|
|
return (T) o;
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void writeOutbound(Object... msgs) throws Exception {
|
|
|
|
for (Object msg : msgs) {
|
|
|
|
ctx.write(msg);
|
|
|
|
}
|
|
|
|
ctx.flush();
|
|
|
|
EmbeddedChannel ch = (EmbeddedChannel) ctx.channel();
|
|
|
|
ch.runPendingTasks();
|
|
|
|
ch.checkException();
|
|
|
|
checkException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void finishAndReleaseAll() throws Exception {
|
|
|
|
checkException();
|
|
|
|
Object o;
|
2016-11-18 12:39:21 +01:00
|
|
|
while ((o = readInboundMessageOrUserEvent()) != null) {
|
2016-08-16 15:22:39 +02:00
|
|
|
ReferenceCountUtil.release(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Channel channel() {
|
|
|
|
return ctx.channel();
|
|
|
|
}
|
2016-11-18 12:39:21 +01:00
|
|
|
|
|
|
|
private static final class UserEvent {
|
|
|
|
private final Object evt;
|
|
|
|
|
|
|
|
UserEvent(Object evt) {
|
|
|
|
this.evt = evt;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 15:22:39 +02:00
|
|
|
}
|