JBoss.orgCommunity Documentation

Mobicents JAIN SLEE Google Talk Bot Example User Guide

by Eduardo Martins and Ivelin Ivanov

Abstract

TODO


This manual uses several conventions to highlight certain words and phrases and draw attention to specific pieces of information.

In PDF and paper editions, this manual uses typefaces drawn from the Liberation Fonts set. The Liberation Fonts set is also used in HTML editions if the set is installed on your system. If not, alternative but equivalent typefaces are displayed. Note: Red Hat Enterprise Linux 5 and later includes the Liberation Fonts set by default.

Four typographic conventions are used to call attention to specific words and phrases. These conventions, and the circumstances they apply to, are as follows.

Mono-spaced Bold

Used to highlight system input, including shell commands, file names and paths. Also used to highlight key caps and key-combinations. For example:

The above includes a file name, a shell command and a key cap, all presented in Mono-spaced Bold and all distinguishable thanks to context.

Key-combinations can be distinguished from key caps by the hyphen connecting each part of a key-combination. For example:

The first sentence highlights the particular key cap to press. The second highlights two sets of three key caps, each set pressed simultaneously.

If source code is discussed, class names, methods, functions, variable names and returned values mentioned within a paragraph will be presented as above, in Mono-spaced Bold. For example:

Proportional Bold

This denotes words or phrases encountered on a system, including application names; dialogue box text; labelled buttons; check-box and radio button labels; menu titles and sub-menu titles. For example:

The above text includes application names; system-wide menu names and items; application-specific menu names; and buttons and text found within a GUI interface, all presented in Proportional Bold and all distinguishable by context.

Note the > shorthand used to indicate traversal through a menu and its sub-menus. This is to avoid the difficult-to-follow 'Select Mouse from the Preferences sub-menu in the System menu of the main menu bar' approach.

Mono-spaced Bold Italic or Proportional Bold Italic

Whether Mono-spaced Bold or Proportional Bold, the addition of Italics indicates replaceable or variable text. Italics denotes text you do not input literally or displayed text that changes depending on circumstance. For example:

Note the words in bold italics above username, domain.name, file-system, package, version and release. Each word is a placeholder, either for text you enter when issuing a command or for text displayed by the system.

Aside from standard usage for presenting the title of a work, italics denotes the first use of a new and important term. For example:

If you find a typographical error in this manual, or if you have thought of a way to make this manual better, we would love to hear from you! Please submit a report in the the Issue Tracker, against the product Mobicents JAIN SLEE Google Talk Bot Example, or contact the authors.

When submitting a bug report, be sure to mention the manual's identifier: JAIN_SLEE_GoogleTalkBot_EXAMPLE_User_Guide

If you have a suggestion for improving the documentation, try to be as specific as possible when describing it. If you have found an error, please include the section number and some of the surrounding text so we can find it easily.

This section provides instructions on how to obtain and build the Google Talk Bot Example from source code.

  1. Downloading the source code

    Use SVN to checkout a specific release source, the base URL is http://mobicents.googlecode.com/svn/tags/servers/jain-slee/2.x.y/examples/google-talk-bot, then add the specific release version, lets consider 2.0.0.GA.

    [usr]$ svn co http://mobicents.googlecode.com/svn/tags/servers/jain-slee/2.x.y/examples/google-talk-bot/2.0.0.GA slee-example-google-talk-bot-2.0.0.GA
  2. Building the source code

    Important

    Maven 2.0.9 (or higher) is used to build the release. Instructions for using Maven2, including install, can be found at http://maven.apache.org

    Use Maven to build the deployable unit binary.

    				    [usr]$ cd slee-example-google-talk-bot-2.0.0.GA
    				    [usr]$ mvn install
    				    

    Once the process finishes you should have the deployable-unit jar file in the target directory, if Mobicents JAIN SLEE is installed and environment variable JBOSS_HOME is pointing to its underlying JBoss Application Server directory, then the deployable unit jar will also be deployed in the container.

    Important

    This procedure does not install the Example's dependencies

