Search in sources :

Example 6 with MessageEvent

use of org.jboss.netty.channel.MessageEvent in project NabAlive by jcheype.

the class IqHandler method onMessage.

public void onMessage(ChannelHandlerContext ctx, MessageEvent e, Status status, String message, Document document) {
    String id = document.getDocumentElement().getAttribute("id");
    String from = document.getDocumentElement().getAttribute("from");
    Jid jid = new Jid(from);
    String to = document.getDocumentElement().getAttribute("to");
    final Matcher matcher = statusChangePattern.matcher(message);
    if (matcher.find()) {
        String resource = matcher.group(1);
        logger.info("change status: " + resource);
        String reply = "<iq id='" + id + "' type='result'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + "<jid>" + jid.getUser() + "@" + jid.getServer() + "/" + resource + "</jid></bind></iq>";
        write(e.getChannel(), reply);
        jid.setResource(resource);
        status.setJid(jid);
        status.onEvent(new Event(message, Event.Type.BIND_RESOURCE));
    } else if (message.contains("<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>")) {
        String reply = "<iq id='" + id + "' type='result' from='" + to + "'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>";
        write(e.getChannel(), reply);
    } else if (message.contains("<query xmlns=\"violet:iq:sources\"><packet xmlns=\"violet:packet\" format=\"1.0\"/></query>")) {
        String reply = "<iq from='net.violet.platform@xmpp.nabaztag.com/sources'" + "to='" + from + "' id='" + id + "' type='result'>" + "<query xmlns='violet:iq:sources'>" + "<packet xmlns='violet:packet' format='1.0' ttl='604800'>fwQAAAx////+BAAFAA7/CAALAAABAP8=</packet>" + "</query>" + "</iq>";
        write(e.getChannel(), reply);
    } else if (message.contains("<unbind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>")) {
        String reply = "<iq id='" + id + "' type='result'/>";
        write(e.getChannel(), reply);
        status.onEvent(new Event(message, Event.Type.UNBIND_RESOURCE));
    }
}
Also used : Jid(com.nabalive.server.jabber.util.Jid) Matcher(java.util.regex.Matcher) MessageEvent(org.jboss.netty.channel.MessageEvent) Event(com.nabalive.common.server.Event)

Example 7 with MessageEvent

use of org.jboss.netty.channel.MessageEvent in project NabAlive by jcheype.

the class MessageHandler method onMessage.

@Override
protected void onMessage(ChannelHandlerContext ctx, MessageEvent e, Status status, String message, Document document) {
    String from = document.getDocumentElement().getAttribute("from");
    if (message.contains("<button xmlns=\"violet:nabaztag:button\">"))
        status.onEvent(new Event(message, Event.Type.BUTTON));
    else if (message.contains("<ears xmlns=\"violet:nabaztag:ears\">"))
        status.onEvent(new Event(message, Event.Type.EARS));
    else
        status.onEvent(new Event(message, Event.Type.UNKNOWN_MESSAGE));
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) Event(com.nabalive.common.server.Event)

Example 8 with MessageEvent

use of org.jboss.netty.channel.MessageEvent in project NabAlive by jcheype.

the class PresenceHandler method onMessage.

public void onMessage(ChannelHandlerContext ctx, MessageEvent e, Status status, String message, Document document) {
    String id = document.getDocumentElement().getAttribute("id");
    String from = document.getDocumentElement().getAttribute("from");
    String reply = "<presence from='" + from + "' to='" + from + "' id='" + id + "'/>";
    write(e.getChannel(), reply);
    Matcher matcher = pattern.matcher(from);
    if (matcher.find()) {
        String presence = matcher.group(1);
        status.setPresence(presence);
    }
    status.onEvent(new Event(message, Event.Type.PRESENCE));
}
Also used : Matcher(java.util.regex.Matcher) MessageEvent(org.jboss.netty.channel.MessageEvent) Event(com.nabalive.common.server.Event)

Example 9 with MessageEvent

use of org.jboss.netty.channel.MessageEvent in project adbcj by mheath.

the class Handler method handleDownstream.

public void handleDownstream(ChannelHandlerContext context, ChannelEvent event) throws Exception {
    if (!(event instanceof MessageEvent)) {
        context.sendDownstream(event);
        return;
    }
    MessageEvent e = (MessageEvent) event;
    if (!(e.getMessage() instanceof ClientRequest)) {
        context.sendDownstream(event);
        return;
    }
    ChannelBuffer buffer = ChannelBuffers.buffer(1024);
    ChannelBufferOutputStream out = new ChannelBufferOutputStream(buffer);
    encoder.encode((ClientRequest) e.getMessage(), out);
    Channels.write(context, e.getFuture(), buffer);
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) MessageEvent(org.jboss.netty.channel.MessageEvent) ClientRequest(org.adbcj.mysql.codec.ClientRequest) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 10 with MessageEvent

use of org.jboss.netty.channel.MessageEvent in project hadoop by apache.

the class TestShuffleHandler method testSendMapCount.

@Test(timeout = 4000)
public void testSendMapCount() throws Exception {
    final List<ShuffleHandler.ReduceMapFileCount> listenerList = new ArrayList<ShuffleHandler.ReduceMapFileCount>();
    final ChannelHandlerContext mockCtx = Mockito.mock(ChannelHandlerContext.class);
    final MessageEvent mockEvt = Mockito.mock(MessageEvent.class);
    final Channel mockCh = Mockito.mock(AbstractChannel.class);
    // Mock HttpRequest and ChannelFuture
    final HttpRequest mockHttpRequest = createMockHttpRequest();
    final ChannelFuture mockFuture = createMockChannelFuture(mockCh, listenerList);
    // Mock Netty Channel Context and Channel behavior
    Mockito.doReturn(mockCh).when(mockCtx).getChannel();
    Mockito.when(mockCtx.getChannel()).thenReturn(mockCh);
    Mockito.doReturn(mockFuture).when(mockCh).write(Mockito.any(Object.class));
    Mockito.when(mockCh.write(Object.class)).thenReturn(mockFuture);
    //Mock MessageEvent behavior
    Mockito.doReturn(mockCh).when(mockEvt).getChannel();
    Mockito.when(mockEvt.getChannel()).thenReturn(mockCh);
    Mockito.doReturn(mockHttpRequest).when(mockEvt).getMessage();
    final ShuffleHandler sh = new MockShuffleHandler();
    Configuration conf = new Configuration();
    sh.init(conf);
    sh.start();
    int maxOpenFiles = conf.getInt(ShuffleHandler.SHUFFLE_MAX_SESSION_OPEN_FILES, ShuffleHandler.DEFAULT_SHUFFLE_MAX_SESSION_OPEN_FILES);
    sh.getShuffle(conf).messageReceived(mockCtx, mockEvt);
    assertTrue("Number of Open files should not exceed the configured " + "value!-Not Expected", listenerList.size() <= maxOpenFiles);
    while (!listenerList.isEmpty()) {
        listenerList.remove(0).operationComplete(mockFuture);
        assertTrue("Number of Open files should not exceed the configured " + "value!-Not Expected", listenerList.size() <= maxOpenFiles);
    }
    sh.close();
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) MessageEvent(org.jboss.netty.channel.MessageEvent) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ArrayList(java.util.ArrayList) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

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