If you are looking for Jitsi Meet, the WebRTC compatible video conferencing product click here.

previous | TOC | next

Implementing a Protocol

OperationSetBasicTypingNotifications.

This is probably going to be the shortest trail in the tutorial since it is really extremely easy to implement typing notifications in Jitsi. Quite as with instant messaging we need to do 2 things:

Register event listeners and dispatch typing notification events

In the previous trails we’ve already seen how we manage listeners and fireXxxEvent() methods so I won’t be going into the details. It all comes down to using a Vector for the registered listeners, and making sure that we sync everything properly.

If you’re curious (or just looking for a copy/paste source) look at the following three methods in OperationSetTypingNotificationsGibberishImpl:

public void addTypingNotificationsListener(TypingNotificationsListener listener);
public void removeTypingNotificationsListener(TypingNotificationsListener listener);
private void fireTypingNotificationsEvent(Contact sourceContact, int evtCode);

Send typing notifications

Once again the tricky part comes from the fact that we’d like typing notifications to be sent from one provider to another if appropriate:

public void sendTypingNotification(Contact notifiedContact, int typingState)
    throws IllegalStateException, IllegalArgumentException
{
    if( !(notifiedContact instanceof ContactGibberishImpl) )
       throw new IllegalArgumentException(
           "The specified contact is not a Gibberish contact."
           + notifiedContact);


    String userID = notifiedContact.getAddress();

    //if the user id is owr own id, then this message is being routed to us
    //from another instance of the gibberish provider.
    if (userID.equals(this.parentProvider.getAccountID().getUserID()))
    {
        //check who is the provider sending the message
        String sourceUserID = notifiedContact.getProtocolProvider()
            .getAccountID().getUserID();

        //check whether they are in our contact list
        Contact from = opSetPersPresence.findContactByID(sourceUserID);

        //and if not - add them there as volatile.
        if (from == null)
        {
            from = opSetPersPresence.createVolatileContact(sourceUserID);
        }

        //and now fire the message received event.
        fireTypingNotificationsEvent(from, typingState);
    }
    else
    {
        //if userID is not our own, try a check whether another provider
        //has that id and if yes - deliver the message to them.
        ProtocolProviderServiceGibberishImpl gibberishProvider
            = this.opSetPersPresence.findProviderForGibberishUserID(userID);
        if (gibberishProvider != null)
        {
            OperationSetTypingNotificationsGibberishImpl opSetTN
                = (OperationSetTypingNotificationsGibberishImpl)
                gibberishProvider.getOperationSet(
                    OperationSetTypingNotifications.class);
            opSetTN.sendTypingNotification(notifiedContact, typingState);
        }
        else
        {
            //if we got here then "to" is simply someone in our contact
            //list so let's just echo the message.
            fireTypingNotificationsEvent(notifiedContact, typingState);
        }
    }
}

And once again we implement this by splitting possible scenarios into three cases:

  1. Another protocol provider would like to send us a typing notification - in this case we check whether we have them in our contact list and if not we add them as a non-persistent contact, then we deliver the typing notification to the user as one being sent from that contact.
  2. The second case is actually the opposite of the first one. The user id that we are trying to deliver the typing notification to is not our own so we try to find whether another provider has that id, if they do, then we call their sendTypingNotification() method and it would execute its case 1 just as we described it above.
  3. The third case actually handles all the bogus contacts that a user can send typing notifications to in the Gibberish protocol and it simply echos the typing notification back to the user.

previous | TOC | next