示例#1
0
def work(host, port, brainfile, *, queue=None):
    canvas = Canvas()
    task = spawn(canvas.serve, host, port)
    if queue is not None:
        queue.put(canvas.serialize)

    mtime = 0

    with open(brainfile, 'r') as f:
        code = compile(f.read(), "somefile.py", 'exec')

    while True:
        gsleep(1)
        if mtime < os.stat(brainfile).st_mtime:
            canvas.fire('UNLOAD')
            canvas.events.clear()
            try:
                exec(code, {'on': canvas.on, '__file__': brainfile}, {})
            except:
                log.exception('Brain failed')
                continue
            canvas.fire('LOAD')
            mtime = os.stat(brainfile).st_mtime

    task.join()
示例#2
0
 def text(self, x, y, text, delay=0, linespace=1):
     for i, line in enumerate(text.splitlines()):
         line += '   '
         for j, c in enumerate(line):
             self.putc(x + j * self.font_res, y + i * self.font_res, ord(c))
             gsleep(delay)
         y += linespace
示例#3
0
 def text(self, x, y, text, delay=0, linespace=1):
     for i, line in enumerate(text.splitlines()):
         line += '   '
         for j, c in enumerate(line):
             self.putc(x+j*self.font_res, y+i*self.font_res, ord(c))
             gsleep(delay)
         y += linespace
示例#4
0
    def _loop(self):
        doptim = 1.0 / 30
        flip = pygame.display.flip
        getevents = pygame.event.get
        
        while True:
            t1 = time.time()

            for e in getevents():
                if e.type == pygame.VIDEORESIZE:
                    old = self.screen.copy()
                    self.screen = pygame.display.set_mode(e.size, self.flags)
                    self.screen.blit(old, (0,0))
                    self.width, self.height = e.size
                    self.fire('RESIZE')
                elif e.type == pygame.QUIT:
                    self.fire('QUIT')
                    return
                elif e.type == pygame.KEYDOWN:
                    self.fire('KEYDOWN-' + e.unicode)

            self.fire('TICK')
            self.frames += 1

            flip()

            dt = time.time() - t1
            gsleep(max(doptim-dt, 0))
示例#5
0
    def manage_web_socket(self, mws):
        """
        Adds mws instance to managed set (for receiving telemetry),
        and then continuously checks web socket for new messages, which
        may affect its state.
        When web-socket is considered closed, it is removed from the
        managed set and this method returns
        :param mws: Managed web-socket instance
        """
        self.dbg_message(
            f"Adding record for new web-socket ID:{mws.id} with IP: {mws.client_ip}"
        )
        self._socket_set.add(mws)

        while mws.is_alive:
            self.dbg_message(f"Polling web-socket record ID {mws.id} ")
            msg_processed = self.poll_websocket(mws)
            if not msg_processed:
                # If no message received, then sleep a lil
                gsleep(AITOpenMctPlugin.DEFAULT_WEBSOCKET_CHECK_SLEEP_SECS)

        # Web-socket is considered closed, so remove from set and return
        rem_msg_state = 'err' if mws.is_error else 'closed'
        self.dbg_message(
            f"Removing {rem_msg_state} web-socket record ID {mws.id}")
        self._socket_set.remove(mws)
示例#6
0
    def _loop(self):
        doptim = 1.0 / 25
        flip = pygame.display.flip
        getevents = pygame.event.get

        while True:
            t1 = time.time()

            for e in getevents():
                if e.type == pygame.VIDEORESIZE:
                    old = self.screen.copy()
                    self.screen = pygame.display.set_mode(e.size, self.flags)
                    self.screen.blit(old, (0, 0))
                    self.width, self.height = e.size
                    self.fire('RESIZE')
                elif e.type == pygame.QUIT:
                    self.fire('QUIT')
                    return
                elif e.type == pygame.KEYDOWN:
                    self.fire('KEYDOWN-' + e.unicode)

            self.fire('TICK')
            self.frames += 1

            flip()

            dt = time.time() - t1
            gsleep(max(doptim - dt, 0))
