summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/xmlstream')
-rw-r--r--sleekxmpp/xmlstream/handler/base.py2
-rw-r--r--sleekxmpp/xmlstream/handler/callback.py4
-rw-r--r--sleekxmpp/xmlstream/scheduler.py87
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py22
-rw-r--r--sleekxmpp/xmlstream/statemachine.py245
-rw-r--r--sleekxmpp/xmlstream/xmlstream.py323
6 files changed, 502 insertions, 181 deletions
diff --git a/sleekxmpp/xmlstream/handler/base.py b/sleekxmpp/xmlstream/handler/base.py
index 5d55f4ee..a44edf0e 100644
--- a/sleekxmpp/xmlstream/handler/base.py
+++ b/sleekxmpp/xmlstream/handler/base.py
@@ -18,7 +18,7 @@ class BaseHandler(object):
def match(self, xml):
return self._matcher.match(xml)
- def prerun(self, payload):
+ def prerun(self, payload): # what's the point of this if the payload is called again in run??
self._payload = payload
def run(self, payload):
diff --git a/sleekxmpp/xmlstream/handler/callback.py b/sleekxmpp/xmlstream/handler/callback.py
index 49cfa14d..ea5acb5b 100644
--- a/sleekxmpp/xmlstream/handler/callback.py
+++ b/sleekxmpp/xmlstream/handler/callback.py
@@ -17,13 +17,15 @@ class Callback(base.BaseHandler):
self._once = once
self._instream = instream
- def prerun(self, payload):
+ def prerun(self, payload): # prerun actually calls run?!? WTF! Then it gets run AGAIN!
base.BaseHandler.prerun(self, payload)
if self._instream:
+ logging.debug('callback "%s" prerun', self.name)
self.run(payload, True)
def run(self, payload, instream=False):
if not self._instream or instream:
+ logging.debug('callback "%s" run', self.name)
base.BaseHandler.run(self, payload)
#if self._thread:
# x = threading.Thread(name="Callback_%s" % self.name, target=self._pointer, args=(payload,))
diff --git a/sleekxmpp/xmlstream/scheduler.py b/sleekxmpp/xmlstream/scheduler.py
new file mode 100644
index 00000000..40aaf695
--- /dev/null
+++ b/sleekxmpp/xmlstream/scheduler.py
@@ -0,0 +1,87 @@
+try:
+ import queue
+except ImportError:
+ import Queue as queue
+import time
+import threading
+import logging
+
+class Task(object):
+ """Task object for the Scheduler class"""
+ def __init__(self, name, seconds, callback, args=None, kwargs=None, repeat=False, qpointer=None):
+ self.name = name
+ self.seconds = seconds
+ self.callback = callback
+ self.args = args or tuple()
+ self.kwargs = kwargs or {}
+ self.repeat = repeat
+ self.next = time.time() + self.seconds
+ self.qpointer = qpointer
+
+ def run(self):
+ if self.qpointer is not None:
+ self.qpointer.put(('schedule', self.callback, self.args))
+ else:
+ self.callback(*self.args, **self.kwargs)
+ self.reset()
+ return self.repeat
+
+ def reset(self):
+ self.next = time.time() + self.seconds
+
+class Scheduler(object):
+ """Threaded scheduler that allows for updates mid-execution unlike http://docs.python.org/library/sched.html#module-sched"""
+ def __init__(self, parentqueue=None):
+ self.addq = queue.Queue()
+ self.schedule = []
+ self.thread = None
+ self.run = False
+ self.parentqueue = parentqueue
+
+ def process(self, threaded=True):
+ if threaded:
+ self.thread = threading.Thread(name='shedulerprocess', target=self._process)
+ self.thread.start()
+ else:
+ self._process()
+
+ def _process(self):
+ self.run = True
+ while self.run:
+ try:
+ wait = 1
+ updated = False
+ if self.schedule:
+ wait = self.schedule[0].next - time.time()
+ try:
+ if wait <= 0.0:
+ newtask = self.addq.get(False)
+ else:
+ newtask = self.addq.get(True, wait)
+ except queue.Empty:
+ cleanup = []
+ for task in self.schedule:
+ if time.time() >= task.next:
+ updated = True
+ if not task.run():
+ cleanup.append(task)
+ else:
+ break
+ for task in cleanup:
+ x = self.schedule.pop(self.schedule.index(task))
+ else:
+ updated = True
+ self.schedule.append(newtask)
+ finally:
+ if updated: self.schedule = sorted(self.schedule, key=lambda task: task.next)
+ except KeyboardInterrupt:
+ self.run = False
+ logging.debug("Quitting Scheduler thread")
+ if self.parentqueue is not None:
+ self.parentqueue.put(('quit', None, None))
+
+ def add(self, name, seconds, callback, args=None, kwargs=None, repeat=False, qpointer=None):
+ self.addq.put(Task(name, seconds, callback, args, kwargs, repeat, qpointer))
+
+ def quit(self):
+ self.run = False
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 3f3f5e08..34513807 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -1,9 +1,9 @@
"""
- SleekXMPP: The Sleek XMPP Library
- Copyright (C) 2010 Nathanael C. Fritz
- This file is part of SleekXMPP.
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
- See the file license.txt for copying permission.
+ See the file license.txt for copying permission.
"""
from xml.etree import cElementTree as ET
import logging
@@ -78,6 +78,9 @@ class ElementBase(tostring.ToString):
def __iter__(self):
self.idx = 0
return self
+
+ def __bool__(self):
+ return True
def __next__(self):
self.idx += 1
@@ -319,6 +322,8 @@ class StanzaBase(ElementBase):
def __init__(self, stream=None, xml=None, stype=None, sto=None, sfrom=None, sid=None):
self.stream = stream
+ if stream is not None:
+ self.namespace = stream.default_ns
ElementBase.__init__(self, xml)
if stype is not None:
self['type'] = stype
@@ -326,8 +331,6 @@ class StanzaBase(ElementBase):
self['to'] = sto
if sfrom is not None:
self['from'] = sfrom
- if stream is not None:
- self.namespace = stream.default_ns
self.tag = "{%s}%s" % (self.namespace, self.name)
def setType(self, value):
@@ -380,6 +383,7 @@ class StanzaBase(ElementBase):
def exception(self, e):
logging.error(traceback.format_tb(e))
- def send(self):
- self.stream.sendRaw(self.__str__())
-
+ def send(self, priority=False):
+ if priority: self.stream.sendPriorityRaw(self.__str__())
+ else: self.stream.sendRaw(self.__str__())
+
diff --git a/sleekxmpp/xmlstream/statemachine.py b/sleekxmpp/xmlstream/statemachine.py
index fb7d1508..67b514a2 100644
--- a/sleekxmpp/xmlstream/statemachine.py
+++ b/sleekxmpp/xmlstream/statemachine.py
@@ -7,53 +7,228 @@
"""
from __future__ import with_statement
import threading
+import time
+import logging
+
class StateMachine(object):
- def __init__(self, states=[], groups=[]):
- self.lock = threading.Lock()
- self.__state = {}
- self.__default_state = {}
- self.__group = {}
+ def __init__(self, states=[]):
+ self.lock = threading.Condition(threading.RLock())
+ self.__states= []
self.addStates(states)
- self.addGroups(groups)
+ self.__default_state = self.__states[0]
+ self.__current_state = self.__default_state
def addStates(self, states):
with self.lock:
for state in states:
- if state in self.__state or state in self.__group:
- raise IndexError("The state or group '%s' is already in the StateMachine." % state)
- self.__state[state] = states[state]
- self.__default_state[state] = states[state]
+ if state in self.__states:
+ raise IndexError("The state '%s' is already in the StateMachine." % state)
+ self.__states.append( state )
- def addGroups(self, groups):
- with self.lock:
- for gstate in groups:
- if gstate in self.__state or gstate in self.__group:
- raise IndexError("The key or group '%s' is already in the StateMachine." % gstate)
- for state in groups[gstate]:
- if state in self.__state:
- raise IndexError("The group %s contains a key %s which is not set in the StateMachine." % (gstate, state))
- self.__group[gstate] = groups[gstate]
-
- def set(self, state, status):
+
+ def transition(self, from_state, to_state, wait=0.0, func=None, args=[], kwargs={} ):
+ '''
+ Transition from the given `from_state` to the given `to_state`.
+ This method will return `True` if the state machine is now in `to_state`. It
+ will return `False` if a timeout occurred the transition did not occur.
+ If `wait` is 0 (the default,) this method returns immediately if the state machine
+ is not in `from_state`.
+
+ If you want the thread to block and transition once the state machine to enters
+ `from_state`, set `wait` to a non-negative value. Note there is no 'block
+ indefinitely' flag since this leads to deadlock. If you want to wait indefinitely,
+ choose a reasonable value for `wait` (e.g. 20 seconds) and do so in a while loop like so:
+
+ ::
+
+ while not thread_should_exit and not state_machine.transition('disconnected', 'connecting', wait=20 ):
+ pass # timeout will occur every 20s unless transition occurs
+ if thread_should_exit: return
+ # perform actions here after successful transition
+
+ This allows the thread to be responsive by setting `thread_should_exit=True`.
+
+ The optional `func` argument allows the user to pass a callable operation which occurs
+ within the context of the state transition (e.g. while the state machine is locked.)
+ If `func` returns a True value, the transition will occur. If `func` returns a non-
+ True value or if an exception is thrown, the transition will not occur. Any thrown
+ exception is not caught by the state machine and is the caller's responsibility to handle.
+ If `func` completes normally, this method will return the value returned by `func.` If
+ values for `args` and `kwargs` are provided, they are expanded and passed like so:
+ `func( *args, **kwargs )`.
+ '''
+
+ return self.transition_any( (from_state,), to_state, wait=wait,
+ func=func, args=args, kwargs=kwargs )
+
+
+ def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kwargs={} ):
+ '''
+ Transition from any of the given `from_states` to the given `to_state`.
+ '''
+
+ if not (isinstance(from_states,tuple) or isinstance(from_states,list)):
+ raise ValueError( "from_states should be a list or tuple" )
+
+ for state in from_states:
+ if not state in self.__states:
+ raise ValueError( "StateMachine does not contain from_state %s." % state )
+ if not to_state in self.__states:
+ raise ValueError( "StateMachine does not contain to_state %s." % to_state )
+
with self.lock:
- if state in self.__state:
- self.__state[state] = bool(status)
+ start = time.time()
+ while not self.__current_state in from_states:
+ # detect timeout:
+ if time.time() >= start + wait: return False
+ self.lock.wait(wait)
+
+ if self.__current_state in from_states: # should always be True due to lock
+
+ return_val = True
+ # Note that func might throw an exception, but that's OK, it aborts the transition
+ if func is not None: return_val = func(*args,**kwargs)
+
+ # some 'false' value returned from func,
+ # indicating that transition should not occur:
+ if not return_val: return return_val
+
+ logging.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
+ self.__current_state = to_state
+ self.lock.notify_all()
+ return return_val # some 'true' value returned by func or True if func was None
else:
- raise KeyError("StateMachine does not contain state %s." % state)
-
- def __getitem__(self, key):
- if key in self.__group:
- for state in self.__group[key]:
- if not self.__state[state]:
- return False
- return True
- return self.__state[key]
+ logging.error( "StateMachine bug!! The lock should ensure this doesn't happen!" )
+ return False
+
+
+ def transition_ctx(self, from_state, to_state, wait=0.0):
+ '''
+ Use the state machine as a context manager. The transition occurs on /exit/ from
+ the `with` context, so long as no exception is thrown. For example:
+
+ ::
+
+ with state_machine.transition_ctx('one','two', wait=5) as locked:
+ if locked:
+ # the state machine is currently locked in state 'one', and will
+ # transition to 'two' when the 'with' statement ends, so long as
+ # no exception is thrown.
+ print 'Currently locked in state one: %s' % state_machine['one']
+
+ else:
+ # The 'wait' timed out, and no lock has been acquired
+ print 'Timed out before entering state "one"'
+
+ print 'Since no exception was thrown, we are now in state "two": %s' % state_machine['two']
+
+
+ The other main difference between this method and `transition()` is that the
+ state machine is locked for the duration of the `with` statement. Normally,
+ after a `transition()` occurs, the state machine is immediately unlocked and
+ available to another thread to call `transition()` again.
+ '''
+
+ if not from_state in self.__states:
+ raise ValueError( "StateMachine does not contain from_state %s." % from_state )
+ if not to_state in self.__states:
+ raise ValueError( "StateMachine does not contain to_state %s." % to_state )
+
+ return _StateCtx(self, from_state, to_state, wait)
+
- def __getattr__(self, attr):
- return self.__getitem__(attr)
+ def ensure(self, state, wait=0.0):
+ '''
+ Ensure the state machine is currently in `state`, or wait until it enters `state`.
+ '''
+ return self.ensure_any( (state,), wait=wait )
+
+
+ def ensure_any(self, states, wait=0.0):
+ '''
+ Ensure we are currently in one of the given `states`
+ '''
+ if not (isinstance(states,tuple) or isinstance(states,list)):
+ raise ValueError('states arg should be a tuple or list')
+
+ for state in states:
+ if not state in self.__states:
+ raise ValueError( "StateMachine does not contain state '%s'" % state )
+
+ with self.lock:
+ start = time.time()
+ while not self.__current_state in states:
+ # detect timeout:
+ if time.time() >= start + wait: return False
+ self.lock.wait(wait)
+ return self.__current_state in states # should always be True due to lock
+
def reset(self):
- self.__state = self.__default_state
+ # TODO need to lock before calling this?
+ self.transition(self.__current_state, self._default_state)
+
+
+ def _set_state(self, state): #unsynchronized, only call internally after lock is acquired
+ self.__current_state = state
+ return state
+
+
+ def current_state(self):
+ '''
+ Return the current state name.
+ '''
+ return self.__current_state
+
+
+ def __getitem__(self, state):
+ '''
+ Non-blocking, non-synchronized test to determine if we are in the given state.
+ Use `StateMachine.ensure(state)` to wait until the machine enters a certain state.
+ '''
+ return self.__current_state == state
+
+ def __str__(self):
+ return "".join(( "StateMachine(", ','.join(self.__states), "): ", self.__current_state ))
+
+
+
+class _StateCtx:
+
+ def __init__( self, state_machine, from_state, to_state, wait ):
+ self.state_machine = state_machine
+ self.from_state = from_state
+ self.to_state = to_state
+ self.wait = wait
+ self._timeout = False
+
+ def __enter__(self):
+ self.state_machine.lock.acquire()
+ start = time.time()
+ while not self.state_machine[ self.from_state ]:
+ # detect timeout:
+ if time.time() >= start + self.wait:
+ logging.debug('StateMachine timeout while waiting for state: %s', self.from_state )
+ self._timeout = True # to indicate we should not transition
+ return False
+ self.state_machine.lock.wait(self.wait)
+
+ logging.debug('StateMachine entered context in state: %s',
+ self.state_machine.current_state() )
+ return True
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ if exc_val is not None:
+ logging.exception( "StateMachine exception in context, remaining in state: %s\n%s:%s",
+ self.state_machine.current_state(), exc_type.__name__, exc_val )
+ elif not self._timeout:
+ logging.debug(' ==== TRANSITION %s -> %s',
+ self.state_machine.current_state(), self.to_state)
+ self.state_machine._set_state( self.to_state )
+
+ self.state_machine.lock.notify_all()
+ self.state_machine.lock.release()
+ return False # re-raise any exception
diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py
index 025884b7..a8bcac00 100644
--- a/sleekxmpp/xmlstream/xmlstream.py
+++ b/sleekxmpp/xmlstream/xmlstream.py
@@ -1,9 +1,9 @@
"""
- SleekXMPP: The Sleek XMPP Library
- Copyright (C) 2010 Nathanael C. Fritz
- This file is part of SleekXMPP.
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
- See the file license.txt for copying permission.
+ See the file license.txt for copying permission.
"""
from __future__ import with_statement, unicode_literals
@@ -16,12 +16,14 @@ from . stanzabase import StanzaBase
from xml.etree import cElementTree
from xml.parsers import expat
import logging
+import random
import socket
import threading
import time
import traceback
import types
import xml.sax.saxutils
+from . import scheduler
HANDLER_THREADS = 1
@@ -45,6 +47,10 @@ class CloseStream(Exception):
stanza_extensions = {}
+RECONNECT_MAX_DELAY = 3600
+RECONNECT_QUIESCE_FACTOR = 1.6180339887498948 # Phi
+RECONNECT_QUIESCE_JITTER = 0.11962656472 # molar Planck constant times c, joule meter/mole
+
class XMLStream(object):
"A connection manager with XML events."
@@ -52,8 +58,9 @@ class XMLStream(object):
global ssl_support
self.ssl_support = ssl_support
self.escape_quotes = escape_quotes
- self.state = statemachine.StateMachine()
- self.state.addStates({'connected':False, 'is client':False, 'ssl':False, 'tls':False, 'reconnect':True, 'processing':False, 'disconnecting':False}) #set initial states
+ self.state = statemachine.StateMachine(('disconnected','connecting',
+ 'connected'))
+ self.should_reconnect = True
self.setSocket(socket)
self.address = (host, int(port))
@@ -69,12 +76,14 @@ class XMLStream(object):
self.filesocket = None
self.use_ssl = False
self.use_tls = False
+ self.ca_certs=None
self.stream_header = "<stream>"
self.stream_footer = "</stream>"
self.eventqueue = queue.Queue()
- self.sendqueue = queue.Queue()
+ self.sendqueue = queue.PriorityQueue()
+ self.scheduler = scheduler.Scheduler(self.eventqueue)
self.namespace_map = {}
@@ -83,45 +92,77 @@ class XMLStream(object):
def setSocket(self, socket):
"Set the socket"
self.socket = socket
- if socket is not None:
+ if socket is not None and self.state.transition('disconnected','connecting'):
self.filesocket = socket.makefile('rb', 0) # ElementTree.iterparse requires a file. 0 buffer files have to be binary
- self.state.set('connected', True)
-
+ self.state.transition('connecting','connected')
def setFileSocket(self, filesocket):
self.filesocket = filesocket
- def connect(self, host='', port=0, use_ssl=False, use_tls=True):
- "Link to connectTCP"
- return self.connectTCP(host, port, use_ssl, use_tls)
+ def connect(self, host='', port=0, use_ssl=None, use_tls=None):
+ "Establish a socket connection to the given XMPP server."
+
+ if not self.state.transition('disconnected','connected',
+ func=self.connectTCP, args=[host, port, use_ssl, use_tls] ):
+
+ if self.state['connected']: logging.debug('Already connected')
+ else: logging.warning("Connection failed" )
+ return False
+
+ logging.debug('Connection complete.')
+ return True
+
+ # TODO currently a caller can't distinguish between "connection failed" and
+ # "we're already trying to connect from another thread"
def connectTCP(self, host='', port=0, use_ssl=None, use_tls=None, reattempt=True):
"Connect and create socket"
- while reattempt and not self.state['connected']:
- if host and port:
- self.address = (host, int(port))
- if use_ssl is not None:
- self.use_ssl = use_ssl
- if use_tls is not None:
- self.use_tls = use_tls
- self.state.set('is client', True)
- if sys.version_info < (3, 0):
- self.socket = filesocket.Socket26(socket.AF_INET, socket.SOCK_STREAM)
- else:
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.socket.settimeout(None)
- if self.use_ssl and self.ssl_support:
- logging.debug("Socket Wrapped for SSL")
- self.socket = ssl.wrap_socket(self.socket)
+
+ # Note that this is thread-safe by merit of being called solely from connect() which
+ # holds the state lock.
+
+ delay = 1.0 # reconnection delay
+ while self.run:
+ logging.debug('connecting....')
try:
+ if host and port:
+ self.address = (host, int(port))
+ if use_ssl is not None:
+ self.use_ssl = use_ssl
+ if use_tls is not None:
+ # TODO this variable doesn't seem to be used for anything!
+ self.use_tls = use_tls
+ if sys.version_info < (3, 0):
+ self.socket = filesocket.Socket26(socket.AF_INET, socket.SOCK_STREAM)
+ else:
+ self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.socket.settimeout(None) #10)
+
+ if self.use_ssl and self.ssl_support:
+ logging.debug("Socket Wrapped for SSL")
+ self.socket = ssl.wrap_socket(self.socket,ca_certs=self.ca_certs)
+
self.socket.connect(self.address)
- #self.filesocket = self.socket.makefile('rb', 0)
self.filesocket = self.socket.makefile('rb', 0)
- self.state.set('connected', True)
+
return True
+
except socket.error as serr:
- logging.error("Could not connect. Socket Error #%s: %s" % (serr.errno, serr.strerror))
- time.sleep(1)
+ logging.exception("Socket Error #%s: %s", serr.errno, serr.strerror)
+ if not reattempt: return False
+ except:
+ logging.exception("Connection error")
+ if not reattempt: return False
+
+ # quiesce if rconnection fails:
+ # This algorithm based loosely on Twisted internet.protocol
+ # http://twistedmatrix.com/trac/browser/trunk/twisted/internet/protocol.py#L310
+ delay = min(delay * RECONNECT_QUIESCE_FACTOR, RECONNECT_MAX_DELAY)
+ delay = random.normalvariate(delay, delay * RECONNECT_QUIESCE_JITTER)
+ logging.debug('Waiting %fs until next reconnect attempt...', delay)
+ time.sleep(delay)
+
+
def connectUnix(self, filepath):
"Connect to Unix file and create socket"
@@ -130,14 +171,19 @@ class XMLStream(object):
"Handshakes for TLS"
if self.ssl_support:
logging.info("Negotiating TLS")
- self.realsocket = self.socket
- self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False)
+# self.realsocket = self.socket # NOT USED
+ self.socket = ssl.wrap_socket(self.socket,
+ ssl_version=ssl.PROTOCOL_TLSv1,
+ do_handshake_on_connect=False,
+ ca_certs=self.ca_certs)
self.socket.do_handshake()
if sys.version_info < (3,0):
from . filesocket import filesocket
self.filesocket = filesocket(self.socket)
else:
self.filesocket = self.socket.makefile('rb', 0)
+
+ logging.debug("TLS negotitation successful")
return True
else:
logging.warning("Tried to enable TLS, but ssl module not found.")
@@ -145,67 +191,56 @@ class XMLStream(object):
raise RestartStream()
def process(self, threaded=True):
+ self.scheduler.process(threaded=True)
+ self.run = True
for t in range(0, HANDLER_THREADS):
- self.__thread['eventhandle%s' % t] = threading.Thread(name='eventhandle%s' % t, target=self._eventRunner)
- self.__thread['eventhandle%s' % t].start()
- self.__thread['sendthread'] = threading.Thread(name='sendthread', target=self._sendThread)
- self.__thread['sendthread'].start()
+ th = threading.Thread(name='eventhandle%s' % t, target=self._eventRunner)
+ th.setDaemon(True)
+ self.__thread['eventhandle%s' % t] = th
+ th.start()
+ th = threading.Thread(name='sendthread', target=self._sendThread)
+ th.setDaemon(True)
+ self.__thread['sendthread'] = th
+ th.start()
if threaded:
- self.__thread['process'] = threading.Thread(name='process', target=self._process)
- self.__thread['process'].start()
+ th = threading.Thread(name='process', target=self._process)
+ th.setDaemon(True)
+ self.__thread['process'] = th
+ th.start()
else:
self._process()
- def schedule(self, seconds, handler, args=None):
- threading.Timer(seconds, handler, args).start()
+ def schedule(self, name, seconds, callback, args=None, kwargs=None, repeat=False):
+ self.scheduler.add(name, seconds, callback, args, kwargs, repeat, qpointer=self.eventqueue)
def _process(self):
"Start processing the socket."
- firstrun = True
- while self.run and (firstrun or self.state['reconnect']):
- self.state.set('processing', True)
- firstrun = False
+ logging.debug('Process thread starting...')
+ while self.run:
+ if not self.state.ensure('connected',wait=2): continue
try:
- if self.state['is client']:
- self.sendRaw(self.stream_header)
- while self.run and self.__readXML():
- if self.state['is client']:
- self.sendRaw(self.stream_header)
- except KeyboardInterrupt:
- logging.debug("Keyboard Escape Detected")
- self.state.set('processing', False)
- self.state.set('reconnect', False)
- self.disconnect()
- self.run = False
- self.eventqueue.put(('quit', None, None))
- return
+ self.sendPriorityRaw(self.stream_header)
+ while self.run and self.__readXML(): pass
+ except socket.timeout:
+ logging.debug('socket rcv timeout')
+ pass
except CloseStream:
- return
- except SystemExit:
+ # TODO warn that the listener thread is exiting!!!
+ pass
+ except RestartStream:
+ logging.debug("Restarting stream...")
+ continue # DON'T re-initialize the stream -- this exception is sent
+ # specifically when we've initialized TLS and need to re-send the <stream> header.
+ except (KeyboardInterrupt, SystemExit):
+ logging.debug("System interrupt detected")
+ self.shutdown()
self.eventqueue.put(('quit', None, None))
- return
- except socket.error:
- if not self.state.reconnect:
- return
- else:
- self.state.set('processing', False)
- traceback.print_exc()
- self.disconnect(reconnect=True)
except:
- if not self.state.reconnect:
- return
- else:
- self.state.set('processing', False)
- traceback.print_exc()
+ logging.exception('Unexpected error in RCV thread')
+ if self.should_reconnect:
self.disconnect(reconnect=True)
- if self.state['reconnect']:
- self.reconnect()
- self.state.set('processing', False)
- self.eventqueue.put(('quit', None, None))
- #self.__thread['readXML'] = threading.Thread(name='readXML', target=self.__readXML)
- #self.__thread['readXML'].start()
- #self.__thread['spawnEvents'] = threading.Thread(name='spawnEvents', target=self.__spawnEvents)
- #self.__thread['spawnEvents'].start()
+
+ logging.debug('Quitting Process thread')
def __readXML(self):
"Parses the incoming stream, adding to xmlin queue as it goes"
@@ -218,82 +253,94 @@ class XMLStream(object):
if edepth == 0: # and xmlobj.tag.split('}', 1)[-1] == self.basetag:
if event == b'start':
root = xmlobj
+ logging.debug('handling start stream')
self.start_stream_handler(root)
if event == b'end':
edepth += -1
if edepth == 0 and event == b'end':
- self.disconnect(reconnect=self.state['reconnect'])
+ # what is this case exactly? Premature EOF?
+ logging.debug("Ending readXML loop")
return False
elif edepth == 1:
#self.xmlin.put(xmlobj)
- try:
- self.__spawnEvent(xmlobj)
- except RestartStream:
- return True
- except CloseStream:
- return False
- if root:
- root.clear()
+ self.__spawnEvent(xmlobj)
+ if root: root.clear()
if event == b'start':
edepth += 1
+ logging.debug("Exiting readXML loop")
+ return False
def _sendThread(self):
+ logging.debug('send thread starting...')
while self.run:
- data = self.sendqueue.get(True)
- logging.debug("SEND: %s" % data)
+ if not self.state.ensure('connected',wait=2): continue
+
+ data = None
try:
- self.socket.send(data.encode('utf-8'))
- #self.socket.send(bytes(data, "utf-8"))
- #except socket.error,(errno, strerror):
+ data = self.sendqueue.get(True,5)[1]
+ logging.debug("SEND: %s" % data)
+ self.socket.sendall(data.encode('utf-8'))
+ except queue.Empty:
+# logging.debug('Nothing on send queue')
+ pass
+ except socket.timeout:
+ # this is to prevent a thread blocked indefinitely
+ logging.debug('timeout sending packet data')
except:
logging.warning("Failed to send %s" % data)
- self.state.set('connected', False)
- if self.state.reconnect:
- logging.error("Disconnected. Socket Error.")
- traceback.print_exc()
+ logging.exception("Socket error in SEND thread")
+ # TODO it's somewhat unsafe for the sender thread to assume it can just
+ # re-intitialize the connection, since the receiver thread could be doing
+ # the same thing concurrently. Oops! The safer option would be to throw
+ # some sort of event that could be handled by a common thread or the reader
+ # thread to perform reconnect and then re-initialize the handler threads as well.
+ if self.should_reconnect:
self.disconnect(reconnect=True)
def sendRaw(self, data):
- self.sendqueue.put(data)
+ self.sendqueue.put((1, data))
+ return True
+
+ def sendPriorityRaw(self, data):
+ self.sendqueue.put((0, data))
return True
def disconnect(self, reconnect=False):
- self.state.set('reconnect', reconnect)
- if self.state['disconnecting']:
+ if not self.state.transition('connected','disconnected'):
+ logging.warning("Already disconnected.")
return
- if not self.state['reconnect']:
- logging.debug("Disconnecting...")
- self.state.set('disconnecting', True)
- self.run = False
- if self.state['connected']:
- self.sendRaw(self.stream_footer)
- time.sleep(1)
- #send end of stream
- #wait for end of stream back
+ logging.debug("Disconnecting...")
+ self.sendPriorityRaw(self.stream_footer)
+ time.sleep(5)
+ #send end of stream
+ #wait for end of stream back
try:
+# self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
+ except socket.error as (errno,strerror):
+ logging.exception("Error while disconnecting. Socket Error #%s: %s" % (errno, strerror))
+ try:
self.filesocket.close()
- self.socket.shutdown(socket.SHUT_RDWR)
- except socket.error as serr:
- #logging.warning("Error while disconnecting. Socket Error #%s: %s" % (errno, strerror))
- #thread.exit_thread()
- pass
- if self.state['processing']:
- #raise CloseStream
- pass
-
- def reconnect(self):
- self.state.set('tls',False)
- self.state.set('ssl',False)
- time.sleep(1)
- self.connect()
+ except socket.error as (errno,strerror):
+ logging.exception("Error closing filesocket.")
+
+ if reconnect: self.connect()
+ def shutdown(self):
+ '''
+ Disconnects and shuts down all event threads.
+ '''
+ self.disconnect()
+ self.run = False
+ self.scheduler.run = False
+
def incoming_filter(self, xmlobj):
return xmlobj
-
+
def __spawnEvent(self, xmlobj):
"watching xmlOut and processes handlers"
#convert XML into Stanza
+ # TODO surround this log statement with an if, it's expensive
logging.debug("RECV: %s" % cElementTree.tostring(xmlobj))
xmlobj = self.incoming_filter(xmlobj)
stanza = None
@@ -305,48 +352,54 @@ class XMLStream(object):
if stanza is None:
stanza = StanzaBase(self, xmlobj)
unhandled = True
+ # TODO inefficient linear search; performance might be improved by hashtable lookup
for handler in self.__handlers:
if handler.match(stanza):
+ logging.debug('matched stanza to handler %s', handler.name)
handler.prerun(stanza)
self.eventqueue.put(('stanza', handler, stanza))
- if handler.checkDelete(): self.__handlers.pop(self.__handlers.index(handler))
+ if handler.checkDelete():
+ logging.debug('deleting callback %s', handler.name)
+ self.__handlers.pop(self.__handlers.index(handler))
unhandled = False
if unhandled:
stanza.unhandled()
#loop through handlers and test match
#spawn threads as necessary, call handlers, sending Stanza
-
+
def _eventRunner(self):
logging.debug("Loading event runner")
while self.run:
try:
event = self.eventqueue.get(True, timeout=5)
except queue.Empty:
+# logging.debug('Nothing on event queue')
event = None
if event is not None:
etype = event[0]
handler = event[1]
args = event[2:]
- #etype, handler, *args = event #python 3.x way
+ #etype, handler, *args = event #python 3.x way
if etype == 'stanza':
try:
handler.run(args[0])
except Exception as e:
- traceback.print_exc()
+ logging.exception("Exception in event handler")
args[0].exception(e)
elif etype == 'sched':
try:
+ #handler(*args[0])
handler.run(*args)
except:
logging.error(traceback.format_exc())
elif etype == 'quit':
logging.debug("Quitting eventRunner thread")
return False
-
+
def registerHandler(self, handler, before=None, after=None):
"Add handler with matcher class and parameters."
self.__handlers.append(handler)
-
+
def removeHandler(self, name):
"Removes the handler."
idx = 0
@@ -432,4 +485,4 @@ class XMLStream(object):
def start_stream_handler(self, xml):
"""Meant to be overridden"""
- pass
+ logging.warn("No start stream handler has been implemented.")