# 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.factory.on_connect = None
            #
            #print "waiting for messages..."
            #print "use the redis client to send messages:"
            #print "$ redis-cli publish zz test"
            #print "$ redis-cli publish foo.bar hello world"
            #self.subscribe("zz")
            #self.psubscribe("foo.*")
            #reactor.callLater(10, self.unsubscribe, "zz")
            #reactor.callLater(15, self.punsubscribe, "foo.*")

            # self.continueTrying = False
            # self.transport.loseConnection()

    def messageReceived(self, pattern, channel, message):
        if message.startswith('j:'):
            message = json.loads(message[2:])
        else:
            message = message[2:]

        callbacks = []
        if pattern and pattern in self.callbacks:
            callbacks = self.callbacks[pattern]
        elif channel and channel in self.callbacks:
            callbacks = self.callbacks[channel]

        for clb in callbacks:
            clb(channel, message)

    def connectionLost(self, reason):

# 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.factory.on_connect = None
            #
            #print "waiting for messages..."
            #print "use the redis client to send messages:"
            #print "$ redis-cli publish zz test"
            #print "$ redis-cli publish foo.bar hello world"
            #self.subscribe("zz")
            #self.psubscribe("foo.*")
            #reactor.callLater(10, self.unsubscribe, "zz")
            #reactor.callLater(15, self.punsubscribe, "foo.*")

            # self.continueTrying = False
            # self.transport.loseConnection()

    def messageReceived(self, pattern, channel, message):
        if message.startswith('j:'):
            message = json.loads(message[2:])
        else:
            message = message[2:]

        callbacks = []
        if pattern and pattern in self.callbacks:
            callbacks = self.callbacks[pattern]
        elif channel and channel in self.callbacks:
            callbacks = self.callbacks[channel]

        for clb in callbacks:
            clb(channel, message)

    def connectionLost(self, reason):

        log.msg("Connection lost: %s" % reason)