示例#7
0
    def start_thread():
        while True:
            try:
                schedule.run_pending()
            except Exception as e:
                print(str(e))

            gsleep(1)
示例#8
0
    def set_state(self, state):
        self._state = state
        self._state_change.set()

        # Wake up anyone waiting
        gsleep(0)

        self._state_change.clear()
示例#9
0
def sleep(seconds):
    """
    Facade for the sleep function. Do not use time.sleep if you are
    running green threads.

    :param  int time: time to sleep, in seconds. (Float for second
        divisions)
    """
    gsleep(seconds)
    def run(self):
        # Array since we need to pass by ref
        last_task_complete = [time()]
        last_maintenance_complete = time()
        active_upload_counter = ThreadsafeCounter()

        task_queue = TaskQueue()

        logging.info("Starting server on %d...", self._port)

        logging.debug("Task completed callback %s", last_task_complete)

        def completed_callback(last_task_complete):
            logging.debug("Task completed callback %s", last_task_complete)
            last_task_complete[:] = [time()]

        workers = WorkerPoolExecutor(
            partial(completed_callback, last_task_complete))
        workers.start(task_queue, self._workers)

        remote_push_adapter_map, users, do_maintenance = self.parse_config()

        webapp = UploadWebApp(__name__, users, self._managed_repos,
                              active_upload_counter, remote_push_adapter_map,
                              task_queue)

        http_server = WSGIServer(('', self._port), webapp)
        http_server.start()

        logging.info("Server started on %s", self._port)

        # loop until interrupted
        while True:
            try:
                gsleep(5)
                task_queue.join()
                logging.debug("Task queue empty, %s uploads ongoing",
                              str(active_upload_counter.count))

                # Continue looping if maintenance not desired
                if do_maintenance:
                    last_maintenance_complete = self.perform_maintenance(
                        workers, task_queue, last_maintenance_complete,
                        last_task_complete)
            except (KeyboardInterrupt, SystemExit):
                break

        logging.info("Cleaning up resources...")

        http_server.stop()

        workers.stop()
示例#11
0
    def run(self):
        self.running = True

        while self.running:
            try:
                work = self.queue.get_nowait()
            except EmptyError as ee:
                pass
            else:
                self.work(work)
            gsleep(0)

        logger.warn('Worker exiting')
示例#12
0
 def _loop(self):
     while True:
         gsleep(0.01) # Required to allow other tasks to run
         for e in pygame.event.get():
             if e.type == pygame.VIDEORESIZE:
                 self.on_resize(e.size)
             if e.type == pygame.KEYDOWN and e.unicode == 'q':
                 return
             if e.type == pygame.KEYDOWN and e.unicode == 'c':
                 self.clear()
             if e.type == pygame.QUIT:
                 return
         self.ticks += 1
         if 0 and self.ticks % 1000 == 0:
             pygame.image.save(self.screen,
             'hist%000000d.png' % (self.ticks/1000))
         pygame.display.flip()
         for client in self.clients.itervalues():
             client.tick()
示例#13
0
    def serve(self, socket):
        self.canvas.fire('CONNECT', self)

        with self.lock:
            self.socket = socket
            readline = self.socket.makefile().readline

        try:
            while self.socket:
                gsleep(10.0/self.pps)
                for i in range(10):
                    line = readline(1024).strip()
                    if not line:
                        break
                    arguments = line.split()
                    command = arguments.pop(0)
                    if not self.canvas.fire('COMMAND-%s' % command.upper(), self, *arguments):
                        self.disconnect()
        finally:
            self.disconnect()
示例#14
0
 def _loop(self):
     while True:
         gsleep(0.01) # Required to allow other tasks to run
         for e in pygame.event.get():
             if e.type == pygame.VIDEORESIZE:
                 old = self.screen.copy()
                 self.screen = pygame.display.set_mode(e.size, self.flags)
                 self.screen.blit(old, (0,0))
                 self.width, self.height = e.size
                 self.fire('RESIZE')
             elif e.type == pygame.QUIT:
                 self.fire('QUIT')
                 return
             elif e.type == pygame.KEYDOWN:
                 self.fire('KEYDOWN-' + e.unicode)
         self.ticks += 1
         self.fire('TICK', self.ticks)
         for c in self.clients.values():
             c.tick()
         pygame.display.flip()
