Lab Description

advertisement

CS590L: Lab 3 – JavaSpaces

Submission Deadline: 3/12/2004 (F), Midnight,

Submit the package to your website

Introduction

This lab is designed to study the use of JavaSpace. JavasSpace is simple, yet powerful programming tool used for distributed computing applications [1]. JavaSpace is like a shared space which is used by distributed applications to exchange objects used to coordinate between distributed applications. These objects are passive data that can be read, inserted, removed, updated and reinserted into the space by the applications.

JavaSpaces is a part of Jini technology but can be used effectively with the non-Jini applications. One of the applications of JavaSpaces is compute server which is explained in [2]. The use of JavaSpace to coordinate distributed application is explained in [3] and [4] explains how JavaSpaces is used with Jini transaction management APIs.

Lab 3

In this lab we try to use JavaSpaces to coordinate the BlackJack client and server application. Please go through [1] and [3] before staring this lab so that you will have clear idea about how JavaSpaces works.

The previous lab was quite weak in controlling and coordinating players connecting to the server. We try to improve the BlackJack application in this area by using

JavaSpaces. We use the concept of tickets, explained in [3], for this. The tickets are the objects created the server. The number of tickets generated by the server is equal to number of players allowed to play the game at a time. So if any a player wants to play game then it need to acquire the ticket first. The server generates these tickets and writes them into the space.

When a player wants to play game, it searches the ticket in the space.

If any ticket is available in the space then the player takes this ticket and uses it to play the game. If ticket is not available in the space then the player needs to wait for the ticket. If the player has a ticket and wants to leave the game then it writes the ticket back into the space, which is then taken by the other player who is waiting for the ticket. Each player also writes its own object into the space which keeps the client proxy object that is used by the server to communicate with the client application.

The main operations in JavaSpaces are

Write : used to write the object into the space

Read : read is used to read the object from the space. A copy of object is returned to the application and the object is retained by the space .

Take : take is similar to read, used to read object from the space but it will remove the object from the space.

Implementation Steps

Please use the new JiniExample1 package.

Extract the given package in a temporary folder.

Environment modifications

1.

Copy the ‘net’ folder, which has net/jini/space/JavaSpaces.class [5] file, to the jini1_2_1_001/lib folder of the previous lab. JavaSpaces is an interface which is implemented by the server that provides JavaSpaces service. This class file is a part of Jini and is also available in outrigger-dl.jar file, but it’s not working directly.

2.

Copy the spaceaccessor.jar file to the jini/lib folder. It contains the utility class called SpaceAccessor which is used to access space. [6]

3.

Replace the old JiniExample1 folder with the new one.

4.

To use JavaSpaces we need to start server which provides JavaSpaces service.

This server needs transaction management service so we need to start Jini transaction management service first. So with r1, r2, r3 we have other two batch files, r4 and r5, which starts Jini transaction manager and JavaSpaces servers respectively.

5.

In the BlackJack folder you will find two new files, JoinReq.java and Ticket.Java.

These implements the objects that are shared in space between BlackJack client and server application. The server program creates object of Ticket class and put it in the space while the client program creates objects of JoinReq class to store client proxy object in space . Open these files and study the constructors of these classes. These classes implement Entry interface.

Program Modifications

1.

Open the BlackJackServ.java file and do the following changes a.

Import following files. net.jini.core.lease.Lease; net.jini.space.JavaSpace; jsbook.util.SpaceAccessor; b.

In the constructor add following code

To get access to the JavaSpace

space = SpaceAccessor.getSpace();

Now we need to generate tickets. Before that remove the existing old tickets from the space.

System.out.println("Removing tickets from space");

Ticket tickettmp = new Ticket("BlackJack");

while (space.takeIfExists(tickettmp,null,JavaSpace.NO_WAIT) != null){System.out.println("Got previous ticket");}

Here we first created a template of the Ticket object by using the constructor of

Ticket class that takes just the game name as an input. Then we use takeIfExists method of JavaSpace that takes the object from the space if the object matching to the template is present in the space.

The Ticket class has different variables and we just set one variable in the template object. Jini considers null value for the other unspecified variables and hence we can find the required object even by partial match of template and object. The second parameter of takeIfExists (or any other JavaSpace method) is the Jini transaction.

Since we are not using transactions in our application we put this parameter as null. The third parameter tells whether the function should wait for the object if the object is not present in the space.

Here we specify NO_WAIT so that takeIfExists won’t wait for ticket object after it has removed all the ticket objects from the space.

Now we create 2 ticket objects (maximum 2 players can play this game at time) and write them into the space.

for (int i =0; i < 2; i++)

{

System.out.println("Writing ticket in java space");

tickettmp = new Ticket("BlackJack",this,i);

space.write(tickettmp, null, Lease.FOREVER);

}

Here the ticket objects are created by providing all the required fields of ticket object. Also while writing object into the space we specify leasing period as FOREVER so that the object will remain in the space forever. Since the space is persistent, the object will remain in the space event though we close the server program. So we first remove all the old ticket objects from the space whenever we restart the server program. c.

The registerPlayer method of BlackJackServ is called when the player wants to join the game. This method takes player name and player id as input.

In the try-catch block of registerPlayer add the following code

Create a template entry to search user’s entry object in space using the player name.

JoinReq tmpjoinreq = new JoinReq(player,"BlackJack");

