Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(950)

Unified Diff: net/tools/testserver/xmppserver.py

Issue 7753023: [Sync] Add tests for migration triggered by notifications (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/tools/testserver/testserver.py ('k') | net/tools/testserver/xmppserver_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/tools/testserver/xmppserver.py
diff --git a/net/tools/testserver/xmppserver.py b/net/tools/testserver/xmppserver.py
index e883d315d86a824e67a1c2399d70c4ed2d1e60e6..dfac6a1d8f7889b5f79155ef03a53e36a6ff63b5 100644
--- a/net/tools/testserver/xmppserver.py
+++ b/net/tools/testserver/xmppserver.py
@@ -488,6 +488,14 @@ class XmppServer(asyncore.dispatcher):
asyncore.loop(30.0, False, socket_map)
"""
+ # Used when sending a notification.
+ _NOTIFICATION_STANZA = ParseXml(
+ '<message>'
+ ' <push xmlns="google:push">'
+ ' <data/>'
+ ' </push>'
+ '</message>')
+
def __init__(self, socket_map, addr):
asyncore.dispatcher.__init__(self, None, socket_map)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -497,11 +505,14 @@ class XmppServer(asyncore.dispatcher):
self._socket_map = socket_map
self._connections = set()
self._handshake_done_connections = set()
+ self._notifications_enabled = True
def handle_accept(self):
(sock, addr) = self.accept()
xmpp_connection = XmppConnection(sock, self._socket_map, self, addr)
self._connections.add(xmpp_connection)
+ # Return the new XmppConnection for testing.
+ return xmpp_connection
def close(self):
# A copy is necessary since calling close on each connection
@@ -510,6 +521,39 @@ class XmppServer(asyncore.dispatcher):
connection.close()
asyncore.dispatcher.close(self)
+ def EnableNotifications(self):
+ self._notifications_enabled = True
+
+ def DisableNotifications(self):
+ self._notifications_enabled = False
+
+ def MakeNotification(self, channel, data):
+ """Makes a notification from the given channel and encoded data.
+
+ Args:
+ channel: The channel on which to send the notification.
+ data: The notification payload.
+ """
+ notification_stanza = CloneXml(self._NOTIFICATION_STANZA)
+ push_element = notification_stanza.getElementsByTagName('push')[0]
+ push_element.setAttribute('channel', channel)
+ data_element = push_element.getElementsByTagName('data')[0]
+ encoded_data = base64.b64encode(data)
+ data_text = notification_stanza.parentNode.createTextNode(encoded_data)
+ data_element.appendChild(data_text)
+ return notification_stanza
+
+ def SendNotification(self, channel, data):
+ """Sends a notification to all connections.
+
+ Args:
+ channel: The channel on which to send the notification.
+ data: The notification payload.
+ """
+ notification_stanza = self.MakeNotification(channel, data)
+ self.ForwardNotification(None, notification_stanza)
+ notification_stanza.unlink()
+
# XmppConnection delegate methods.
def OnXmppHandshakeDone(self, xmpp_connection):
self._handshake_done_connections.add(xmpp_connection)
@@ -519,6 +563,9 @@ class XmppServer(asyncore.dispatcher):
self._handshake_done_connections.discard(xmpp_connection)
def ForwardNotification(self, unused_xmpp_connection, notification_stanza):
- for connection in self._handshake_done_connections:
- print 'Sending notification to %s' % connection
- connection.ForwardNotification(notification_stanza)
+ if self._notifications_enabled:
+ for connection in self._handshake_done_connections:
+ print 'Sending notification to %s' % connection
+ connection.ForwardNotification(notification_stanza)
+ else:
+ print 'Notifications disabled; dropping notification'
« no previous file with comments | « net/tools/testserver/testserver.py ('k') | net/tools/testserver/xmppserver_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698