Exemplo n.º 1
0
    def test_add(self):
        import select

        client, server = self.socket_pair()

        ep = select.epoll(2)
        ep.register(server, select.EPOLLIN | select.EPOLLOUT)
        ep.register(client, select.EPOLLIN | select.EPOLLOUT)
        ep.close()

        # adding by object w/ fileno works, too.
        ep = select.epoll(2)
        ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
        ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
        ep.close()

        ep = select.epoll(2)
        # TypeError: argument must be an int, or have a fileno() method.
        raises(TypeError, ep.register, object(), select.EPOLLIN | select.EPOLLOUT)
        raises(TypeError, ep.register, None, select.EPOLLIN | select.EPOLLOUT)
        # ValueError: file descriptor cannot be a negative integer (-1)
        raises(ValueError, ep.register, -1, select.EPOLLIN | select.EPOLLOUT)
        # IOError: [Errno 9] Bad file descriptor
        raises(IOError, ep.register, 10000, select.EPOLLIN | select.EPOLLOUT)
        # registering twice also raises an exception
        ep.register(server, select.EPOLLIN | select.EPOLLOUT)
        raises(IOError, ep.register, server, select.EPOLLIN | select.EPOLLOUT)
        ep.close()
Exemplo n.º 2
0
    def test_add(self):
        server, client = self._connected_pair()

        ep = select.epoll(2)
        try:
            ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
            ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
        finally:
            ep.close()

        # adding by object w/ fileno works, too.
        ep = select.epoll(2)
        try:
            ep.register(server, select.EPOLLIN | select.EPOLLOUT)
            ep.register(client, select.EPOLLIN | select.EPOLLOUT)
        finally:
            ep.close()

        ep = select.epoll(2)
        try:
            # TypeError: argument must be an int, or have a fileno() method.
            self.assertRaises(TypeError, ep.register, object(), select.EPOLLIN | select.EPOLLOUT)
            self.assertRaises(TypeError, ep.register, None, select.EPOLLIN | select.EPOLLOUT)
            # ValueError: file descriptor cannot be a negative integer (-1)
            self.assertRaises(ValueError, ep.register, -1, select.EPOLLIN | select.EPOLLOUT)
            # IOError: [Errno 9] Bad file descriptor
            self.assertRaises(IOError, ep.register, 10000, select.EPOLLIN | select.EPOLLOUT)
            # registering twice also raises an exception
            ep.register(server, select.EPOLLIN | select.EPOLLOUT)
            self.assertRaises(IOError, ep.register, server, select.EPOLLIN | select.EPOLLOUT)
        finally:
            ep.close()
def main():
    args = aux.parse_args('Echo server with epoll-processing model.')
    server_socket = aux.listening_socket(args.port)
    server_fileno = server_socket.fileno()

    fileno_to_data = {}
    fileno_to_socket = {}

    read_epoll = select.epoll()
    read_epoll.register(server_fileno, select.EPOLLIN)

    while True:
        for fileno, eventmask in read_epoll.poll(0.05):
            if fileno == server_fileno:
                client_socket, _ = server_socket.accept()
                client_fileno = client_socket.fileno()

                fileno_to_data[client_fileno] = ''
                fileno_to_socket[client_fileno] = client_socket

                read_epoll.register(client_fileno, select.EPOLLIN)
            else:
                client_socket = fileno_to_socket[fileno]

                data = client_socket.recv(1024)
                if not data:
                    read_epoll.unregister(fileno)
                    del fileno_to_data[fileno]
                    del fileno_to_socket[fileno]
                    client_socket.close()
                else:
                    fileno_to_data[fileno] += data

        check_writability = [f for f, d in fileno_to_data.iteritems() if d]
        if not check_writability:
            continue

        write_epoll = select.epoll()
        for fileno in check_writability:
            write_epoll.register(fileno, select.EPOLLOUT)

        for fileno, eventmask in write_epoll.poll(0.05):
            if eventmask & (select.EPOLLERR | select.EPOLLHUP):
                read_epoll.unregister(fileno)
                fileno_to_socket[fileno].close()
                del fileno_to_data[fileno]
                del fileno_to_socket[fileno]
                continue

            client_socket = fileno_to_socket[fileno]
            data = fileno_to_data[fileno]

            n = client_socket.send(data)
            if n > 0:
                fileno_to_data[fileno] = data[n:]

        write_epoll.close()
Exemplo n.º 4
0
	def __init__(self, pipe):
		if isinstance(pipe, int):
			fd = self._fd = pipe
			pipe = os.fromfd(pipe)
		else: fd = self._fd = pipe.fileno()
		self._poll_in, self._poll_out = epoll(), epoll()
		self._poll_in.register(fd, EPOLLIN | EPOLLERR | EPOLLHUP)
		self._poll_out.register(fd, EPOLLOUT | EPOLLERR | EPOLLHUP)
		self.close = pipe.close
		self.reads = pipe.read
		self.writes = pipe.write
Exemplo n.º 5
0
    def setup_class(cls):
        cls.space = gettestobjspace(usemodules=["select", "_socket", "posix"])

        import errno
        import select

        if not hasattr(select, "epoll"):
            py.test.skip("test requires linux 2.6")
        try:
            select.epoll()
        except IOError, e:
            if e.errno == errno.ENOSYS:
                py.test.skip("kernel doesn't support epoll()")
Exemplo n.º 6
0
 def test_create(self):
     try:
         ep = select.epoll(16)
     except OSError as e:
         raise AssertionError(str(e))
     self.assertTrue(ep.fileno() > 0, ep.fileno())
     self.assertTrue(not ep.closed)
     ep.close()
     self.assertTrue(ep.closed)
     self.assertRaises(ValueError, ep.fileno)
     if hasattr(select, "EPOLL_CLOEXEC"):
         select.epoll(select.EPOLL_CLOEXEC).close()
         self.assertRaises(OSError, select.epoll, flags=12356)
Exemplo n.º 7
0
Arquivo: aio.py Projeto: mk-fg/fgc
 def __init__(self, pipe, leash=None):
     fd = self._fd = pipe.fileno()
     if leash:
         self.__leash = leash  # fd source object, leashed here to stop gc
     self._poll_in, self._poll_out = epoll(), epoll()
     self._poll_in.register(fd, EPOLLIN)
     self._poll_out.register(fd, EPOLLOUT)
     self.close = pipe.close
     try:  # file
         self.reads = pipe.read
         self.writes = pipe.write
     except AttributeError:  # socket
         self.reads = pipe.recv
         self.writes = pipe.send
Exemplo n.º 8
0
    def __iter__(self):
        """
        Wait for incoming events and receive them upon arrival.

        This methods implicitly calls :meth:`start()`, and starts polling the
        :meth:`fileno` of this monitor.  If a event comes in, it receives the
        corresponding device and yields it to the caller.

        The returned iterator is endless, and continues receiving devices
        without ever stopping.

        Yields ``(action, device)`` (see :meth:`receive_device` for a
        description).

        .. deprecated:: 0.16
           Will be removed in 1.0. Use an explicit loop over :meth:`poll()`
           instead, or monitor asynchronously with :class:`MonitorObserver`.
        """
        import warnings
        warnings.warn('Will be removed in 1.0. Use an explicit loop over '
                      '"poll()" instead, or monitor asynchronously with '
                      '"MonitorObserver".', DeprecationWarning)
        self.start()
        with closing(select.epoll()) as notifier:
            notifier.register(self, select.EPOLLIN)
            while True:
                events = notifier.poll()
                for event in events:
                    device = self._receive_device()
                    yield device.action, device
