reimplement XMPP connection

This commit reworks the way that the XMPP connection takes place; in
particular, we replace iterparse with our own XML event handler, which allows
us to poll for messages without blocking on the socket.

This has several benefits:

    - it allows us to more easily implement polling of jobs via the REST API
      (which is necessary for some users for whom XMPP notifications are
      unreliable - issue #48)

    - it allows us to better monitor the state of the XMPP connection, and
      reconnect it if it should fail - issue #41

    - rather than establishing a new connection between each job, we can use
      one long-running connection, which eliminates the race condition whereby
      jobs may arrive between polling the REST API and establishing the XMPP
      connection

Fixes #41 - Need to restart service after loss of connection
move xmpp stuff to another file
Send XMPP keepalives every 60s
fix 'invalid argument' errors
remove a couple of bits of debug code

Poll for jobs before making xmpp connection, so that basic functionality still
works, even when xmpp auth fails
Deal with comments raised by armoo in pull request:

* Create new logger for xmpp
* Put doc comments in the right place
* Fix method naming to be consistent with rest of code
Raise keepalive period from 1 to 10 minutes.

Research suggests that 1-2 hours is fast enough.
(cherry picked from commit bfc706fdc5e39046ff03188bb2a67bd7839b98eb)
Collect and consolidate the various timers in the app.

Poll period when xmpp is working, and when it is not, plus
the keepalive ping time.
(cherry picked from commit 91eae404aaa14950209dc1acc9541cc4df9a3f2a)
Variable spelling errors in xmpp.py exception handling.

Missing 'n' in XmppConnection self._conected.
(cherry picked from commit 7fa4647dfc38e4272e72fcb4d46c8fb2bec66c6c)
Unhandled exception in XMPP recv.

The following was in the user.log.

Traceback (most recent call last):
012  File "/usr/share/cloudprint/cloudprint/cloudprint.py", line 373, in process_jobs
012    xmpp_conn.await_notification(sleeptime)
012  File "/usr/share/cloudprint/cloudprint/xmpp.py", line 189, in await_notification
012    self._read_socket()
012  File "/usr/share/cloudprint/cloudprint/xmpp.py", line 55, in _read_socket
012    data = self._wrappedsock.recv(1024)
012  File "/usr/lib/python2.7/ssl.py", line 305, in recv
012    return self.read(buflen)
012  File "/usr/lib/python2.7/ssl.py", line 224, in read
012    return self._sslobj.read(len)
012error: [Errno 104] Connection reset by peer

Assume that this is because the exception is not of type 'Exception',
and change the except in process_jobs to pure except:.
(cherry picked from commit a7cdfdb1fca55cf8d83698f49945f60aef5f9adc)

Fixes #28 - cloudprint crashes unpredictably in gentoo
Fix for another unhandled exception.

This one would be caught by a previous fix to process_jobs().
This change restores the close() call to the connection.

ERROR: Could not Connect to Cloud Service. Will Try again in %d Seconds
012Traceback (most recent call last):
012  File "/usr/share/cloudprint/cloudprint/cloudprint.py", line 371, in process_jobs
012    XMPP_USE_SSL,xmpp_auth)
012  File "/usr/share/cloudprint/cloudprint/xmpp.py", line 123, in connect
012    self._msg('<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-GOOGLE-TOKEN">%s</auth>' % sasl_token)
012  File "/usr/share/cloudprint/cloudprint/xmpp.py", line 87, in _msg
012    assert not elem.tag.endswith('failure') and not elem.tag.endswith('error')
012AssertionError
(cherry picked from commit 2c3582f87d281d612280def305871f8ce3dd20fc)
One more possible uncaught exception (not seen in the wild).

in XmppConnection _write_socket().
(cherry picked from commit 603ee43ff179f130d53cf50aba12364133d70ea1)
Fix TypeError when xmpp connection fails

Closes #58 - Poll for new jobs via HTTP as well as listening for XMPP notifications
Remove debug traceback to log on XMPP connect err
Harden keepalive processing

The following pattern showed up in the logs:

Oct 21 18:12:21 debian-testing cloudprint.py: Polling for jobs on MFCJ5910DW
Oct 21 19:12:22 debian-testing cloudprint.py: Polling for jobs on MFC490CW
Oct 21 19:12:22 debian-testing cloudprint.py: Polling for jobs on MFCJ5910DW
Oct 21 19:31:53 debian-testing cloudprint.py: Sending XMPP keepalive
Oct 21 19:31:53 debian-testing cloudprint.py: ERROR: Could not Connect to Cloud Service. Will Try again in 60 Seconds
Oct 21 19:32:53 debian-testing cloudprint.py: Polling for jobs on MFC490CW
Oct 21 19:32:53 debian-testing cloudprint.py: Polling for jobs on MFCJ5910DW
Oct 21 19:32:54 debian-testing cloudprint.py: Establishing connection to xmpp server talk.google.com:5223
Oct 21 19:32:54 debian-testing cloudprint.py: ERROR: Could not Connect to Cloud Service. Will Try again in 60 Seconds
Oct 21 19:33:54 debian-testing cloudprint.py: Polling for jobs on MFC490CW
Oct 21 19:33:54 debian-testing cloudprint.py: Polling for jobs on MFCJ5910DW

keepalives are oddly rare (while set to 600 seconds). In this log,
the first keepalive was over a day in coming, and xmpp persistently failed
after the first keepalive attempt.

This commit has a number of changes to avoid problems with keepalive processing:
 - Initialize at class creation
 - Set to a valid number on close
 - Update early in the read/write routines.
Add xmpp error check to _check_for_notification()

It looks like xmpp msg()'s can result in more than one clause
returned by the host, and that only one is processed per msg()
call. If so, it is possible that a failure may not be processed
until the notification call, which does not check.

Add a failure check to the notification method.
xmpp.py: Extend try block in await_notifications

There were cases where the xmpp connection was not closed when
it probably should have been. Extend the try block so that the
connection is explicitly closed on errors in check_for_notifications
or select.
xmpp.py: Refactor xmpp failure into get_elem()
Remove unused printer_id member in CloudPrintProxy
Increase printer poll to 1/hr when XMPP working.