def __init__(self, RequestHandlerClass):
        if not hasattr(self, 'ServerName'):
            self.ServerName = 'Eloipool'

        self.RequestHandlerClass = RequestHandlerClass

        self.running = False
        self.keepgoing = True
        self.rejecting = False
        self.lastidle = 0

        self._epoll = select.epoll()
        self._fd = {}
        self.connections = {}

        self._sch = ScheduleDict()
        self._schEH = {}
        if self.schMT:
            self._schLock = threading.Lock()
        else:
            self._schLock = WithNoop

        self.TrustedForwarders = ()

        if self.waker:
            (r, w) = os.pipe()
            o = _Waker(self, r)
            self.register_socket(r, o)
            self.waker = w
Ejemplo n.º 2
0
    def __init__(self, RequestHandlerClass):
        if not hasattr(self, "ServerName"):
            self.ServerName = "Eloipool"

        self.RequestHandlerClass = RequestHandlerClass

        self.running = False
        self.keepgoing = True
        self.rejecting = False

        self._epoll = select.epoll()
        self._fd = {}
        self.connections = {}

        self._sch = ScheduleDict()
        self._schEH = {}
        if self.schMT:
            self._schLock = threading.Lock()
        else:
            self._schLock = WithNoop

        self.TrustedForwarders = ()

        if self.waker:
            (r, w) = os.pipe()
            o = _Waker(self, r)
            self.register_socket(r, o)
            self.waker = w
Ejemplo n.º 3
0
    def __init__(self, RequestHandlerClass):
        if not hasattr(self, 'ServerName'):
            self.ServerName = 'Eloipool'

        self.RequestHandlerClass = RequestHandlerClass

        self.running = False
        self.keepgoing = True

        self._epoll = select.epoll()
        self._fd = {}

        self._sch = ScheduleDict()
        self._schEH = {}

        self.TrustedForwarders = ()

        if self.waker:
            (r, w) = os.pipe()
            o = _Waker(self, r)
            self.register_socket(r, o)
            self.waker = w
Ejemplo n.º 4
0
	def __init__(self, RequestHandlerClass):
		if not hasattr(self, 'ServerName'):
			self.ServerName = 'Eloipool'
		
		self.RequestHandlerClass = RequestHandlerClass
		
		self.running = False
		self.keepgoing = True
		
		self._epoll = select.epoll()
		self._fd = {}
		
		self._sch = ScheduleDict()
		self._schEH = {}
		
		self.TrustedForwarders = ()
		
		if self.waker:
			(r, w) = os.pipe()
			o = _Waker(self, r)
			self.register_socket(r, o)
			self.waker = w