示例#15
0
文件: server.py 项目: Fy-/Fy_IRCd
    def update_all(server):
        while 1:
            server.logger.info('update_all - check pings and timeouts...')

            _users = [
                user for user in User.by_nick.values()
                if user.fake == False and user.greet == True and (time.time() - user.ping) > server.ping_timeout
            ]
            for user in _users:
                user.quit('Ping timeout: {0}'.format(int(time.time() - user.ping)))

            half_timeout = server.ping_timeout / 2.0

            _users = [
                user for user in User.by_nick.values()
                if user.fake == False and user.greet == True and (time.time() - user.ping) > half_timeout
            ]
            for user in _users:
                user.write('PING :%s' % (server.name))

            gsleep(10)
示例#16
0
    def serve(self, socket):
        self.canvas.fire('CONNECT', self)

        with self.lock:
            self.socket = socket
            readline = self.socket.makefile().readline

        try:
            while self.socket:
                gsleep(10.0 / self.pps)
                for i in range(10):
                    line = readline(1024).strip()
                    if not line:
                        break
                    arguments = line.split()
                    command = arguments.pop(0)
                    if not self.canvas.fire('COMMAND-%s' % command.upper(),
                                            self, *arguments):
                        self.disconnect()
        finally:
            self.disconnect()
示例#17
0
    def serve(self, socket):
        self.canvas.fire('CONNECT', self)

        with self.lock:
            self.socket = socket
            readline = self.socket.makefile().readline

        try:
            while self.socket:
                gsleep(10.0 / self.pps)
                for i in range(10):
                    # wall has 16*24 pixels a 3 byte = 1152
                    # factor 4/3 for base64 encoding: 1536 so settle for 1600 with overhead
                    line = readline(1600).strip()
                    if not line:
                        break
                    arguments = line.split()
                    command = arguments.pop(0)
                    if not self.canvas.fire('COMMAND-%s' % command.upper(),
                                            self, *arguments):
                        self.disconnect()
        finally:
            self.disconnect()
示例#18
0
                      default="0.0.0.0", type="string",
                      help="specify hostname to run on")
    parser.add_option("-p", "--port", dest="portnum", default=1234,
                      type="int", help="port number to run on")

    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")

    canvas = Canvas()
    task = spawn(canvas.serve, options.hostname, options.portnum)
    
    brainfile = args[0]
    mtime = 0
    
    while True:
        gsleep(1)
        if mtime < os.stat(brainfile).st_mtime:
            canvas.fire('UNLOAD')
            canvas.events.clear()
            try:
                execfile(brainfile, {'on':canvas.on, '__file__': brainfile})
            except:
                log.exception('Brain failed')
                continue
            canvas.fire('LOAD')
            mtime = os.stat(brainfile).st_mtime
    
    task.join()

 def set_state(self, newstate):
     self.state = newstate
     self.state_change.set()
     gsleep(0) # wake up anyone waiting
     self.state_change.clear()
示例#20
0
 def poll_websocket_periodically_while_alive(self, mws):
     while mws.is_alive:
         gsleep(self.DEFAULT_WEBSOCKET_CHECK_SLEEP_SECS)
         self.poll_websocket(mws)
示例#21
0
def sleep(seconds):
    '''
    Facade for the sleep function. Do not use time.sleep if you are
    running green threads.
    '''
    gsleep(seconds)
    # TODO: Add argparse for these settings
    worker_count = 4

    workers = Workers()
    workers.start(task_list, worker)

    http_server = WSGIServer(('', PORT), app)
    http_server.start()

    print("Server started on %s" % PORT)

    # loop until interrupted
    while True:
        try:
            gsleep(5)
            task_list.join()
            print("task queue empty, " + str(active_upload_counter.count) + " uploads ongoing")
            time_since_maintenance = time() - latest_maintenance_complete
            time_since_task = time() - latest_task_complete
            print("{:.1f} since last task, {:.1f} since last maintenance".format(
                        time_since_task,
                        time_since_maintenance))
            if time_since_maintenance > time_since_task:
                # uploads have been processed since last maintenance
                print("maintenance needed")
                if time_since_task >= MAINTENANCE_WAIT:
                    print("idle, do maintenance")
                    workers.stop()

                    # TODO: don't use shell
