Search in sources :

Example 91 with Channel

use of org.jboss.netty.channel.Channel in project bagheera by mozilla-metrics.

the class SubmissionHandlerTest method testSetFields.

@Test
public void testSetFields() throws Exception {
    SubmissionHandler handler = new SubmissionHandler(null, null, null, null);
    BagheeraMessage.Builder builder = BagheeraMessage.newBuilder();
    BagheeraMessage before = builder.buildPartial();
    assertEquals("", before.getId());
    assertEquals("", before.getNamespace());
    assertEquals("", before.getApiVersion());
    assertEquals(0, before.getPartitionCount());
    assertEquals(0l, before.getTimestamp());
    String expectedNamespace = "test";
    String expectedApiVersion = "2.5";
    String expectedId = "hello there";
    long expectedTimestamp = System.currentTimeMillis();
    List<String> expectedPartitions = new ArrayList<String>();
    BagheeraHttpRequest request = Mockito.mock(BagheeraHttpRequest.class);
    Mockito.when(request.getNamespace()).thenReturn(expectedNamespace);
    Mockito.when(request.getApiVersion()).thenReturn(expectedApiVersion);
    Mockito.when(request.getId()).thenReturn(expectedId);
    Mockito.when(request.getPartitions()).thenReturn(expectedPartitions);
    // Make sure we don't interrogate the mocked InetSocketAddress below.
    Mockito.when(request.getHeader(HttpUtil.X_FORWARDED_FOR)).thenReturn("123.123.123.123");
    MessageEvent event = Mockito.mock(MessageEvent.class);
    Channel channel = Mockito.mock(Channel.class);
    Mockito.when(event.getChannel()).thenReturn(channel);
    InetSocketAddress address = Mockito.mock(InetSocketAddress.class);
    Mockito.when(channel.getRemoteAddress()).thenReturn(address);
    // Do not set the ID
    // handler.setMessageFields(request, event, builder, expectedTimestamp, false);
    BagheeraMessage after = builder.build();
    // <-- missing ID
    assertEquals("", after.getId());
    // assertEquals(expectedNamespace, after.getNamespace());
    // assertEquals(expectedApiVersion, after.getApiVersion());
    // assertEquals(0, after.getPartitionCount());
    // assertEquals(expectedTimestamp, after.getTimestamp());
    builder = BagheeraMessage.newBuilder();
// This time, *do* set the ID
// handler.setMessageFields(request, event, builder, expectedTimestamp, true);
// after = builder.build();
// assertEquals(expectedId, after.getId()); // <-- ID has been set.
// assertEquals(expectedNamespace, after.getNamespace());
// assertEquals(expectedApiVersion, after.getApiVersion());
// assertEquals(0, after.getPartitionCount());
// assertEquals(expectedTimestamp, after.getTimestamp());
// // Test without specifying an apiVersion
// Mockito.when(request.getApiVersion()).thenReturn(null);
// builder = BagheeraMessage.newBuilder();
// handler.setMessageFields(request, event, builder, expectedTimestamp, true);
// after = builder.build();
// assertEquals(expectedId, after.getId()); // <-- ID has been set.
// assertEquals(expectedNamespace, after.getNamespace());
// assertEquals("", after.getApiVersion());
// assertEquals(0, after.getPartitionCount());
// assertEquals(expectedTimestamp, after.getTimestamp());
// Test with some partitions
// Expectedpartitions.add("hello");
// expectedPartitions.add("goodbye");
// Mockito.when(request.getPartitions()).thenReturn(expectedPartitions);
// builder = BagheeraMessage.newBuilder();
// assertEquals(0, builder.getPartitionCount());
// handler.setMessageFields(request, event, builder, expectedTimestamp, true);
// after = builder.build();
// assertEquals(expectedPartitions.size(), after.getPartitionCount());
// for (int i = 0; i < expectedPartitions.size(); i++) {
// assertEquals(expectedPartitions.get(i), after.getPartition(i));
// }
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) BagheeraMessage(com.mozilla.bagheera.BagheeraProto.BagheeraMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 92 with Channel

use of org.jboss.netty.channel.Channel in project bagheera by mozilla-metrics.

the class Bagheera method startServer.

/**
 * Start a Bagheera server with the provided settings.
 * Throws if the server could not be started.
 * The caller is responsible for closing the returned instance, and the
 * channel factory if desired.
 */
public static BagheeraServerState startServer(final int port, final boolean tcpNoDelay, final WildcardProperties props, final Producer producer, final NioServerSocketChannelFactory channelFactory, final String channelGroupName, final MetricsManager manager) throws Exception {
    prepareHealthChecks();
    // HTTP server setup.
    final ChannelGroup channelGroup = new DefaultChannelGroup(channelGroupName);
    final ServerBootstrap server = new ServerBootstrap(channelFactory);
    final HttpServerPipelineFactory pipeFactory = new HttpServerPipelineFactory(props, producer, channelGroup, manager);
    server.setPipelineFactory(pipeFactory);
    server.setOption("tcpNoDelay", tcpNoDelay);
    // Disable keep-alive so client connections don't hang around.
    server.setOption("keepAlive", false);
    final Channel channel = server.bind(new InetSocketAddress(port));
    return new BagheeraServerState(port, producer, channelFactory, channel, channelGroup);
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) ChannelGroup(org.jboss.netty.channel.group.ChannelGroup) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Example 93 with Channel

use of org.jboss.netty.channel.Channel in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandlerClient method write.

private void write(final ChannelFuture channelFuture, final InetSocketAddress address, final OslpEnvelope request) throws IOException {
    final Channel channel = channelFuture.getChannel();
    if (channel != null && channel.isConnected()) {
        LOGGER.info("{} Connection established to: {}", channelFuture.getChannel().getId(), address);
    } else {
        LOGGER.info("The connection for device {} is not successful", request.getDeviceId());
        LOGGER.warn("{} Unable to connect to: {}", channelFuture.getChannel().getId(), address);
        throw new IOException("Channel - Unable to connect");
    }
    try {
        channel.write(request);
    } catch (final Exception e) {
        LOGGER.error("{} Exception while writing request: {}", channelFuture.getChannel().getId(), e.getCause(), e);
        this.callbackHandlers.remove(channel.getId());
        throw e;
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException) NoDeviceResponseException(com.alliander.osgp.shared.exceptionhandling.NoDeviceResponseException)

Example 94 with Channel

use of org.jboss.netty.channel.Channel in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandlerServer method processSignedOslpEnvelope.

/**
 * Called when a signed OSLP envelope arrives from signing server. The
 * envelope will be sent to the device which is waiting for a response. The
 * channel for the waiting device should be present in the channelMap.
 *
 * @param signedOslpEnvelopeDto
 *            DTO containing signed OslpEnvelope.
 */
public void processSignedOslpEnvelope(final SignedOslpEnvelopeDto signedOslpEnvelopeDto) {
    // Try to find the channel.
    final Integer channelId = Integer.parseInt(signedOslpEnvelopeDto.getUnsignedOslpEnvelopeDto().getCorrelationUid());
    final Channel channel = this.findChannel(channelId);
    if (channel == null) {
        LOGGER.error("Unable to find channel for channelId: {}. Can't send response message to device.", channelId);
        return;
    }
    // Get signed envelope, log it and send it to device.
    final OslpEnvelope response = signedOslpEnvelopeDto.getOslpEnvelope();
    this.logMessage(response, false);
    channel.write(response);
    LOGGER.info("{} Send OSLP Response: {}", channelId, response.getPayloadMessage());
}
Also used : Channel(org.jboss.netty.channel.Channel) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 95 with Channel

use of org.jboss.netty.channel.Channel in project load-balancer by RestComm.

the class ServerChannelConnector method channelConnected.

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    Channel channel = e.getChannel();
    channels.add(channel);
    if (configuration.isUseSsl()) {
        SslConfiguration sslConfig = configuration.getSslConfiguration();
        if (sslConfig == null)
            throw new IllegalStateException("sslConfiguration must be set");
        SslContextFactory factory = new SslContextFactory(sslConfig);
        SSLEngine sslEngine = factory.newSslEngine();
        sslEngine.setUseClientMode(false);
        channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_SSL_NAME, new SslHandler(sslEngine));
    }
    channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_PDU_DECODER_NAME, new SmppSessionPduDecoder(new DefaultPduTranscoder(new DefaultPduTranscoderContext())));
    ServerConnectionImpl serverConnectionImpl = new ServerConnectionImpl(server.nextSessionId(), channel, lbServerListener, balancerRunner, monitorExecutor, configuration.isUseSsl());
    channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_WRAPPER_NAME, new ServerConnectionHandlerImpl(serverConnectionImpl));
}
Also used : SslContextFactory(com.cloudhopper.smpp.ssl.SslContextFactory) SslConfiguration(com.cloudhopper.smpp.ssl.SslConfiguration) SSLEngine(javax.net.ssl.SSLEngine) SmppSessionPduDecoder(com.cloudhopper.smpp.channel.SmppSessionPduDecoder) Channel(org.jboss.netty.channel.Channel) DefaultPduTranscoderContext(com.cloudhopper.smpp.transcoder.DefaultPduTranscoderContext) DefaultPduTranscoder(com.cloudhopper.smpp.transcoder.DefaultPduTranscoder) SslHandler(org.jboss.netty.handler.ssl.SslHandler)

Aggregations

Channel (org.jboss.netty.channel.Channel)187 InetSocketAddress (java.net.InetSocketAddress)57 Test (org.junit.Test)52 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)40 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)37 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)34 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)34 SocketAddress (java.net.SocketAddress)33 ChannelFuture (org.jboss.netty.channel.ChannelFuture)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)27 Test (org.testng.annotations.Test)23 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)22 IOException (java.io.IOException)21 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Logger (org.apache.log4j.Logger)19 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)16 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)16 ArrayList (java.util.ArrayList)14