Exemple #1
0
    def handle_poll_results(self, msg):
        if "error" in msg:
            print "I don't know how we handle errors: %s" % (msg["error"], )
            return

        r, w, e = msg["fds"]

        map = self.map

        for fd in r:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.read(obj)

        for fd in w:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.write(obj)

        for fd in e:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore._exception(obj)
Exemple #2
0
    def poll(self, timeout=0.0):
        """Modified version of poll() from asyncore module"""
        if self.sock_map is None:
            Log.warning("Socket map is not registered to Gateway Looper")
        readable_lst = []
        writable_lst = []
        error_lst = []

        if self.sock_map is not None:
            for fd, obj in self.sock_map.items():
                is_r = obj.readable()
                is_w = obj.writable()
                if is_r:
                    readable_lst.append(fd)
                if is_w and not obj.accepting:
                    writable_lst.append(fd)
                if is_r or is_w:
                    error_lst.append(fd)

        # Add wakeup fd
        readable_lst.append(self.pipe_r)

        Log.debug("Will select() with timeout: " + str(timeout) +
                  ", with map: " + str(self.sock_map))
        try:
            readable_lst, writable_lst, error_lst = \
              select.select(readable_lst, writable_lst, error_lst, timeout)
        except select.error as err:
            Log.debug("Trivial error: " + str(err))
            if err.args[0] != errno.EINTR:
                raise
            else:
                return
        Log.debug("Selected [r]: " + str(readable_lst) + " [w]: " +
                  str(writable_lst) + " [e]: " + str(error_lst))

        if self.pipe_r in readable_lst:
            Log.debug("Read from pipe")
            os.read(self.pipe_r, 1024)
            readable_lst.remove(self.pipe_r)

        if self.sock_map is not None:
            for fd in readable_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                asyncore.read(obj)

            for fd in writable_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                asyncore.write(obj)

            for fd in error_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                # pylint: disable=W0212
                asyncore._exception(obj)
Exemple #3
0
    def handle_poll_results(self, msg):
        if "error" in msg:
            print "I don't know how we handle errors: %s" % (msg["error"],)
            return

        r, w, e = msg["fds"]

        map = self.map

        for fd in r:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.read(obj)

        for fd in w:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.write(obj)

        for fd in e:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore._exception(obj)
Exemple #4
0
  def poll(self, timeout=0.0):
    """Modified version of poll() from asyncore module"""
    if self.sock_map is None:
      Log.warning("Socket map is not registered to Gateway Looper")
    readable_lst = []
    writable_lst = []
    error_lst = []

    if self.sock_map is not None:
      for fd, obj in self.sock_map.items():
        is_r = obj.readable()
        is_w = obj.writable()
        if is_r:
          readable_lst.append(fd)
        if is_w and not obj.accepting:
          writable_lst.append(fd)
        if is_r or is_w:
          error_lst.append(fd)

    # Add wakeup fd
    readable_lst.append(self.pipe_r)

    Log.debug("Will select() with timeout: " + str(timeout) + ", with map: " + str(self.sock_map))
    try:
      readable_lst, writable_lst, error_lst = \
        select.select(readable_lst, writable_lst, error_lst, timeout)
    except select.error as err:
      Log.debug("Trivial error: " + str(err))
      if err.args[0] != errno.EINTR:
        raise
      else:
        return
    Log.debug("Selected [r]: " + str(readable_lst) +
              " [w]: " + str(writable_lst) + " [e]: " + str(error_lst))

    if self.pipe_r in readable_lst:
      Log.debug("Read from pipe")
      os.read(self.pipe_r, 1024)
      readable_lst.remove(self.pipe_r)

    if self.sock_map is not None:
      for fd in readable_lst:
        obj = self.sock_map.get(fd)
        if obj is None:
          continue
        asyncore.read(obj)

      for fd in writable_lst:
        obj = self.sock_map.get(fd)
        if obj is None:
          continue
        asyncore.write(obj)

      for fd in error_lst:
        obj = self.sock_map.get(fd)
        if obj is None:
          continue
        # pylint: disable=W0212
        asyncore._exception(obj)
Exemple #5
0
    def testHandshake(self):
        # Trigger an accept.
        asyncore.read(self.srv)
        handler = self.asyncore_map[CONN_SOCK_1_FILENO]

        # Trigger the handler to write its handshake.
        asyncore.write(handler)
        self.assertEquals(self.conn_sock_1.remote_read(), 'bzrobots 1\n')