Similar process as for Section 2.2.1, “Release Source Code Building”, the only change is the SVN source code URL, which is http://mobicents.googlecode.com/svn/trunk/servers/jain-slee/examples/google-talk-bot.

The example application is defined by a service descriptor, which refers the Root SBB. The Root SBB does not defines child relations, which means the application is a single SBB.

Important

To obtain the example's complete source code please refer to Section 2.2, “Mobicents JAIN SLEE Google Talk Bot Example Source Code”.

The Google Talk Bot Example's Root SBB is composed by the abstract class and the XML descriptor.

The class org.mobicents.examples.googletalk.GoogleTalkBotSbb includes all the service logic for the example.

The javax.slee.SbbObject's setSbbContext(SbbContext) is used by SBBs to store the SBB's context into a class field. The SBB should take the opportunity to also store objects, such as SLEE facilities, or env entries property values, which are common for all service logic entities, a.k.a. SbbEntities, and thus may be stored in the SbbObject instance.

The class fields and setSbbContext(SbbContext) method's and related code:



    /*
     * (non-Javadoc)
     * 
     * @see javax.slee.Sbb#setSbbContext(javax.slee.SbbContext)
     */
    public void setSbbContext(SbbContext context) {
        this.sbbContext = context;
        this.tracer = sbbContext.getTracer(getClass().getSimpleName());
        try {
            Context myEnv = (Context) new InitialContext()
                    .lookup("java:comp/env");
            xmppSbbInterface = (XmppResourceAdaptorSbbInterface) myEnv
                    .lookup("slee/resources/xmpp/2.0/xmppinterface");
            xmppActivityContextInterfaceFactory = (XmppActivityContextInterfaceFactory) myEnv
                    .lookup("slee/resources/xmpp/2.0/factoryprovider");
            // env-entries
            username = (String) myEnv.lookup("username");
            password = (String) myEnv.lookup("password");
            tracer.info("setSbbContext() Retrieved uid[" + username + "],"
                    + " passwd[" + password + "]");
        } catch (NamingException ne) {
            tracer.severe("Could not set SBB context:" + ne.getMessage(), ne);
        }
    }
                

The Message event indicates that an XMPP MESSAGE stanza was received in the managed XMPP connection. The application simply checks if its body is time, and if that is the case sends back the system time, otherwise sends the count of chars received..

The event handler method's code:



    /**
     * This is the point where we already have a chat session with the user, so,
     * when they send us messages, we count the chars and reply or tell time :)
     * 
     * @param message
     * @param aci
     */
    public void onMessage(org.jivesoftware.smack.packet.Message message,
            ActivityContextInterface aci) {
        // only process messages which are not an error and not sent by another
        // bot instance
        if (!message.getType().equals(Message.Type.ERROR)
                && !StringUtils.parseBareAddress(message.getFrom()).equals(
                        username + "@" + serviceName)) {
            tracer.info("XMPP Message event type! Message Body: '"
                    + message.getBody() + "'. " + "Sent by '"
                    + message.getFrom() + "'.");
            String body = null;
            if (message.getBody() != null) {
                if (message.getBody().equalsIgnoreCase("time")) {
                    body = "My system time is " + new Date().toString();
                } else {
                    body = message.getBody().length() + " chars in message <"
                            + message.getBody() + ">.";
                }
            }
            Message msg = new Message(message.getFrom(), message.getType());
            msg.setBody(body);
            xmppSbbInterface.sendPacket(connectionID, msg);
        }
    }
                

The Root SBB XML Descriptor has to be provided and match the abstract class code.

First relevant part is the declaration of the sbb-classes element, where the sbb class abstract name must be specified:



        <sbb-classes>
            <sbb-abstract-class>
                <sbb-abstract-class-name>
                    org.mobicents.examples.googletalk.GoogleTalkBotSbb
                </sbb-abstract-class-name>
            </sbb-abstract-class>
        </sbb-classes>
            

