Here is what the "Hello World" application will do:
After the Hello World example is launched on Machine A, it will send a task to each GT node in Machine A's view. After GT Kernel transports the task to the remote GT node, the task is then run and prints on the remote GT Console: "I'm a task from Machine A. Hello World!". The task will then return an "OK" message to Machine A. On Machine A's GT Console, it will print a message when each task is returned: "Task X is returned from Machine XX with result message: OK".
Now, let's start to develop "Hello World" application.
The GT class path should be automatically set up by using GT installer (install.class). In this tutorial, we assume that we are using Windows platform, and we have installed GT in this directory: C:\P2P\GT1_2_2\. Then, the CLASSPATH environment variable should contain the following: .;C:\P2P\GT1_2_2\classes\gt.jar;C:\P2P\GT1_2_2\classes; as shown in Figure 1.
Figure 1. Using "set" command to see if GT class path is properly set in the CLASSPATH environment variable. The hightlighted field contains the GT class path.
Note: It is also assumed that you have already installed JDK 1.3+ and JRE 1.3+ on your machine. Figure 2 shows that the machine is installed with Java version 1.3.1. If Java is not installed on your machine, please go to www.javasoft.com to download and install JDK or JRE 1.3+.
Figure 2. Make sure JDK or JRE1.3+ is properly installed on your machine.
Compile the source code with the following command as shown in Figure 3:
Figure 3. Command to compile HelloApp.java and HelloTask.java
This command will create C:\P2P\GT1_2_2\classes\AHelloWorld\directory and generate HelloApp.class and HelloTask.class files in that directory.
The serlversionUID needs to be generated for all classes that derive from GTSubTask class. Use the command as shown in Figure 4 to generate the serlversionUID and then incorporate it in HelloTask.java.
Figure 4. Using serialver.exe tool to generate serialVersionUID.
In this example, GT home direcotry is: C:\P2P\GT1_2_2\classes. Since the command javac -d C:\P2P\GT1_2_2\classes HelloApp.java HelloTask.java already deploy the class files to GT home, we don't need to do anything else. In case you did not deploy the class files to GT home directory, you can always manually copy them to GT home directory. Please note that all GT applications must reside under the GT home directory, because these classes will need to be uploaded to remote machines for execution.
Before you run Hello World application, GT Kernel must be started. If you installed GT using GT installer, you can start GT as shown in Figure 5. Figure 6 shows the GT Console after GT is started. It also shows that on Machine chris' GT Federation View, there are 3 machines available.
Figure 5. Shows how to start GT from Windows machine.
Figure 6. By typing "sh" command in GT Console, three machines are in the GT federation view of machine 192.168.1.2.
After GT is started, you can start Hello World application using the following command as shown in Figure 7:
Figure 7. Shows the command to start Hello World application. The results are also displayed.
Figure 8, 9, 10 shows the GT Console output when the Hello World application is run. On each machine's GT Console, Hello World application will send a HelloTask and it prints a message to the Console:
Figure 8. GT Console window on machine sony. It shows the number of GT machines on the network, and a message printed by Hello World application's HelloTask.
Figure 9. GT Console window on machine jasonpc. It shows the number of GT machines on the network, and a message printed by Hello World application's HelloTask.
Figure 10. GT Console window on machine chris. "chris" is the machine that originally started the Hello World application. So it shows some GT console messages, as well as a message printed by Hello World application's HelloTask.
package AHelloWorld; import java.util.*; import gt.app.*; /** * All GT application classes extend from gt.app.GTApplication class. * HelloApp will do the following: * 1. create tasks * 2. submit tasks to remote machines * 3. get results for each task * 4. deregister from GT kernel * * The task will print out "Hello World" message, and return * an "OK" message as a result to the originating machine. */ public class HelloApp extends GTApplication { // an array of tasks to be sent to remote machines private ArrayList m_SubTaskList = new ArrayList(); // A machine's view of GT network is called the machine's "GT Federation View". private Vector m_GTFedList = null; private ArrayList m_SubpidList = new ArrayList(); private int m_nSize = 0; // the number of GT nodes in GT federation view. public HelloApp() throws Exception { // Cosntructor must call super() to initialize base class. // super() also registers to GT Kernel super(); // call GTServer API to get a list of machines in GT Federation. m_GTFedList = super.m_GTServerAppIF.getGTFedList(); m_nSize = m_GTFedList.size(); } /** * create a HelloTask for each machine in GT Federation. */ public void createTasks() { for ( int i = 0; i < m_nSize; i++ ) m_SubTaskList.add(new HelloTask(getClass().getName(), super.m_HostIP, super.m_Pid, super.GT_COMPUTE_BOUND, "Hello World!")); } /** * submit one task to each machine in the GT Federation */ public void submitTasks() throws Exception { GTHelperNode hNode = null; String subpid = null; int n = 0; for (Iterator i = m_GTFedList.iterator(); i.hasNext();) { hNode = (GTHelperNode)i.next(); // submit task[n] into a remote machine subpid = super.m_GTServerAppIF.putSubTask(hNode.getIPAddress(), (HelloTask)m_SubTaskList.get(n)); // remember all the subpids for all the tasks m_SubpidList.add(subpid); n++; } } /** * Keep polling GT Server every 100 ms until all results are returned. */ public void getResults() throws Exception { String subpid = null; GTResult result = null; boolean allDone = false; int n = 0; while ( n < m_nSize ) { for (Iterator i = m_SubpidList.iterator(); i.hasNext();) { subpid = (String)i.next(); if ( ( result = super.m_GTServerAppIF.getResult(subpid) ) != null ) { System.out.println("Task " + result.getSubpid() + " is returned from Machine " + result.getHelperInetAddress() + " with result message: " + result.getResult()); n++; } } // sleep 100 ms and then check again to see if the result is ready java.lang.Thread.sleep(100); } } /** * All GT applications must deregister from GT Kernel when done. */ public void deregister() throws Exception { super.m_GTServerAppIF.deregister(super.m_Pid); } public static void main(String[] args) { try { HelloApp app = new HelloApp(); app.createTasks(); app.submitTasks(); app.getResults(); app.deregister(); } catch (Exception e) { e.printStackTrace(); } } }
package AHelloWorld; import java.net.*; /** * * HelloTask is a subtask that is to be transmitted to remote machines * for execution. It prints out a "Hello World" message, and then gone. * * - All subtasks must derive from gt.app.GTSubTask. * - All subtasks must have a serialVersionUID that can be generated * through serialver.exe tool of JDK. * */ public class HelloTask extends gt.app.GTSubTask { static final long serialVersionUID = -3900668746090171404L; String m_WorkData; public HelloTask() { // super() must seem to be called here due to // JDK serialization Bug Id 4186885 super(); } public HelloTask(String progName, InetAddress inetAddress, String pid, int type, String workData) { super(progName, inetAddress, pid, type); m_WorkData = workData; } public void doWork() { String s = "I'm task "+super.getSubpid()+ " from Machine "+super.getIPAddress()+". "+m_WorkData+"."; System.out.println(s); } /** * This piece of code will actually be executed in remote machines. * It overrides the base class run() method. * A task's real work is done in this method. */ public void run() { doWork(); try { super.m_GTResult.setResult("OK"); super.m_ResultMgrIF.putResult(super.m_Pid, super.m_Subpid, super.m_nType, super.m_GTResult); } catch(Exception ex) { System.out.println("***Can not put result back to remote parent ResultMgr RMI Server for subtask: "+ this.toString()+"|"+m_WorkData+"\nException="+ex); } } }