use of org.jboss.netty.channel.MessageEvent in project adbcj by mheath.
the class Encoder method handleDownstream.
public void handleDownstream(ChannelHandlerContext context, ChannelEvent event) throws Exception {
if (!(event instanceof MessageEvent)) {
context.sendDownstream(event);
return;
}
MessageEvent e = (MessageEvent) event;
Object message = e.getMessage();
boolean singleMessage = message instanceof AbstractFrontendMessage;
boolean multipleMessages = message instanceof AbstractFrontendMessage[];
if (!singleMessage && !multipleMessages) {
context.sendDownstream(event);
return;
}
ChannelBuffer buffer = ChannelBuffers.buffer(1024);
ChannelBufferOutputStream out = new ChannelBufferOutputStream(buffer);
if (singleMessage) {
encoder.encode(out, (AbstractFrontendMessage) e.getMessage());
} else {
encoder.encode(out, (AbstractFrontendMessage[]) e.getMessage());
}
Channels.write(context, e.getFuture(), buffer);
}
use of org.jboss.netty.channel.MessageEvent 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.MessageEvent in project neo4j by neo4j.
the class NetworkReceiverTest method testMessageReceivedOriginFix.
@Test
public void testMessageReceivedOriginFix() throws Exception {
LogProvider logProvider = mock(LogProvider.class);
when(logProvider.getLog(NetworkReceiver.class)).thenReturn(mock(Log.class));
NetworkReceiver networkReceiver = new NetworkReceiver(mock(NetworkReceiver.Monitor.class), mock(NetworkReceiver.Configuration.class), logProvider);
// This defines where message is coming from
final InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", PORT);
final Channel channel = mock(Channel.class);
when(channel.getLocalAddress()).thenReturn(inetSocketAddress);
when(channel.getRemoteAddress()).thenReturn(inetSocketAddress);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.getChannel()).thenReturn(channel);
final Message message = Message.to(new MessageType() {
@Override
public String name() {
return "test";
}
}, new URI("cluster://anywhere"));
MessageEvent messageEvent = mock(MessageEvent.class);
when(messageEvent.getRemoteAddress()).thenReturn(inetSocketAddress);
when(messageEvent.getMessage()).thenReturn(message);
when(messageEvent.getChannel()).thenReturn(channel);
// the original FROM header should be ignored
message.setHeader(Message.FROM, "cluster://someplace:1234");
networkReceiver.new MessageReceiver().messageReceived(ctx, messageEvent);
assertEquals("FROM header should have been changed to visible ip address: " + message.getHeader(Message.FROM), "cluster://127.0.0.1:1234", message.getHeader(Message.FROM));
}
use of org.jboss.netty.channel.MessageEvent in project neo4j by neo4j.
the class ServerTest method message.
private MessageEvent message(RequestType reqType, RequestContext ctx, Channel serverToClientChannel, Serializer payloadSerializer) throws IOException {
ByteBuffer backingBuffer = ByteBuffer.allocate(1024);
protocol.serializeRequest(new RecordingChannel(), new ByteBufferBackedChannelBuffer(backingBuffer), reqType, ctx, payloadSerializer);
MessageEvent event = mock(MessageEvent.class);
when(event.getMessage()).thenReturn(new ByteBufferBackedChannelBuffer(backingBuffer));
when(event.getChannel()).thenReturn(serverToClientChannel);
return event;
}
use of org.jboss.netty.channel.MessageEvent in project databus by linkedin.
the class MockServerChannelHandler method writeRequested.
@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("<=<=<=<=WRite requested" + e.getMessage());
printMessage("<=<=<=<=", e);
}
MessageEvent newMessage = e;
if (_saveTheFuture) {
if (_future == null) {
// save the real future - so we can fail it
_future = e.getFuture();
// to make sure client's Netty thread will not get the update - we substitute a fake future
ChannelFuture newFuture = Channels.future(ctx.getChannel());
newMessage = new DownstreamMessageEvent(ctx.getChannel(), newFuture, e.getMessage(), e.getRemoteAddress());
if (LOG.isDebugEnabled())
LOG.debug("Saving the future:" + _future);
}
}
super.writeRequested(ctx, newMessage);
}
Aggregations