|
Sofar the attributes for all our entries have been made up of primitive types described as objects. On this page we will create an entry that includes an array of integers as one of its fields.
The code presented in Table 1 defines tthe entry.
import net.jini.core.entry.*; /* ---------------------------------------------- */ /* */ /* ARRAY ENTRY CLASS */ /* */ /* ---------------------------------------------- */ public class ArrayEntry implements Entry { /* --- FIELDS --- */ public String name; // ArrayName public Integer[] array; /* --- CONSTRUCTORS --- */ public ArrayEntry() { } public ArrayEntry(String name, int[] arrayData) { // entru name this.name = name; // Dimension array array = new Integer[arrayData.length]; // Loop for (int index=0;index < arrayData.length;index++) { array[index] = new Integer(arrayData[index]); } } } |
Table 1: ArrayEntry entry class
Tables 2 and 3 show the code for a pair of Master and Client applications respectively.
/* ----------------------------------------------------------- */ /* */ /* ARRAY ENTRY MASTER 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 ArrayEntryMasterApp { /* ------ 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 createArrayEntry("data1",5); createArrayEntry("data2",10); } // Catch block catch(Exception e) { e.printStackTrace(); } // End System.out.println("END"); System.exit(0); } /* CREATE ARRAY ENTRY */ private static void createArrayEntry(String name, int length) throws TransactionException,RemoteException { int[] dataArray = new int[length]; // Create data elements for (int index=0;index < length;index++) { System.out.println("Create array entry element" + index + " = " + (index*10)); dataArray[index] = index*10; } ArrayEntry newArrayEntry = new ArrayEntry(name,dataArray); space.write(newArrayEntry,null,Lease.FOREVER); } } |
Table 2: ArrayEntryMasterApp application class
/* ------------------------------------------------------- */ /* */ /* ARRAY ENTRY CLIENT 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 ArrayEntryClientApp { /* ------ FIELDS ------ */ 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 readArrayEntry(); readArrayEntry(); } // Catch block catch(Exception e) { e.printStackTrace(); } // End System.out.println("END"); System.exit(0); } /* READ ARRAY ENTRY */ public static void readArrayEntry() throws UnusableEntryException, TransactionException, InterruptedException, RemoteException { // try block try { // Create a template ArrayEntry template = new ArrayEntry(); // Get task bag item ArrayEntry result = (ArrayEntry) space.take(template,null, Long.MAX_VALUE); // output System.out.println("ArrayEntry = " + result.name); for(int index=0;index < result.array.length;index++) { System.out.println("(" + index + ") = " + result.array[index]); } } // Catch block catch(Exception e) { e.printStackTrace(); } } } |
Table 3: ArrayEntryClientApp application class
Tables 4 and 5 show some example output for the master and client processes rerspectively.
START jiniURL = jini://linux10 spaceName = frans_space Create array entry element0 = 0 Create array entry element1 = 10 Create array entry element2 = 20 Create array entry element3 = 30 Create array entry element4 = 40 Create array entry element0 = 0 Create array entry element1 = 10 Create array entry element2 = 20 Create array entry element3 = 30 Create array entry element4 = 40 Create array entry element5 = 50 Create array entry element6 = 60 Create array entry element7 = 70 Create array entry element8 = 80 Create array entry element9 = 90 END |
Table 4: Sample output form master process
START jiniURL = jini://linux10 spaceName = frans_space ArrayEntry = data1 (0) = 0 (1) = 10 (2) = 20 (3) = 30 (4) = 40 ArrayEntry = data2 (0) = 0 (1) = 10 (2) = 20 (3) = 30 (4) = 40 (5) = 50 (6) = 60 (7) = 70 (8) = 80 (9) = 90 END |
Table 5: Sample output form client process