% $Id: PrintServer.oz,v 1.1 2010/04/20 15:06:48 leavens Exp $ \insert 'NewPortObjectDebug.oz' declare % Handles the following messages: % ::= % | cancel() % | queue() % | printAnother % ::= print( ) %% ::= printQueue(>) fun {PrintServer} MyPort % the port of this port object proc {ActualPrint Str} {System.showInfo Str} {Delay {Length Str}} {Send MyPort printAnother} end in MyPort = {NewPortObjectDebug % demonstration of debugging ... printQueue(nil) fun {$ printQueue(Jobs) Msg} case Msg of print(Str JobNum X) then local NewJobs in if Jobs == nil then thread {ActualPrint Str} X=unit end NewJobs = nil else NewJobs = {Append Jobs [Msg]} end printQueue(NewJobs) end [] cancel(JobNum) then printQueue({RemoveJob Jobs JobNum}) [] queue(V) then V = {Map Jobs fun {$ print(_ JN _)} JN end} printQueue(Jobs) [] printAnother then case Jobs of print(Str JN Var)|T then thread {ActualPrint Str} Var=unit end printQueue(T) else % Jobs is nil printQueue(nil) end end end} MyPort end fun {RemoveJob Jobs N} {Filter Jobs fun {$ print(Str JN X)} JN \= N end } end