|
Distributed data structures are similar to their standard counter parts except that the elements comprise entries. The simplest distributed data structure we can create is the shared variable (use of such a variable was described in the extended "Hello World" program.
A distributed array comprises a sequence of entries each describing a shared variable. Because the elements are entries they can be accessed independently, i.e. we do not need to take the entire array out of the space to change an element, but only the element in question.
Table 1 shows an appropriatly defined distributed integer array entry class. The entry comprises three fields:
import net.jini.core.entry.*; /* ------------------------------------------------ */ /* */ /* ARRAY ELEMENT CLASS */ /* */ /* ------------------------------------------------ */ public class Element implements Entry { /* --- FIELDS --- */ public String name; public Integer index; public Integer value; /* --- CONSTRUCTORS --- */ public Element() { } public Element(String name) { this.name = name; } public Element(String name, int index, int startValue) { this.name = name; this.index = new Integer(index); this.value = new Integer(startValue); } } |
Table 1: Element entry class (based on that contained in Freeman et al., 1999).
Traditionally we also have some meta knoqwledge to hand describing things such as the length of the array. The entry class in Table 2 defines an entry that describes meta knowledge about distributed arrays.
import net.jini.core.entry.*; /* -------------------------------------------- */ /* */ /* META DATA CLASS */ /* */ /* -------------------------------------------- */ /* Class to store meta information about an array. */ public class ArrayMetaData implements Entry { /* --- FIELDS --- */ public String name; public Integer length; /* --- CONSTRUCTORS --- */ public ArrayMetaData() { } public ArrayMetaData(String name) { this.name = name; } public ArrayMetaData(String name, int len) { this.name = name; this.length = new Integer(len); } } |
Table 2: ArrayMetaData entry classes
The code presented in Table 3 creates two distributed arrays, one called data1 and one called data2 of lengths 5 and 10 respectively. Some output is presented in Table 4.
/* ---------------------------------------------------------- */ /* */ /* DISTRIBUTED ARRAY APPLICATION */ /* */ /* ---------------------------------------------------------- */ // 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 DistribArrayApp { /* ------ FIELDS ------ */ public static final int ARRAY_LENGTH = 10; public static final String ARRAY_NAME = "data_array"; public static JavaSpace space; /* ------ METHODS ------ */ /* MAIN */ public static void main(String[] args) { System.out.println("START"); // try block try { // Get JavaSpace SpaceAccessor newSpaceAccessor = new SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" + "JavaSpaces/JavaSpacesUtils/frans_space.prop"); space = newSpaceAccessor.getSpace(); // Create distributed array objects and write into space createArray("data1",5); createArray("data2",10); // Read distributed arrays readArray("data1"); readArray("data2"); } // Catch block catch(Exception e) { e.printStackTrace(); } // End System.out.println("END"); System.exit(0); } /* CREATE ARRAY */ private static void createArray(String name, int length) throws TransactionException,RemoteException { // Create distributed array object entry and write into space System.out.print("Create ArrayMetaData entry: "); ArrayMetaData newMetaData = new ArrayMetaData(name,length); System.out.println("name = " + newMetaData.name + ", length = " + newMetaData.length); space.write(newMetaData,null,Lease.FOREVER); // Create data elements for (int index=0;index < length;index++) { System.out.println("Create Element entry " + index); Element newElement = new Element(name,index,index); space.write(newElement,null,Lease.FOREVER); } } /* READ ARRAY */ public static void readArray(String name) throws UnusableEntryException, TransactionException, InterruptedException, RemoteException { System.out.println("Read array " + name); // Get array length ArrayMetaData template1 = new ArrayMetaData(name); ArrayMetaData result1 = (ArrayMetaData) space.read(template1,null, Long.MAX_VALUE); // Read elements Element template2 = new Element(name); for (int index=0;index < result1.length.intValue();index++) { template2.index = new Integer(index); Element result2 = (Element) space.read(template2,null, Long.MAX_VALUE); System.out.println("Element (" + index + ") = " + result2.value); } } } |
Table 3: Distributed array application
$ ./javaRun DistribArrayApp START jiniURL = jini://linux10 spaceName = frans_space Create ArrayMetaData entry: name = data1, length = 5 Create Element entry 0 Create Element entry 1 Create Element entry 2 Create Element entry 3 Create Element entry 4 Create ArrayMetaData entry: name = data2, length = 10 Create Element entry 0 Create Element entry 1 Create Element entry 2 Create Element entry 3 Create Element entry 4 Create Element entry 5 Create Element entry 6 Create Element entry 7 Create Element entry 8 Create Element entry 9 Read array data1 Element (0) = 0 Element (1) = 1 Element (2) = 2 Element (3) = 3 Element (4) = 4 Read array data2 Element (0) = 0 Element (1) = 1 Element (2) = 2 Element (3) = 3 Element (4) = 4 Element (5) = 5 Element (6) = 6 Element (7) = 7 Element (8) = 8 Element (9) = 9 END |
Table 4: Result if running the code presented in Table 3