What's new? | Help | Directory | Sign in
Google
emite
Let's make gwt apps chat each other!
  
  
  
  
    
Search
for
Updated May 06, 2008 by vruiz.jurado
Labels: Featured, Phase-Implementation
HowToExtendEmite  

Extend emite its an easy task. You can use the emite object to subscribe, publish events or send stanzas.

Write the components

Components are simple objects implementing Installable interface. Normally they have a dependency (in the constructor) with emite. A simple Echo component would be like this:

public class Echo implements Installable {

  private final Emite emite;

  public Echo(final Emite emite) {
    this.emite = emite;
  }

  public void install() {
    emite.subscribe(Matchers.when("message"), new PacketListener() {
      public void handle(final IPacket received) {
        echo(new Message(received));
      }
    });
  }

  private void echo(final Message message) {
    // exchange the from and to...
    final Message response = new Message(message.getToURI(), message.getFromURI(), message.getBody());
    emite.send(response);
  }
}

Write a module

We should pack several related components in a module: just a class with a couple of static methods. We use the container to register objects, but also to retrieve other components previously registered (the dependencies). Here we need the emite component from the CoreModule:

public class EchoModule {

  public static final String COMPONENT_ECHO = "echo:echo";

  public static Echo getEcho(final Container container) {
    return (Echo) container.get(COMPONENT_ECHO);
  }

  public static void load(final Container container) {
    final Emite emite = CoreModule.getEmite(container);
    final Echo echo = new Echo(emite);
    container.register(COMPONENT_ECHO, echo);
  }
}

Use the module

You can install the module in a container. The xmpp is a container preloaded with three modules (CoreModule, !XMPPModule and InstantMessagingModule).

final Xmpp xmpp = Xmpp.create(new BoshOptions("/http-base"));
EchoModule.install(xmpp);

Testing the components

There is a special handy class EmiteTestHelper that implements Emite interface and also provides

  1. receives method to simulate an incoming event or stanza
  2. verifyXXX family methods to verify the behaviour of our components
All the testing infraestrcture is based on JUnit and Mockito, so you can use all tools they provide. Emite library have hundred of test; they are one of the fastest way to learn how emite works. Here is our test:

public class EchoTest {
  @Test
  public void shouldAnswer() {
    final EmiteTestHelper emite = new EmiteTestHelper();
    emite.install(new Echo(emite));
    emite.receives("<message from='contact@domain' to='me@domain'><body>Hello!</body></message>");
    emite.verifySent("<message from='me@domain' to='contact@domain'><body>Hello!</body></message>");
  }
}


Sign in to add a comment