Tuesday, February 3, 2009

Ch5, Ex2

a.
Well, In the current implementation, one controller can be associated with one lift only. If we have one controller and every lift send it step(N), controller will keep on moving one lift only. Obviously its a pretty bad idea.

b.
fun {Controller Init}
Tid={Timer}
Cid={NewPortObject Init
fun {$ Msg state(Motor F Lid)}
case Motor
of running then
case Msg
of stoptimer then
{Send Lid 'at'(F)}
state(stopped F Lid)
end
[] stopped then
case Msg
of step(Dest) then
if F==Dest then
state(stopped F Lid)
elseif F < Dest then X=Dest-F in %CHANGED HERE
{Send Tid starttimer(5000*X Cid)}
state(running F+X Lid)
else X=F-Dest in % F>Dest
{Send Tid starttimer(5000*X Cid)}
state(running F-X Lid)
end
end
end
end}
in Cid end

fun {Lift Num Init Cid Floors}
{NewPortObject Init
fun {$ Msg state(Pos Sched Moving)}
case Msg
of call(N) then
{Browse 'Lift '#Num#' needed at floor '#N}
if N==Pos andthen {Not Moving} then
{Wait {Send Floors.Pos arrive($)}}
state(Pos Sched false)
else Sched2 in
Sched2={ScheduleLast Sched N}
if {Not Moving} then
{Send Cid step(N)} end
state(Pos Sched2 true)
end
[] 'at'(NewPos) then
{Browse 'Lift '#Num#' at floor '#NewPos}
case Sched
of S|Sched2 then
if NewPos==S then
{Wait {Send Floors.S arrive($)}}
if Sched2==nil then
state(NewPos nil false)
else
{Send Cid step(Sched2.1)}
state(NewPos Sched2 true)
end
else %NOT CHANGED REALLY
{Browse 'ERROR: this should not happen'}
{Send Cid step(S)}
state(NewPos Sched Moving)
end
end
end
end}
end

No comments:

Post a Comment