ibm tspaces lab 1 introduction. summary tspaces overview basic definitions basic primitive...

19
IBM TSpaces Lab 1 Introduction

Upload: muriel-gilbert

Post on 03-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

IBM TSpacesLab 1

Introduction

Page 2: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Summary

TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld program Installing TSpaces Compiling/running programs in TSpaces

Page 3: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

TSpaces Overview

TSpaces is a Java implementation of the Linda coordination model. It enables communication between applications and devices in a network of heterogeneous computers and operating systems.

Page 4: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Basic Definitions

Field– Most basic component of Tuplespace data

structure. It contains: Type (i.e. "java.lang.String" or

"com.company.appl.EmployeeObject") Value Field Name (optional): Specifying a field name implies

that this field will be indexed (and searched).

Page 5: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Basic Definitions (2)

Tuple– A Tuple object is an ordered sequence of Fields. – Implemented by SuperTuple class (abstract

class)– Clients should create objects of either Tuple or

SubclassableTuple classes. – A template tuple is a Tuple that is used for

matching. One or more of the Fields in a template may be "wildcards" and consist of only the class type with no value.

Page 6: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Basic Definitions (3)

TupleSpace– A class implementing a space where tuples can be

added/removed– It is stored and administered across a network on one or

more "TSpace Servers“. A TSpace server may contain many Tuplespaces.

– Several threads on the same or different machines can be accessing the space simultaneously.

– For each different TupleSpace a client wishes to access, it requires a separate instance of this class, even if they are managed by the same server.

– In order to obtain an instance of a specific tuplespace you need the name of the space, and the name of a server that manages that space.

Page 7: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Basic primitive operations

write ( tuple ): Adds a tuple to the space. take( template_tuple ): Searches for a tuple that

matches the template. When found, the tuple is removed from the space and returned. If none is found, returns null.

waitToTake(template_tuple ): Searches for a tuple that matches the template. Blocks until match is found. Removes and returns the matched tuple from the space.

read( template_tuple ): Like "take" above, except that the tuple is not removed from the tuple space.

Page 8: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Basic primitive operations (2)

waitToRead( template_tuple ): Like "waitToTake" above, except that the tuple is not removed from the tuple space.

scan( template_tuple ): Like "read" above, except that it returns the entire set of tuples that match.

countN( template_tuple ): Like "scan" above, except that it returns the number of matching tuples rather than the set of tuples itself.

There are also numerous other more specializedcommands and there is an ability to define your own specialized commands.

Page 9: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Reading and writing tuples

Step 1: Create a new tuplespace or get access to an already created tuplespace String host = "tserver1.company.com"; ... TupleSpace ts = new TupleSpace("Example1",host);

The above will contact the TSpaces server that is running on a machine with hostname "tserver1.company.com" and return a handle to the TupleSpace that is named "Example1". If the "Example1" TupleSpace does not exist, it will be created at this time.

Page 10: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Reading and writing tuples (2)

Step 2: Create and write tuples to tuplespace

Tuple t1 = new Tuple("Key1","Data1"); ts.write(t1); Tuple t2 = new Tuple("Key2",new

Integer(999),"Data2"); ts.write(t2); Tuple t3 = new Tuple("Key2","Data3"); ts.write(t3);

The above will create and write 3 Tuples to the TupleSpace allocated previously. Tuples can contain any number and any type of fields.

Page 11: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Reading and writing tuples (3)

Other ways to create and write a tuple are the

following: Field f1 = new Field("Key1"); Field f2 = new Field("Data1"); Tuple t1 = new Tuple(); t1.add(f1); t1.add(f2); ts.write(t1); -or- ts.write("Key1","Data1"); .

Page 12: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Reading and writing tuples (4)

Step 3: Reading tuples from tuplespace

In order to read a tuple you must create a template Tuple. Template Tuple is a Tuple that is used to select matching Tuples. It will contain 0 or more Fields that are either Actual Fields or Formal Fields. Actual fields have a value that must be matched exactly

against the corresponding Fields in the tuples that are in the TupleSpace.

Formal fields have a class but no value and only describe the type of value that is to be returned. Basically they act as wildcards.

Page 13: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Reading and writing tuples (5)

Tuple template = new Tuple("Key2", new Field(String.class)); Tuple tuple = ts.read(template); String data = (String)tuple.getField(1).getValue();

The above code creates a template that matches any tuple having two fields of type String. The first field must contain the value "Key2". The second field can match any field of String type. In order to read the matching Tuples from Tuplespace we use the command read. The command take can also be used but it will remove the matching Tuples from the TupleSpace database and return it to the caller.

Page 14: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

HelloWorld Example

import com.ibm.tspaces.*;import java.io.Serializable;public class HelloWorld { public static void main(String[] args) { String myName = "World"; boolean needReply = false; if ( args.length > 0) myName = args[0]; try { TupleSpace space = new TupleSpace(); Tuple template = new Tuple(myName, new Field(String.class),new Field(Serializable.class));

Page 15: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

HelloWorld Example (2)

System.out.println("Checking for message in tuplespace...");Tuple msg = space.take(template); if ( msg == null) { System.out.println("No message for me!\n Sending message to World..."); msg = new Tuple("World", myName,"Hello World"); needReply = true; space.write(msg); } else {

System.out.println("Message received is: "+ msg); System.out.println("Sending reply message to : "+ (String)msg.getField(1).getValue());

Page 16: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

HelloWorld Example (3)

msg = new Tuple((String)msg.getField(1).getValue(),myName,"Hi");

space.write(msg); } if (needReply) { // Wait for a reply

System.out.println("Waiting for reply ..."); msg = space.waitToTake(template);

System.out.println("Message received is: "+msg); } } catch (Exception e) { System.out.println(e); } }}

Page 17: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Installing Tspaces

Download TSpaces package (tspaces212.zip) from here

Unzip package to a directory tspaces Exit to dos command window and change directory

to tspaces In order to start a TSpaces Server execute

command: bin\tspacesIf server is started successfully the prompt TSServer:> is appeared

In order to stop the TSpaces Server execute command exit

Page 18: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

Compiling/running programs

In order to compile and run TSpaces programs you need to include the jar files tspaces_client.jar and tspaces.jar in your CLASSPATH. These files are located in the tspaces_dir\lib (where tspaces_dir is the path to the directory where TSpaces is installed).

Compile: javac -classpath .;tspaces_dir\lib\tspaces_client.jar;

tspaces_dir\lib\tspaces.jar yourfile.java Run: java -cp .;tspaces_dir\lib\tspaces_client.jar;

tspaces_dir\lib\tspaces.jar yourfile

Replace tspaces_dir with the path to the directory where TSpaces is installed.

Compile and run the HelloWorld Example

Page 19: IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld

HTTP Server Interface

The TSpaces Server contains a light built-in HTTP Server. You can use this interface to determine the status of TSpaces or obtain debug information.

In order to obtain this information access HTTP server at http://hostname:8201/debug where hostname is the name of the machine that TSpaces server runs.