Exemple #1
0
 def close(self):
     """Generic close function that register the server un _{r,w,x}list in
     case of successful _close"""
     self.logger.info("Closing connection to %s", self.id)
     with _lock:
         if not hasattr(self, "_close") or self._close():
             if self in _rlist:
                 _rlist.remove(self)
             if self in _wlist:
                 _wlist.remove(self)
             if self in _xlist:
                 _xlist.remove(self)
             return True
     return False
Exemple #2
0
    def run(self):
        from select import select
        from nemubot.server import _lock, _rlist, _wlist, _xlist

        self.stop = False
        while not self.stop:
            with _lock:
                try:
                    rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1)
                except:
                    logger.error("Something went wrong in select")
                    fnd_smth = False
                    # Looking for invalid server
                    for r in _rlist:
                        if not hasattr(r, "fileno") or not isinstance(r.fileno(), int) or r.fileno() < 0:
                            _rlist.remove(r)
                            logger.error("Found invalid object in _rlist: " + str(r))
                            fnd_smth = True
                    for w in _wlist:
                        if not hasattr(w, "fileno") or not isinstance(w.fileno(), int) or w.fileno() < 0:
                            _wlist.remove(w)
                            logger.error("Found invalid object in _wlist: " + str(w))
                            fnd_smth = True
                    for x in _xlist:
                        if not hasattr(x, "fileno") or not isinstance(x.fileno(), int) or x.fileno() < 0:
                            _xlist.remove(x)
                            logger.error("Found invalid object in _xlist: " + str(x))
                            fnd_smth = True
                    if not fnd_smth:
                        logger.exception("Can't continue, sorry")
                        self.quit()
                    continue

                for x in xl:
                    try:
                        x.exception()
                    except:
                        logger.exception("Uncatched exception on server exception")
                for w in wl:
                    try:
                        w.write_select()
                    except:
                        logger.exception("Uncatched exception on server write")
                for r in rl:
                    for i in r.read():
                        try:
                            self.receive_message(r, i)
                        except:
                            logger.exception("Uncatched exception on server read")


            # Launch new consumer threads if necessary
            while self.cnsr_queue.qsize() > self.cnsr_thrd_size:
                # Next launch if two more items in queue
                self.cnsr_thrd_size += 2

                c = Consumer(self)
                self.cnsr_thrd.append(c)
                c.start()

            while self.sync_queue.qsize() > 0:
                action = self.sync_queue.get_nowait()
                if action[0] == "exit":
                    self.quit()
                elif action[0] == "loadconf":
                    for path in action[1:]:
                        logger.debug("Load configuration from %s", path)
                        self.load_file(path)
                    logger.info("Configurations successfully loaded")
                self.sync_queue.task_done()