Search in sources :

Example 1 with MessageEvent

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);
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) AbstractFrontendMessage(org.adbcj.postgresql.codec.frontend.AbstractFrontendMessage) MessageEvent(org.jboss.netty.channel.MessageEvent) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with MessageEvent

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));
// }
}
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 3 with MessageEvent

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));
}
Also used : Message(org.neo4j.cluster.com.message.Message) Log(org.neo4j.logging.Log) InetSocketAddress(java.net.InetSocketAddress) MessageEvent(org.jboss.netty.channel.MessageEvent) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) URI(java.net.URI) LogProvider(org.neo4j.logging.LogProvider) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 4 with MessageEvent

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;
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) ByteBuffer(java.nio.ByteBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer)

Example 5 with MessageEvent

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);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DownstreamMessageEvent(org.jboss.netty.channel.DownstreamMessageEvent) MessageEvent(org.jboss.netty.channel.MessageEvent) DownstreamMessageEvent(org.jboss.netty.channel.DownstreamMessageEvent)

Aggregations

MessageEvent (org.jboss.netty.channel.MessageEvent)14 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)4 Test (org.junit.Test)4 Event (com.nabalive.common.server.Event)3 Channel (org.jboss.netty.channel.Channel)3 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)2 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 DefaultChannelFuture (org.jboss.netty.channel.DefaultChannelFuture)2 UpstreamMessageEvent (org.jboss.netty.channel.UpstreamMessageEvent)2 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)2 BagheeraMessage (com.mozilla.bagheera.BagheeraProto.BagheeraMessage)1 Jid (com.nabalive.server.jabber.util.Jid)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 ClientRequest (org.adbcj.mysql.codec.ClientRequest)1 AbstractFrontendMessage (org.adbcj.postgresql.codec.frontend.AbstractFrontendMessage)1