Tuesday, February 17, 2009

Ch7, Ex5

I- Synchronous RMI
declare
class ServerProc
meth init
skip
end
meth send(Msg)
case Msg of calc(X Y) then
Y=X*X+2.0*X+2.0
end
end
end
Server = {NewActive ServerProc init}

declare
class ClientProc
meth init
skip
end
meth send(Msg)
case Msg
of work(Y) then
Y1 Y2 in
{Server send(calc(10.0 Y1))}
{Wait Y1}
{Server send(calc(20.0 Y2))}
{Wait Y2}
Y=Y1+Y2
end
end
end
Client = {NewActive ClientProc init}

{Browse {Client send(work($))}}

II- Asynchronous RMI
Only ClientProc is changed, everything else remains same as the synchronous RMI.
declare
class ClientProc
meth init
skip
end
meth send(Msg)
case Msg
of work(Y) then
Y1 Y2 in
{Server send(calc(10.0 Y1))}
{Server send(calc(20.0 Y2))}
Y=Y1+Y2
end
end
end

III- RMI with callback(with thread)
declare
class ServerProc
meth init
skip
end
meth send(Msg)
%{Browse server#Msg}
case Msg
of calc(X Y Client) then
X1 D in
{Client send(delta(D))}
X1=X+D
Y=X1*X1+2.0*X1+2.0
end
end
end
Server = {NewActive ServerProc init}

declare
class ClientProc
meth init
skip
end
meth send(Msg)
%{Browse client#Msg}
case Msg
of work(Z) then
Y in
{Server send(calc(10.0 Y self))}
%here we don't need to create another
%thread as we're passing self and not
%Client
Z=Y+100.0
[] delta(D) then
D=1.0
end
end
end
Client = {NewActive ClientProc init}

{Browse {Client send(work($))}}

IV- RMI with callback (using record continuation)
declare
class ServerProc
meth init
skip
end
meth send(Msg)
%{Browse server#Msg}
case Msg
of calc(X Client Cont) then
X1 D Y in
{Client send(delta(D))}
X1=X+D
Y=X1*X1+2.0*X1+2.0
{Client send(Cont#Y)}
end
end
end
Server = {NewActive ServerProc init}

declare
class ClientProc
meth init
skip
end
meth send(Msg)
%{Browse client#Msg}
case Msg
of work(?Z) then
{Server send(calc(10.0 self cont(Z)))}
[] cont(Z)#Y then
Z=Y+100.0
[] delta(?D) then
D=1.0
end
end
end
Client = {NewActive ClientProc init}

{Browse {Client send(work($))}}

V- RMI with callback (using procedure continuation)
Only ClientProc is changed, everything else remains same as the record continuation technique
class ClientProc
meth init
skip
end
meth send(Msg)
%{Browse client#Msg}
case Msg
of work(?Z) then
C = proc {$ Y} Z=Y+100.0 end
in
{Server send(calc(10.0 self cont(C)))}
[] cont(C)#Y then
{C Y}
[] delta(?D) then
D=1.0
end
end
end

VI- Error Reporting
class ServerProc
meth init
skip
end
meth send(Msg)
case Msg
of sqrt(X Y E) then
try
Y={Sqrt X}
E=normal
catch Exc then
E=exception(Exc)
end
end
end
end
Server = {NewActive ServerProc init}

%It can be used as
{Server send(sqrt X Y E)}
case E of exception(Exc) then
raise Exc end end

VII- Asynchronous RMI with callback
declare
class ServerProc
meth init
skip
end
meth send(Msg)
case Msg
of calc(X ?Y Client) then
X1 D in
{Client send(delta(D))}
thread
X1=X+D
Y=X1*X1+2.0*X1+2.0
end
end
end
end
Server = {NewActive ServerProc init}

declare
class ClientProc
meth init
skip
end
meth send(Msg)
case Msg
of work(?Y) then
Y1 Y2 in
{Server send(calc(10.0 Y1 self))}
{Server send(calc(20.0 Y2 self))}
thread Y=Y1+Y2 end
[] delta(?D) then
D=1.0
end
end
end
Client = {NewActive ClientProc init}

{Browse {Client send(work($))}}

VIII-Double Callbacks
declare
class ServerProc
meth init
skip
end
meth send(Msg)
case Msg
of calc(X ?Y Client) then
X1 D in
{Client send(delta(D))}
thread
X1=X+D
Y=X1*X1+2.0*X1+2.0
end
[] serverdelta(?S) then
S=0.01
end
end
end
Server = {NewActive ServerProc init}

declare
class ClientProc
meth init
skip
end
meth send(Msg)
case Msg
of work(Z) then
Y in
{Server send(calc(10.0 Y self))}
thread Z=Y+100.0 end
[] delta(?D) then S in
{Server send(serverdelta(S))}
thread D=1.0+S end
end
end
end
Client = {NewActive ClientProc init}

{Browse {Client send(work($))}}

No comments:

Post a Comment