Exemplo n.º 9
0
def epoll_response():
	server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server.setblocking(False)
	server.bind((HOST,PORT))
	server.listen(1024)

	READ_ONLY = select.EPOLLIN | select.EPOLLPRI
	epoll = select.epoll()
	epoll.register(server, READ_ONLY)

	timeout = 60

	fd_to_socket = {server.fileno():server}

	while True:
		events = epoll.poll(timeout)
		for fd,flag in events:
			sock = fd_to_socket[fd]
			if flag & READ_ONLY:
				if sock is server:
					conn, client_address = sock.accept()
					conn.setblocking(False)
					fd_to_socket[conn.fileno()] = conn
					epoll.register(conn, READ_ONLY)
				else:
					request = sock.recv(BUFFER_SIZE)
					sock.send(response)
					sock.close()
					del fd_to_socket[fd]
Exemplo n.º 10
0
	def __collect_piece(self, ips, headers, origin_urls):
		
		# create sockets
		fd_2_sock	= {}
		fd_2_req	= {}
		fd_2_data	= {}
	
		for i in xrange(len(ips)):
			sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			sock.setblocking(0)
			fd_2_sock[sock.fileno()] = sock
			fd_2_req[sock.fileno()]  = Request(ips[i], headers[i], origin_urls[i])
		print "Create %d Sockets Finished!" % len(fd_2_sock.items())
	
		# create listener
		epoll = select.epoll()
	
		# non-blocking connect
		for (fd, sock) in fd_2_sock.items():
			try:
				sock.connect( (fd_2_req[fd].ip, 80) )
			except Exception, e:
				if e.args[0] == 115:
					epoll.register(fd, select.EPOLLOUT)
				else:
					print "[Connection Error] Url %s : %s" % (fd_2_req[fd].origin_url, str(e))
Exemplo n.º 11
0
 def __init__(self):
     self.epoll = select.epoll()
     # TODO should we set any other masks?
     # http://docs.python.org/library/select.html#epoll-objects
     self.READ_MASK = select.EPOLLIN | select.EPOLLPRI
     self.WRITE_MASK = select.EPOLLOUT
     self.ERR_MASK = select.EPOLLERR | select.EPOLLHUP
Exemplo n.º 12
0
    def __init__(self, *cmds):
        self._lock = Lock()
        self._members = {}
        self._poller = epoll()

        for cmd in cmds:
            self.create_process(cmd)
Exemplo n.º 13
0
    def __init__(self, popenToWrap):
        self._streamLock = threading.Lock()
        self._proc = popenToWrap

        self._stdout = StringIO()
        self._stderr = StringIO()
        self._stdin = StringIO()

        fdout = self._proc.stdout.fileno()
        fderr = self._proc.stderr.fileno()
        self._fdin = self._proc.stdin.fileno()

        self._closedfds = []

        self._poller = select.epoll()
        self._poller.register(fdout, select.EPOLLIN | select.EPOLLPRI)
        self._poller.register(fderr, select.EPOLLIN | select.EPOLLPRI)
        self._poller.register(self._fdin, 0)
        self._fdMap = {fdout: self._stdout,
                       fderr: self._stderr,
                       self._fdin: self._stdin}

        self.stdout = io.BufferedReader(self._streamWrapper(self,
                                        self._stdout, fdout), BUFFSIZE)

        self.stderr = io.BufferedReader(self._streamWrapper(self,
                                        self._stderr, fderr), BUFFSIZE)

        self.stdin = io.BufferedWriter(self._streamWrapper(self,
                                       self._stdin, self._fdin), BUFFSIZE)

        self._returncode = None

        self.blocking = False
Exemplo n.º 14
0
    def __init__( self, sett ):
        self._evpoll = select.epoll()
        h.set_close_exec( self._evpoll.fileno() )
        self.poll_threshold = sett['poll_threshold']
        self.poll_timeout = sett['poll_timeout']

        # Book keeping
        self._handlers = {}
        """A map of polled descriptor and callback handlers."""
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        """Lock object to handle ioloop callbacks in a multi-threaded
        environment."""
        self._timeouts = []
        """A heap queue list to manage timeout events and its callbacks."""
        self._running = False
        """Initialized to True when start() is called and set to False to
        indicate that stop() is called."""
        self._stopped = False
        """Set to True when stop() is called and reset to False when start()
        exits."""
        self._thread_ident = None
        self._blocking_signal_threshold = None

        self._waker = Waker()
        """Create a pipe that we send bogus data to when we want to wake
        the I/O loop when it is idle."""

        # log.debug( "Adding poll-loop waker ..." )
        self.add_handler(
            self._waker.fileno(), lambda fd, events: self._waker.consume(),
            self.READ )
Exemplo n.º 15
0
def epoll_poller(timeout=0.0, map=None):
    """A poller which uses epoll(), supported on Linux 2.5.44 and newer."""
    if map is None:
        map = socket_map
    pollster = select.epoll()
    if map:
        for fd, obj in map.items():
            flags = 0
            if obj.readable():
                flags |= select.POLLIN | select.POLLPRI
            if obj.writable():
                flags |= select.POLLOUT
            if flags:
                # Only check for exceptions if object was either readable
                # or writable.
                flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
                pollster.register(fd, flags)
        try:
            r = pollster.poll(timeout)
        except select.error, err:
            if err.args[0] != EINTR:
                raise
            r = []
        for fd, flags in r:
            obj = map.get(fd)
            if obj is None:
                continue
            readwrite(obj, flags) 
Exemplo n.º 16
0
 def __init__(self, addr, port, logic):
     dbgPrint("\n__init__: start!")
     #初始化状态字典
     self.conn_state = {}
     #初始化socket对象,tcp/ipv4 类型
     self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     self.listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.listen_sock.bind((addr, port))
     self.listen_sock.listen(10)
     #使用setFd设置socket状态,4个头,读写缓冲,将当前scoket状态类放入字典中
     #并打印当前scoket状态
     self.setFd(self.listen_sock)
     self.epoll_sock = select.epoll()
     #注册 listen_sock 至epollin interface
     self.epoll_sock.register(self.listen_sock.fileno(), select.EPOLLIN)
     self.logic = logic
     #初始化状态机字典
     self.sm = {
         "accept" : self.accept2read,
         "read"   : self.read2process,
         "write"  : self.write2read,
         "process": self.process,
         "closing": self.close,
     }
     dbgPrint("\n__init__: end, register no: %s" %
             self.listen_sock.fileno())
Exemplo n.º 17
0
Arquivo: evdev.py Projeto: mk-fg/fgc
	def __init__(self, paths):
		self.reactor = epoll()
		self.devices = map(Device, paths)
		self.fds = map(op.attrgetter('fd'), self.devices)
		for fd in self.fds: self.reactor.register(fd, EPOLLIN)
		self.packet_size = Event.get_format_size()
		log.debug('DevGroup init: %r, %s'%(self, self.devices))
