Subject: Iq Filters on Iq receive
Hi
I have built a few test applications and I am pretty happy so far. One thing that I do not feel I have a grip on is how to handle Iq's where the application is the service provider. If I send an Iq (as a client or service consumer), I can register a method that gets called when a response is returned.
which then returns a response to:
At the other end of this, I process an OnIq event, with something like this:
This is all fine, but if I have a BOT for instance that I want to respond to several types or classes of Iq, I end up with a complicated event handler that switches on the event type. This is OK, but a little unwieldy. I could create a dictionary of event type to method and register each of the event types and then the main Iq handler could then look up the event type and then delegate it the previously registered method. This would keep the OnIq handler tidy and there would then be a handler method for each type of Iq.
I strikes me that this is capability that could be in the Matrix client. Have I overlooked a capability that is already there? Or should I use a mechanism such as I described to manage this myself?
Thanks
John
I have built a few test applications and I am pretty happy so far. One thing that I do not feel I have a grip on is how to handle Iq's where the application is the service provider. If I send an Iq (as a client or service consumer), I can register a method that gets called when a response is returned.
var diq = new DispatcherIq
{
Type = Matrix.Xmpp.IqType.get,
To = item.DestinationJid + "/" + client.Resource,
Dispatcher = new Dispatcher { SenderMAN = client.Username, RecipientMAN = item.DestinationJid, Filename = item.Filename }
};
client.IqFilter.SendIq(diq, DispatcherResponse);
{
Type = Matrix.Xmpp.IqType.get,
To = item.DestinationJid + "/" + client.Resource,
Dispatcher = new Dispatcher { SenderMAN = client.Username, RecipientMAN = item.DestinationJid, Filename = item.Filename }
};
client.IqFilter.SendIq(diq, DispatcherResponse);
which then returns a response to:
private void DispatcherResponse(object sender, Matrix.Xmpp.Client.IqEventArgs e)
{
var iq = e.Iq;
if (iq.Type == Matrix.Xmpp.IqType.result)
{
var dispatcher = iq.Element<Dispatcher>();
if (dispatcher != null)
{
log("Got a dispatch response " + dispatcher.Filename);
//do something interesting
}
}
}
{
var iq = e.Iq;
if (iq.Type == Matrix.Xmpp.IqType.result)
{
var dispatcher = iq.Element<Dispatcher>();
if (dispatcher != null)
{
log("Got a dispatch response " + dispatcher.Filename);
//do something interesting
}
}
}
At the other end of this, I process an OnIq event, with something like this:
void client_OnIq(object sender, IqEventArgs e)
{
try //no parent calling function, so put a try catch block
{
if (e.Iq.Type == Matrix.Xmpp.IqType.get && e.Iq.Query is Dispatcher)
{
// this is now acting as a service provider from another client
var dispatcher = e.Iq.Query as Dispatcher;
//now send response
var diq = new DispatcherIq
{
Id = e.Iq.Id, //used to correlate with the request !!!
To = e.Iq.From,
Type = Matrix.Xmpp.IqType.result,
Dispatcher = new Dispatcher { Filename = dispatcher.Filename, MessageBody = "success"}
};
// send the response
client.Send(diq);
}
}
catch (Exception ex)
{
log("On_Iq error: " + ex.ToString());
}
}
{
try //no parent calling function, so put a try catch block
{
if (e.Iq.Type == Matrix.Xmpp.IqType.get && e.Iq.Query is Dispatcher)
{
// this is now acting as a service provider from another client
var dispatcher = e.Iq.Query as Dispatcher;
//now send response
var diq = new DispatcherIq
{
Id = e.Iq.Id, //used to correlate with the request !!!
To = e.Iq.From,
Type = Matrix.Xmpp.IqType.result,
Dispatcher = new Dispatcher { Filename = dispatcher.Filename, MessageBody = "success"}
};
// send the response
client.Send(diq);
}
}
catch (Exception ex)
{
log("On_Iq error: " + ex.ToString());
}
}
This is all fine, but if I have a BOT for instance that I want to respond to several types or classes of Iq, I end up with a complicated event handler that switches on the event type. This is OK, but a little unwieldy. I could create a dictionary of event type to method and register each of the event types and then the main Iq handler could then look up the event type and then delegate it the previously registered method. This would keep the OnIq handler tidy and there would then be a handler method for each type of Iq.
I strikes me that this is capability that could be in the Matrix client. Have I overlooked a capability that is already there? Or should I use a mechanism such as I described to manage this myself?
Thanks
John