Then the events handled by the SBB must be specified too:



        <event event-direction="Receive" initial-event="True">
            <event-name>StartServiceEvent</event-name>
            <event-type-ref>
                <event-type-name>
                    javax.slee.serviceactivity.ServiceStartedEvent
                </event-type-name>
                <event-type-vendor>javax.slee</event-type-vendor>
                <event-type-version>1.1</event-type-version>
            </event-type-ref>
            <initial-event-select variable="ActivityContext" />
        </event>
        <event event-direction="Receive" initial-event="False">
            <event-name>ActivityEndEvent</event-name>
            <event-type-ref>
                <event-type-name>
                    javax.slee.ActivityEndEvent
                </event-type-name>
                <event-type-vendor>javax.slee</event-type-vendor>
                <event-type-version>1.0</event-type-version>
            </event-type-ref>
        </event>
        <event event-direction="Receive" initial-event="False">
            <event-name>Message</event-name>
            <event-type-ref>
                <event-type-name>
                    org.jivesoftware.smack.packet.Message
                </event-type-name>
                <event-type-vendor>
                    org.jivesoftware.smack
                </event-type-vendor>
                <event-type-version>1.0</event-type-version>
            </event-type-ref>
        </event>
        <event event-direction="Receive" initial-event="False">
            <event-name>Presence</event-name>
            <event-type-ref>
                <event-type-name>
                    org.jivesoftware.smack.packet.Presence
                </event-type-name>
                <event-type-vendor>
                    org.jivesoftware.smack
                </event-type-vendor>
                <event-type-version>1.0</event-type-version>
            </event-type-ref>
        </event>
            

Note that there is a single event defined as initial, which triggers the xmpp connection creation, remaining events all happen in the XMPP connection activity, that the service instance is already attached.

Next are the env-entries, which are used as configuration properties, and contains the Google Talk account credentials:



        <env-entry>
            <env-entry-name>username</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>mobicents.org</env-entry-value>
        </env-entry>

        <env-entry>
            <env-entry-name>password</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>m0b1c3nts</env-entry-value>
        </env-entry>
            

Finally, the XMPP Resource Adaptor must be specified also, otherwise SLEE won't put its SBB Interface in the SBB's JNDI Context:



        <resource-adaptor-type-binding>
            <resource-adaptor-type-ref>
                <resource-adaptor-type-name>
                    XMPPResourceAdaptorType
                </resource-adaptor-type-name>
                <resource-adaptor-type-vendor>
                    org.mobicents
                </resource-adaptor-type-vendor>
                <resource-adaptor-type-version>
                    2.0
                </resource-adaptor-type-version>
            </resource-adaptor-type-ref>
            <activity-context-interface-factory-name>
                slee/resources/xmpp/2.0/factoryprovider
            </activity-context-interface-factory-name>
            <resource-adaptor-entity-binding>
                <resource-adaptor-object-name>
                    slee/resources/xmpp/2.0/xmppinterface
                </resource-adaptor-object-name>
                <resource-adaptor-entity-link>
                    XMPPRA
                </resource-adaptor-entity-link>
            </resource-adaptor-entity-binding>
        </resource-adaptor-type-binding>
            

To try the example application a Google Talk account and client is needed, refer to the Google Talk website for instructions.

Important

To exchange messages, the Google Talk account needs to add the Google Talk Bot account in its buddy list. The Google Talk Bot account is mobicents.org@gmail.com.

Once Google Talk is fully setup and running, start Mobicents JAIN SLEE and once the service is activated the Hi, I'm online too! message should popup, as depicted in the image below.

Google Talk Bot saying Hi!

At this point messages can be exchanged, sending time will get a reply with the system time, and sending hello will get a 5 chars in message ... reply, as depicted in the image below.

Google Talk Bot message exchanging.

Revision History
Revision 1.0Tue Dec 30 2009Eduardo Martins
Creation of the Mobicents JAIN SLEE Google Talk Bot Example User Guide.