示例#23
0
def custom_sleep(seconds=0):
    return gsleep(seconds=0)
示例#24
0
    m = size_pattern.match(options.size)
    size = (640, 480)
    if m:
        size = (int(m.group(1)), int(m.group(2)))
    else:
        parser.error("invalid size argument, try 640x480")

    canvas = Canvas(size=size, scale=options.zoom)
    task = spawn(canvas.serve, options.hostname, options.portnum)

    brainfile = args[0]
    mtime = 0

    with open(brainfile, 'r') as f:
        code = compile(f.read(), "somefile.py", 'exec')

    while True:
        gsleep(1)
        if mtime < os.stat(brainfile).st_mtime:
            canvas.fire('UNLOAD')
            canvas.events.clear()
            try:
                exec(code, {'on': canvas.on, '__file__': brainfile}, {})
            except:
                log.exception('Brain failed')
                continue
            canvas.fire('LOAD')
            mtime = os.stat(brainfile).st_mtime

    task.join()
    def run(self):
        # Array since we need to pass by ref
        latest_task_complete = [time()]
        latest_maintenance_complete = time()
        active_upload_counter = ThreadsafeCounter()

        task_list = TaskList()

        logging.info("Starting server on %d..." % self._port)

        logging.debug("task completed callback %s", latest_task_complete)

        def completed_callback(latest_task_complete):
            logging.debug("task completed callback %s", latest_task_complete)
            latest_task_complete[:] = [time()]

        workers = Workers(self._repo,
                          partial(completed_callback, latest_task_complete))
        workers.start(task_list, self._workers)

        def webapp_callback(task_name, filepath):
            task_list.add_task(Task(task_name, filepath))

        webapp = UploadWebApp(__name__, active_upload_counter, webapp_callback)

        http_server = WSGIServer(('', self._port), webapp)
        http_server.start()

        logging.info("Server started on %s" % self._port)

        # loop until interrupted
        while True:
            try:
                gsleep(5)
                task_list.join()
                logging.debug("task queue empty, " +
                              str(active_upload_counter.get_count()) +
                              " uploads ongoing")
                time_since_maintenance = time() - latest_maintenance_complete
                time_since_task = time() - latest_task_complete[0]
                logging.debug("{:.1f} complete".format(time_since_task))
                logging.debug(
                    "{:.1f} since last task, {:.1f} since last maintenance".
                    format(time_since_task, time_since_maintenance))
                if time_since_maintenance > time_since_task:
                    # uploads have been processed since last maintenance
                    logging.debug("maintenance needed")
                    if time_since_task >= MAINTENANCE_WAIT:
                        logging.debug("idle, do maintenance")
                        workers.stop()

                        try:
                            output = check_output([
                                "flatpak", "build-update-repo",
                                "--generate-static-deltas", "--prune",
                                self._repo
                            ],
                                                  stderr=STDOUT)
                            logging.info("completed maintenance: " + output)
                        except CalledProcessError as e:
                            logging.error("failed maintenance: " + e.output)

                        latest_maintenance_complete = time()
                        workers.start(task_list, self._workers)

            except (KeyboardInterrupt, SystemExit):
                break

        logging.info("Cleaning up resources...")

        http_server.stop()

        workers.stop()