Exemple #6
0
    def testHandshake(self):
        # Trigger an accept.
        asyncore.read(self.srv)
        handler = self.asyncore_map[CONN_SOCK_1_FILENO]

        # Trigger the handler to write its handshake.
        asyncore.write(handler)
        self.assertEquals(self.conn_sock_1.remote_read(), 'bzrobots 1\n')
Exemple #7
0
def poll(timeout=0.0, map=None, mdisp=None):
    if map is None:
        map = asyncore.socket_map
    if mdisp is None:
        mdisp = multi_dispatcher
    if map:
        if mdisp.timeout != -1:
            timeout = min(timeout, mdisp.timeout/1000.0)

        r = []; w = []; e = []
        for fd, obj in map.items():
            is_r = obj.readable()
            is_w = obj.writable()
            if is_r:
                r.append(fd)
            if is_w:
                w.append(fd)
            if is_r or is_w:
                e.append(fd)
        if [] == r == w == e:
            if timeout > 0:
                time.sleep(timeout)
        else:
            try:
                r, w, e = select.select(r, w, e, timeout)
            except select.error, err:
                if err[0] != EINTR:
                    raise
                else:
                    return

        if [] == r == w == e:
            mdisp.socket_action(SOCKET_TIMEOUT, 0)
            return

        for fd in r:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.read(obj)

        for fd in w:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.write(obj)

        for fd in e:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore._exception(obj)
 def test_readwriteexc(self):
     tr1 = exitingdummy()
     self.assertRaises(asyncore.ExitNow, asyncore.read, tr1)
     self.assertRaises(asyncore.ExitNow, asyncore.write, tr1)
     self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1)
     tr2 = crashingdummy()
     asyncore.read(tr2)
     self.assertEqual(tr2.error_handled, True)
     tr2 = crashingdummy()
     asyncore.write(tr2)
     self.assertEqual(tr2.error_handled, True)
     tr2 = crashingdummy()
     asyncore._exception(tr2)
     self.assertEqual(tr2.error_handled, True)
def poll(timeout=0.0):
    global sockets_to_close
    map = asyncore.socket_map
    if not map:
        return False
    while 1:
        r = []
        w = []
        e = []
        for fd, obj in map.items():
            if obj.readable():
                r.append(fd)
            if obj.writable():
                w.append(fd)
        if not sockets_to_close:  # Set by writeable()
            break
        for s in sockets_to_close:
            s.close()
        sockets_to_close = []
    if [] == r == w == e:
        time.sleep(timeout)
    else:
        #@+<< try r, w, e = select.select >>
        #@+node:EKR.20040517080250.41: *4* << try r, w, e = select.select >>
        try:
            r, w, e = select.select(r, w, e, timeout)
        except select.error:  # as err:
            # if err[0] != EINTR:
            # raise
            # else:
            # return False
            return False  # EKR: EINTR is undefined.
        #@-<< try r, w, e = select.select >>
    for fd in r:
        #@+<< asyncore.read(map.get(fd)) >>
        #@+node:EKR.20040517080250.42: *4* << asyncore.read(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.read(obj)
        #@-<< asyncore.read(map.get(fd)) >>
    for fd in w:
        #@+<< asyncore.write(map.get(fd)) >>
        #@+node:EKR.20040517080250.43: *4* << asyncore.write(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.write(obj)
        #@-<< asyncore.write(map.get(fd)) >>
    return len(r) > 0 or len(w) > 0
Exemple #10
0
def poll(timeout=0.0):

    global sockets_to_close
    map = asyncore.socket_map
    if not map:
        return False
    while 1:
        r = []; w = []; e = []
        for fd, obj in map.items():
            if obj.readable():
                r.append(fd)
            if obj.writable():
                w.append(fd)
        if not sockets_to_close: # Set by writeable()
            break
        for s in sockets_to_close:
            s.close()
        sockets_to_close = []
    if [] == r == w == e:
        time.sleep(timeout)
    else:
        #@+<< try r, w, e = select.select >>
        #@+node:EKR.20040517080250.41: *4* << try r, w, e = select.select >>
        try:
            r, w, e = select.select(r, w, e, timeout)
        except select.error as err:
            # if err[0] != EINTR:
                # raise
            # else:
                # return False
            return False # EKR: EINTR is undefined.
        #@-<< try r, w, e = select.select >>
    for fd in r:
        #@+<< asyncore.read(map.get(fd)) >>
        #@+node:EKR.20040517080250.42: *4* << asyncore.read(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.read(obj)
        #@-<< asyncore.read(map.get(fd)) >>
    for fd in w:
        #@+<< asyncore.write(map.get(fd)) >>
        #@+node:EKR.20040517080250.43: *4* << asyncore.write(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.write(obj)
        #@-<< asyncore.write(map.get(fd)) >>
    return len(r) > 0 or len(w) > 0
