Example #1
0
    def main(self):
        xmpp = yield self.xmpp_connect()
        room = yield xmpp.muc.join(self.room)

        yield idiokit.pipe(
            room,
            events.stanzas_to_events(),
            self._recv()
        )
Example #2
0
    def main(self):
        xmpp = yield self.xmpp_connect()
        room = yield xmpp.muc.join(self.room)

        yield idiokit.pipe(
            room,
            events.stanzas_to_events(),
            self._recv()
        )
Example #3
0
 def handle_room(inner, self, name):
     self.log.info("Joining room %r", name)
     room = yield inner.sub(self.xmpp.muc.join(name, self.bot_name))
     self.log.info("Joined room %r", name)
     try:
         yield inner.sub(events.events_to_elements()
                         | room
                         | events.stanzas_to_events()
                         | self.distribute(name))
     finally:
         self.log.info("Left room %r", name)
Example #4
0
 def handle_room(inner, self, name):
     self.log.info("Joining room %r", name)
     room = yield inner.sub(self.xmpp.muc.join(name, self.bot_name))
     self.log.info("Joined room %r", name)
     try:
         yield inner.sub(events.events_to_elements()
                         | room
                         | events.stanzas_to_events()
                         | self.distribute(name))
     finally:
         self.log.info("Left room %r", name)
Example #5
0
    def handle_room(inner, self, name):
        self.log.info("Calling handle_room")
        self.log.info("Joining room %r", name)
        room = yield inner.sub(self.xmpp.muc.join(name, self.bot_name))
        self.log.info("Joined room %r", name)

        try:
            yield inner.sub(room
                            | events.stanzas_to_events()
                            | self.collect(name))
        finally:
            self.log.info("Left room %r", name)
Example #6
0
    def handle_room(inner, self, name):
        self.log.info("Joining room %r", name)
        room = yield inner.sub(self.xmpp.muc.join(name, self.bot_name))
        self.log.info("Joined room %r", name)

        try:
            yield inner.sub(room
                            | self.skip_own(room)
                            | events.stanzas_to_events()
                            | self.history.collect(unicode(room.room_jid))
                            | threado.dev_null())
        finally:
            self.log.info("Left room %r", name)
Example #7
0
    def session(self, state, src_room, dst_room, window_time=60.0, rule=None):
        if rule is None:
            rule = rules.Anything()
        rule = rules.rule(rule)

        queue = collections.deque()
        ids = dict()

        to = self.to_room(dst_room)
        idiokit.pipe(self.purge(ids, queue), events.events_to_elements(), to)

        yield idiokit.pipe(self.from_room(src_room),
                           events.stanzas_to_events(), self.match(rule),
                           self.process(ids, queue, window_time),
                           events.events_to_elements(), to)
Example #8
0
    def session(self, state, src_room, dst_room, window_time=60.0, rule=None):
        if rule is None:
            rule = rules.Anything()
        rule = rules.rule(rule)

        queue = collections.deque()
        ids = dict()

        to = self.to_room(dst_room)
        idiokit.pipe(self.purge(ids, queue), events.events_to_elements(), to)

        yield idiokit.pipe(
            self.from_room(src_room),
            events.stanzas_to_events(),
            self.match(rule),
            self.process(ids, queue, window_time),
            events.events_to_elements(),
            to
        )
Example #9
0
    def _handle_room(self, name):
        msg = "room {0!r}".format(name)

        attrs = events.Event({
            "type": "room",
            "service": self.bot_name,
            "room": unicode(name)
        })

        with self.log.stateful(repr(self.xmpp.jid), "room", repr(name)) as log:
            log.open("Joining " + msg, attrs, status="joining")
            room = yield self.xmpp.muc.join(name, self.bot_name)

            log.open("Joined " + msg, attrs, status="joined")
            try:
                yield idiokit.pipe(
                    room,
                    events.stanzas_to_events(),
                    self._archive(room.jid.bare())
                )
            finally:
                log.close("Left " + msg, attrs, status="left")
