コード例 #1
0
class BizLogic(object):

    def __init__(self, args):
        self.host = args.host
        self.port = args.port
        self.tether = Tether(None, "//%s/bizlogic" % self.host, None,
                             host=self.host, port=self.port,
                             agent_type="bizlogic", policy="ordered")

        self.users = {}
        self.user_reread_period = 30  # seconds
        self.linker = Linker()
        self.container = Container(Processor(self))
        self.container[self.tether.agent] = Agent(self.tether)

    def on_reactor_init(self, event):
        event.reactor.acceptor(self.host, self.port, self.container)
        self.tether.start(event.reactor)
        event.reactor.schedule(0, self)
        self.linker.start(event.reactor)

    def on_timer_task(self, event):
        self.users = common.load_data("users.pickle")
        event.reactor.schedule(self.user_reread_period, self)

    def on_message(self, event):
        bark = common.Bark(*event.message.body)
        words = bark.content.split()
        mentions = [word[1:] for word in words if word.startswith("@")]
        user = self.users[bark.user]
        followers = user.getFollowers(self.users)
        targets = set(mentions + followers + [bark.user])
        for target in targets:
            sender = self.linker.sender("//%s/inbox/%s" % (self.host, target))
            sender.send(event.message.body)
コード例 #2
0
class AutoBark(object):
    def __init__(self, rate, hostname):
        self.users = {}
        try:
            self.quotes = open("quotes.txt").read().split("\n")
        except IOError:
            self.quotes = [
                """"On the Internet, everybody knows you're a dog." --Oscar Wilde"""
            ]
        self.last_user_reread = 0
        self.user_reread_period = 30  # seconds
        self.bark_period = 1.0 / rate  # seconds
        self.linker = Linker()
        self.hostname = hostname

    def make_random_bark(self):
        while True:
            username = random.choice(self.users.keys())
            user = self.users[username]
            if user.autobark:
                break
        if random.random() > 0.9:
            messageText = random.choice(self.quotes)
        else:
            words = [
                random.choice(["woof", "arf", "ruff", "yap"])
                for idx in range(random.randint(3, 8))
            ]
            if random.random() > 0.75:
                words.append(
                    "@" + random.choice(user.follows))  # one can always dream
            if random.random() > 0.9:
                words.append("#subwoofer")
            messageText = " ".join(words)
        return common.Bark(username, messageText)

    def on_reactor_init(self, event):
        event.reactor.schedule(0, self)
        self.linker.start(event.reactor)

    def on_timer_task(self, event):
        now = time.time()
        if now - self.last_user_reread > self.user_reread_period:
            self.users = common.load_data("users.pickle")
            self.last_user_reread = now

        bark = self.make_random_bark()
        sender = self.linker.sender("//%s/outbox/%s" %
                                    (self.hostname, bark.user))
        sender.send(tuple(bark))

        event.reactor.schedule(self.bark_period, self)
コード例 #3
0
ファイル: autobark.py プロジェクト: alisheikh/datawire-common
class AutoBark(object):

    def __init__(self, rate, hostname):
        self.users = {}
        try:
            self.quotes = open("quotes.txt").read().split("\n")
        except IOError:
            self.quotes = [
            """"On the Internet, everybody knows you're a dog." --Oscar Wilde"""
            ]
        self.last_user_reread = 0
        self.user_reread_period = 30  # seconds
        self.bark_period = 1.0 / rate  # seconds
        self.linker = Linker()
        self.hostname = hostname

    def make_random_bark(self):
        while True:
            username = random.choice(self.users.keys())
            user = self.users[username]
            if user.autobark:
                break
        if random.random() > 0.9:
            messageText = random.choice(self.quotes)
        else:
            words = [random.choice(["woof", "arf", "ruff", "yap"]) for idx in range(random.randint(3, 8))]
            if random.random() > 0.75:
                words.append("@" + random.choice(user.follows))  # one can always dream
            if random.random() > 0.9:
                words.append("#subwoofer")
            messageText = " ".join(words)
        return common.Bark(username, messageText)

    def on_reactor_init(self, event):
        event.reactor.schedule(0, self)
        self.linker.start(event.reactor)

    def on_timer_task(self, event):
        now = time.time()
        if now - self.last_user_reread > self.user_reread_period:
            self.users = common.load_data("users.pickle")
            self.last_user_reread = now

        bark = self.make_random_bark()
        sender = self.linker.sender("//%s/outbox/%s" % (self.hostname, bark.user))
        sender.send(tuple(bark))

        event.reactor.schedule(self.bark_period, self)
コード例 #4
0
ファイル: linker.py プロジェクト: forkkit/datawire-common
    def testLinker(self, address_count=1, message_count=1):
        self.server.max_connections = address_count
        linker = Linker()
        linker.start(self.reactor)
        for i in range(address_count):
            for j in range(message_count):
                snd = linker.sender("//localhost:%s/%s" % (PORT, i))
                assert len(linker.senders) == i + 1
                snd.send(dict(i=i, j=j))
        linker.close()
        self.reactor.run()

        by_addr = dict((i,[]) for i in range(address_count))
        for m in self.sink.messages:
          by_addr[m["i"]].append(m)
        for addr, msgs in by_addr.iteritems():
          self.assertSequenceEqual(msgs, list(sorted(msgs, key=lambda x:(x["i"],x["j"]))))
        self.assertEqual(len(self.sink.messages), address_count*message_count)
コード例 #5
0
ファイル: linker.py プロジェクト: datawire/datawire-common
    def testLinker(self, address_count=1, message_count=1):
        self.server.max_connections = address_count
        linker = Linker()
        linker.start(self.reactor)
        for i in range(address_count):
            for j in range(message_count):
                snd = linker.sender("//localhost:%s/%s" % (PORT, i))
                assert len(linker.senders) == i + 1
                snd.send(dict(i=i, j=j))
        linker.close()
        self.reactor.run()

        by_addr = dict((i, []) for i in range(address_count))
        for m in self.sink.messages:
            by_addr[m["i"]].append(m)
        for addr, msgs in by_addr.iteritems():
            self.assertSequenceEqual(msgs, list(sorted(msgs, key=lambda x: (x["i"], x["j"]))))
        self.assertEqual(len(self.sink.messages), address_count * message_count)