Moved getNewId and getId to XMLStream.
This prepares the way for moving add_handler to XMLStream. Since stanzas, matchers, and handlers in any XML stream will typically use unique IDs, XMLStream is a good place for these methods.
This commit is contained in:
parent
55cfe69fef
commit
5522443e0e
@ -114,20 +114,12 @@ class basexmpp(object):
|
|||||||
for plugin in self.plugin:
|
for plugin in self.plugin:
|
||||||
self.plugin[plugin].post_init()
|
self.plugin[plugin].post_init()
|
||||||
|
|
||||||
def getNewId(self):
|
|
||||||
with self.id_lock:
|
|
||||||
self.id += 1
|
|
||||||
return self.getId()
|
|
||||||
|
|
||||||
def add_handler(self, mask, pointer, name=None, disposable=False, threaded=False, filter=False, instream=False):
|
def add_handler(self, mask, pointer, name=None, disposable=False, threaded=False, filter=False, instream=False):
|
||||||
# threaded is no longer needed, but leaving it for backwards compatibility for now
|
# threaded is no longer needed, but leaving it for backwards compatibility for now
|
||||||
if name is None:
|
if name is None:
|
||||||
name = 'add_handler_%s' % self.getNewId()
|
name = 'add_handler_%s' % self.getNewId()
|
||||||
self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer, once=disposable, instream=instream))
|
self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer, once=disposable, instream=instream))
|
||||||
|
|
||||||
def getId(self):
|
|
||||||
return "%x".upper() % self.id
|
|
||||||
|
|
||||||
def sendXML(self, data, mask=None, timeout=10):
|
def sendXML(self, data, mask=None, timeout=10):
|
||||||
return self.send(tostring(data), mask, timeout)
|
return self.send(tostring(data), mask, timeout)
|
||||||
|
|
||||||
|
@ -101,7 +101,9 @@ class XMLStream(object):
|
|||||||
disconnect -- Disconnect from the server and terminate
|
disconnect -- Disconnect from the server and terminate
|
||||||
processing.
|
processing.
|
||||||
event -- Trigger a custom event.
|
event -- Trigger a custom event.
|
||||||
|
get_id -- Return the current stream ID.
|
||||||
incoming_filter -- Optionally filter stanzas before processing.
|
incoming_filter -- Optionally filter stanzas before processing.
|
||||||
|
new_id -- Generate a new, unique ID value.
|
||||||
process -- Read XML stanzas from the stream and apply
|
process -- Read XML stanzas from the stream and apply
|
||||||
matching stream handlers.
|
matching stream handlers.
|
||||||
reconnect -- Reestablish a connection to the server.
|
reconnect -- Reestablish a connection to the server.
|
||||||
@ -144,6 +146,8 @@ class XMLStream(object):
|
|||||||
self.removeHandler = self.remove_handler
|
self.removeHandler = self.remove_handler
|
||||||
self.setSocket = self.set_socket
|
self.setSocket = self.set_socket
|
||||||
self.sendRaw = self.send_raw
|
self.sendRaw = self.send_raw
|
||||||
|
self.getId = self.get_id
|
||||||
|
self.getNewId = self.new_id
|
||||||
|
|
||||||
self.ssl_support = SSL_SUPPORT
|
self.ssl_support = SSL_SUPPORT
|
||||||
|
|
||||||
@ -178,8 +182,29 @@ class XMLStream(object):
|
|||||||
self.__root_stanza = []
|
self.__root_stanza = []
|
||||||
self.__handlers = []
|
self.__handlers = []
|
||||||
|
|
||||||
|
self._id = 0
|
||||||
|
self._id_lock = threading.Lock()
|
||||||
|
|
||||||
self.run = True
|
self.run = True
|
||||||
|
|
||||||
|
def new_id(self):
|
||||||
|
"""
|
||||||
|
Generate and return a new stream ID in hexadecimal form.
|
||||||
|
|
||||||
|
Many stanzas, handlers, or matchers may require unique
|
||||||
|
ID values. Using this method ensures that all new ID values
|
||||||
|
are unique in this stream.
|
||||||
|
"""
|
||||||
|
with self._id_lock:
|
||||||
|
self._id += 1
|
||||||
|
return self.get_id()
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
"""
|
||||||
|
Return the current unique stream ID in hexadecimal form.
|
||||||
|
"""
|
||||||
|
return "%X" % self._id
|
||||||
|
|
||||||
def connect(self, host='', port=0, use_ssl=False,
|
def connect(self, host='', port=0, use_ssl=False,
|
||||||
use_tls=True, reattempt=True):
|
use_tls=True, reattempt=True):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user