# Path: mcloud/util.py
# def txtimeout(deferred, timeout, fail):
# 
#     def _raise():
#         deferred.errback(Failure(TxTimeoutEception(fail)))
# 
#     delayedCall = reactor.callLater(timeout, _raise)
# 
#     def gotResult(result):
#         if delayedCall.active():
#             delayedCall.cancel()
#         return result
#     deferred.addBoth(gotResult)
# 
#     return deferred


import json
import txredisapi as redis
from twisted.internet import reactor, defer
from twisted.python import log
from mcloud.util import txtimeout
        self.protocol.on(pattern, callback)
        log.msg('Registered %s for channel: %s' % (callback, pattern))

    def cancel(self, pattern, callback):
        if not self.protocol:
            raise Exception('Event bus is not connected yet!')
        self.protocol.cancel(pattern, callback)
        log.msg('unRegistered %s for channel: %s' % (callback, pattern))

    def once(self, pattern, callback):
        if not self.protocol:
            raise Exception('Event bus is not connected yet!')

        def _once_and_remove(*args, **kwargs):
            self.protocol.cancel(pattern, _once_and_remove)
            callback(*args, **kwargs)

        self.protocol.on(pattern, _once_and_remove)
        log.msg('Registered %s for single invocation on channel: %s' % (callback, pattern))

    def wait_for_event(self, pattern, timeout=False):
        d = defer.Deferred()

        def _on_message(channel, message):
            if not d.called:
                d.callback(message)

        self.on(pattern, _on_message)

        if not timeout == 0:

# Path: mcloud/util.py
# def txtimeout(deferred, timeout, fail):
# 
#     def _raise():
#         deferred.errback(Failure(TxTimeoutEception(fail)))
# 
#     delayedCall = reactor.callLater(timeout, _raise)
# 
#     def gotResult(result):
#         if delayedCall.active():
#             delayedCall.cancel()
#         return result
#     deferred.addBoth(gotResult)
# 
#     return deferred


# Path: mcloud/events.py
import json
import txredisapi as redis
from twisted.internet import reactor, defer
from twisted.python import log
from mcloud.util import txtimeout

        self.protocol.on(pattern, callback)
        log.msg('Registered %s for channel: %s' % (callback, pattern))

    def cancel(self, pattern, callback):
        if not self.protocol:
            raise Exception('Event bus is not connected yet!')
        self.protocol.cancel(pattern, callback)
        log.msg('unRegistered %s for channel: %s' % (callback, pattern))

    def once(self, pattern, callback):
        if not self.protocol:
            raise Exception('Event bus is not connected yet!')

        def _once_and_remove(*args, **kwargs):
            self.protocol.cancel(pattern, _once_and_remove)
            callback(*args, **kwargs)

        self.protocol.on(pattern, _once_and_remove)
        log.msg('Registered %s for single invocation on channel: %s' % (callback, pattern))

    def wait_for_event(self, pattern, timeout=False):
        d = defer.Deferred()

        def _on_message(channel, message):
            if not d.called:
                d.callback(message)

        self.on(pattern, _on_message)

        if not timeout == 0:

            return txtimeout(d, timeout, lambda: d.callback(None))