-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Sending Iq messages
Sending an Iq message always involves receiving an immediate response from server. The only way to determine that a specific iq response is associated to a specific iq we previously sent is through its id attribute.
An iq response always carries the same id as the iq request we initially sent, therefore some kind of mapping between received responses and sent iq requests must be done. This is done in YowProtocolLayers by using the method "_sendIq" for sending Iqs. The method accepts as parameters the IqProtocolEntity to send, an optional success callback for when receiving iq response of type "result", and an optional error callback for when receiving iq response of type "error"
Example:
class GroupsLayer(YowProtocolLayer):
def getMyGroups(self):
self._sendIq(ListGroupsIqProtocolEntity(), self.onGroupsResult, self.onGroupsError)
def onGroupsResult(self, protocolTreeNode, listGroupsIqProtocolEntity):
self.toUpper(ListGroupsResultIqProtocolEntity.fromProtocolTreeNode(protocolTreeNode))
def onGroupsError(self, protocolTreeNode, listGroupsIqProtocolEntity):
self.toUpper(ErrorIqProtocolEntity.fromProtocolTreeNode(protocolTreeNode))
The same method also is available to YowInterfaceLayer with a slight variation. The callbacks get an actual ProtocolEntity instance instead of ProtocolTreeNode instance, due to the fact that a YowInterfaceLayer is placed on top of YowProtocolLayers which carry the translation of ProtocolTreeNode to ProtocolEntity.
class GroupsInterfaceLayer(YowInterfaceLayer):
def createGroup(self, subject):
self._sendIq(CreateGroupsIqProtocolEntity(subject), self.onGroupCreateResult, self.onGroupCreateError)
def onGroupCreateResult(self, successCreateGroupsIqProtocolEntity, createGroupsIqProtocolEntity):
print(successCreateGroupsIqProtocolEntity.groupId)
def onGroupCreateError(self, errorIqProtocolEntity, createGroupsIqProtocolEntity):
print(errorIqProtocolEntity.code)