Search in sources :

Example 36 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project Terasology by MovingBlocks.

the class ServerInfoService method requestInfo.

public Future<ServerInfoMessage> requestInfo(final String address, final int port) {
    return pool.submit(() -> {
        InetSocketAddress remoteAddress = new InetSocketAddress(address, port);
        ChannelFuture connectCheck = bootstrap.connect(remoteAddress);
        connectCheck.syncUninterruptibly();
        Channel channel = connectCheck.getChannel();
        channel.getCloseFuture().syncUninterruptibly();
        ServerInfoRequestHandler handler = channel.getPipeline().get(ServerInfoRequestHandler.class);
        ServerInfoMessage serverInfo = handler.getServerInfo();
        return serverInfo;
    });
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) ServerInfoRequestHandler(org.terasology.network.internal.ServerInfoRequestHandler)

Example 37 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project smscgateway by RestComm.

the class TestSmppSession method sendRequestPdu.

public WindowFuture<Integer, PduRequest, PduResponse> sendRequestPdu(PduRequest pdu, long timeoutMillis, boolean synchronous) throws RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException {
    // assign the next PDU sequence # if its not yet assigned
    if (!pdu.hasSequenceNumberAssigned()) {
        pdu.setSequenceNumber(this.getSequenceNumber().next());
    }
    // encode the pdu into a buffer
    ChannelBuffer buffer;
    if (this.malformedPacket) {
        this.malformedPacket = false;
        buffer = this.testPduTranscoder.encode(pdu);
    } else {
        buffer = this.getTranscoder().encode(pdu);
    }
    WindowFuture<Integer, PduRequest, PduResponse> future = null;
    try {
        future = this.getSendWindow().offer(pdu.getSequenceNumber(), pdu, timeoutMillis, this.getConfiguration().getRequestExpiryTimeout(), synchronous);
    } catch (DuplicateKeyException e) {
        throw new UnrecoverablePduException(e.getMessage(), e);
    } catch (OfferTimeoutException e) {
        throw new SmppTimeoutException(e.getMessage(), e);
    }
    if (this.sessionHandler instanceof SmppSessionListener) {
        if (!((SmppSessionListener) this.sessionHandler).firePduDispatch(pdu)) {
            // logger.info("dispatched request PDU discarded: {}", pdu);
            // @todo probably throwing exception here is better solution?
            future.cancel();
            return future;
        }
    }
    // during the encoding process such as looking up the result message
    if (this.getConfiguration().getLoggingOptions().isLogPduEnabled()) {
        if (synchronous) {
        // logger.info("sync send PDU: {}", pdu);
        } else {
        // logger.info("async send PDU: {}", pdu);
        }
    }
    // write the pdu out & wait timeout amount of time
    ChannelFuture channelFuture = this.getChannel().write(buffer).await();
    // check if the write was a success
    if (!channelFuture.isSuccess()) {
        // the write failed, make sure to throw an exception
        throw new SmppChannelException(channelFuture.getCause().getMessage(), channelFuture.getCause());
    }
    return future;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) PduRequest(com.cloudhopper.smpp.pdu.PduRequest) SmppTimeoutException(com.cloudhopper.smpp.type.SmppTimeoutException) PduResponse(com.cloudhopper.smpp.pdu.PduResponse) UnrecoverablePduException(com.cloudhopper.smpp.type.UnrecoverablePduException) OfferTimeoutException(com.cloudhopper.commons.util.windowing.OfferTimeoutException) SmppChannelException(com.cloudhopper.smpp.type.SmppChannelException) DuplicateKeyException(com.cloudhopper.commons.util.windowing.DuplicateKeyException) SmppSessionListener(com.cloudhopper.smpp.SmppSessionListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 38 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project feeyo-hlsserver by variflight.

the class HttpUtil method sendError.

public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus code) throws InterruptedException {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, code);
    ChannelFuture channelFuture = ctx.getChannel().write(response).sync();
    if (channelFuture.isSuccess()) {
        channelFuture.getChannel().close();
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 39 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project feeyo-hlsserver by variflight.

the class HttpUtil method sendNotModified.

public static void sendNotModified(ChannelHandlerContext ctx) {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_MODIFIED);
    ChannelFuture channelFuture = ctx.getChannel().write(response);
    if (channelFuture.isSuccess()) {
        channelFuture.getChannel().close();
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 40 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project tez by apache.

the class TestShuffleHandler method createMockChannelFuture.

public ChannelFuture createMockChannelFuture(Channel mockCh, final List<ShuffleHandler.ReduceMapFileCount> listenerList) {
    final ChannelFuture mockFuture = mock(ChannelFuture.class);
    when(mockFuture.getChannel()).thenReturn(mockCh);
    Mockito.doReturn(true).when(mockFuture).isSuccess();
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // Add ReduceMapFileCount listener to a list
            if (invocation.getArguments()[0].getClass() == ShuffleHandler.ReduceMapFileCount.class)
                listenerList.add((ShuffleHandler.ReduceMapFileCount) invocation.getArguments()[0]);
            return null;
        }
    }).when(mockFuture).addListener(Mockito.any(ShuffleHandler.ReduceMapFileCount.class));
    return mockFuture;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)138 Channel (org.jboss.netty.channel.Channel)40 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)38 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)28 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)27 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)26 InetSocketAddress (java.net.InetSocketAddress)25 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)23 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)23 Test (org.junit.Test)15 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 ConnectException (java.net.ConnectException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6