Read the JoinReq object that corresponds to the player and extract the client proxy object from this object.

Object myServerInterface =

((JoinReq)space.read(tmpjoinreq,null,Long.MAX_VALUE)).playerproxy;

Here we ask program to wait for Long.MAX_VALUE if the required object is not present in the space.

Create a PlayerInfo object for this user and put that object in the local hashtable used to store information about the current players.

PlayerInfo newplayer = new PlayerInfo(player, playerid, myServerInterface);

currentplayers.put(new Integer(playerid),newplayer);

Send a message to the player that he has been added in the list of players.

((BlackJackClientIntf)myServerInterface).setMessage("Welcome " + player + ",

Your player id is " + playerid);

players++; d.

The leaveGame method is called by the client when the player wants to leave the game. In the leaveGame function remove the PlayerInfo object that corresponds to the player.

currentplayers.remove(new Integer(playerid));

2.

Do the following changes in BlackJackApp.java a.

Import

net.jini.core.lease.Lease;

net.jini.space.JavaSpace;

jsbook.util.SpaceAccessor; b.

In the JbIniti function, add following statements. try{

System.setSecurityManager (new RMISecurityManager ()); blackjackservobj =

(BlackJackServInterface)Naming.lookup("//134.193.128.214:1098/BS");

System.out.println("got blackjackobj from server");

space = blackjackservobj.getJavaSpace();

}

catch(Exception ed)

{

System.out.println(ed.getMessage());

return;

}

This code is used to get the remote server object to communicate with the server and then we call the getJavaSpace method of the remote server to get the space used by the serve to exchange objects. c.

In Button_joingame_actionPerformed method do the following changes.

Create an object of BlackJackClient as ‘blackjackclient’.

Create JoinReq entry object as below

JoinReq blackjackjoinreq= new

JoinReq(this.Text_playername .getText(),blackjackclient,"BlackJack");

This object represents client and it should be written into the space.

. Write this object into the space using following statement.

space.write(blackjackjoinreq, null, Lease.FOREVER);

Now we need to get ticket to play the game. Create a template of ticket object and search the ticket object in the space.

Ticket tickettmp = new Ticket("BlackJack"); //check the constructor used ticket = (Ticket)space.take(tickettmp, null, Long.MAX_VALUE);

Here the client program waits until it gets the available ticket.

Once the client gets the ticket object it extracts the playerid from the ticket and register with the server using this playerid. this.playerid = ticket.playerid.intValue(); while(blackjackservobj.registerPlayer( this.Text_playername .getText(),this.playerid )

== 0) {};

this.label_playername.setText("Player " + playerid + " : " + this.Text_playername .getText() );

Here the program waits on regiserPlayer call until the client gets registered with the server. The server won’t register new player if the game has already started. d.

When the player wants to leave the game, the client program should return the ticket to the space so that some other client can use it. It also needs to remove the entry object from the space that corresponds to the client.

In leaveGame method do the following changes.

After “returning ticket” statement add following code.

space.write(ticket,null,Lease.FOREVER);

JoinReq blackjackjoinreq = new JoinReq(this.Text_playername.getText(), null,

"BlackJack");

space.take(blackjackjoinreq, null, Long.MAX_VALUE);

This will complete all the modification that you need to do for this lab.

Running the program

Run r1, r2, r3, r4 and r5. Compile the source by running c.bat file. Then start server by running r.bat stored in server-side folder. Observe the outputs. When you run server for first time it won’t remove any object from space but when you run it for the second time it will remove old ticket objects.

In another command window run the client program by running r.bat of client-side.

Start 3 clients so see whether the third client is waiting for ticket and how the client and server programs are coordinating.

Notes

Make sure that you modify SetInstallPoint.bat file as per your environment setting.

Also modify IP addresses used in different program and batch files before compiling and running programs. Study the r4 and r5 batch files to understand how to start Jini transaction manager and JavaSpace service.

Report

Please explain this lab in the report. Write about what you lean new thru this lab and what you found interesting and new in this lab. Put the screenshots and explain the problems that you faced in this lab. Explain how you can use JavaSpaces in any other application. Also explain how you tested this application and what you observed during your testing.

Links

[1] Make room for JavaSpaces, Part 1 – Ease the development of distributed app with

JavaSpaces http://www.javaworld.com/javaworld/jw-11-1999/jw-11-jiniology.html

?

[2] Make room for JavaSpaces, Part 2 – Build compute server with JavaSpaces http://www.javaworld.com/javaworld/jw-01-2000/jw-01-jiniology.html

?

[3] Make room for JavaSpaces, Part 3 – Coordinate your Jini applications with

JavaSpaces http://www.javaworld.com/javaworld/jw-03-2000/jw-03-jiniology.html

?

[4] Make room for JavaSpaces, Part 4 – Explore Jini transactions with JavaSpaces http://www.javaworld.com/javaworld/jw-04-2000/jw-0421-jiniology.html

?

[5] JavaSpaces interface, http://java.sun.com/products/jini/2.0/doc/api/net/jini/space/JavaSpace.html

[6] JavaSpaces Principles, Patterns, and Practice http://java.sun.com/docs/books/jini/javaspaces/

[7] Errata for JavaSpaces Principles, Patterns, and Practice (It gives the compilation details ) http://java .sun.com/docs/books/jini/javaspaces/errata.html

Download