def recv_feature(self, data, method='mask', use_values=True, timeout=1): """ """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') if self.xmpp.socket.is_live: # we are working with a live connection, so we should # verify what has been received instead of simulating # receiving data. recv_data = self.xmpp.socket.next_recv(timeout) xml = self.parse_xml(data) recv_xml = self.parse_xml(recv_data) if recv_data is None: self.fail("No stanza was received.") if method == 'exact': self.failUnless(self.compare(xml, recv_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % ( tostring(xml), tostring(recv_xml))) elif method == 'mask': matcher = MatchXMLMask(xml) self.failUnless(matcher.match(recv_xml), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % tostring(xml) + \ "Stanza:\n%s" % tostring(recv_xml)) else: raise ValueError("Uknown matching method: %s" % method) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
def recv_feature(self, data, method='mask', use_values=True, timeout=1): """ """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') if self.xmpp.socket.is_live: # we are working with a live connection, so we should # verify what has been received instead of simulating # receiving data. recv_data = self.xmpp.socket.next_recv(timeout) xml = self.parse_xml(data) recv_xml = self.parse_xml(recv_data) if recv_data is None: self.fail("No stanza was received.") if method == 'exact': self.failUnless( self.compare(xml, recv_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % (tostring(xml), tostring(recv_xml))) elif method == 'mask': matcher = MatchXMLMask(xml) self.failUnless(matcher.match(recv_xml), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % tostring(xml) + \ "Stanza:\n%s" % tostring(recv_xml)) else: raise ValueError("Uknown matching method: %s" % method) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
def send(self, data, mask=None, timeout=RESPONSE_TIMEOUT): """ A wrapper for send_raw for sending stanza objects. May optionally block until an expected response is received. Arguments: data -- The stanza object to send on the stream. mask -- Deprecated. An XML snippet matching the structure of the expected response. Execution will block in this thread until the response is received or a timeout occurs. timeout -- Time in seconds to wait for a response before continuing. Defaults to RESPONSE_TIMEOUT. """ if hasattr(mask, 'xml'): mask = mask.xml data = str(data) if mask is not None: log.warning("Use of send mask waiters is deprecated.") wait_for = Waiter("SendWait_%s" % self.new_id(), MatchXMLMask(mask)) self.register_handler(wait_for) self.send_raw(data) if mask is not None: return wait_for.wait(timeout)
def send_feature(self, data, method='mask', use_values=True, timeout=1): """ """ sent_data = self.xmpp.socket.next_sent(timeout) xml = self.parse_xml(data) sent_xml = self.parse_xml(sent_data) if sent_data is None: self.fail("No stanza was sent.") if method == 'exact': self.failUnless(self.compare(xml, sent_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % ( tostring(xml), tostring(sent_xml))) elif method == 'mask': matcher = MatchXMLMask(xml) self.failUnless(matcher.match(sent_xml), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % tostring(xml) + \ "Stanza:\n%s" % tostring(sent_xml)) else: raise ValueError("Uknown matching method: %s" % method)
def add_handler(self, mask, pointer, name=None, disposable=False, threaded=False, filter=False, instream=False): """ A shortcut method for registering a handler using XML masks. Arguments: mask -- An XML snippet matching the structure of the stanzas that will be passed to this handler. pointer -- The handler function itself. name -- A unique name for the handler. A name will be generated if one is not provided. disposable -- Indicates if the handler should be discarded after one use. threaded -- Deprecated. Remains for backwards compatibility. filter -- Deprecated. Remains for backwards compatibility. instream -- Indicates if the handler should execute during stream processing and not during normal event processing. """ # To prevent circular dependencies, we must load the matcher # and handler classes here. from sleekxmpp.xmlstream.matcher import MatchXMLMask from sleekxmpp.xmlstream.handler import XMLCallback if name is None: name = 'add_handler_%s' % self.getNewId() self.registerHandler( XMLCallback(name, MatchXMLMask(mask), pointer, once=disposable, instream=instream))