Exemple #11
0
def poll(map, timeout=0.0):
    """Asyncore poll function."""
    if map:
        r = []
        w = []
        e = []
        for fd, obj in map.items():
            is_r = obj.readable()
            is_w = obj.writable()
            if is_r:
                r.append(fd)
            if is_w:
                w.append(fd)
            if is_r or is_w:
                e.append(fd)
        if [] == r == w == e:
            time.sleep(timeout)
        else:
            try:
                if use_select_emulation:
                    r, w, e = clewn_select(r, w, e, timeout)
                else:
                    r, w, e = select.select(r, w, e, timeout)
            except select.error, err:
                if err[0] != errno.EINTR:
                    raise
                else:
                    return

        for fd in r:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.read(obj)

        for fd in w:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore.write(obj)

        for fd in e:
            obj = map.get(fd)
            if obj is None:
                continue
            asyncore._exception(obj)
Exemple #12
0
    def run(self, timeout=0.0):
        """Run the asyncore poll function."""
        if self.socket_map:
            r = []
            w = []
            e = []
            for fd, obj in self.socket_map.items():
                is_r = obj.readable()
                is_w = obj.writable()
                if is_r:
                    r.append(fd)
                if is_w:
                    w.append(fd)
                if is_r or is_w:
                    e.append(fd)
            if [] == r == w == e:
                time.sleep(timeout)
            else:
                try:
                    if use_select_emulation:
                        r, w, e = clewn_select(r, w, e, timeout, self)
                    else:
                        r, w, e = select.select(r, w, e, timeout)
                except select.error, err:
                    if err[0] != errno.EINTR:
                        raise
                    else:
                        return

            for fd in r:
                obj = self.socket_map.get(fd)
                if obj is None:
                    continue
                asyncore.read(obj)

            for fd in w:
                obj = self.socket_map.get(fd)
                if obj is None:
                    continue
                asyncore.write(obj)

            for fd in e:
                obj = self.socket_map.get(fd)
                if obj is None:
                    continue
                asyncore._exception(obj)
Exemple #13
0
def write_file(keys):
    with open("log.txt", "w") as f:
        for key in keys:
            k = str(key).replace("'", "")
            if k.find("space") > 0:
                f.write('\n')
            elif k.find("Key") == -1:
                f, write(k)
Exemple #14
0
def poll(timeout=0.0):
    global sockets_to_close
    map = asyncore.socket_map
    if not map:
        return False
    e: Any
    f: Any
    r: Any = []
    w: Any
    while 1:
        e = f = w = []
        for fd, obj in map.items():
            if obj.readable():
                r.append(fd)
            if obj.writable():
                w.append(fd)
        if not sockets_to_close:  # Set by writeable()
            break
        for s in sockets_to_close:
            s.close()
        sockets_to_close = []
    if [] == r == w == e:  # pylint: disable=bad-option-value,use-implicit-booleaness-not-comparison
        time.sleep(timeout)
    else:
        #@+<< try r, w, e = select.select >>
        #@+node:EKR.20040517080250.41: *4* << try r, w, e = select.select >>
        try:
            r, w, e = select.select(r, w, e, timeout)
        except select.error:  # as err:
            return False  # EKR: EINTR is undefined.
        #@-<< try r, w, e = select.select >>
    for fd in r:
        #@+<< asyncore.read(map.get(fd)) >>
        #@+node:EKR.20040517080250.42: *4* << asyncore.read(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.read(obj)
        #@-<< asyncore.read(map.get(fd)) >>
    for fd in w:
        #@+<< asyncore.write(map.get(fd)) >>
        #@+node:EKR.20040517080250.43: *4* << asyncore.write(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.write(obj)
        #@-<< asyncore.write(map.get(fd)) >>
    return len(r) > 0 or len(w) > 0
Exemple #15
0
    def test_readwriteexc(self):
        # Check exception handling behavior of read, write and _exception

        # check that ExitNow exceptions in the object handler method
        # bubbles all the way up through asyncore read/write/_exception calls
        tr1 = exitingdummy()
        self.assertRaises(asyncore.ExitNow, asyncore.read, tr1)
        self.assertRaises(asyncore.ExitNow, asyncore.write, tr1)
        self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1)

        # check that an exception other than ExitNow in the object handler
        # method causes the handle_error method to get called
        tr2 = crashingdummy()
        asyncore.read(tr2)
        self.assertEqual(tr2.error_handled, True)

        tr2 = crashingdummy()
        asyncore.write(tr2)
        self.assertEqual(tr2.error_handled, True)

        tr2 = crashingdummy()
        asyncore._exception(tr2)
        self.assertEqual(tr2.error_handled, True)
