Example #1
0
        def tick():
            start = time.time()

            # Check for new scheduled commands
            # TODO: New periodic commands
            while True:
                try:
                    triple = incoming.get(timeout=1 / 6 * duration)
                except queue.Empty:
                    break
                else:
                    db["saxo_schedule"].insert(triple)
                    elapsed = time.time() - start
                    if elapsed > (1 / 3 * duration):
                        break

            # Periodic commands
            for (period, command, args), when in periodic.items():
                if when < start:
                    # Calling this command causes the following del to fail
                    cmd = command.decode("ascii")
                    client.put((cmd,) + common.b64unpickle(args))
                    periodic[(period, command, args)] += period

            # Scheduled commands
            schedule = db["saxo_schedule"].rows(order="unixtime")
            for (unixtime, command, args) in schedule:
                if unixtime > start:
                    break
                # Calling this command causes the following del to fail
                cmd = command.decode("ascii")
                client.put((cmd,) + common.b64unpickle(args))
                del db["saxo_schedule"][(unixtime, command, args)]

            elapsed = time.time() - start
            if elapsed < duration:
                time.sleep(duration - elapsed)

            return True
Example #2
0
File: irc.py Project: nslater/saxo
            def handle(connection, client):
                try:
                    for octets in connection.makefile("rb"):
                        try:
                            text = octets.decode("ascii", "replace")
                            text = text.strip("\n")

                            if " " in text:
                                instruction, data = text.split(" ", 1)
                                args = common.b64unpickle(data)
                            else:
                                instruction, args = text, tuple()

                            incoming.put((instruction,) + args)
                        except Exception as err:
                            debug("ERROR!", err.__class__.__name__, err)
                finally:
                    connection.close()
Example #3
0
File: irc.py Project: m481114/saxo
            def handle(connection, client):
                try:
                    for octets in connection.makefile("rb"):
                        try:
                            text = octets.decode("ascii", "replace")
                            text = text.strip("\n")

                            if " " in text:
                                instruction, data = text.split(" ", 1)
                                args = common.b64unpickle(data)
                            else:
                                instruction, args = text, tuple()

                            incoming.put((instruction, ) + args)
                        except Exception as err:
                            debug("ERROR!", err.__class__.__name__, err)
                finally:
                    connection.close()
Example #4
0
    def tick(self):
        start = time.time()
        # Check for new scheduled commands
        # TODO: New periodic commands
        while True:
            try: a, b = incoming.get(timeout=self.duration / 6)
            except queue.Empty:
                break
            else:
                if a == "connected":
                    self.connections += 1
                    self.message("connected (%s)" % self.connections)
                    self.connected = True
                elif a == "disconnected":
                    self.message("disconnected, and stopped")
                    self.connected = False
                    self.running = False
                elif a == "start":
                    if self.connected:
                        self.message("started at tick %s" % start)
                        self.running = True
                elif a == "stop":
                    self.message("stopped")
                    self.running = False
                elif a == "schedule.add":
                    if "saxo_schedule" in self.db:
                        self.db["saxo_schedule"].insert(b)
                else:
                    self.message("unknown instruction: %s" % a)

                elapsed = time.time() - start
                if elapsed > (self.duration / 3):
                    break

        if self.connected and self.running:
            # Periodic commands
            if "saxo_periodic" in self.db:
                deletions = []
                additions = []
                periodic = self.db["saxo_periodic"].rows(order="recent")
                for (name, period, recent, command, args) in periodic:
                    # find next %0 point after start
                    unixtime = recent - (recent % period) + period
                    if unixtime > int(start):
                        continue
                    cmd = command.decode("ascii")
                    self.client.put((cmd,) + common.b64unpickle(args))
                    deletions.append((name, period, recent, command, args))
                    additions.append((name, period, int(start), command, args))

                for deletion in deletions:
                    del self.db["saxo_periodic"][deletion]
                for addition in additions:
                    self.db["saxo_periodic"].insert(addition)

            # Scheduled commands
            if "saxo_schedule" in self.db:
                deletions[:] = []
                schedule = self.db["saxo_schedule"].rows(order="unixtime")
                for (unixtime, command, args) in schedule:
                    if unixtime > start:
                        break
                    cmd = command.decode("ascii")
                    self.client.put((cmd,) + common.b64unpickle(args))
                    deletions.append((unixtime, command, args))

                for deletion in deletions:
                    del self.db["saxo_schedule"][deletion]

        elapsed = time.time() - start
        if elapsed < self.duration:
            time.sleep(self.duration - elapsed)

        return True
Example #5
0
    def tick(self):
        start = time.time()
        # Check for new scheduled commands
        # TODO: New periodic commands
        while True:
            try:
                a, b = incoming.get(timeout=self.duration / 6)
            except queue.Empty:
                break
            else:
                if a == "connected":
                    self.connections += 1
                    self.message("connected (%s)" % self.connections)
                    self.connected = True
                elif a == "disconnected":
                    self.message("disconnected, and stopped")
                    self.connected = False
                    self.running = False
                elif a == "start":
                    if self.connected:
                        self.message("started at tick %s" % start)
                        self.running = True
                elif a == "stop":
                    self.message("stopped")
                    self.running = False
                elif a == "schedule.add":
                    if "saxo_schedule" in self.db:
                        self.db["saxo_schedule"].insert(b)
                else:
                    self.message("unknown instruction: %s" % a)

                elapsed = time.time() - start
                if elapsed > (self.duration / 3):
                    break

        if self.connected and self.running:
            # Periodic commands
            if "saxo_periodic" in self.db:
                deletions = []
                additions = []
                periodic = self.db["saxo_periodic"].rows(order="recent")
                for (name, period, recent, command, args) in periodic:
                    # find next %0 point after start
                    unixtime = recent - (recent % period) + period
                    if unixtime > int(start):
                        continue
                    cmd = command.decode("ascii")
                    self.client.put((cmd, ) + common.b64unpickle(args))
                    deletions.append((name, period, recent, command, args))
                    additions.append((name, period, int(start), command, args))

                for deletion in deletions:
                    del self.db["saxo_periodic"][deletion]
                for addition in additions:
                    self.db["saxo_periodic"].insert(addition)

            # Scheduled commands
            if "saxo_schedule" in self.db:
                deletions[:] = []
                schedule = self.db["saxo_schedule"].rows(order="unixtime")
                for (unixtime, command, args) in schedule:
                    if unixtime > start:
                        break
                    cmd = command.decode("ascii")
                    self.client.put((cmd, ) + common.b64unpickle(args))
                    deletions.append((unixtime, command, args))

                for deletion in deletions:
                    del self.db["saxo_schedule"][deletion]

        elapsed = time.time() - start
        if elapsed < self.duration:
            time.sleep(self.duration - elapsed)

        return True