class AsyncSocketServer:
    logger = logging.getLogger('SocketServer')

    waker = False
    schMT = False

    def __init__(self, RequestHandlerClass):
        if not hasattr(self, 'ServerName'):
            self.ServerName = 'Eloipool'

        self.RequestHandlerClass = RequestHandlerClass

        self.running = False
        self.keepgoing = True
        self.rejecting = False
        self.lastidle = 0

        self._epoll = select.epoll()
        self._fd = {}
        self.connections = {}

        self._sch = ScheduleDict()
        self._schEH = {}
        if self.schMT:
            self._schLock = threading.Lock()
        else:
            self._schLock = WithNoop

        self.TrustedForwarders = ()

        if self.waker:
            (r, w) = os.pipe()
            o = _Waker(self, r)
            self.register_socket(r, o)
            self.waker = w

    def register_socket(self, fd, o, eventmask=EPOLL_READ):
        self._epoll.register(fd, eventmask)
        self._fd[fd] = o

    def register_socket_m(self, fd, eventmask):
        try:
            self._epoll.modify(fd, eventmask)
        except IOError:
            raise socket.error

    def unregister_socket(self, fd):
        del self._fd[fd]
        try:
            self._epoll.unregister(fd)
        except IOError:
            raise socket.error

    def schedule(self, task, startTime, errHandler=None):
        with self._schLock:
            self._sch[task] = startTime
            if errHandler:
                self._schEH[id(task)] = errHandler
        return task

    def rmSchedule(self, task):
        with self._schLock:
            del self._sch[task]
            k = id(task)
            if k in self._schEH:
                del self._schEH[k]

    def pre_schedule(self):
        pass

    def wakeup(self):
        if not self.waker:
            raise NotImplementedError('Class `%s\' did not enable waker' %
                                      (self.__class__.__name__))
        os.write(self.waker, b'\1')  # to break out of the epoll

    def final_init(self):
        pass

    def boot_all(self):
        conns = tuple(self.connections.values())
        for c in conns:
            tryErr(lambda: c.boot())

    def serve_forever(self):
        self.running = True
        self.final_init()
        while self.keepgoing:
            self.doing = 'pre-schedule'
            self.pre_schedule()
            self.doing = 'schedule'
            timeNow = time()
            if len(self._sch):
                while True:
                    with self._schLock:
                        if not len(self._sch):
                            timeout = -1
                            break
                        timeNext = self._sch.nextTime()
                        if timeNow < timeNext:
                            timeout = timeNext - timeNow
                            break
                        f = self._sch.shift()
                    k = id(f)
                    EH = None
                    if k in self._schEH:
                        EH = self._schEH[k]
                        del self._schEH[k]
                    try:
                        f()
                    except socket.error:
                        if EH: tryErr(EH.handle_error)
                    except:
                        self.logger.error(traceback.format_exc())
                        if EH: tryErr(EH.handle_close)
            else:
                timeout = -1
            if self.lastidle < timeNow - 1:
                timeout = 0
            elif timeout < 0 or timeout > 1:
                timeout = 1

            self.doing = 'poll'
            try:
                events = self._epoll.poll(timeout=timeout)
            except (IOError, select.error):
                continue
            except:
                self.logger.error(traceback.format_exc())
                continue
            self.doing = 'events'
            if not events:
                self.lastidle = time()
            for (fd, e) in events:
                o = self._fd.get(fd)
                if o is None: continue
                self.lastHandler = o
                try:
                    if e & EPOLL_READ:
                        o.handle_read()
                    if e & EPOLL_WRITE:
                        o.handle_write()
                except socket.error:
                    tryErr(o.handle_error)
                except:
                    self.logger.error(traceback.format_exc())
                    tryErr(o.handle_error)
        self.doing = None
        self.running = False
Ejemplo n.º 6
0
class AsyncSocketServer:
    logger = logging.getLogger("SocketServer")

    waker = False
    schMT = False

    def __init__(self, RequestHandlerClass):
        if not hasattr(self, "ServerName"):
            self.ServerName = "Eloipool"

        self.RequestHandlerClass = RequestHandlerClass

        self.running = False
        self.keepgoing = True
        self.rejecting = False

        self._epoll = select.epoll()
        self._fd = {}
        self.connections = {}

        self._sch = ScheduleDict()
        self._schEH = {}
        if self.schMT:
            self._schLock = threading.Lock()
        else:
            self._schLock = WithNoop

        self.TrustedForwarders = ()

        if self.waker:
            (r, w) = os.pipe()
            o = _Waker(self, r)
            self.register_socket(r, o)
            self.waker = w

    def register_socket(self, fd, o, eventmask=EPOLL_READ):
        self._epoll.register(fd, eventmask)
        self._fd[fd] = o

    def register_socket_m(self, fd, eventmask):
        try:
            self._epoll.modify(fd, eventmask)
        except IOError:
            raise socket.error

    def unregister_socket(self, fd):
        del self._fd[fd]
        try:
            self._epoll.unregister(fd)
        except IOError:
            raise socket.error

    def schedule(self, task, startTime, errHandler=None):
        with self._schLock:
            self._sch[task] = startTime
            if errHandler:
                self._schEH[id(task)] = errHandler
        return task

    def rmSchedule(self, task):
        with self._schLock:
            del self._sch[task]
            k = id(task)
            if k in self._schEH:
                del self._schEH[k]

    def pre_schedule(self):
        pass

    def wakeup(self):
        if not self.waker:
            raise NotImplementedError("Class `%s' did not enable waker" % (self.__class__.__name__))
        os.write(self.waker, b"\1")  # to break out of the epoll

    def final_init(self):
        pass

    def boot_all(self):
        conns = tuple(self.connections.values())
        for c in conns:
            tryErr(lambda: c.boot())

    def serve_forever(self):
        self.running = True
        self.final_init()
        while self.keepgoing:
            self.doing = "pre-schedule"
            self.pre_schedule()
            self.doing = "schedule"
            if len(self._sch):
                timeNow = time()
                while True:
                    with self._schLock:
                        if not len(self._sch):
                            timeout = -1
                            break
                        timeNext = self._sch.nextTime()
                        if timeNow < timeNext:
                            timeout = timeNext - timeNow
                            break
                        f = self._sch.shift()
                    k = id(f)
                    EH = None
                    if k in self._schEH:
                        EH = self._schEH[k]
                        del self._schEH[k]
                    try:
                        f()
                    except socket.error:
                        if EH:
                            tryErr(EH.handle_error)
                    except:
                        self.logger.error(traceback.format_exc())
                        if EH:
                            tryErr(EH.handle_close)
            else:
                timeout = -1

            self.doing = "poll"
            try:
                events = self._epoll.poll(timeout=timeout)
            except (IOError, select.error):
                continue
            except:
                self.logger.error(traceback.format_exc())
                continue
            self.doing = "events"
            for (fd, e) in events:
                o = self._fd[fd]
                self.lastHandler = o
                try:
                    if e & EPOLL_READ:
                        o.handle_read()
                    if e & EPOLL_WRITE:
                        o.handle_write()
                except socket.error:
                    tryErr(o.handle_error)
                except:
                    self.logger.error(traceback.format_exc())
                    tryErr(o.handle_error)
        self.doing = None
        self.running = False
