How to use the Mobilis XMPP Bean Layer General

How to use the Mobilis XMPP Bean
Layer
General
The preferred way of managing the Info / Query (IQ) communication is via the reusable XMPP Bean
Layer. Each Bean class represents an XMPP IQ. It has methods that define how to convert a Bean into
an XMPP IQ to send it and how to parse the attributes from an incoming IQ into the Bean class. It is
convenient to find both methods in only one location. The other advantage is that the Bean Layer is
used on the server and on the client side. This approach assures that exactly the same IQ syntax is
used on both sides. Furthermore the developer only has to manage the IQs at one location and not
on the server and on the client.
Create your own Bean classes
Every IQ should be encapsulated in a correspondent bean class. These beans should be located in an
own sub package within the project “MobilisXMPP” (see SVN). Example beans can also be found
there. Import the project MobilisXMPP into the build path of your applications.
The super class of each bean is the XMPPBean class. At first you have to override the methods
getChildElement() and getNamespace() that are used to identify the IQ later on.
Furthermore, the methods payloadToXML() and fromXML() have to override the ones from the
super class. They determine how the XML-Structure of a Bean is created and how the attribute
values of an XMPP IQ stanza are parsed into the bean’s attributes. Looking at some example beans
should help to understand how this is done.
During sending and receiving it may be required to have an exact copy of a bean. Therefore you also
have to implement the clone() method for each bean.
Bean handling on the client side
On the client side, there is a class called Parceller, that converts an IQ into a bean and backwards.
Copy that class from another Mobilis application into your project. You have to register your beans to
the Parceller with an instance (prototype) of every bean it should support (see Figure 1).
Figure 1: Registering a bean to the Parceller class.
Receiving IQs
To receive an IQ you have to register an IXMPPIQCallback on the IXMPPService of the MXA. Call the
method registerIQCallback() with the explicit callback, the child element’s name and namespace of
the IQ you want to receive (see Figure 2).
Figure 2: Registering an IQ Callback.
The IXMPPIQCallback could look like the one in Figure 3. Here the Parceller is used to convert the
incoming IQ into a bean.
Figure 3: An IQ Callback.
Sending IQs
Create an instance of the bean you want to send. Add payload information and the IQ attributes
(recipient, type, …). Finally convert it with the Parceller and send it with the IXMPPService. (see
Figure 4)
Figure 4: Sending of an IQ using a bean.
Bean handling on the server side using Smack
Receiving IQs
On the server side you are using Smack as an XMPP library. Smack doesn’t understand the Bean
classes from our Mobilis project. Therefore there are several adapter classes.
If you want to receive an IQ with Smack, you have to register a PacketListener at first. Therefore, you
must create an instance of every Bean you want to receive. These instances (also called prototype)
have to be adopted as an IQ Provider and then have to be added to the ProviderManager (see Figure
5). After that a PacketListener has to be added to the XMPPConnection. Figure 5 demonstrates how
this is done.
Figure 5: Registering of packet listeners.
If the Beans are registered correctly, the method processPacket() of the PacketListener will be called
upon arrival of an IQ stanza. At first it has to be converted into a Bean object with the help of the
BeanIQAdapter. After that you can check what special kind of IQ came in and what its type is (see
Figure 6).
Figure 6: Receiving and processing IQ packets.
Sending IQs
Sending IQs is pretty easy. You only have to create the bean you want to send, add the payload, set
the IQ data (recipient, type, …) , convert it into an IQ with the BeanIQAdapter and then send it (see
Figure 7).
Figure 7: Sending of IQs