Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Mobilis Beans

ubuntudroid edited this page May 21, 2013 · 13 revisions

Table of Contents

General

The preferred way of managing the XMPP communication is via the reusable XMPP Bean Layer. Each Bean class represents an XMPP stanza. It has methods that define how to convert a Bean into an XMPP stanza to send it and how to parse the attributes from an incoming stanza into the Bean class. It is convenient to find both methods in one location. The other advantage is that the Bean Layer is used on the server and on the client side. This approach ensures that exactly the same stanza model is used on both sides. Furthermore the developer only has to manage the stanza model at one location (which is the MSDL) and not separately in the server and the client project.

Note: References on this page to AService simply refer to the name of your Service as defined in your MSDL. The XSLT scripts generate class names accordingly.

Receiving IQs

Android clients

After having connected to MXA and the XMPP server you have to register an IXMPPIQCallback at the IXMPPService which is part of MXA to receive XMPP stanzas. Call the method registerIQCallback() with the explicit callback, the child element’s name and namespace of the stanza you want to receive:

// Get XMPPService instance
IXMPPService xmppService = MXAController.get().getXMPPService();

// Register IQ Callbacks
xmppService.registerIQCallback(
   groupInfoCallback,
   ABean.CHILD_ELEMENT,
   ABean.NAMESPACE);

The IXMPPIQCallback could be implemented like in the following example. Here the Parceller is used to convert the incoming stanza into a bean.

// Callback which is informed about arrival of a GroupInfoIQ
IXMPPIQCallback aBeanCallback = new IXMPPIQCallback.Stub(){
   @Override
   public void processIQ(XMPPIQ iq) throws RemoteException {
      parser.setInput(new StringReader(iq.payload)); // parser is an instance of XmlPullParser, you can use one instance for all your callbacks
      ABean aBean = new ABean();
      aBean.fromXML(parser);
      // read data from bean
   }
};

Java clients and Mobilis Services

With pure Java applications you have to use the MXJ instead of the MXA. As the MXJ uses Smack for XMPP communication the process of receiving beans is identical to how Mobilis Services receive (and send) Beans.

If you want to receive an stanza with Smack, you have to register a PacketListener at first. To do so you must create a prototype instance of every Bean you want to receive. These instances have to be adopted as an IQProvider and then have to be added to the ProviderManager (see following example). After that a PacketListener implementation has to be added to the XMPPConnection.

To send and receive XMPP stanzas you have to register your beans first:

private static void registerPacketListener() {
	beanPrototypes.put(ABean.NAMESPACE, ABean.CHILD_ELEMENT, new ABean());
	beanPrototypes.put(AnotherBean.NAMESPACE, AnotherBean.CHILD_ELEMENT, new AnotherBean());
		
	for (XMPPBean prototype : beanPrototypes.getListOfAllValues()) {
		new BeanProviderAdapter(new ProxyBean(prototype.getNamespace(), prototype.getChildElement())).addToProviderManager();
	}

	IQListener iqListener = new IQListener(); // IQListener is your PacketListener implementation
	PacketTypeFilter filter = new PacketTypeFilter(IQ.class);

	con.addPacketListener(iqListener, filter); // con is a XMPPConnection object
}

As soon as the Beans are registered correctly the method processPacket() of your PacketListener implementation will be called upon arrival of an IQ stanza. At first you have to cast the packet to the corresponding Bean class. Eventually you can handle the bean:

@Override
public void processPacket(Packet p){
   super.processPacket(p);
   if (packet instanceof BeanIQAdapter) {
	System.out.println("is of type BeanIQAdapter");
	XMPPBean inBean = ((BeanIQAdapter) packet).getBean();
				
	if (inBean instanceof ProxyBean) {
		ProxyBean proxyBean = (ProxyBean) inBean;
		XMPPBean outBean = null;
		
		if (proxyBean.isTypeOf(ABean.NAMESPACE, ABean.CHILD_ELEMENT)) {
			outBean = aServiceIncomingHandler.onABeanOperation((ABean) proxyBean.parsePayload(new ABean())); // aServiceIncomingHandler is your implementation of the from MSDL generated IAServiceIncomingHandler interface
		} else if (proxyBean.isTypeOf(AnotherBean.NAMESPACE, AnotherBean.CHILD_ELEMENT)) {
			aServiceIncomingHandler.onAnotherBeanOperation((ABean) proxyBean.parsePayload(new AnotherBean()));
		} else {
                        // handling of unexpected beans
		}
	}
   }
}

