zoukankan      html  css  js  c++  java
  • Monkey源代码分析之事件源


    上一篇文章《Monkey源代码分析之执行流程》给出了monkey执行的整个流程。让我们有一个概貌,那么往后的文章我们会尝试进一步的阐述相关的一些知识点。

    这里先把整个monkey类的结构图给出来供大家參考。该图源自网上(我自己的backbook pro上没有安装OmniGraffle工具。55美金。不舍得,所以直接贴网上的)



    图中有几点须要注意下的:

    • MonkeyEventScript应该是MonkeySourceScript
    • MonkeyEventRandom应该是MonkeySourceRandom
    • 这里没有列出其它源。比方我们今天描写叙述的重点MonkeySourceNetwork,由于它不是由MonkeyEventQueque这个类维护的,但其维护的事件队列和MonkeyEventQueque一样都是继承于LinkedList的。所以大同小异

    本文我们重点是以处理来来自网络sokcet也就是monkeyrunner的命令为样例来阐述事件源是怎么处理的,其它的源大同小异。

    1. 事件队列维护者CommandQueque

    在開始之前我们须要先去了解几个基础类。这样子我们才方便分析。

    我们在获取了事件源之后,会把这些事件排队放入一个队列,然后其它地方就能够去把队列里面的事件取出来进一步进行处理了。

    那么这里我们先看下维护这个事件队列的对应代码:

        public static interface CommandQueue {
            /**
             * Enqueue an event to be returned later.  This allows a
             * command to return multiple events.  Commands using the
             * command queue still have to return a valid event from their
             * translateCommand method.  The returned command will be
             * executed before anything put into the queue.
             *
             * @param e the event to be enqueued.
             */
            public void enqueueEvent(MonkeyEvent e);
        };
    
        // Queue of Events to be processed.  This allows commands to push
        // multiple events into the queue to be processed.
        private static class CommandQueueImpl implements CommandQueue{
            private final Queue<MonkeyEvent> queuedEvents = new LinkedList<MonkeyEvent>();
    
            public void enqueueEvent(MonkeyEvent e) {
                queuedEvents.offer(e);
            }
    
            /**
             * Get the next queued event to excecute.
             *
             * @return the next event, or null if there aren't any more.
             */
            public MonkeyEvent getNextQueuedEvent() {
                return queuedEvents.poll();
            }
        };
    

    接口CommandQueue仅仅定义个了一个方法enqueueEvent,由实现类CommandQueueImpl来实现。而实现类维护了一个MonkeyEvent类型的由LinkedList实现的队列quequeEvents,然后实现了两个方法来分别往这个队列里面放和取事件。挺简单的实现,这里主要是要提醒大家queueEvents这个队列的重要性。这里要注意的是MonkeyEventScript和monkeyEventRandom这两个事件源维护队列的类略微有些不一样,用的是MonkeyEventQueue这个类。可是事实上这个类也是继承自上面描写叙述的LinkedList的,所以原理是一样的。

    最后创建和维护一个CommandQueueImple这个实现类的一个实例commandQueque来转被对里面的quequeEvents进行管理。

        private final CommandQueueImpl commandQueue = new CommandQueueImpl();


    2. 事件翻译员MonkeyCommand

    下一个我们须要了解的基础内部类就是MonkeCommand。从数据源过来的命令都是一串字符串。我们须要把它转换成相应的monkey事件并存入到我们上面提到的由CommandQueque维护的事件队列quequeEvents里面。首先我们看下MonkeyCommand这个接口:

        /**
         * Interface that MonkeyCommands must implement.
         */
        public interface MonkeyCommand {
            /**
             * Translate the command line into a sequence of
    
  • 相关阅读:
    MutationObserver 简单应用场景
    call apply bind sleep
    js 继承,Object.setPrototypeOf | Object.getPrototypeOf | Object.create class
    JSON.stringify
    javascript 与node的 event-loop
    js 不常用面试题 数组对象深度取值
    Oracle单表备份
    mybatis批量写法
    mybatis批量更新
    Python中if __name__ == '__main__':理解
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7244321.html
Copyright © 2011-2022 走看看