Presentation is loading. Please wait.

Presentation is loading. Please wait.

IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.

Similar presentations


Presentation on theme: "IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user."— Presentation transcript:

1 IBM TSpaces Lab 2 Customizing tuples and fields

2 Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user defined objects in tuplespace

3 Blocking commands When an application issues a WaitToRead or WaitToTake call, and the data is not yet there on the server, the application blocks on the call until an answer is returned. When a tuple arrives on the server that matches the Read or Take query, it is sent to the client and the application resumes. As an example, assume that in a client running on machine1, you had the following code: try { TupleSpace ts = new TupleSpace("Example",host); Tuple answer = ts.waitToTake("3",new Field(String.class));... } catch(TupleSpaceException tse) { System.out.println("TupleSpace Exception: " + tse.getMessage()); }

4 Blocking commands (2) And assume that in a client running on machine2, you had the following code: try { TupleSpace ts = new TupleSpace("Example",host); ts.write("1","Data1"); Thread.sleep(5000); ts.write("2","Data2"); Thread.sleep(5000); ts.write("3","Data3"); } catch(TupleSpaceException tse) { System.out.println("TupleSpace Exception: " + tse.getMessage()); } Assuming that the applications started at the same time, the application on machine1 would issue the waitToTake and then be blocked. However, about 10 seconds later as soon as the

5 Blocking commands (3) matching Tuple arrived at the server, the server would send it to machine1 which would unblock and process the matching Tuple. The default for blocking commands is to wait forever. However one can specify an optional timeout parameter to specify that if the operation has not be satisfied when the time limit is exceeded, then the waiting operation should be terminated and the null should be returned. So if we wanted to only wait for 60 seconds, then the code above would now look like the following:

6 Blocking commands (4) try { TupleSpace ts = new TupleSpace("Example",host); Tuple template = new Tuple( "3",new Field(String.class)); Tuple answer = ts.waitToTake(template,60*1000)); if (answer == null) { System.out.println("Operation timed out");... } } catch(TupleSpaceException tse) { System.out.println("TupleSpace Exception: " + tse.getMessage()); }

7 Tuple Expiration Normally when a client writes a tuple to a space it will stay in the space until some client does a take or delete for the Tuple. However one can specify an expiration time value for the Tuple so that after the time expires, the Tuple will be automatically deleted from the space. Tuple myTuple = new Tuple("Hello","World"); myTuple.setExpire(5*60*1000); // expire in 5 minutes ts.write(myTuple); Tuple result = ts.read(myTuple); //will return myTuple Thread.sleep(6*60*1000); result = ts.read(myTuple); // will return null

8 Extending Tuples The Tuple class is declared final and cannot be subclassed. An alternative to Tuple is the SubclassableTuple class which can be subclassed (extended). Advantages of defining your own tuples: – You can make constructors for easiest creation of templates template=new MyTuple(key) instead of template= new Tuple(key,new Field(String.class)) – You can create methods for easy retrieval of tuple data data=mytuple.getData() instead of data= (String) tuple.getField(1).getValue

9 Extending Tuples (2) Advantages of defining your own tuples (cont): – You can include your own validity checks on the fields of your tuple so that applications cannot write invalid tuples i.e. check that the “age” field of your tuple is always a positive number Disadvantages of defining your own tuples: – The same version of our tuple definition ( MyTuple.class ) must be available to both the clients using this kind of tuples and the TSpace Server.

10 Extending Tuples (3) class MyTuple extends SubclassableTuple { public MyTuple(String key,String data) { super(key,data); } public MyTuple(String key) { // build a template if only one operand super(key,new Field(String.class)); } public String getData() throws TupleSpaceException { return (String)this.getField(1).getValue(); }

11 Reading/writing user defined objects Beside the basic Java types like String and Integer, any Java object can be placed in a Tuple and written to TSpaces. However, these objects must obey to the following restrictions: – The object must be Serializable (implement the Serializable intarface). – An equivalent class file for the object must be available to both the clients and the TSpaces server. (As we will see later, this restriction can be avoided).

12 Reading/writing user defined objects(2) UserObj obj = new UserObj("User Object 1"); Field f = new Field(obj); ts.write("Key1",f); obj = new UserObj("User Object 2"); f.setValue(obj); ts.write("Key2",f); Tuple template = new Tuple("Key2",new Field(UserObj.class)); Tuple mytuple = ts.take(template); UserObj objreturned = (UserObj) mytuple.getField(1).getValue(); System.out.println("Object content is:“ + objreturned.getData());

13 Reading/writing user defined objects(3) Defining the user object: class UserObj implements Serializable { String userdata; public String getData() { return userdata; } public boolean equals( Object other ) { if ( (other == null)|| !(other instanceof UserObj) ) return false; if ( userdata == null ) return ( ((UserObj)other).getData() == null); return userdata.equals(((UserObj)other).getData()); } // end equals() } //end class

14 Reading/writing user defined objects(4) The things to note in this definition are: The class is declared to implement Serializable. An equals() method has been added. This is required if you plan to use the object as a search field in a template. The equals() method will override the Object.equals(Object other) method. This method will be invoked on the server to match a supplied template to the instances of UserObj that are in the TupleSpace. You should be careful to handle all the possible cases such as null values.

15 Reading/writing user defined objects(5) Using FieldPS In many cases, it is not feasible to ensure that the class definition for a client defined object be available in the TSpaces server. The best way to bypass this problem is to serialize the object into a byte array prior to sending it to the server and just send the server a field that is defined as byte[]. The FieldPS object is designed to do this Field Pre Serialization. FieldPS is a subclass of Field and acts like a Field object except when handed an Object, it automatically serializes it into a byte[] object and stores that. When requested to return the value of the Field, it automatically returns the deserialized value.

16 Reading/writing user defined objects(6) Using FieldPS UserObj obj = new UserObj("User Object 1"); FieldPS f2 = new FieldPS(obj); // changed ts.write("Key1",f2); obj = new UserObj("User Object 2"); f2.setValue(obj); ts.write("Key2",f2); Tuple template= new Tuple("Key2", new FieldPS(UserObj.class)); //changed Tuple mytuple = ts.take(template); UserObj objreturned = (UserObj) mytuple.getField(1).getValue(); System.out.println("Object content is:“ + objreturned.getData());  Download examples from herehere


Download ppt "IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user."

Similar presentations


Ads by Google