示例#26
0
    def mimic_tlm(self, ait_tlm_pkt_name, ait_tlm_pkt_fill=None):
        """Used for debugging, creates an instance of a packet based on
        packet name, and fills it with zero data.
        Special case for '1553_HS_Packet' which will get random number
        data fills.
        If HTTP Request query includes a value for 'repeat', then this
        will continue emitting telemetry.
        """

        # Http query option, if it is set to anything, consider it true
        self._debugMimicRepeat = len(str(bottle.request.query.repeat)) > 0

        # This will be helpful in testing by simulating TLM
        # but by a rest call instead of actual telem
        ait_pkt_defn = None
        if ait_tlm_pkt_name:
            ait_pkt_defn = tlm.getDefaultDict()[ait_tlm_pkt_name]
        else:
            ait_pkt_defn = tlm.getDefaultDict().values()[0]

        # Create the expected message format
        pkt_size_bytes = ait_pkt_defn.nbytes

        # if self._debugMimicRepeat:
        repeat_str = " REPEATED " if self._debugMimicRepeat else " a single "
        info_msg = ("Received request to mimic" + repeat_str +
                    "telemetry packet for " + ait_pkt_defn.name)
        self.dbg_message(info_msg)

        # Create a binary array of size filled with 0
        dummy_data = bytearray(pkt_size_bytes)

        info_msg = ""

        while True:

            # Special handling for simply integer based packet, others will
            # have all 0 zero
            if ait_pkt_defn.name == "1553_HS_Packet":
                hs_packet = struct.Struct(">hhhhh")
                random_num = random.randint(1, 100)
                dummy_data = hs_packet.pack(random_num, random_num, random_num,
                                            random_num, random_num)

            tlm_pkt = tlm.Packet(ait_pkt_defn, data=bytearray(dummy_data))
            self._process_telem_msg(tlm_pkt)

            info_msg = (
                "AIT OpenMct Plugin submitted mimicked telemetry for " +
                ait_pkt_defn.name + " (" + str(datetime.datetime.now()) +
                ") to telem queue")
            self.dbg_message(info_msg)

            # sleep if mimic on
            if self._debugMimicRepeat:
                gsleep(5)

            # either it was immediate or we woke up, check break condition
            if not self._debugMimicRepeat:
                break

        # Return last status message as result to client
        return info_msg
示例#27
0
            return self.favicon(environ, start_response)

        if environ['PATH_INFO'] == '/ws':
            environ['ws4py.app'] = self
            return self.ws(environ, start_response)

        return None

    def favicon(self, environ, start_response):
        status = '200 OK'
        headers = [('Content-type', 'text/plain')]
        start_response(status, headers)
        return ""

if __name__ == "__main__":
    kill_event = Event()
    gui = Gui(kill_event)
    gui.start()
    gui.set_status_server(True)

    http_server = WSGIServer((HOST, PORT), app)
    http_server.start()
    
    websocket_application = EchoWebSocketApplication(HOST, PORT_WS)
    websocket_server = WSGIServer((HOST, PORT_WS), websocket_application)
    websocket_server.start()

    while not kill_event.is_set():
        gsleep(0.1)

    http_server.stop()
示例#28
0
 def poll_telemetry_periodically(self):
     while True:
         real_tlm_emitted = self.poll_telemetry()
         if not real_tlm_emitted:
             gsleep(AITOpenMctPlugin.DEFAULT_TELEM_CHECK_SLEEP_SECS)
示例#29
0
def sleep(seconds):
    '''
    Facade for the sleep function. Do not use time.sleep if you are
    running green threads.
    '''
    gsleep(seconds)
示例#30
0
 def text(self, x, y, text, delay=0):
     for i, line in enumerate(text.splitlines()):
         for j, c in enumerate(line):
             self.putc(x+j*self.font_res, y+i*self.font_res, c)
             if delay: gsleep(delay)
示例#31
0
        if environ['PATH_INFO'] == '/ws':
            environ['ws4py.app'] = self
            return self.ws(environ, start_response)

        return None

    def favicon(self, environ, start_response):
        status = '200 OK'
        headers = [('Content-type', 'text/plain')]
        start_response(status, headers)
        return ""


if __name__ == "__main__":
    kill_event = Event()
    gui = Gui(kill_event)
    gui.start()
    gui.set_status_server(True)

    http_server = WSGIServer((HOST, PORT), app)
    http_server.start()

    websocket_application = EchoWebSocketApplication(HOST, PORT_WS)
    websocket_server = WSGIServer((HOST, PORT_WS), websocket_application)
    websocket_server.start()

    while not kill_event.is_set():
        gsleep(0.1)

    http_server.stop()