Refactor queueing code out of daemon.c into a separate file

Also make a slightly cleaner extraction.

This hopefully has a couple of advantages:

1) The code should now be a bit cleaner
2) It makes testing the queueing code easier
3) In future it might be possible to have a separate queue per style
   allowing e.g. to perform fair queueing between styles.
Add tests for the refactored queueing code