Exemple #16
0
    def test_readwriteexc(self):
        # Check exception handling behavior of read, write and _exception

        # check that ExitNow exceptions in the object handler method
        # bubbles all the way up through asyncore read/write/_exception calls
        tr1 = exitingdummy()
        self.assertRaises(asyncore.ExitNow, asyncore.read, tr1)
        self.assertRaises(asyncore.ExitNow, asyncore.write, tr1)
        self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1)

        # check that an exception other than ExitNow in the object handler
        # method causes the handle_error method to get called
        tr2 = crashingdummy()
        asyncore.read(tr2)
        self.assertEqual(tr2.error_handled, True)

        tr2 = crashingdummy()
        asyncore.write(tr2)
        self.assertEqual(tr2.error_handled, True)

        tr2 = crashingdummy()
        asyncore._exception(tr2)
        self.assertEqual(tr2.error_handled, True)
Exemple #17
0
	def run_forever(self):
		sometimes_counter = 0
		
		# Run any run_once methods that children have
		for child in self.__Children.values():
			if hasattr(child, 'run_once'):
				child.run_once()
		
		while 1:
			try:
				while self.inQueue:
					message = self.inQueue.pop(0)
					
					# If it's targeted at us, process it
					if message.targets == ['Postman']:
						# Reload our config
						if message.ident == REQ_REHASH:
							self.__Rehash()
						
						# Die!
						elif message.ident == REQ_SHUTDOWN:
							self.__Shutdown(message.data[0])
						
						# Someone wants some stats
						elif message.ident == GATHER_STATS:
							message.data['plugins'] = len([v for v in self.__Children.values() if isinstance(v, Plugin)])
							message.data['started'] = self.__Started
							self.sendMessage('BotStatus', GATHER_STATS, message.data)
						
						# A child just shut itself down. If it was a plugin,
						# "unimport" it.
						elif message.ident == REPLY_SHUTDOWN:
							child = message.source
							try:
								if issubclass(globals()[child], Plugin):
									self.__Plugin_Unload(child)
									
									# If it's really being reloaded, do that
									if self.__reloadme.has_key(child):
										del self.__reloadme[child]
										self.__Plugin_Load(child, runonce=1)
										
										# If reloadme is empty, rehash now
										if not self.__reloadme:
											self.sendMessage(None, REQ_REHASH, None)
							
							except KeyError:
								tolog = "Postman received a message from ghost plugin '%s'" % (child)
								self.logger.warn(tolog)
					
					else:
						# Log the message if debug is enabled
						self.logger.debug(message)
						
						# If it's a global message, send it to everyone
						if message.targetstring == 'ALL':
							for child in self.__Children.values():
								child.inQueue.append(message)
						
						# If it's not, send it to each thread listed in targets
						else:
							for name in message.targets:
								if name in self.__Children:
									self.__Children[name].inQueue.append(message)
								else:
									tolog = "Invalid target for Message ('%s') : %s" % (name, message)
									self.logger.warn(tolog)
				
				
				# Deliver any waiting messages to children
				for name, child in self.__Children.items():
					if not child.inQueue:
						continue
					
					message = child.inQueue.pop(0)
					
					methname = '_message_%s' % (message.ident)
					if hasattr(child, methname):
						getattr(child, methname)(message)
					else:
						tolog = 'Unhandled message in %s: %s' % (name, message.ident)
						self.logger.debug(tolog)
				
				
				# Poll our sockets
				results = asyncore.poller.poll(0)
				for fd, event in results:
					obj = asyncore.socket_map.get(fd)
					if obj is None:
						tolog = 'Invalid FD for poll(): %d - unregistered' % (fd)
						self.logger.critical(tolog)
						asyncore.poller.unregister(fd)
						continue
					
					if event & select.POLLIN:
						try:
							asyncore.read(obj)
						except socket.error, msg:
							obj.really_close(msg)
					elif event & select.POLLOUT:
						asyncore.write(obj)
					elif event & select.POLLNVAL:
						tolog = "FD %d is still in the poll, but it's closed!" % (fd)
						self.logger.critical(tolog)
					else:
						tolog = 'Bizarre poll response! %d: %d' % (fd, event)
						self.logger.critical(tolog)
