从上一节的描述可以知道,MonkeyRunner发送给Monkey的命令是以字符串的形式交互的,那么事件处理的第一步当然是先去获得MonkeyRunner发送过来的字串命令了。
在事件源MonkeySourceNetwork初始化的时候构造函数会创建一个ServerSocket来监听来自客户端的链接和数据,但这个时候客户端并不能真正实现和服务端通信,因为该ServerSocket尚处于阻塞状态。既然ServerSocket是MonkeySourceNetwork的构造函数创建的,那么建立通信的又是哪个方法呢?真正的通信又是什么时候开始建立呢?
这里我们先回答第一个问题,建立通信是由MonkeySourceNetwork的私有成员方法startServer来处理的:
569 private void startServer()
570 throws IOException
571 {
572 this.clientSocket = this.serverSocket.accept();
...
577 MonkeySourceNetworkViews.setup();
578
579 wake();
580
581 this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
582
583 this.output = new PrintWriter(this.clientSocket.getOutputStream(), true);
584 }代码6-2-1 MonkeySourceNetwork - startServer回答完第一个问题后,我们跟着看第二个问题,什么时候开始建立通信的?其实这个问题到了现在可以替换成:startServer是谁触发调用的。是从Monkey类的循环执行事件方法runMonkeyCyles调用mEventSource.getNextEvent开始触发的。上一章已经分析过这里的mEventSource是被初始化成MonkeySourceNetwork,因为Monkey是通过MonkeyRunner发送的命令”monkey --port 12345”来启动的。那么我们进入MonkeySourceNetwork的getNextEvent方法:
682 public MonkeyEvent getNextEvent()
683 {
684 if (!this.started) {
685 try {
686 startServer();
687 } catch (IOException e) {
688 Log.e("MonkeyStub", "Got IOException from server", e);
689 return null;
690 }
691 this.started = true;
692 }
...
696 try
697 {
698 for (;;)
699 {
700 MonkeyEvent queuedEvent = this.commandQueue.getNextQueuedEvent();
701 if (queuedEvent != null)
702 {
703 return queuedEvent;
704 }
...
709 if (deferredReturn != null) {
710 Log.d("MonkeyStub", "Waiting for event");
711 MonkeyCommandReturn ret = deferredReturn.waitForEvent();
712 deferredReturn = null;
713 handleReturn(ret);
714 }
715
716 String command = this.input.readLine();
717 if (command == null) {
718 Log.d("MonkeyStub", "Connection dropped.");
719
720
721 command = "done";
722 }
723
724 if ("done".equals(command))
725 {
726 try {
727 stopServer();
728 } catch (IOException e) {
729 Log.e("MonkeyStub", "Got IOException shutting down!", e);
730 return null;
731 }
732
733
734 return new MonkeyNoopEvent();
735 }
736
737
738 if ("quit".equals(command))
739 {
740 Log.d("MonkeyStub", "Quit requested");
741
742 returnOk();
743 return null;
744 }
...
748
749 if (!command.startsWith("#"))
750 {
...
755 translateCommand(command);
756 }
757 }
758
759 return null;
760 }
761 catch (IOException e)
762 {
763 Log.e("MonkeyStub", "Exception: ", e);
764 }
765 }代码6-2-2 MonkeySourceNetwork - getNextEvent注:更多文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然,也非常欢迎您直接微信(zhubaitian1)勾搭。本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。
第6章2节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-获取命令字串
原文:http://blog.csdn.net/zhubaitian/article/details/50212395