I am trying to implement a couple new IQ types for my application, and to familiarise myself with the framework I wanted to implement an IQ type NameListIQ that allows a client to ask another client for a list of names (strings), and to receive an answer.
The provider architecture documentation
seems to deal only with the problem of parsing the custom IQ packets (task (1.) below). But really I need four things:
So I have to implement
(1.) Parse XML -> custom IQ packet (Type.GET)
(2.) Serialise custom IQ packet -> XML (Type.RESULT)
(3.) Functionality that listens for the RESULT packet.
(4.) Functionality that listens for a GET packet and responds with the appropriate RESULT packet.
I implemented getChildElementXML() in my custom IQ class. This solves (2).
I also implemented a subclass NameListIQProvider of IQProvider and implemented its parseIQ() method. I registered a NameListIQProvider instance with the ProviderManager. This sovles (1.)
For (3.), right after I have sent the packet I add a listener to the packet’'s ID. I have to manually remove the listener after the response packet has been received.
For (4.), right after the connection is established I add a PacketTypeListener for NameListIQ.class. This listener modifies the packet by setting its type to Type.RESULT, and initializes the object’'s data structures with the appropriate values. It leaves the PacketID unmodified and sends the packet back to its original sender.
Is the above the standard way of doing these things? It would be nice if there was one interface that had methods for doing all four of these actions, so that one could keep all the code in one place and register it with Smack, rather than having to spread related code over four different places. Specifically, the practice of adding a packet ID listener and removing it after the resulting packet has been received could be implemented by an abstract class in Smack from which we derive.
Also, maybe the provider architecture documentation should cover all four aspects of implementing a new IQ type, rather than just part (1.)? What do you think? Is there an easier way of doing these things.
Also, for the Time IQ packet that you guys always give as an example, I couldn’'t figure out where all this code is implemented. Time is a bean, so (1.) and (2.) are both covered by the Time class itself. But where in Smack is the code for (3.) and (4.)? If I send a Time packet then the other client automatically responds, so at least (4.) must be implemented for Time somewhere in Smack.
I’'d greatly appreciate any help or comments. Thanks in advance!