def wait_for_pin_state_changes(readers_by_name, devices_by_name):
    fds_to_pins = {}

    # Fetch a list of pins to watch, each of which maps to
    # a card reader
    pin_objects_to_watch = build_hardware_pin_map(readers_by_name)


    # Configure the select handler, setting it to watch the
    # filedescriptors that underly the hardware pins.
    # To do this, we grab the filedescriptors that underly the
    # pins, and build a map between filedescriptors and objects
    epoll_handler = select.epoll()
    for pin_num in pin_objects_to_watch:
        handler_object = pin_objects_to_watch[pin_num]['handler_object']
        gpio_pin = pin_objects_to_watch[pin_num]['gpio_pin']

        epoll_handler.register(gpio_pin, select.EPOLLET)
        fds_to_pins[gpio_pin.fileno()] = pin_num

    # Loop forever, waiting for pin state changes, and triggering the
    # pins based on their values
    while True:
        logging.debug("Waiting for event on reader pins")
        events = epoll_handler.poll()
        for filedescriptor, event in events:
            pin_no = fds_to_pins[filedescriptor]
            logging.debug("Got event on pin number %s" % pin_no)
            pin_value = pin_objects_to_watch[pin_no]['gpio_pin'].value
            pin_objects_to_watch[pin_no]['handler_object'].trigger_pin_state_change(
                        pin_value, devices_by_name)
