% $Id: NewEmailServer.oz,v 1.1 2011/11/22 21:49:26 leavens Exp leavens $ \insert 'NewPortObject.oz' declare % ::= send() % | receive( ?) % ::= original( ) % | forwarded( ) % ::= % States are represented as follows: % ::= mailboxes() % ::= (#)'|' % | nil fun {NewEmailServer} {NewPortObject mailboxes(nil) % state, which is an fun {$ mailboxes(AL) Msg} % message handling function case Msg of send(Mail) then mailboxes({PutForReceiver AL Mail {ReceiverOf Mail}}) [] receive(Recvr ?X) then X={Assoc AL Recvr} mailboxes(AL) end end } end % Assoc : }: > fun {Assoc AL Rcvr} case AL of (K#V)|Rest then if K == Rcvr then V else {Assoc Rest Rcvr} end else nil end end % PutForReceiver : }: > fun {PutForReceiver AL Mail Rcvr} case AL of (K#V)|Rest then if K == Rcvr then (K#(Mail|V))|Rest else (K#V) |{PutForReceiver Rest Mail Rcvr} end else (Rcvr#[Mail])|nil end end % ReceiverOf: }: > fun {ReceiverOf Mail} case Mail of original(Rcvr _) then Rcvr [] forwarded(Rcvr _) then Rcvr end end