Hello World Source Code


Source code for Client, Host, TaskServer, TaskServerProxy, HelloworldTask, & HelloworldResult classes

ClientId

public class ClientId
{
    public Long id;

    public ClientId( Long id )
    {
        this.id = id;
    }
}

JobId

// JobId is relative to the client:  The ClientId:JobId should be unique; JobId by itself may not be.
public class JobId
{
    public Long id;

    public JobId( Long id )
    {
        this.id = id;
    }
}

Task

public class Task implements Entry
{
    public ClientId clientId;
    public JobId jobId;

    public Task( ClientId clientId, JobId jobId )
    {
        this.clientId = clientId;
        this.jobId = jobId;
    }

    public void execute( TaskServerProxy taskServer ) {}
}

HelloworldTask

package javelin.demos.Helloworld;
import javelin.*;
import java.net.*;

public class HelloworldTask extends Task
{
    public void execute( TaskServerProxy taskServer ) // result: host's domain name
    {
        String result = null;

        try
        {
            result = InetAddress.getLocalHost().getHostName();
            taskServer.putTask( new HelloworldResult( result ) );
        }
        catch (UnknownHostException exception)
        {
            System.err.println("Exception: " + exception.toString());
        }
        catch (RemoteException e)
        {
            System.err.println("Exception: " + exception.toString());
        }
    }
}

HelloworldResult

package javelin.demos.Helloworld;

public class HelloworldResult extends Task
{
    String result;

    public HelloworldResult( ClientId clientId, JobId jobId, String result )
    {
        super( clientId, jobId );
        this.result = result;
    }
}

TaskServerProxy

import javelin.*;

public class TaskServerProxy
{
    TaskServer taskServer;

    public TaskServerProxy()
    {
        // Find a TaskServer; get its remote reference.
        //     This may involve a Jini lookup, or some other method.
    }

    public Task getTask( Task template )
    {
        Task task;

        try
        {
            return task = taskServer.getTask( template );
        }
        catch ( RemoteException e )
        {
        }
    }
    public void putTask( Task task )
    {
        //store task in local cache

        //store task in (remote) TaskServer
        try
        {
            taskServer.putTask( task );
        }
        catch ( RemoteException e )
        {
        }
    }
}

TaskServer

import javelin.*;

public class TaskServer
{
    JavaSpace javaSpace;

    public TaskServer()
    {
        javaSpace = new JavaSpace();
    }

    public Task getTask( Task template )
    {
        try
        {
            return Task = (Task) javaSpace.take( template, ... );
        }
        catch ( RemoteException e )
        {
        }
    }
    public void putTask( Task task )
    {
        try
        {
            javaSpace.write( task, ... );
        }
        catch ( RemoteException e )
        {
        }
    }
}

Client

package javelin.demos.Helloworld;
import javelin.*;

public class Client
{
    JobId jobId;
    ClientId clientId;
    TaskServerProxy taskServer;
    HelloworldResult result;

    public Client()
    {
        clientId = new ClientId( 7 ); // Something more sophisticated to come.
        jobId = new JobId( 3 );

        HelloworldResult resultTemplate = new HelloworldResult();
        try
        {
            taskServer = new TaskServerProxy();
            for (int i = 0; i < 10; i++)
                taskServer.putTask( new HelloWorldTask( clientId, jobId ) );
            for (int i = 0; i < 10; i++)
            {
                result = taskServer.getTask( resultTemplate );
                System.out.println("Result: " + result.result);
            }
        }
        catch (Exception e)
        {
            System.out.println("Host: TaskServerProxy construction: failed.");
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static void main(String[] args)
    {
          Client client = new Client();
    }
}

Host

import javelin.*;

public class Host implements Runnable
{
    TaskServerProxy taskServer;
    Task task, result;
    Transaction txn;

    public Host()
    {
        try
        {
            taskServer = new TaskServerProxy();
        }
        catch (Exception e)
        {
            System.out.println("Host: TaskServerProxy construction: failed.");
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static void main(String[] args)
    {
          Host host = new Host();
    }

    public void run()
    {
        while (true)
        {
            txn = getTransaction();
            if ( txn == null )
                throw new RuntimeException( "Host: Can't obtain a transaction.");
            try
            {
                task = taskServer.getTask( null ); // template _really_ should only allow non-Result type tasks.
                result = task.execute( taskServer ); //reference to proxy, so task can putTask & putArg
                if ( result != null )
                    taskServer.putResult( result );
                txn.commit();
            }
            catch (RemoteException e )
            {
                e.printStackTrace();
            }
                    catch (TransactionException e )
            {
                e.printStackTrace();
            }
            catch (InterupptedException e )
            {
                try
                {
                    txn.abort();
                }
                catch (Exception ex)
                {
                    //RemoteException of BadTransactionException
                    //lease expiration takes care of transaction
                }
                System.out.println("Task cancelled.");
            }
        }
    }
}