Sending XMPP stanzas

Android clients

As of now we don't recommend using the IAServiceProxy generated by the MSDL for sending XMPP stanzas on Android for Mobilis beginners as the code can get a little complex during the process and is thus not suitable for describing it in this wiki. If you're an advanced Mobilis programmer who wants to attach callbacks to send requests and wants to make use of the convenient methods of IAServiceProxy have a look at the XHunt Android client implementation for a hint on how to use the proxy with MXA (start with inspecting the MobilisXHuntProxy class).

The recommended way for Mobilis beginners utilizing the IXMPPService methods looks like this:

	
// Get XMPPService instance
IXMPPService xmppService = MXAController.get().getXMPPService();
// send an IQ (for sending Messages and Presence stanzas use the appropriate IXMPPService methods)
xmppService.sendIQ(new Messenger(sendIQAckHandler), new Messenger(
					sendIQResHandler), 0, beanToIQ(out, false));

// handler for ACKs
private Handler sendIQAckHandler = new Handler() {
	@Override
	public void handleMessage(Message msg) {
		Log.v(TAG, "Received IQ Ack");
	}
};

// handler for IQ results
private Handler sendIQResHandler = new Handler() {
	@Override
	public void handleMessage(Message msg) {
		Log.v(TAG, "Received IQ response");
	}
};

// helper method for converting a bean to a XMPPIQ
private XMPPIQ beanToIQ(XMPPBean bean, boolean mergePayload) {
	// default XMPP IQ type
	int type = XMPPIQ.TYPE_GET;

	switch (bean.getType()) {
	case XMPPBean.TYPE_GET:
		type = XMPPIQ.TYPE_GET;
		break;
	case XMPPBean.TYPE_SET:
		type = XMPPIQ.TYPE_SET;
		break;
	case XMPPBean.TYPE_RESULT:
		type = XMPPIQ.TYPE_RESULT;
		break;
	case XMPPBean.TYPE_ERROR:
		type = XMPPIQ.TYPE_ERROR;
		break;
	}

	XMPPIQ iq;

	if (mergePayload)
		iq = new XMPPIQ(bean.getFrom(), bean.getTo(), type, null, null,
				bean.toXML());
	else
		iq = new XMPPIQ(bean.getFrom(), bean.getTo(), type,
				bean.getChildElement(), bean.getNamespace(),
				bean.payloadToXML());

	iq.packetID = bean.getId();

	return iq;
}

You can handle the result IQs in the appropriate IQCallback as described above.

Java clients and Mobilis Services

To send XMPP stanzas in Java you have to implement the from MSDL generated IAServiceOutgoing interface.

public static class MySender implements IAServiceOutgoing {

	@Override
	public void sendXMPPBean(XMPPBean out) {
		out.setFrom(xmppJid);
		con.sendPacket(new BeanIQAdapter(out)); // con is a XMPPConnection object
	}
		
	@Override
	public void sendXMPPBean(XMPPBean out, IXMPPCallback<? extends XMPPBean> callback) {
		con.sendPacket(new BeanIQAdapter(out)); // con is a XMPPConnection object
	}
}

Eventually you can instantiate the AServiceProxy (also generated from MSDL) like so:

AServiceProxy proxy = new AServiceProxy(aServiceOutgoingStub);

The proxy class contains methods to send beans declared in the MSDL.

You can also use the BeanExchanger or BeanSenderReceiver classes for synchronous bean sending/receiving.

Clone this wiki locally