Exemplo n.º 19
0
def main():
    ip_list = ("127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4")
    client_infos = {}
    e = select.epoll()
    # Fork all ssh for clients and register all sockets
    for ip in ip_list:
        p = subprocess.Popen(['./yes.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        (fh_in, fh_out) = (p.stdout, p.stdin)
        client_infos[fh_in.fileno()] = (fh_in, fh_out, ip, 0)
        e.register(fh_in)
    # Main read loop
    try:
        while True:
            events = e.poll(1)
            logging.debug("events : %s"%events)
            for fileno, event_type in events:
                (fh_in, fh_out, ip, current_block_number) = client_infos[fileno]

                if event_type & select.EPOLLIN:
                    msg = fh_in.read(1)
                    process_msg(msg, fh_out, ip, current_block_number)
                    current_block_number += 1
                    client_infos[fileno] = (fh_in, fh_out, ip, current_block_number)
                else:
                    logging.debug("Evt d'un type non gere : %i"%event_type)

    finally:
        logging.debug("Fermeture de tous les sockets")
        for fileno in client_infos:
            fh_in = client_infos[fileno][0]
            e.unregister(fh_in)
            fh_in.close()
        e.close()
Exemplo n.º 20
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
Exemplo n.º 21
0
def run_main(listen_fd):
    try:
        epoll_fd = select.epoll()
        epoll_fd.register(listen_fd.fileno(), select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
        print "listen_fd:%s" % listen_fd.fileno()
    except select.error, msg:
        logger.error(msg)
Exemplo n.º 22
0
    def mainloop(self):
        self.socket.setblocking(0) # Non blocking socket server
        self.clients = {}

        self.epoll = select.epoll()
        self.epoll.register(self.socket.fileno(), select.EPOLLIN) # Level triggerred

        while True:
            if self.shutdown: # Gracefully shutdown
                break
            events = self.epoll.poll(1) # Timeout 1 second
            for fileno, event in events:
                try:
                    if fileno == self.socket.fileno(): # New connection on server socket
                        self.accept_clients()
                    elif event & select.EPOLLIN: # Although it's level triggerred, read or write as more as possible
                        self.epoll_in(fileno)
                    elif event & select.EPOLLOUT:
                        self.epoll_out(fileno)
                    elif event & select.EPOLLHUP:
                        self.epoll_hup(fileno)
                except socket.error, err:
                    debug('epoll event exception: %s', err)
                    if err.errno == 11: # Catch the Errno
                        pass
                    else:
                        raise
Exemplo n.º 23
0
 def setdirection(self,val):
     if val in ['in','out']:
         if os.access(gpio_path+self.name+'/direction', os.W_OK):
             fdir=open(gpio_path+self.name+'/direction','w+',0)
             fdir.write(val)
             fdir.close()
             try:
                 self.epoll_obj.unregister(self.fvalue)
                 self.fvalue.close()
             except:
                 pass
             if val=='in' and os.access(gpio_path+self.name+'/value', os.W_OK):
                 self.fvalue=open(gpio_path+self.name+'/value')
             if val=='out' and os.access(gpio_path+self.name+'/value', os.W_OK):
                 self.fvalue=open(gpio_path+self.name+'/value','w+',0)
             if os.access(gpio_path+self.name+'/edge', os.W_OK) and val=='in':
                 self.epoll_obj=select.epoll()
                 self.epoll_obj.register(self.fvalue,select.EPOLLET)
             return True
         else:
             print('Access denied')
             return False
     else:
         print('Error value must be "in" or "out"')
         return False
Exemplo n.º 24
0
    def low_memory_watchdog(self):
        self.counts["low_mem_events"] = 0
        self.__watchdog__run = True
        efd = eventfd(0, 0)
        mp = open(self.MEM_PRES_PATH)
        with open(self.EVENT_CONTR_PATH, "w") as cgc:
            cgc.write("%d %d %s" % (efd, mp.fileno(), self.PRESSURE_LEVEL))

        p = epoll()
        p.register(efd, EPOLLIN)

        self.host.log_info('"Low memory" watchdog started(pressure level=%r).' % self.PRESSURE_LEVEL)
        err = "shutdown event"
        while self.__watchdog__run:
            try:
                events = p.poll(2)
                for fd, event in events:
                    if event & EPOLLIN:
                        # In an eventfd, there are always 8 bytes
                        ret = os.read(efd, 64 / 8)
                        num = struct.unpack("Q", ret)
                        break
                else:
                    continue
            except (ValueError, OSError, IOError) as err:
                break
            self.host.log_debug('"Low memory" notification received: %d' % num)
            for callback in self.low_memory_callbacks:
                callback()
            self.counts["low_mem_events"] += 1

        p.close()
        os.close(efd)
        self.host.log_info('"Low memory" watchdog stopped(msg="%s").' % err)
Exemplo n.º 25
0
def main():
    pipe_r, pipe_w = os.pipe()
    flags = fcntl.fcntl(pipe_w, fcntl.F_GETFL, 0)
    flags = flags | os.O_NONBLOCK
    fcntl.fcntl(pipe_w, fcntl.F_SETFL, flags)
    
    signal.signal(signal.SIGCHLD, lambda x,y: None)
    signal.signal(signal.SIGALRM, lambda x,y: None)
    signal.siginterrupt(signal.SIGCHLD,False) #makes no difference
    signal.siginterrupt(signal.SIGALRM,False) #makes no difference
    signal.set_wakeup_fd(pipe_w)
    signal.setitimer(signal.ITIMER_REAL, 2, 2)
    
    poller = select.epoll()
    poller.register(pipe_r, select.EPOLLIN)
    poller.register(sys.stdin, select.EPOLLIN)
    
    print "Main screen turn on"
    while True:
        events=[]
        try:
            events = poller.poll()
            try:
                for fd, flags in events:
                    ch=os.read(fd, 1)
                    if fd==pipe_r:
                        sys.stdout.write( "We get Signal" )
                    if fd==sys.stdin.fileno():
                        sys.stdout.write( ch )
                    sys.stdout.flush()
            except IOError as e:
                print "exception loop" + str(e)
        except IOError as e:
            print "exception poll" + str(e)
Exemplo n.º 26
0
    def run(self):
        """
        Main event loop
        """
        # Create dict of Event File Descriptor: Event File Object
        event_file_map = {event_file.fileno(): event_file for event_file in self.open_event_files}

        # Create epoll object
        poll_object = select.epoll()

        # Register files with select
        for event_fd in event_file_map.keys():
            poll_object.register(event_fd, select.EPOLLIN | select.EPOLLPRI)

        # Loop
        while not self._shutdown:
            # epoll is nice but it wasn't getting events properly :(

            try:  # Cheap hack until i merged new code
                if self._use_epoll:
                    self._poll_epoll(poll_object, event_file_map)
                else:
                    self._poll_read()
            except (IOError, OSError):  # Basically if there's an error, most likely device has been removed then it'll get deleted properly
                pass

            time.sleep(SPIN_SLEEP)

        # Unbind files and close them
        for event_fd, event_file in event_file_map.items():
            poll_object.unregister(event_fd)
            event_file.close()

        poll_object.close()
Exemplo n.º 27
0
    def run(self):
        self.poller = select.epoll()
        self.pollmask = select.EPOLLIN | select.EPOLLHUP | select.EPOLLERR
        self.poller.register(self.server, self.pollmask)
        while True:
            try:
                fds = self.poller.poll(timeout=self.timeout)
            except:
                return

            for (fd, event) in fds:
                if event & (select.POLLHUP | select.POLLERR):
                    self.handle_error(fd)
                    continue
                if fd == self.server.fileno():
                    self.handle_server()
                else:
                    self.handle_client(fd)

            to_delete = []
            for fileNo, client in self.clients.iteritems():
                inactive_time = seconds() - client[1]
                if inactive_time > self.timeout:
                    to_delete.append(fileNo)

            for fileNo in to_delete:
                self.close_client(fileNo)
Exemplo n.º 28
0
 def __init__(self, *, check_args=True):
     super().__init__(check_args=check_args)
     self._running = True
     self._stopping = False
     self._readers = {}
     self._writers = {}
     self._poll = select.epoll()
def server(ip, port):
    """ 利用IO复用在监听并处理客户连接"""
    # 创建监听套接字
    listen_socket = socket.socket()
    serve_address = (ip, port)
    # 设置地址重用和端口重用
    listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    listen_socket.bind(serve_address)
    listen_socket.listen(1024)

    # 创建epoller对象
    epoller = epoll()

    # 将listen_socket加入到epoller的监听中
    epoller.register(listen_socket, select.EPOLLIN)

    # SIGINT信号的处理函数
    def sigint_handler(sig_num, addtion):
        listen_socket.close()
        epoller.close()
        print('程序被强制退出...')
        sys.exit()
    
    # 注册对退出信号SIGINT的处理
    signal.signal(signal.SIGINT, sigint_handler)

    print('进程', os.getpid(), '已启动')

    # 开始epoll轮询
    epoll_loop(epoller, listen_socket)
Exemplo n.º 30
0
 def epoll_run(self):
     """async version"""
     try:
         epoll_fd = select.epoll()
         epoll_fd.register(self.server_socket.fileno(), select.EPOLLIN)
     except select.error, msg:
         exit_error(msg)
Exemplo n.º 31
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--listen", help="listen", action="store_true")
    parser.add_argument("-p", "--port", type=int, help="port")
    parser.add_argument("-H", "--host", help="remote host")

    args = parser.parse_args()

    _connector = None
    if args.listen:
        if not args.port:
            print 'server mode must give port'
            return 1

        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        serversocket.bind(('0.0.0.0', args.port))
        serversocket.listen(1)
        _connector,address = serversocket.accept()
        print 'accept connection:' + str(_connector.fileno())
    else:
        if not args.host or not args.port:
            print 'client mode must give host and port'
            return 1
        _connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        _connector.connect((args.host, args.port))
        print 'connect success'


    _connector.setblocking(0)

    epoll = select.epoll()
    epoll.register(sys.stdin.fileno(), select.EPOLLIN )
    epoll.register(_connector.fileno(), select.EPOLLIN)

    while True:
        events = epoll.poll(1)
        for fileno, event in events:
            if fileno == sys.stdin.fileno():
                msg = sys.stdin.readline().strip('\n')
                bytes = msg.split(' ')
                result_array = bytearray(len(bytes))
                for i in range(len(bytes)):
                    result_array[i] = int(bytes[i],16)
                _connector.send(result_array)
            elif fileno == _connector.fileno():
                error_happen = False
                try:
                    recv_mes = _connector.recv(1024)
                    if len(recv_mes) == 0:
                        error_happen = True
                    else:
                        print ' '.join(['0x%.2x' % x for x in bytearray(recv_mes)])
                except socket.error, e:
                    err = e.args[0]
                    if err is not errno.EAGAIN:
                        error_happen = True
                if error_happen:
                    print 'connection closed'
                    _connector.close()
                    return 1
Exemplo n.º 32
0
 def __init__(self):
     super().__init__()
     self._epoll = select.epoll()
Exemplo n.º 33
0
    def test_close_twice(self):
        import select

        ep = select.epoll()
        ep.close()
        ep.close()
Exemplo n.º 34
0
pserver.bind((HOST, PPORT))
pserver.listen(backlog)

#server for ctrl channel
cserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
cserver.bind((HOST, CPORT))
cserver.listen(backlog)

psocket = None  #connected payload socket
csocket = None  #connected  ctrl   socket

totalBytes = 0
timeout = 1000

epfd = select.epoll()
epfd.register(pserver.fileno(), select.EPOLLIN)
epfd.register(cserver.fileno(), select.EPOLLIN)

while True:
    print_info("waiting for traffic; sleeping %d seconds on epoll..." %
               timeout)  #dbg
    events = epfd.poll(timeout)
    print_info("--------> epoll returned %d input fds" % len(events))  #dbg

    for fileno, event in events:
        if cserver and fileno == cserver.fileno(
        ):  # new connection on ctrl server socket
            if csocket: raise Exception("payload socket is already connected")
            csocket, address = cserver.accept()
            epfd.register(csocket.fileno(), select.EPOLLIN)
Exemplo n.º 35
0
 def __init__(self):
     super(EpollSelector, self).__init__()
     self._epoll = select.epoll()
Exemplo n.º 36
0
    def __init__(self):
        self.epoll = select.epoll()

        self.runnables = deque()
        self.handlers = dict()
Exemplo n.º 37
0
def run_server():
    epoll = select.epoll()
    __poll_srv(epoll)
    connections = {}
    requests = {}
    while True:
        try:
            events = epoll.poll()
        except Exception:
            continue
        for fileno, event in events:
            if (event & (select.POLLIN | select.POLLPRI)):
                dev = __get_dev(fileno)
                if (None != dev):
                    if ("uart" == dev["type"]):
                        global request_uart
                        recv_buff = dev["fd"].read(
                            decode.CONFIG_TERMINAL_RECV_LENGTH - request_uart[
                                dev["fd"].fileno()].iRecvBufferTailCursor)
                        request_uart[
                            dev["fd"].fileno()].sRecvBuffer = request_uart[
                                dev["fd"].fileno()].sRecvBuffer + recv_buff
                        request_uart[
                            dev["fd"].fileno()].iRecvBufferTailCursor = len(
                                request_uart[dev["fd"].fileno()].sRecvBuffer)
                        while (0 == decode.sg_get_cmd(
                                ctypes.byref(
                                    request_uart[dev["fd"].fileno()]))):
                            str_recv = request_uart[
                                dev["fd"].fileno()].sRecvBuffer
                            decode_command = decode.sg_decode(str_recv)
                            result = str(
                                handler.call_handle(
                                    decode_command.sCommandKeyword,
                                    decode_command.sCommandParameters))
                            ret = decode.sg_call_result(
                                ctypes.byref(decode_command), result,
                                len(result))
                            if (0 == ret):
                                dev["fd"].write(decode_command.sCommandReply)
                            decode.sg_destroy_result(
                                ctypes.byref(decode_command))
                        break
                    elif ("tcp" == dev["type"]):
                        connection, addr = dev["fd"].accept()
                        connection.setblocking(0)
                        epoll.register(connection, READ_ONLY)
                        connections[connection.fileno()] = connection
                        requests[connection.fileno(
                        )] = decode.SeNetWorkCustomRecv()
                        decode.sg_network_init(
                            ctypes.byref(requests[connection.fileno()]))
                else:
                    if (0 == decode.sg_recv(ctypes.c_int(fileno),
                                            ctypes.byref(requests[fileno]))):
                        epoll.modify(fileno, READ_WRITE)
                    else:
                        epoll.unregister(fileno)
                        del requests[fileno]
                        connections[fileno].close()
            elif (event & select.EPOLLOUT):
                try:
                    while (0 == decode.sg_get_cmd(
                            ctypes.byref(requests[fileno]))):
                        str_recv = requests[fileno].sRecvBuffer
                        decode_command = decode.sg_decode(str_recv)
                        result = str(
                            handler.call_handle(
                                decode_command.sCommandKeyword,
                                decode_command.sCommandParameters))
                        ret = decode.sg_call_result(
                            ctypes.byref(decode_command), result, len(result))
                        if (0 == ret):
                            decode.sg_send(fileno,
                                           decode_command.sCommandReply,
                                           len(decode_command.sCommandReply))
                        decode.sg_destroy_result(ctypes.byref(decode_command))
                except Exception:
                    pass
                epoll.modify(fileno, READ_ONLY)
            elif (event & select.EPOLLHUP):
                epoll.unregister(fileno)
                connections[fileno].close()
                del connections[fileno]
                del requests[fileno]
    return True
Exemplo n.º 38
0
    def __init__(self,
                 _id,
                 view,
                 N,
                 committee_addresses=[],
                 max_requests=None):
        """
        @committee_addresses is a list of tuples containing the socket and ip addresses
        for all the nodes that are currently members of the committee
        """
        self.max_requests = max_requests
        self.kill_flag = False
        # self.ecdsa_key = ecdsa_sig.get_asymm_key(0, "verify")
        # f = open("hello_0.dat", 'r')
        # self.hello_sig = f.read()
        # f.close()
        self.connections = 0
        self._id = _id  # id
        self.view = view  # current view number
        self.view_active = True  # If we have majority agreement on view number
        self.N = N  # total number of replicas
        self.f = int((N - 1) / 3)  # number of failues tolerated
        self.low_bound = 0  # lower bound for acceptable sequence numbers
        self.high_bound = 0  # upper bound for acceptable sequence numbers
        self.primary = view % N
        self.seq = 0  # next available sequence number if this node is primary
        self.last_executed = 0
        self.last_stable_checkpoint = 0
        self.checkpoint_proof = []
        self.checkpoint_interval = 100
        self.vmin = 0  # used for view changes
        self.vmax = 0
        self.waiting = {}
        self.timeout = 10 * 60  # time interval (seconds), before view change
        self.lock = Lock()
        # hack
        self.clientbuff = bytes()
        self.clientlock = Lock()

        self.fdmap = {}  # file descriptor to socket mapping
        self.buffmap = {}  # file descriptor to incomming (recv) buffer mapping
        self.outbuffmap = {}
        self.p = select.epoll()  # epoll object
        # message log for node communication related to the
        # PBFT protocol: [type][seq][id] -> request
        self.node_message_log = {
            "PRPR": {},
            "PREP": {},
            "COMM": {},
            "INIT": {},
            "REQU": {},
            "VCHA": {},
            "NEVW": {},
            "CHKP": {},
        }
        # message log for client communication (requests)
        # [client id][timestamp] -> request
        self.client_message_log = {}
        # dictionary for prepare messages
        # (digest -> (number prepares received, 'prepared'[T/F])
        self.prep_dict = {}
        self.comm_dict = {}
        # dictionary for all requests that have prepared (seq -> request)
        self.prepared = {}
        self.active = {}  # dictionary for all currently active requests
        self.view_dict = {}  # [view num] -> [list of ids]
        # self.key_dict = {}
        self.replica_map = {}
        self.bank = bank.bank(id, 1000)
        self.committee_ids = committee_addresses

        self.request_types = {
            "REQU": self.process_client_request,
            "PRPR": self.process_preprepare,
            "PREP": self.process_prepare,
            "COMM": self.process_commit,
            "INIT": self.process_init,
            "VCHA": self.process_view_change,
            "NEVW": self.process_new_view,
            "CHKP": self.process_checkpoint,
        }
        # log for executed commands
        COMMIT_LOG_FILE = os.path.join(
            config_general.get("log", "root_folder"),
            "replica%s_commits.txt" % self._id)
        self.commitlog = open(COMMIT_LOG_FILE, 'w', 1)
        # log for debug messages
        replica_log_loc = os.path.join(
            config_general.get("log", "root_folder"),
            "replica" + str(self._id) + "_log.txt")
        self.debuglog = open(replica_log_loc, 'w', 1)
Exemplo n.º 39
0
def main():
    global sock, epoll
    sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW,
                         NETLINK_NEMS_ATH9K)
    sock.bind((0, 0))
    sock.setsockopt(270, 1, NETLINK_NEMS_ATH9K_GROUP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**28)
    sock.setblocking(0)

    epoll = select.epoll()
    epoll.register(sock.fileno(), select.EPOLLIN)

    enq_timestamp = {}
    air_timestamp = {}
    ack_timestamp = {}
    latency = []
    try:
        while True:
            events = epoll.poll(timeout=TIMEOUT)
            if not events:
                break
            for fileno, event in events:
                if event & select.EPOLLIN:
                    b = sock.recvfrom(NETLINK_BUF_LENGTH)[0]
                    b = b[b.find(b'[') + 1:b.find(b']')].decode('utf-8')
                    addr, seqno, timestamp, mode = b.split('|')

                    #if addr != TARGET_MAC_ADDR:
                    #    continue

                    print(b)
                    continue

                    if mode == 'enq':
                        enq_timestamp[seqno] = int(timestamp)
                    elif mode == 'ack':
                        ack_timestamp[seqno] = int(timestamp)
                        if seqno in enq_timestamp:
                            latency.append(
                                (int(timestamp) - enq_timestamp[seqno]) / 1000)
                            # enq_timestamp.pop(seqno)
                    elif mode == 'air':
                        if seqno in ack_timestamp and seqno in enq_timestamp:
                            print(int(timestamp),
                                  ack_timestamp[seqno] - enq_timestamp[seqno])

                    if len(latency) > 200:
                        latency.pop(0)

                    if len(latency) == 200:
                        a = sum(latency[:100]) / 100
                        b = sum(latency) / 200
                        current_quantum = get_airtime_quantum_low()
                        #print(f'{a: 5.3f} {b: 5.3f}', get_airtime_quantum_low())
                        if a > 15:
                            set_airtime_quantum_low(
                                max(50, current_quantum - 5))
                        else:
                            set_airtime_quantum_low(
                                min(500, current_quantum + 2))
    except socket.timeout:
        sock.close()
Exemplo n.º 40
0
    def test_non_inheritable(self):
        import select, posix

        ep = select.epoll()
        assert posix.get_inheritable(ep.fileno()) == False
        ep.close()
Exemplo n.º 41
0
 def _create_poller():
     """
     :rtype: `select.poll`
     """
     return select.epoll()  # pylint: disable=E1101
Exemplo n.º 42
0
    def test_errors(self):
        import select

        raises(ValueError, select.epoll, -2)
        raises(ValueError, select.epoll().register, -1, select.EPOLLIN)
Exemplo n.º 43
0
def run(hostIP, port):
    running = 1
    counter = 0
    bufferSize = 1024
    #Maintain amount of data sent to and from server
    dataTotal = 0
    #Create an epoll object
    epoll = select.epoll()
    #The connection dictionary maps file descriptors (integers) to their corresponding network connection objects.
    requests = {}
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    #Register interest in read events on the server socket. A read event will occur any time the server socket accepts a socket connection.
    epoll.register(serversocket, select.EPOLLIN | select.EPOLLET)
    requests.update({serversocket.fileno(): serversocket})
    #This method allows a bind() to occur even if a program was recently bound to the port.
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.bind((hostIP, port))
    #The listen backlog queue size
    serversocket.listen(10000)
    #Since sockets are blocking by default, this is necessary to use non-blocking (asynchronous) mode.
    serversocket.setblocking(0)

    
    
    try:
        
        while running:
            events = epoll.poll(-1)
            for fileno, event in events:
                # If a socket connection has been created
                if fileno == serversocket.fileno(): 
                    while 1: 
                        try: 
                            clientConnection, clientAddress = serversocket.accept()
                            counter +=1
                            #Set client connection to non blocking
                            clientConnection.setblocking(0)
                            requests.update({clientConnection.fileno(): clientConnection})
                            #Register EPOLLIN interest.
                            epoll.register(clientConnection, select.EPOLLIN | select.EPOLLET)
                            #If a read event occured, get client data
                            print (str(clientAddress) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
                            text_file.write(str(clientAddress) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
                        except: 
                            break
                elif event & select.EPOLLIN:
                    clientConnection = requests.get(fileno)
                    # Send client data back 
                    try:
                        data = clientConnection.recv(bufferSize)
                        #Get client IP and port
                        clientIP, clientSocket = clientConnection.getpeername()
                        dataSize = len(data)
                        #Add data of client message to total data
                        dataTotal += dataSize
                        #Log data received
                        text_file.write(str(getTime()) + " - Size of data received (" + clientIP + ":" + str(clientSocket) + ") = " + str(dataSize) + '\n')
                        clientConnection.send(data)
                    except:
                        pass

    # Handle a keyboard disconnect.
    except KeyboardInterrupt:
        print ("\nA keyboardInterruption has occured.")
        close(epoll, serversocket, counter, dataTotal)
    
    except Exception,e:
        print ("Unknown Error has occured." + str(e))
        close(epoll, serversocket, counter, dataTotal)
Exemplo n.º 44
0
    def serve_forever(self):
        if hasattr(select, 'epoll'):

            fn_map = {}
            p = select.epoll()
            for sock in self.sockets:
                fn = sock.fileno()
                sock.setblocking(0)
                p.register(fn,
                           select.EPOLLIN | select.EPOLLHUP | select.EPOLLPRI)
                fn_map[fn] = sock

            while self.running:
                try:
                    events = p.poll(timeout=1)
                except IOError as e:
                    if e.errno != 4:  # EINTR:
                        raise
                    else:
                        time.sleep(1)
                        continue

                for fn, event in events:
                    if fn not in fn_map:
                        self.logger.error("p.poll get fn:%d", fn)
                        continue

                    sock = fn_map[fn]
                    try:
                        (sock, address) = sock.accept()
                    except IOError as e:
                        if e.args[0] == 11:
                            # Resource temporarily unavailable is EAGAIN
                            # and that's not really an error.
                            # It means "I don't have answer for you right now and
                            # you have told me not to wait,
                            # so here I am returning without answer."
                            continue

                        if e.args[0] == 24:
                            self.logger.warn(
                                "max file opened when sock.accept")
                            time.sleep(30)
                            continue

                        self.logger.warn("socket accept fail(errno: %s).",
                                         e.args[0])
                        continue

                    try:
                        self.process_connect(sock, address)
                    except Exception as e:
                        self.logger.exception("process connect error:%r", e)

        else:
            while self.running:
                r, w, e = select.select(self.sockets, [], [], 1)
                for rsock in r:
                    try:
                        (sock, address) = rsock.accept()
                    except IOError as e:
                        self.logger.warn("socket accept fail(errno: %s).",
                                         e.args[0])
                        if e.args[0] == 10022:
                            self.logger.info("restart socket server.")
                            self.server_close()
                            self.init_socket()
                        break

                    self.process_connect(sock, address)
        self.server_close()
Exemplo n.º 45
0
 def __init__(self, ):
     if not hasattr(select, 'epoll'):
         raise SystemError("Not support epoll for current system.")
     self._epoll = select.epoll()
     self._loop_type = "epoll"
Exemplo n.º 46
0
def run(hostIP, port):
    running = 1
    counter = 0
    bufferSize = 1024
    dataSentTotal = 0
    dataReceivedTotal = 0
    dataSSize = 0
    dataRSize = 0
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind((hostIP, port))
    #The listen backlog queue size
    serversocket.listen(socket.SOMAXCONN)
    #Since sockets are blocking by default, this is necessary to use non-blocking (asynchronous) mode.
    serversocket.setblocking(0)
    #Create an epoll object.
    epoll = select.epoll()
    #Register interest in read events on the server socket. A read event will occur any time the server socket accepts a socket connection.
    epoll.register(serversocket.fileno(), select.EPOLLIN)
    try:
        #The connection dictionary maps file descriptors (integers) to their corresponding network connection objects.
        requests = {}
        while running:
            #Query the epoll object to find out if any events of interest may have occurred. The parameter "1" signifies that we are willing to wait up to one second for such an event to occur. If any events of interest occurred prior to this query, the query will return immediately with a list of those events.
            events = epoll.poll(-1)
            #Events are returned as a sequence of (fileno, event code) tuples. fileno is a synonym for file descriptor and is always an integer.
            for fileno, event in events:
                #If a read event occurred on the socket server, then a new socket connection may have been created.
                if fileno == serversocket.fileno():
                    clientConnection, clientAddress = serversocket.accept()
                    counter += 1
                    requests.update(
                        {clientConnection.fileno(): clientConnection})
                    #Set new socket to non-blocking mode.
                    clientConnection.setblocking(0)
                    #Register interest in read (EPOLLIN) events for the new socket.
                    epoll.register(clientConnection.fileno(), select.EPOLLIN)
                    logging.info(str(clientAddress) + " just connected.")
                    logging.info("Currently connected clients: " +
                                 str(counter))
                    print(
                        str(clientAddress) +
                        " just connected. \nCurrently connected clients: " +
                        str(counter))
                elif event & select.EPOLLIN:
                    receiveSock = requests.get(fileno)
                    data = receiveSock.recv(bufferSize)
                    clientIP, clientSocket = receiveSock.getpeername()
                    dataRSize = len(data)
                    dataReceivedTotal += dataRSize
                    logging.info("Size of data received (" + clientIP + ":" +
                                 str(clientSocket) + ") = " + str(dataRSize))
                    dataSSize = len(data)
                    dataSentTotal += dataSSize
                    receiveSock.send(data)
                    logging.info("Size of data sent (" + clientIP + ":" +
                                 str(clientSocket) + ") = " + str(dataSSize))
                elif event & select.EPOLLERR:
                    counter -= 1
                elif event & select.EPOLLHUP:
                    counter -= 1
    #Handle keyboard interrupts (Mainly ctrl+c)
    except KeyboardInterrupt:
        close(epoll, serversocket, counter, dataReceivedTotal, dataSentTotal)
        print("\nA keyboardInterruption has occured.")
    #Handle all other exceptions in hopes to close 'cleanly'
    except Exception, e:
        print("Server connection has ran into an unexpected error: " + str(e))
        close(epoll, serversocket, counter, dataReceivedTotal, dataSentTotal)
Exemplo n.º 47
0
 def __init__(self, app):
     self.app = app
     self.server_socket = init_server_socket()
     self.epoll = select.epoll()
     self.streams = {}
     self.epoll.register(self.server_socket.fileno(), select.EPOLLIN)
Exemplo n.º 48
0
 def __init__(self):
     self._epoll = select.epoll()
Exemplo n.º 49
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--listen", help="listen", action="store_true")
    parser.add_argument("-p", "--port", type=int, help="port")
    parser.add_argument("-H", "--host", help="remote host")

    args = parser.parse_args()

    _connector = None
    if args.listen:
        if not args.port:
            print 'server mode must give port'
            return 1

        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        serversocket.bind(('0.0.0.0', args.port))
        serversocket.listen(1)
        _connector, address = serversocket.accept()
        print 'accept connection:' + str(_connector.fileno())
    else:
        if not args.host or not args.port:
            print 'client mode must give host and port'
            return 1
        _connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        _connector.connect((args.host, args.port))
        print 'connect success'

    devFd = tun_create('yt_tun', IFF_TUN | IFF_NO_PI)
    if devFd < 0:
        raise OSError

    _connector.setblocking(0)

    epoll = select.epoll()
    epoll.register(_connector.fileno(), select.EPOLLIN)
    epoll.register(devFd, select.EPOLLIN | select.EPOLLHUP)

    MAXSIZE = 4096
    while True:
        events = epoll.poll(1)
        for fileno, event in events:
            if fileno == devFd:
                buf = os.read(fileno, MAXSIZE)
                _connector.send(buf)
            elif fileno == _connector.fileno():
                error_happen = False
                try:
                    recv_mes = _connector.recv(MAXSIZE)
                    if len(recv_mes) == 0:
                        error_happen = True
                    else:
                        os.write(devFd, recv_mes)
                except socket.error, e:
                    err = e.args[0]
                    if err is not errno.EAGAIN:
                        error_happen = True
                if error_happen:
                    print 'connection closed'
                    _connector.close()
                    return 1
Exemplo n.º 50
0
 def __init__(self):
     self._epoll = epoll()
Exemplo n.º 51
0
 def __init__(self):
     self._poller = epoll()
Exemplo n.º 52
0
 def __init__(self, halt_notify=None, **kvargs):
     self.__epfd = select.epoll()
     self.__handlers = {}
     self.__halt_notify = halt_notify
     self.__halt = False
     self.__obj_manager = None
Exemplo n.º 53
0
def main():
    a = argparse.ArgumentParser(description='Rasberry Pi LED flashing thingy')
    a.add_argument('--pipe',
                   '-p',
                   help='Pi-blaster pipe',
                   default='/dev/pi-blaster')
    args = a.parse_args()

    state = {}
    state['pipe'] = open(args.pipe, 'w')
    state['c_red'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 0,
        'brightness': 1,
        'channel': 2
    }
    state['c_green'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 1 / 3,
        'brightness': 1,
        'channel': 5
    }
    state['c_blue'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 2 / 3,
        'brightness': 1,
        'channel': 6
    }

    # Maps file descriptors to netstate instances
    connections = {}

    spec = ctimerfd.itimerspec()
    spec.it_interval.tv_sec = 0
    spec.it_interval.tv_nsec = long(1e9 / 24)
    spec.it_value.tv_sec = 0
    spec.it_value.tv_nsec = 1
    t = ctimerfd.timerfd_create(ctimerfd.CLOCK_MONOTONIC,
                                ctimerfd.TFD_CLOEXEC | ctimerfd.TFD_NONBLOCK)
    ctimerfd.timerfd_settime(t, 0, ctypes.pointer(spec), None)

    s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
    s.bind(('', 12345))
    s.listen(5)

    epoll = select.epoll()
    util.set_cloexec(
        epoll.fileno()
    )  # XXX As of Python 2.3, flags=EPOLL_CLOEXEC can be used when creating the epoll instead
    epoll.register(t, select.POLLIN)
    epoll.register(s, select.POLLIN)

    while True:
        for fd, event in eintr_wrap(epoll.poll):
            # timer
            if fd == t and event & select.POLLIN:
                os.read(t, 8)
                wrap(on_timer, state)
            # listening socket
            elif fd == s.fileno() and event & select.POLLIN:
                conn, addr = eintr_wrap(s.accept)
                print 'Connection accepted from [{}]:{} (fd={})'.format(
                    addr[0], addr[1], conn.fileno())
                conn.setblocking(False)
                epoll.register(conn.fileno(), select.POLLIN)
                connections[conn.fileno()] = netstate(conn, bytearray())
            # connection socket
            elif fd in connections and event & select.POLLIN:
                ns = connections[fd]
                while True:
                    x = eagain_wrap(eintr_wrap, ns.socket.recv, 4096)
                    if x is None:
                        break
                    if len(x) == 0:
                        print 'Connection closed (fd={})'.format(
                            ns.socket.fileno())
                        del connections[ns.socket.fileno()]
                        epoll.unregister(ns.socket.fileno())
                        ns.socket.close()
                        break
                    ns.buf.extend(x)
                while True:
                    try:
                        i = ns.buf.index('\r\n')
                    except ValueError:
                        break
                    args = [bytes(x) for x in ns.buf[:i].split()]
                    del ns.buf[:i + 2]

                    fn = commands.commands.get(args[0], commands.unknown)
                    try:
                        fn(ns.socket, args[1:], state)
                    except Exception as e:
                        eintr_wrap(ns.socket.send, b'500 {}\r\n'.format(e))
                        traceback.print_exc()
            # anything else is unexpected
            else:
                msg = 'Bad event (fd={} event={})'.format(fd, event)
                raise Exception(msg)
Exemplo n.º 54
0
# endregion -------------------------------------------------------------------

# region #* Исполнение кода в 1 поток. Модуль Select() ------------------------
"""В операционной системе существует модуль select, позволяеющий организовать
работу с неблокирующим вводом-выводом.
"""
sock = socket.socket()  # создание объекта сокета
sock.bind()
sock.listen()

conn1, addr = sock.accept()
conn2, addr = sock.accept()

conn1.setblocking(0)  # перевод соединения в неблокирующий режим. В этом режиме
conn2.setblocking(0)  # вместо исключения идет некий ответ об ошибке

# _ образцец для Линукс
epoll = select.epoll()  # создание объекта epoll
epoll.register(conn1.fileno(), select.EPOLLIN | select.EPOLLOUT)
"""в обхекте epoll регистриуются файловые дескрипторы coll1.fileno, а также
маску, то есть говорим на какие события файловых дескрипторов мы подписываемся.
Чтение и запись в совектах """
epoll.register(conn2.fileno(), select.EPOLLIN | select.EPOLLOUT)
conn_map = {
    conn1.fileno(): conn1,
    conn2.fileno(): conn2,
}
# создается словрь объектов файловых соединений.

# endregion -------------------------------------------------------------------
Exemplo n.º 55
0
@contact: [email protected]
@file: pipe_with_epoll.py
@time: 2015/12/7 15:09
"""
import socket
import select
import Queue
import new_type

from multiprocessing.dummy import Pool as ThreadPool
max_read_header = 50
queue = Queue.Queue()
addr = '0.0.0.0'
port = 8080
raw_conn = {}
epoll_sock = select.epoll()

status = {'close': 1, 'more': 2, 'complete': 3}
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((addr, port))
sock.listen(1)
sock.setblocking(0)
epoll_sock.register(sock.fileno(), select.EPOLLIN)


class listen_worker():
    def __init__(self, addr, port):
        print 'listening on port: ' + str(port)
        while 1:
            for fd, events in epoll_sock.poll():
Exemplo n.º 56
0
#-*- encoding=UTF-8 -*-
Exemplo n.º 57
0
    def get_command_output(
        cls,
        command,
        is_posix,
        timeout=10,
    ):
        process = None
        all_output = ''
        start_time = time.time()

        try:
            poller = select.epoll()

            process = subprocess.Popen(
                args=shlex.split(
                    s=command,
                    posix=is_posix,
                ),
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
            )

            poller.register(process.stdout, select.EPOLLHUP | select.EPOLLIN)

            while start_time + timeout > time.time():
                for fileno, event in poller.poll(timeout=1, ):
                    if event & select.EPOLLHUP:
                        poller.unregister(fd=fileno, )

                        break

                    output = process.stdout.read()
                    output = output.decode(
                        encoding='utf-8',
                        errors='ignore',
                    )
                    all_output += output

                try:
                    process.wait(0)

                    break
                except subprocess.TimeoutExpired:
                    pass
        finally:
            if process is not None:
                process.terminate()

                try:
                    os.waitpid(
                        process.pid,
                        0,
                    )
                except ChildProcessError:
                    pass

                output = process.stdout.read()
                output = output.decode(
                    encoding='utf-8',
                    errors='ignore',
                )
                all_output += output

                try:
                    process.kill()
                except ProcessLookupError:
                    pass

        empty_whois_result = all_output.strip() == ''
        timedout_whois_result = all_output.strip(
        ) == 'Interrupted by signal 15...'

        if empty_whois_result or timedout_whois_result:
            raise _resolver.WhoisTimedOut()
        else:
            return all_output.strip()
Exemplo n.º 58
0
    def __init__(self,
                 nodetype,
                 addr=args.addr,
                 port=args.port,
                 givenbootstraplist=args.bootstrap,
                 debugoption=args.debug,
                 objectname=args.objectname,
                 instantiateobj=False,
                 configpath=args.configpath,
                 logger=args.logger,
                 writetodisk=args.writetodisk):
        self.addr = addr if addr else findOwnIP()
        self.port = port
        self.type = nodetype
        self.debug = debugoption
        self.durable = writetodisk
        if instantiateobj:
            if objectname == '':
                parser.print_help()
                self._graceexit(1)
            self.objectname = objectname
        # initialize receive queue
        self.receivedmessages_semaphore = Semaphore(0)
        self.receivedmessages = []
        # lock to synchronize message handling
        self.lock = Lock()
        # create server socket and bind to a port
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.socket.setblocking(0)
        if self.port:
            try:
                self.socket.bind((self.addr, self.port))
            except socket.error as e:
                print "Cannot bind to port %d" % self.port
                print "Socket Error: ", e
                self._graceexit(1)
        else:
            for i in range(50):
                self.port = random.randint(14000, 15000)
                try:
                    self.socket.bind((self.addr, self.port))
                    break
                except socket.error as e:
                    print "Socket Error: ", e
                    pass
        self.socket.listen(10)
        self.connectionpool = ConnectionPool()
        try:
            self.connectionpool.epoll = select.epoll()
        except AttributeError as e:
            # the os doesn't support epoll
            self.connectionpool.epoll = None
        self.alive = True
        self.me = Peer(self.addr, self.port, self.type)
        # set id
        self.id = '%s:%d' % (self.addr, self.port)

        # set path for additional configuration data
        self.configpath = configpath
        # set the logger
        try:
            LOGGERNODE = load_configdict(self.configpath)['LOGGERNODE']
        except KeyError as e:
            if logger:
                LOGGERNODE = logger
            else:
                LOGGERNODE = None
        self.logger = Logger("%s-%s" % (node_names[self.type], self.id),
                             lognode=LOGGERNODE)
        print "%s-%s connected." % (node_names[self.type], self.id)
        # Initialize groups
        # Keeps {peer:outofreachcount}
        self.replicas = {}
        self.acceptors = {}
        self.nameservers = {}
        self.groups = {
            NODE_REPLICA: self.replicas,
            NODE_ACCEPTOR: self.acceptors,
            NODE_NAMESERVER: self.nameservers
        }
        self.groups[self.me.type][self.me] = 0

        # Keeps the liveness of the nodes
        self.nodeliveness = {}
        # connect to the bootstrap node
        if givenbootstraplist:
            self.bootstraplist = []
            self.discoverbootstrap(givenbootstraplist)
            self.connecttobootstrap()
        if self.type == NODE_REPLICA or self.type == NODE_NAMESERVER:
            self.stateuptodate = False
Exemplo n.º 59
0
import socket
import select
import queue

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ("0.0.0.0", 9999)
server.bind(server_address)
server.listen(5)
server.setblocking(False)
print("Server is listening....")

timeout = 10
# 新建epoll事件对象, 后续要监控的事件, 添加到其中
epoll = select.epoll()
# 添加服务器socket的fd, 到等待读事件集合
epoll.register(server.fileno(), select.EPOLLIN)
# 每一个连接的消息字典{socket: 队列}
message_queues = {}

fd_to_socket = {server.fileno(): server,}


while True:
    print("等待活动连接......")
    # 轮训注册的事件集合
    events = epoll.poll(timeout)
    if not events:
        print("epoll 超时无活动连接, 重新轮询...")
        continue
    print("有%d个新事件, 开始处理......" % len(events))
Exemplo n.º 60
0
def main():
    args = getArgs()

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((args.ip, args.port))
    s.listen(1)
    s.setblocking(0)

    epoll = select.epoll()
    epoll.register(s.fileno(), select.EPOLLIN)

    try:
        conn = {}
        txbuf = {}
        rxbuf = {}
        while True:
            events = epoll.poll(1)
            for fileno, event in events:
                if fileno == s.fileno():
                    c, addr = s.accept()
                    c.setblocking(0)
                    conn[c.fileno()] = c
                    txbuf[c] = ''
                    rxbuf[c] = ''
                    epoll.register(c.fileno(), select.EPOLLOUT)
                elif event & select.EPOLLOUT:
                    c = conn[fileno]
                    try:
                        n = c.send(txbuf[c])
                    except Exception as e:
                        print e
                        c.close()
                        epoll.unregister(fileno)
                        del rxbuf[c]
                        del txbuf[c]
                        del conn[fileno]
                        continue
                    txbuf[c] = txbuf[c][n:]
                    if not len(txbuf[c]):
                        epoll.modify(fileno, select.EPOLLIN)
                elif event & select.EPOLLIN:
                    c = conn[fileno]
                    try:
                        rxbuf[c] += c.recv(1024)
                    except Exception as e:
                        print e
                        c.close()
                        epoll.unregister(fileno)
                        del rxbuf[c]
                        del txbuf[c]
                        del conn[fileno]
                        continue
                    if not len(rxbuf[c]):
                        c.close()
                        epoll.unregister(fileno)
                        del rxbuf[c]
                        del txbuf[c]
                        del conn[fileno]
                        continue
                    l, msg, rxbuf[c] = msgUnpack(rxbuf[c])
                    if l:
                        for sock in conn.values():
                            if sock == c: continue
                            txbuf[sock] += msgPack(msg)
                            epoll.modify(sock.fileno(), select.EPOLLOUT)
                else:
                    print fileno, event

    except KeyboardInterrupt:
        epoll.close()
        s.close()