EXTENDING THE HELLO WORLD PROGRAM

Frans Coenen

1. INTRODUCTION

The Hello World programme described previously consisted of a single process; this hardly warrents the use of a distributed approach!.




2. MESSAGE CLASS

Table 1 shows an extended version of the Message class described previously, and includes an additional variable field counter. In a distributed application such a variable is known as a shared variable. A shared variable is the simplest kind of distributed data structure we can create.

import net.jini.core.entry.*;

/* ----------------------------------------- */
/*                                           */
/*               MESSAGE CLASS               */
/*                                           */
/* ----------------------------------------- */

public class Message implements Entry {

    // Fields

    public String content;
    public Integer counter;

    // Constructors

    public Message() {
        }

    public Message(String content, int initVal) {
    	this.content = content;
	counter = new Integer(initVal);
	}

    // Methods

    public String toString() {
  	return(content + " read " + counter + " times");
	}
    
    public void increment() {
        counter = new Integer(counter.intValue() + 1);
	}
    }

Table 1: Message class (based on that contained in Freeman et al., 1999).




3. HelloWorld CLASS

Table 2 gives a revised version of the HelloWorld class described previously. The code creates a Message entry and then continualy reads the entry and outputs its fields. Note: that by running this piece of code on its own the output will always be:

Hello World read 0 times

as there is nothing that alters the counter.

/* ------------------------------------------ */
/*                                            */
/*                 HELLO WORLD 2              */
/*                                            */
/* ------------------------------------------ */

// JavaSpacesUtil package

import JavaSpacesUtils.SpaceAccessor;

// Jini core packages

import net.jini.core.lease.*;

// Jini extension package

import net.jini.space.JavaSpace;

public class HelloWorld2 {

    /* ------ METHODS ------ */
        
    /* MAIN */
    
    public static void main(String[] args) {
        System.out.println("START");
		 
        // try block
        try {
	    // Create Message object entry
	    System.out.println("Create Message entry");
	    Message msg = new Message("Hello World",0);
	    // Get JavaSpace     
	    SpaceAccessor  newSpaceAccessor = new 
	    		SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
				"JavaSpaces/JavaSpacesUtils/frans_space.prop");
	    System.out.println("Get JavaSpace");
	    JavaSpace space = newSpaceAccessor.getSpace();
	    // Send Message entry into space
	    space.write(msg,null,Lease.FOREVER);
	    // Create a template
	    Message template = new Message();

	    // Loop
	    for(;;) {
		// Read entry
		Message result = (Message) 
			    space.read(template,null,Long.MAX_VALUE);
                // Output, automatically calls toString method
                System.out.println(result);
		// Delay 1 second
		Thread.sleep(1000);
		}
	    }
	// Catch block
	catch(Exception e) {
	    e.printStackTrace();
	    }
        }
    }

Table 2: HelloWorld2 class

The process in Table 2 creates a Message entry and then continualy reads the entry and outputs its fields. We will now create a second process to upadte the counter (Section 4).




3. HelloWorldClient CLASS

The code in Table 3 describes a second process which take a Message object from the space (using a template), increments the counter and writes the revised Message object back into the space.

/* ----------------------------------------- */
/*                                           */
/*             HELLO WORLD CLIENT            */
/*                                           */
/* ----------------------------------------- */

// JavaSpacesUtil package

import JavaSpacesUtils.SpaceAccessor;

// Jini core packages

import net.jini.core.lease.*;

// Jini extension package

import net.jini.space.JavaSpace;

public class HelloWorldClient {

    /* ------ METHODS ------ */
        
    /* MAIN */
    
    public static void main(String[] args) {
        // try block
        try {
	    // Get JavaSpace     
	    SpaceAccessor  newSpaceAccessor = new 
	    		SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
				"JavaSpaces/JavaSpacesUtils/frans_space.prop");
	    System.out.println("Get JavaSpace");
	    JavaSpace space = newSpaceAccessor.getSpace();
	    // Create a template
	    Message template = new Message();

	    // Loop
	    for(;;) {
		// Take entry
		Message result = (Message) 
				space.take(template,null,Long.MAX_VALUE);
                // Increment counter
		result.increment();
                // Write back into space
		System.out.println("Increment counter " + result.counter + "+ 1");
		space.write(result,null,Lease.FOREVER);
		// Delay 1 second
		Thread.sleep(1000);
		}
	    }
	// Catch block
	catch(Exception e) {
	    e.printStackTrace();
	    }
	}
    }

Table 3: HelloWorldClient class




4. RUNNING THE EXAMPLE

]

Compile the above and make sure that all of the above are in the same directory together with:

  1. The policy.all security policy text file.
  2. The javaRun shell script.

Start up the HelloWorld2 application on one machine, and two clients on two other machimes. We should see some output of the form presented in Table 4, 5 and 6.

$ ./javaRun HelloWorld2
START
Create Message entry
jiniURL   = jini://linux10
spaceName = frans_space
Get JavaSpace
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 0 times
Hello World read 1 times
Hello World read 2 times
Hello World read 3 times
Hello World read 4 times
Hello World read 5 times
Hello World read 6 times
Hello World read 8 times
Hello World read 10 times
Hello World read 12 times
Hello World read 14 times
Hello World read 16 times
Hello World read 18 times
Hello World read 20 times
Hello World read 22 times
Hello World read 24 times
Hello World read 26 times
Hello World read 28 times
Hello World read 30 times
Hello World read 31 times
Hello World read 32 times
Hello World read 32 times

Table 4: HelloWorld master process

 
$ ./javaRun HelloWorldClient
jiniURL   = jini://linux10
spaceName = frans_space
Get JavaSpace
Increment counter 1+ 1
Increment counter 2+ 1
Increment counter 3+ 1
Increment counter 4+ 1
Increment counter 5+ 1
Increment counter 6+ 1
Increment counter 8+ 1
Increment counter 10+ 1
Increment counter 12+ 1
Increment counter 14+ 1
Increment counter 16+ 1
Increment counter 18+ 1
Increment counter 20+ 1
Increment counter 22+ 1
Increment counter 24+ 1
Increment counter 26+ 1
Increment counter 28+ 1
Increment counter 30+ 1
Increment counter 31+ 1
Increment counter 32+ 1

Table 5: HelloWorldClient Process 1

$ ./javaRun HelloWorldClient
jiniURL   = jini://linux10
spaceName = frans_space
Get JavaSpace
Increment counter 7+ 1
Increment counter 9+ 1
Increment counter 11+ 1
Increment counter 13+ 1
Increment counter 15+ 1
Increment counter 17+ 1
Increment counter 19+ 1
Increment counter 21+ 1
Increment counter 23+ 1
Increment counter 25+ 1
Increment counter 27+ 1
Increment counter 29+ 1

Table 6: HelloWorldClient process 2




REFERENCES

  1. Freeman, E., Hupfer, S. and Arnold, K. (1999). JavaSpacesTM Principles, Patterns and Practice. Addison-Wesley.



Created and maintained by Frans Coenen. Last updated 24 June 2002