Advanced Internet Programming

tamerunΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

248 εμφανίσεις


Advanced Internet Programming

Exercise 3: Remote Method
I
nvocations


Exercise 1
-

Remote Bank Account Application


NetBeans Setup



Copy RMI exercise folder from
X:
\
Ran Brandes
\
Ex3
\
RMI

to your local directory



Create a new project in NetBeans called 'Labs_RM
I'

and mount the src and
classes fold
ers



Set the compilation products to be under RMI
\
classes



Open the server folder and edit the Java files according to the following steps


Step 1

-

Remote Interface

-

RemoteBan
kAccount

(just the headers do not implement)


In the
bank.server

package look at the remote interface
RemoteBankAccount


This interface is already written for you; note that all the methods throw a
RemoteException


Step 2

-

Implement the

Remote Interface
-

BankAccountImpl



In the
bank.server

package implement all methods that are declared in
RemoteBankAccount



define two instance variables that will store data about the account:



o

private double
balance


o

p
rivate String
accountName



In the constructor of the
class accept a String that will represent the Name of the bank
account, set the value at instantiation time to the instance variable
accountName
. Print a
message upon creation with the new account name.



Define all methods that are declared in the remote in
terface
.


(Don't forget to declare them as throwing
Remote Exception
)

1.

deposit

-

check that the amount is > 0, if so incre
ment

the balance in this amount, else
throw a new

Remote Exception

with a proper message
.

2.

getAccountName

-

returns the account name
.

3.

ge
tBalance

-

returns th
e current account balance value.

4.

withdraw

-

check that the balance minus the amount to withdraw is > 0, if so reduce
the balance value in the withdraw amount, else throw a new
Remote Exception

with a
proper message
.




Mark this class as

an RMI class.

Right click on the class name
-
> tools
-
> Mark as RMI

(works in 3.5.x in 3.6
use
: rmic

v1.2
className

from the command line
Start
-
>Run

cmd


)


Step 3

-

Publish the RMI Service


RegisterRemoteObjects

(The main!!)


In the project root edit t
he
RegisterRemoteObjects

class



In the main method create an RMI registry at port 1099.



Create a new instance of
BankAccountImpl

with your name as this bank account.



Bind the object to the RMI Registry.



Print a message that the binding was created.







St
ep
4

-

Writing the RMI Client

-

BankClient



In

folder bank.client edit the
BankClient

class



Look up the registry for the Stub of
BankAccount
I
mp
l

and
assign

it to the instance
variable of
RemoteBankAccount



Print a message with the account name and balance
amount.



Perform the following actions on the account and print the
balance

amount

after
each action

i.

deposit 1300 shekels

ii.

withdraw

500 shekels

iii.

in a try...catch block withdraw 1000 shekels

iv.

deposit 5000 shekels


Step

5

-

Compile and Run
the

e
xample




Compile
all classes, make sure that the stubs are generated



Run the
RegisterRemoteObject

class, see that the stub is bin
d
ed to the RMI
service



Run the
BankClient

class



Exercise 2
-

Invoking your
neighbors

machine example


In this example your client class will
invoke your
neighbors'

server example

Edit your client class
BankClient



Change the URL attribute in the
BankAccountc
l
ient

to have the IP of the other machine instead of
'localhost'

(to find what is the IP of a machine, open a dos
console and type
ipconfig
/all

and all the
machines IP will be listed).



Re
-
compile your client class.



Run your client class
.