Example #10
0
 def _handle_augment(self, src_room, dst_room, args):
     return idiokit.pipe(self.from_room(src_room),
                         events.stanzas_to_events(), self.augment(*args),
                         events.events_to_elements(),
                         self.to_room(dst_room))
Example #11
0
class OverviewBot(RoomBot):
    def __init__(self, *args, **keys):
        RoomBot.__init__(self, *args, **keys)
        self._aggregates = dict(self.aggregates())

    def aggregates(self):
        return dict(max=Max, min=Min, count=Count, sum=Sum)

    @idiokit.stream
    def throttle(self, throttle_time):
        """
        Ensure that updates for a given event id are sent at least
        throttle_time apart.
        """

        sleeper = timer.sleep(1.0)

        ids = dict()
        queue = list()

        while True:
            yield timer.sleep(1.0)

            item = yield idiokit.next()
            current_time = time.time()

            id, event = item
            previous, _, update_time = ids.get(id, (None, None, None))
            if update_time is None:
                update_time = current_time + 1.0
                heapq.heappush(queue, (update_time, id))
            ids[id] = previous, event, update_time

            while queue and queue[0][0] <= current_time:
                _, id = heapq.heappop(queue)

                previous, next, _ = ids[id]
                if previous == next == None:
                    del ids[id]
                    continue

                if previous != next:
                    if next is None:
                        event = events.Event()
                    else:
                        event = events.Event(next)
                    event.add("id", id)
                    yield idiokit.send(event)

                if previous != next:
                    update_time = current_time + throttle_time
                    heapq.heappush(queue, (update_time, id))
                else:
                    update_time = None
                ids[id] = next, next, update_time

    @idiokit.stream
    def aggregate(self, group_keys, window_info):
        """
        Create aggregated events and ids for them.
        """

        group_keys = tuple(set(group_keys))
        key_groups = dict()

        while True:
            yield timer.sleep(1.0)

            event = yield idiokit.next()

            current_time = time.time()
            updated = set()

            key = tuple(tuple(sorted(event.values(x))) for x in group_keys)
            updated.add(key)

            if key not in key_groups:
                windows = []
                for constructor, keys, output_key in window_info:
                    windows.append((constructor(**keys), output_key))
                key_groups[key] = windows

            for window, output_key in key_groups[key]:
                window.push(current_time, event)

            for key, windows in list(key_groups.iteritems()):
                any_expired = False
                for window, _ in windows:
                    any_expired = window.expire(current_time) or any_expired

                if not (any_expired or key in updated):
                    continue

                output = None

                for window, output_key in windows:
                    value = window.value()
                    if value is None:
                        continue

                    if output is None:
                        output = events.Event()
                    output.add(output_key, unicode(value))

                id = hashlib.md5(repr(key)).hexdigest()
                if output is not None:
                    for group_key, group_values in zip(group_keys, key):
                        output.update(group_key, group_values)

                yield idiokit.send(id, output)
                if output is None:
                    del key_groups[key]

    @idiokit.stream
    def session(self,
                state,
                src_room,
                dst_room,
                group_keys=[],
                aggregates=[],
                throttle_time=10.0):
        aggregate_list = list()
        for original_aggregate in aggregates:
            aggregate = dict(original_aggregate)

            try:
                name = aggregate.pop("name")
                output_key = aggregate.pop("output_key")
            except KeyError, error:
                self.log.error("No key %r defined in %r" %
                               (error.args[0], original_aggregate))
                raise

            try:
                aggregate_cls = self._aggregates[name]
            except KeyError:
                self.log.error("No aggregate type %r available" % (name, ))
                raise

            aggregate_list.append((aggregate_cls, aggregate, output_key))

        yield (self.from_room(src_room)
               | events.stanzas_to_events()
               | self.aggregate(group_keys, aggregate_list)
               | self.throttle(throttle_time)
               | events.events_to_elements()
               | self.to_room(dst_room))