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));
// }
}
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);
}
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;
}
}
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());
}
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));
}
Aggregations