Skip to main content

Typing Detection in SailFin CAFE

Posted by binod on November 16, 2009 at 8:22 AM PST




RFC 3994 standardized how SIP applications implement "Indication of Messages" or "Typing detection" at protocol level. SailFin CAFE provides a simple way to detect typing using a Communication Bean. Also a web application can send a "Message Indication" to a SIP client.

Here is the code that implements receiving Instant Messages and Message Indication from a SIP client.

@CommunicationBean
public class SimpleCommsBean {

    @Context CommunicationContext ctx;

    @CommunicationEvent(type=CommunicationEvent.Type.MESSAGEINDICATION)
    void handleMessageIndication() {
        IMConversation conv = (IMConversation) ctx.getCommunication();
        MessageIndication msg = (MessageIndication) ctx.getMessage();
        System.out.println("Message State : " + msg.getState());
        System.out.println("Next Message Type : " + msg.getNextMessageType());
    }

    @CommunicationEvent(type=CommunicationEvent.Type.MESSAGEARRIVED)
    void handleMessage() {
        IMConversation conv = (IMConversation) ctx.getCommunication();
        TextMessage msg = (TextMessage) ctx.getMessage();
        System.out.println("Received Message" + msg.getText());
    }
}

In the code above, the first event gives an indication about the message (eg: whether the user is actively typing, what is the type of next message etc).
In the next event, the application receives the actual message. Both these , on a typical SIP Servlet application , would be received as the content of a SIP
message and application would need to interpret the content. Message Indication is an xml document, which would otherwise need to be parsed by the SIP Servlet. Here the CAFE framework
handles the parsing the message content and provides it as an appropriate POJO to the application.

Similarly, a servlet or JSP would send a Message Indication or Text Message to SIP client by creating an IMConversation. Here is the sample
servlet code that send IM message.

   @Context CommunicationSession session;

    @Override
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    java.io.IOException {
   
        java.io.PrintWriter out = response.getWriter();
        String party1 = request.getParameter("party1");
        String party2 = request.getParameter("party2");
        try {
            out.println("");
            IMConversation conv  = session.createIMConversation(party1);
            conv.addParticipant(party2);
            conv.createMessageIndication().send();
            conv.createTextMessage("Hi There").send();

            out.println("IMConversation started between " + party1 + "and" + party2);
        } catch (Exception e) {
            out.println(e);
        }

For more information take a look at the javadoc for IMConversation, TextMessage and MessageIndication.





Related Topics >>