← Back to team overview

mqtt-users team mailing list archive

Re: mosquitto_loop + other toolkit event loops

 


On 11/02/2011, at 6:47 AM, Christoph Willing wrote:


On 10/02/2011, at 7:43 PM, Roger Light wrote:

Hi Chris,

An alternative to incorporating Qt events into the mosquitto event loop would be to incorporate mosquitto events into Qt's event loop (googling suggests either QAbstractEventDispatcher or QSocketNotifier as possibilities to do it). However that probably entails reimplementing much, or all, of the
existing libmosquitto(pp) - not sure if I'm up to that ...

I don't think it's as complicated as you suggest :)

I included what I hope is enough functionality to support this kind of
problem, but whether it works in all cases remains to be seen. In
terms of adding the mosquitto event loop to an application already
using select() or similar, you would add the mosquitto client socket
to your fd sets using mosquitto_socket(), then if after the select
call it was ready for reading use mosquitto_loop_read() and for
writing mosquitto_loop_write(). You'd also call mosquitto_loop_misc()
to do the other bits as required (checking keepalive/ping and retrying
messages).

Another less elegant way of doing it would be to call mosquitto_loop()
with the timeout parameter set to zero. This would introduce a second
select() call to your own in order to monitor the mosquitto client
socket, but it would return straight away.

QSocketNotifier looks like a good candidate. You could create one
instance for notifying about read events and use it to call
mosquitto_loop_read() and another for write events that would call
mosquitto_loop_write(). There is the problem of what to do about
mosquitto_loop_misc() - putting it in the handler for the write events
should be sufficient - in fact, putting it before the call to
mosquitto_loop_write() makes most sense as it may produce things for
loop write to do.


OK thanks for the advice Roger. I'll see how I go with QSocketNotifier.


Just an update - this strategy of using QSocketNotifiers has worked out really well. There were a few c++ hoops to jump through (since the QSocketNotifier's activity "signal" needs to be delivered to some object's "slot"). After a few false starts and dead ends, the end result was not nearly as complicated as I originally thought it might be - pretty neat actually.

Thanks again Roger.


chris





Follow ups

References