6th Chapter 2 "Monkeyrunner Source Analysis" Monkey principle-Event Source-Event Source Overview-Get command string

Source: Internet
Author: User

As you can tell from the previous section, the command that Monkeyrunner sends to monkey is interactive in the form of strings, so the first step in event handling is, of course, to get the string command that Monkeyrunner sent over.

When the event source Monkeysourcenetwork is initialized, the constructor creates a serversocket to listen for links and data from the client, but this time the client does not actually implement the communication with the server because the ServerSocket is still in a blocked state. Since ServerSocket is created by Monkeysourcenetwork's constructor, what is the method of establishing communication? When does the real communication start to build?

Here we answer the first question, the establishment of communication is handled by Monkeysourcenetwork Private member method StartServer:

569   private void StartServer () 570     throws IOException571   {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   }
Code 6-2-1 monkeysourcenetwork-startserver

    • 572 line: By calling ServerSocket's accept method to establish a socket communication channel with the client, and assign the socket instance to Clientsocket, and then by manipulating the socket instance to interact with the client
    • 581 rows: Initializes a BufferedReader instance that reads data from Clientsocket and assigns it to the input member variable, and the next code only needs to invoke the ReadLine method of input to get a line of command string data
    • 583 rows: Initializes the PrintWriter instance to the Clientsocket printout and assigns it to the output member variable, and the subsequent code only needs to invoke output of print or println to send the data to the client

After answering the first question, let's look at the second question, when do we start building communications? In fact, this problem can now be replaced by: StartServer who triggered the call. is triggered from the Monkey class's loop execution event method Runmonkeycyles call Meventsource.getnextevent. The previous chapter has analyzed that the meventsource here is initialized to Monkeysourcenetwork, because Monkey is initiated by monkeyrunner the command "Monkey--port 12345" sent. So we go into Monkeysourcenetwork's Getnextevent method:

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 try697 {698 for (;;) 699 {monkeyevent queuedevent = This.commandQueue.getNextQueuedEvent (); 701 if (queuedevent! = nul L) 702 {703 return queuedevent;704} ... 709 if (Deferredreturn! = null) {710 LOG.D ("Monkeystub", "Waiting for Event"); 711 Monkeycomman         Dreturn ret = deferredreturn.waitforevent (); 712 Deferredreturn = null;713 Handlereturn (ret); 714 }715 716 String command = this.input.readLine () 717 if (command = = null) {718 LOG.D ("Monkeystu B "," Connection dropped. "); 719 720 721 Command = "Done", 722}723 724 if ("Done". Equals (command)) 725 {726 try {727 stopserver (); 728} catch (IOException e) {729 L OG.E ("Monkeystub", "Got IOException shutting down!", e); 730 return null;731}732 733 734 R Eturn 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 (ioexcept Ion e) 762 {763 LOG.E ("Monkeystub", "Exception:", e); 764}765}
Code 6-2-2 monkeysourcenetwork-getnextevent

    • 684-687 rows: If you have not established communication with the client before, then call the StartServer method to establish the communication
    • 691 Line: Save the communication status. The next time you call getnextevent to get a command string, you will determine whether the communication is established on line 684, and then use the existing communication instead of creating a new socket communication.
    • 698 rows: Enter an infinite loop until you get to an event location
    • 700-703 rows: If there are events in the command queue that are not processed, get an event from the command queue to return
    • Line 716: Get a command string sent by the Monkeyrunner client by invoking the ReadLine method of the input mentioned above
    • 724-728 Line: Determine if the command string sent over by Monkeyrunner is "done", close the socket communication with the client
    • 738-744 line: Determine if Monkeyrunner sent over the command string is "quit", the direct exit loop
    • 749-756 Line: Judge if the command string sent by Monkeyrunner is not the above two cases, and does not start with the "#" number, Call Monkeysourcenetwork's Translatecommand to perform the parsing of the command string down the translation process

Note: More articles please pay attention to the public number: Techgogogo or personal blog http://techgogogo.com. Of course, you are also very welcome to pick up directly (ZHUBAITIAN1). This article by Heaven Zhuhai Branch Rudder original. Reproduced please consciously, whether the complaint rights to look at the mood.





6th Chapter 2 "Monkeyrunner Source Analysis" Monkey principle-Event Source-Event Source Overview-Get command string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.