Changes between Version 32 and Version 33 of ProtocolOne
- Timestamp:
- Jun 29, 2011 1:52:21 PM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ProtocolOne
v32 v33 45 45 The user may choose to execute commands by either explicitly instantiating the command objects or calling a relevant function. Internally, all commands are represented as command-specific objects. These objects handle writing the specified command to the server, automatically update data fields with any relevant server data response, and maintain auxiliary data related to the command, such as command [[exitcode]] and runtime. To execute a command explicitly using the object, simply call the member function {{{run()}}}. Function calls automatically instantiate, run, and return the relevant object. 46 46 It is important to note that all Commands are implemented as [http://download.oracle.com/javase/1.4.2/docs/api/java/util/TimerTask.html TimerTasks], so a user may easily implement an application involving high-level scheduling or planning using [http://download.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html Timers] (this is actually how the periodic execution is implemented). 47 It is recommended that users new to Quagents use the command functions, as these provide a more intuitive command interface and also allow the client to record any results in its command history (although this may be moved into the Command class to ensure identical functionality between both methods of execution). 47 It is recommended that users new to Quagents use the command functions, as these provide a more intuitive command interface and also allow the client to record any results in its command history (although this may be moved into the Command class to ensure identical functionality between both methods of execution). Command objects may be reused an arbitrary number of times, to reexecute the same command. 48 48 49 49 ''Example'' 50 The following two code blocks are identical.50 The following two code blocks are functionally identical. 51 51 ''Functional'' 52 52 {{{ … … 55 55 ''Object-Oriented'' 56 56 {{{ 57 FireWeapon fw = new FireWeapon( );57 FireWeapon fw = new FireWeapon(2, client.qid, client.getWriter()); 58 58 fw.run(); 59 59 }}} … … 61 61 62 62 = Retrieving Data = 63 Client/Server data response in Quagents 3 is asynchronous. Thus, the object-oriented and functional methods of execution do not directly return related data. Instead, the user gets a reference to the related command object. This object automatically updates data specific to itself as it is read from the socket. The user must poll this object using object query functions to gain access to data. The following example illustrates thisinteraction.63 Client/Server data response in Quagents 3 is asynchronous. Thus, the object-oriented and functional methods of execution do not directly return related data. Instead, the user gets a reference to the related command object. This object automatically updates data specific to itself as it is read from the socket. The user must poll this object using object query functions to gain access to data. '''It is important to note that these object poll functions block for a response from the server.''' The following example illustrates the interaction. 64 64 65 The following is an example of a complete Quagents session: 65 ''Example'' 66 The following code blocks are functionally identical. The functionality of the code is to access the Quagent's current health. 66 67 {{{ 67 Client quagent = new Client("127.0.0.1"); 68 69 //begin initial phase 70 quagent.name("Billy"); 71 //end initial phase 72 quagent.ready(); 73 //begin live phase 74 quagent. 68 client.currentHealth().getCurrentHealth(); 69 }}} 70 {{{ 71 CurrentHealth ch = new CurrentHealth(1, client.qid, client.getWriter()); 72 ch.getCurrentHealth(); 73 }}} 74 {{{ 75 CurrentHealth ch = client.currentHealth(); 76 ch.getCurrentHealth(); 75 77 }}} 76 78 77 Note that using multiple quagents is as simple as creating a new Client object, and following the same steps as above for each. 79 = Periodic Commands = 80 The clientside structure has built-in support for periodic commands. Simply add an integer parameter to the end of any command function or constructor and the client will automatically execute the command systematically using the integer as the period. These commands are useful when it is desired 78 81 79 = Class structure = 82 ''Example'' 83 The following illustrates the usage of a periodic command. 84 {{{ 85 CurrentArmor updatedarmor = client.currentArmor(1000); 86 87 //arbitrary code 88 89 //now, we can call this whenever we want the Quagent's current armor, 90 from the nearest second. 91 updatedarmor.getCurrentArmor(); 92 }}} 93 94 = Multiple Quagents 95 Using multiple quagents is as simple as creating multiple Client objects, and following the same steps as above for each. 96 97 ''Example'' 98 {{{ 99 Client client = new Client("127.0.0.1"); 100 Client client2 = new Client("127.0.0.1"); 101 Client client3 = new Client("127.0.0.1"); 102 client.name("Bo"); 103 client2.name("Luke"); 104 client3.name("Daisy"); 105 client.ready(); 106 client2.ready(); 107 client2.moveForever(); 108 client2.followEntity(client.qid); 109 client3.ready(); 110 client3.switchWeapon(weapon.SHOTGUN); 111 client3.say("Y'all get back here!"); 112 }}} 113 80 114 81 115 = Example behaviors =