BAGS

Frans Coenen

1. INTRODUCTION

A bag is an unordered collection of objects of the same type (or some subtype of the same type). Table 1 shows an entry that can be used the describe a bag of items.

import net.jini.core.entry.*;

/* ---------------------------------------------- */
/*                                                */
/*                BAG ELEMENT CLASS               */
/*                                                */
/* ---------------------------------------------- */

public class BagEntry implements Entry {

    // Fields

    public String name;

    // Constructors

    public BagEntry() {
        }

    public BagEntry(String name) {
    	this.name      = name;
	}
    }

Table 1: BagEntry entry class

A common method of working is to have a task bag containing tasks to be done and a seconf bag, the results bag containing completed tasks. Two appropriate entries are shown in Tables 2 and 3.

    
import net.jini.core.entry.*;

/* ------------------------------------------- */
/*                                             */
/*                TASK BAG CLASS               */
/*                                             */
/* ------------------------------------------- */

public class TaskBag extends BagEntry {

    public TaskBag() {
        }

    public TaskBag(String name) {
        super(name);
        }
    }

Table 2: TaskBag entry class

import net.jini.core.entry.*;

/* --------------------------------------------- */
/*                                               */
/*                RESULT BAG CLASS               */
/*                                               */
/* --------------------------------------------- */

public class ResultBag extends BagEntry {
    public String colour;

    public ResultBag() {
        }

    public ResultBag(String name, String colour) {
    	super(name);
	this.colour = colour;
	}
    }

Table 3: ResultBag entry class

Table 4 shows an example application class that places items into a bag be assigned a colour. Table 5 shows another application that selects an item from the bag, assigns a colour and returns it int the space.

/* ----------------------------------------- */
/*                                           */
/*               TASK BAG CLIENT             */
/*                                           */
/* ----------------------------------------- */

// JavaSpacesUtil package

import JavaSpacesUtils.SpaceAccessor;

// Jini core packages

import net.jini.core.lease.*;
import net.jini.core.entry.*;
import net.jini.core.transaction.*;

// RMI packages

import java.rmi.*;

// Jini extension package

import net.jini.space.JavaSpace;

public class TaskBagClient {

    /* ------ FIELDS ------ */

    public static JavaSpace space;

    /* ------ METHODS ------ */

    /* MAIN */

    public static void main(String[] args) {
        String name;
	
	// Get JavaSpace     
	    
	SpaceAccessor newSpaceAccessor = new 
	    		SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
				"JavaSpaces/JavaSpacesUtils/frans_space.prop");
	space = newSpaceAccessor.getSpace();

        // Loop
	for(int index=0;index<10;index++) {
	    // Create entry
	    name = "bagItem" + index;
	    // try block
     	    try {
     	        // Create bag item
	        TaskBag taskBagItem = new TaskBag(name);
        	// Send bag item into space
	        space.write(taskBagItem,null,Lease.FOREVER);
	        System.out.println(taskBagItem.name + " written to taskBag");
		// Delay 3 second
		Thread.sleep(3000);
		}
	    // Catch block
	    catch(Exception e) {
	        e.printStackTrace();
	        }
	    }
	
	// End
	System.exit(0);
	}    
    }

Table 4: TaskBagClient class

/* ------------------------------------------- */
/*                                             */
/*               RESULT BAG CLIENT             */
/*                                             */
/* ------------------------------------------- */

// JavaSpacesUtil package

import JavaSpacesUtils.SpaceAccessor;

// Jini core packages

import net.jini.core.lease.*;
import net.jini.core.entry.*;
import net.jini.core.transaction.*;

// RMI packages

import java.rmi.*;

// Jini extension package

import net.jini.space.JavaSpace;

public class ResultBagClient {

    /* ------ FIELDS ------ */

    public static JavaSpace space;

    /* ------ METHODS ------ */

    /* MAIN */

    public static void main(String[] args) throws InterruptedException {
        String name;
	
	// Get JavaSpace     
	    
	SpaceAccessor newSpaceAccessor = new 
	    		SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
				"JavaSpaces/JavaSpacesUtils/frans_space.prop");
	space = newSpaceAccessor.getSpace();

        // Task loop
	
        for (int index=0;index<10;index++) {
            name = getBagItem();
            assignToResultBag(name,index);
            // Delay 1 second
            Thread.sleep(1000);
            }
	
	// End
	System.exit(0);
        }

    /* GET BAG ITEM FROM TASK BAG */

    public static String getBagItem() {
        // try block
        try {
	    // Create a template
	    TaskBag template = new TaskBag();
	    // Get task bag item
	    TaskBag taskBagItem = (TaskBag) space.take(template,null,Long.MAX_VALUE);
            // Return
            System.out.println(taskBagItem.name + " taken from task bag");
	    return(taskBagItem.name);
	    }
	// Catch block
	catch(Exception e) {
	    e.printStackTrace();
	    }
        
	// End
	return(null);
	}
	
    /* ASSIGN TO RESULT BAG */

    public static void assignToResultBag(String name, int num) {
        ResultBag resultBagItem;
	
	// try block
        try {
            switch(num) {
                case 0:
                    // Create blue result bag item
	            resultBagItem = new ResultBag(name,"blue");
	            break;
	        case 1:
	            // Create red result bag item
	            resultBagItem = new ResultBag(name,"red");
	            break;
	        default:
	            // Create yellow result bag item
	            resultBagItem = new ResultBag(name,"yellow");
	    	}
	    // Send Message entry into space
	    space.write(resultBagItem,null,Lease.FOREVER);	
            System.out.println(resultBagItem.name + " (" + resultBagItem.colour +
        			" written to result bag");
           }
	// Catch block
	catch(Exception e) {
	    e.printStackTrace();
	    }
	}
    }    

Table 5: ResultBagClient class

This illustrates a common paradigm where where an application "looking for a service" (having a colour assigned to it) places an entry into a task bag; while an application "looking to provide the service" (assign a colour) removes entries from the task bag, performs the service and then returns the entries to a result bag.




2. RELPLICATED WORKER COMPUTING

Replicated worker computing (or master worker computing) is an approach to parallelising processes where the same process in carried out repeatedly in a loop. In RWC a master process divides the "work" into a number of tasks placed in a task bag. Other worker processes take the tasks, work on them, and place them into a result bag.