Refactor barn-agent.cpp

This is the final of 3 refactoring commits. The changes should not
have any functional changes, just change the code layout.
This change tries to make the barn-agent main loop easier to read
and to modify.