Ejemplo n.º 7
0
class AsyncSocketServer:
	logger = logging.getLogger('SocketServer')
	
	waker = False
	
	def __init__(self, RequestHandlerClass):
		if not hasattr(self, 'ServerName'):
			self.ServerName = 'Eloipool'
		
		self.RequestHandlerClass = RequestHandlerClass
		
		self.running = False
		self.keepgoing = True
		
		self._epoll = select.epoll()
		self._fd = {}
		
		self._sch = ScheduleDict()
		self._schEH = {}
		
		self.TrustedForwarders = ()
		
		if self.waker:
			(r, w) = os.pipe()
			o = _Waker(self, r)
			self.register_socket(r, o)
			self.waker = w
	
	def register_socket(self, fd, o, eventmask = EPOLL_READ):
		self._epoll.register(fd, eventmask)
		self._fd[fd] = o
	
	def register_socket_m(self, fd, eventmask):
		try:
			self._epoll.modify(fd, eventmask)
		except IOError:
			raise socket.error
	
	def unregister_socket(self, fd):
		del self._fd[fd]
		try:
			self._epoll.unregister(fd)
		except IOError:
			raise socket.error
	
	def schedule(self, task, startTime, errHandler=None):
		self._sch[task] = startTime
		if errHandler:
			self._schEH[id(task)] = errHandler
		return task
	
	def rmSchedule(self, task):
		del self._sch[task]
		k = id(task)
		if k in self._schEH:
			del self._schEH[k]
	
	def pre_schedule(self):
		pass
	
	def wakeup(self):
		if not self.waker:
			raise NotImplementedError('Class `%s\' did not enable waker' % (self.__class__.__name__))
		os.write(self.waker, b'\1')  # to break out of the epoll
	
	def final_init(self):
		pass
	
	def serve_forever(self):
		self.running = True
		self.final_init()
		while self.keepgoing:
			self.doing = 'pre-schedule'
			self.pre_schedule()
			self.doing = 'schedule'
			if len(self._sch):
				timeNow = time()
				while True:
					timeNext = self._sch.nextTime()
					if timeNow < timeNext:
						timeout = timeNext - timeNow
						break
					f = self._sch.shift()
					k = id(f)
					EH = None
					if k in self._schEH:
						EH = self._schEH[k]
						del self._schEH[k]
					try:
						f()
					except socket.error:
						if EH: tryErr(EH.handle_error)
					except:
						self.logger.error(traceback.format_exc())
						if EH: tryErr(EH.handle_close)
					if not len(self._sch):
						timeout = -1
						break
			else:
				timeout = -1
			
			self.doing = 'poll'
			try:
				events = self._epoll.poll(timeout=timeout)
			except (IOError, select.error):
				continue
			except:
				self.logger.error(traceback.format_exc())
			self.doing = 'events'
			for (fd, e) in events:
				o = self._fd[fd]
				self.lastHandler = o
				try:
					if e & EPOLL_READ:
						o.handle_read()
					if e & EPOLL_WRITE:
						o.handle_write()
				except socket.error:
					tryErr(o.handle_error)
				except:
					self.logger.error(traceback.format_exc())
					tryErr(o.handle_error)
		self.doing = None
		self.running = False