diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-16 17:12:08 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-16 17:12:08 +0000 |
commit | 0590a140d2291f2aebfb54179f1282d798faa6a8 (patch) | |
tree | cc8eba1baa094554d03b3727d12cf1f3ab1f95fc /net | |
parent | 37b01e6d24bf3c251708cff470b65b2574c582b5 (diff) | |
download | chromium_src-0590a140d2291f2aebfb54179f1282d798faa6a8.zip chromium_src-0590a140d2291f2aebfb54179f1282d798faa6a8.tar.gz chromium_src-0590a140d2291f2aebfb54179f1282d798faa6a8.tar.bz2 |
[Sync] Merged MediatorThread and PushNotificationsThread
Removed most of the legacy XMPP notification code.
Simplified notification structs and calls a bit.
BUG=76130
TEST=
Review URL: http://codereview.chromium.org/6693007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78385 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/tools/testserver/xmppserver.py | 75 | ||||
-rw-r--r-- | net/tools/testserver/xmppserver_test.py | 12 |
2 files changed, 32 insertions, 55 deletions
diff --git a/net/tools/testserver/xmppserver.py b/net/tools/testserver/xmppserver.py index de93076..e883d31 100644 --- a/net/tools/testserver/xmppserver.py +++ b/net/tools/testserver/xmppserver.py @@ -243,9 +243,9 @@ class HandshakeTask(object): # The id attribute is filled in later. _IQ_RESPONSE_STANZA = ParseXml('<iq id="" type="result"/>') - def __init__(self, connection, id_generator, resource_prefix): + def __init__(self, connection, resource_prefix): self._connection = connection - self._id_generator = id_generator + self._id_generator = IdGenerator(resource_prefix) self._username = '' self._domain = '' self._jid = None @@ -357,18 +357,10 @@ class XmppConnection(asynchat.async_chat): Google notification protocol. """ - # We use this XML template for subscription responses as well as - # notifications (conveniently enough, the same template works - # for both). + # Used for acknowledgements to the client. # - # The from, to, id, and type attributes are filled in later. - _NOTIFIER_STANZA = ParseXml( - """<iq from="" to="" id="" type=""> - <not:getAll xmlns:not="google:notifier"> - <Result xmlns=""/> - </not:getAll> - </iq> - """) + # The from and id attributes are filled in later. + _IQ_RESPONSE_STANZA = ParseXml('<iq from="" id="" type="result"/>') def __init__(self, sock, socket_map, delegate, addr): """Starts up the xmpp connection. @@ -398,9 +390,7 @@ class XmppConnection(asynchat.async_chat): self._addr = addr addr_str = AddrString(self._addr) - self._id_generator = IdGenerator(addr_str) - self._handshake_task = ( - HandshakeTask(self, self._id_generator, addr_str)) + self._handshake_task = HandshakeTask(self, addr_str) print 'Starting connection to %s' % self def __str__(self): @@ -427,8 +417,12 @@ class XmppConnection(asynchat.async_chat): def FeedStanza(self, stanza): if self._handshake_task: self._handshake_task.FeedStanza(stanza) - elif stanza.tagName == 'iq': - self._HandleIq(stanza) + elif stanza.tagName == 'iq' and stanza.getAttribute('type') == 'result': + # Ignore all client acks. + pass + elif (stanza.firstChild and + stanza.firstChild.namespaceURI == 'google:push'): + self._HandlePushCommand(stanza) else: raise UnexpectedXml(stanza) @@ -439,36 +433,20 @@ class XmppConnection(asynchat.async_chat): self._delegate.OnXmppHandshakeDone(self) print "Handshake done for %s" % self - def _HandleIq(self, iq): - if (iq.firstChild and - iq.firstChild.namespaceURI == 'google:notifier'): - iq_id = iq.getAttribute('id') - self._HandleNotifierCommand(iq_id, iq.firstChild) - elif iq.getAttribute('type') == 'result': - # Ignore all client acks. - pass - else: - raise UnexpectedXml(iq) - - def _HandleNotifierCommand(self, id, command_xml): - command = command_xml.tagName - if command == 'getAll': + def _HandlePushCommand(self, stanza): + if stanza.tagName == 'iq' and stanza.firstChild.tagName == 'subscribe': # Subscription request. - if not command_xml.getElementsByTagName('SubscribedServiceUrl'): - raise UnexpectedXml(command_xml) - self._SendNotifierStanza(id, 'result') - elif command == 'set': + self._SendIqResponseStanza(stanza) + elif stanza.tagName == 'message' and stanza.firstChild.tagName == 'push': # Send notification request. - self._delegate.SendNotification(self) + self._delegate.ForwardNotification(self, stanza) else: raise UnexpectedXml(command_xml) - def _SendNotifierStanza(self, id, type): - stanza = CloneXml(self._NOTIFIER_STANZA) + def _SendIqResponseStanza(self, iq): + stanza = CloneXml(self._IQ_RESPONSE_STANZA) stanza.setAttribute('from', str(self._jid.GetBareJid())) - stanza.setAttribute('to', str(self._jid)) - stanza.setAttribute('id', id) - stanza.setAttribute('type', type) + stanza.setAttribute('id', iq.getAttribute('id')) self.SendStanza(stanza) def SendStanza(self, stanza, unlink=True): @@ -490,10 +468,11 @@ class XmppConnection(asynchat.async_chat): # (some minidom library functions return unicode strings). self.push(data.encode('ascii')) - def SendNotification(self): - """Sends a notification to the client.""" - next_id = self._id_generator.GetNextId() - self._SendNotifierStanza(next_id, 'set') + def ForwardNotification(self, notification_stanza): + """Forwards a notification to the client.""" + notification_stanza.setAttribute('from', str(self._jid.GetBareJid())) + notification_stanza.setAttribute('to', str(self._jid)) + self.SendStanza(notification_stanza, False) class XmppServer(asyncore.dispatcher): @@ -539,7 +518,7 @@ class XmppServer(asyncore.dispatcher): self._connections.discard(xmpp_connection) self._handshake_done_connections.discard(xmpp_connection) - def SendNotification(self, unused_xmpp_connection): + def ForwardNotification(self, unused_xmpp_connection, notification_stanza): for connection in self._handshake_done_connections: print 'Sending notification to %s' % connection - connection.SendNotification() + connection.ForwardNotification(notification_stanza) diff --git a/net/tools/testserver/xmppserver_test.py b/net/tools/testserver/xmppserver_test.py index 61a996ff..dd276e2 100644 --- a/net/tools/testserver/xmppserver_test.py +++ b/net/tools/testserver/xmppserver_test.py @@ -110,9 +110,8 @@ class HandshakeTaskTest(unittest.TestCase): def DoHandshake(self, resource_prefix, resource, username, initial_stream_domain, auth_domain, auth_stream_domain): self.data_received = 0 - id_generator = xmppserver.IdGenerator('foo') handshake_task = ( - xmppserver.HandshakeTask(self, id_generator, resource_prefix)) + xmppserver.HandshakeTask(self, resource_prefix)) stream_xml = xmppserver.ParseXml('<stream:stream xmlns:stream="foo"/>') stream_xml.setAttribute('to', initial_stream_domain) self.assertEqual(self.data_received, 0) @@ -195,9 +194,9 @@ class XmppConnectionTest(unittest.TestCase): def OnXmppConnectionClosed(self, xmpp_connection): self.connections.discard(xmpp_connection) - def SendNotification(self, unused_xmpp_connection): + def ForwardNotification(self, unused_xmpp_connection, notification_stanza): for connection in self.connections: - connection.SendNotification() + connection.ForwardNotification(notification_stanza) def testBasic(self): socket_map = {} @@ -212,8 +211,7 @@ class XmppConnectionTest(unittest.TestCase): # Test subscription request. self.assertEqual(len(self.data), 0) xmpp_connection.collect_incoming_data( - '<iq><getAll xmlns="google:notifier">' - '<SubscribedServiceUrl/></getAll></iq>') + '<iq><subscribe xmlns="google:push"></subscribe></iq>') self.assertEqual(len(self.data), 1) # Test acks. @@ -222,7 +220,7 @@ class XmppConnectionTest(unittest.TestCase): # Test notification. xmpp_connection.collect_incoming_data( - '<iq><set xmlns="google:notifier"/></iq>') + '<message><push xmlns="google:push"/></message>') self.assertEqual(len(self.data), 2) # Test unexpected stanza. |