Wednesday, November 21, 2012

A chat service for App Engine

The vector XS project includes Java programming language code for a simple chat service on Google App Engine.

The XMPP servlet manages a list of people in its roster.

The Occupied function sends an online message

    protected static void Occupied(XAddress from, 
                                   XMPPService xs){

        xs.sendPresence(from.toJID(),
                        PresenceType.AVAILABLE,
                        PresenceShow.CHAT,
                        "occupied",
                        Version.From);
    }

The Idle function sends on offline message

    protected static void Idle(XAddress from, 
                               XMPPService xs){

        xs.sendPresence(from.toJID(),
                        PresenceType.AVAILABLE,
                        PresenceShow.AWAY,
                        "idle",
                        Version.From);
    }

The Probe function sends a presence message for this bot

    protected static void Probe(XAddress from, 
                                XMPPService xs){

        if (Person.ActiveRecipients(from)){

            XMPP.Occupied(from,xs);
        }
        else {

            XMPP.Idle(from,xs);
        }
    }

The Subscribe function creates a roster member and sends a presence message

    protected static void Subscribe(XAddress from, 
                                    XMPPService xs){

        Person personFrom = Person.GetCreate(from);
        personFrom.setAvailable(true);
        personFrom.setResource(from.resource);
        if (personFrom.isDirty()){

            personFrom.save();

            XMPP.Occupied(from);
        }
    }

The Unsubscribe function deletes a roster member

    protected static void Unsubscribe(XAddress from, 
                                      XMPPService xs){

        Person personFrom = Person.Get(from);
        if (null != personFrom)
            personFrom.drop();
    }

The Available function marks a current roster member as available

    protected static void Available(XAddress from, 
                                    XMPPService xs){

        Person personFrom = Person.Get(from);
        if (null != personFrom){
            final String resourceFrom = from.resource;
            final String resourcePerson = personFrom.getResource();

            if (null != resourceFrom && null != resourcePerson){

                if (resourceFrom.equals(resourcePerson)){

                    if (personFrom.setAvailable(true)){
                        personFrom.save();
                    }
                }
            }
            else if (personFrom.setAvailable(true)){
                personFrom.save();
            }
        }
        else {

            XMPP.Subscribe(from,xs);
        }
    }

The Unavailable function marks a current roster member as not available

    protected static void Unavailable(XAddress from, 
                                      XMPPService xs){
        Person personFrom = Person.Get(from);
        if (null != personFrom){
            final String resourceFrom = from.resource;
            final String resourcePerson = personFrom.getResource();

            if (null != resourceFrom && null != resourcePerson){

                if (resourceFrom.equals(resourcePerson)){

                    personFrom.setAvailable(false);
                    /*
                     * Too sticky.. (permit to follow)
                     */
                    personFrom.setResource(null);

                    if (personFrom.isDirty())
                        personFrom.save();
                }
            }
            else {
                personFrom.setAvailable(false);
                personFrom.setResource(null);

                if (personFrom.isDirty())
                    personFrom.save();
            }
        }
    }

No comments: