%%%-------------------------------------------------------------------
%%% File    : priorityqueues.erl
%%% Author  :  <it4hian@ituniv.se>
%%% Description : 
%%%
%%% Created : 25 Oct 2005 by  <it4hian@ituniv.se>
%%%-------------------------------------------------------------------
-module(priorityqueues).

-export([start/0, test/0]).

-export([init/1]).

-define(PRIORITIES, [high, medium, low]).

test() ->
    Pid = start(),
    Pid ! {low,    " 1 message low\n"},
    Pid ! {medium, " 2 message medium\n"},
    Pid ! {high,   " 3 message high\n"},
    Pid ! {low,    " 4 message low\n"},
    Pid ! {high,   " 5 message high\n"},
    Pid ! {low,    " 6 message low\n"},
    Pid ! {low,    " 7 message low\n"},
    Pid ! {high,   " 8 message high\n"},
    Pid ! {medium, " 9 message medium\n"},
    Pid ! {low,    "10 message low\n"}.
    

start() ->
    spawn(?MODULE, init, [self()]).

init(From) ->
    timer:sleep(1000),
    loop(From).

loop(From) ->
    handle(From),
    loop(From).

do(Msg, State) ->
    io:format(Msg).

handle(State) ->
    handle(?PRIORITIES, State).
     
handle([], State) ->
    receive {_Priority, Msg} ->
	    do(Msg, State)
    end;
handle([Priority | Tail], State) ->
    receive {Priority, Msg} ->
            do(Msg, State)
    after 0 ->
            handle(Tail, State)
    end.
