getblocktemplate: longpolling support

Resubmission of #1355, since GitHub closed it on me.

I have rebased it and addressed both of @jeremysawicki's concerns:
- Shutdown now disconnects all longpoll sessions with an error.
- Longpolling now mimicks the behaviour of BitcoinMiner by returning a new template after a minute has passed and there are new transactions.

These changes were not exactly trivial, so re-review of the code is probably necessary.