Exemple #18
0
 def update_event(self, inp=-1):
     self.set_output_val(0, asyncore.write(self.input(0)))
Exemple #19
0
class GatewayLooper(EventLooper):
    """A GatewayLooper, inheriting EventLooper

  It is a class wrapping Python's asyncore module (and selector) to dispatch events.
  This class can be used as a looper for an ``asyncore.dispatcher`` class, instead of calling
  ``asyncore.loop()``.

  As it is a subclass of EventLooper, it will execute in a while loop until
  the ``exit_loop()`` is called.

  In order to use this class, users first need to specify a socket map that maps from
  a file descriptor to ``asyncore.dispatcher`` class, using ``prepare_map()`` method.
  The GatewayLooper will dispatch ready events that are in the specified map.
  """
    def __init__(self, socket_map):
        """Initializes a GatewayLooper instance

    :param socket_map: socket map used for asyncore.dispatcher
    """
        super(GatewayLooper, self).__init__()
        self.sock_map = socket_map

        # Pipe used for wake up select
        self.pipe_r, self.pipe_w = os.pipe()

        self.started = time.time()
        Log.debug("Gateway Looper started time: " + str(time.asctime()))

    def do_wait(self):
        next_timeout = self._get_next_timeout_interval()
        if next_timeout > 0:
            self.poll(timeout=next_timeout)
        else:
            self.poll(timeout=0.0)

    def wake_up(self):
        os.write(self.pipe_w, "\n")
        Log.debug("Wake up called")

    def on_exit(self):
        super(GatewayLooper, self).on_exit()
        os.close(self.pipe_r)
        os.close(self.pipe_w)

    # pylint: disable=too-many-branches
    def poll(self, timeout=0.0):
        """Modified version of poll() from asyncore module"""
        if self.sock_map is None:
            Log.warning("Socket map is not registered to Gateway Looper")
        readable_lst = []
        writable_lst = []
        error_lst = []

        if self.sock_map is not None:
            for fd, obj in self.sock_map.items():
                is_r = obj.readable()
                is_w = obj.writable()
                if is_r:
                    readable_lst.append(fd)
                if is_w and not obj.accepting:
                    writable_lst.append(fd)
                if is_r or is_w:
                    error_lst.append(fd)

        # Add wakeup fd
        readable_lst.append(self.pipe_r)

        Log.debug("Will select() with timeout: " + str(timeout) +
                  ", with map: " + str(self.sock_map))
        try:
            readable_lst, writable_lst, error_lst = \
              select.select(readable_lst, writable_lst, error_lst, timeout)
        except select.error, err:
            Log.debug("Trivial error: " + err.message)
            if err.args[0] != errno.EINTR:
                raise
            else:
                return
        Log.debug("Selected [r]: " + str(readable_lst) + " [w]: " +
                  str(writable_lst) + " [e]: " + str(error_lst))

        if self.pipe_r in readable_lst:
            Log.debug("Read from pipe")
            os.read(self.pipe_r, 1024)
            readable_lst.remove(self.pipe_r)

        if self.sock_map is not None:
            for fd in readable_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                asyncore.read(obj)

            for fd in writable_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                asyncore.write(obj)

            for fd in error_lst:
                obj = self.sock_map.get(fd)
                if obj is None:
                    continue
                # pylint: disable=W0212
                asyncore._exception(obj)
Exemple #20
0
        #@nl
    for fd in r:
        #@        << asyncore.read(map.get(fd)) >>
        #@+node:EKR.20040517080250.42:<< asyncore.read(map.get(fd)) >>
        obj = map.get(fd)
        if obj is not None:
            asyncore.read(obj)
        #@nonl
        #@-node:EKR.20040517080250.42:<< asyncore.read(map.get(fd)) >>
        #@nl
    for fd in w:
        #@        << asyncore.write(map.get(fd)) >>
        #@+node:EKR.20040517080250.43:<< asyncore.write(map.get(fd)) >>
         obj = map.get(fd)
         if obj is not None:
            asyncore.write(obj)
        #@-node:EKR.20040517080250.43:<< asyncore.write(map.get(fd)) >>
        #@nl
    return len(r) > 0 or len(w) > 0
#@-node:EKR.20040517080250.40:poll
#@+node:EKR.20040517080250.44:loop
def loop(timeout=5.0, use_poll=0, map=None):
    """
    Override the loop function of asynchore.
    We poll only until there is not read or
    write request pending.
    """
    return poll(timeout)
#@nonl
#@-node:EKR.20040517080250.44:loop
#@+node:EKR.20040517080250.45:plugin_wrapper