[erlang-questions] Selective receive issue

Grasl Christoph <>
Tue Aug 5 15:43:53 CEST 2008


Hi again! 

So why not use a second loop in the processInput/2 - funcction and 
return a state-change to the original loop if desired?

loop(State) ->
   receive
       activate ->  
          NewState = replaceVariable(State, {active, true}),
          loop(NewState);
       deactivate ->
          NewState = replaceVariable(State, {active, false}),
          loop(NewState);
       {input, Data} ->
          case getValue(State, active) of
             false ->
                loop(State);
             true ->
               case processInput(Data) of 
                   {false, Reason} ->
                      exit(Reason); %% so u know when computation is corrupted
                   {true, NewState} ->
               loop(NewState)
          end;
      AnyMessage ->
          case getValue(State, active) of
             false ->
                loop(State);
             true ->
               %% now storing the msg in the loop's state:
               NewState = replaceVariable(State, {pending, AnyMessage}),
               loop(State)
          end
      after 2300 ->    
         timeOutRoutine(),
         loop()
   end.

processInput(Data) -> 
   case whereis(secondLoop) of 
      undefined -> 
         Pid = spawn(secondLoop, secondLoop, [Data, self()]), 
	 register(secondLoop, Pid); 
      _Pid ->
	 void
   end, 
   receive
      {processed, NewState} -> 
         {true, NewState}
      {false, Reason} -> 
         {false, Reason}
      after 2300 -> 
         {false, timeout_occured}
   end.

secondLoop(Data, From) ->
   receive 
      Pattern1 -> 
         NewState = processDataAccordingToPattern(Data, Pattern1),
         From ! {processed, NewState};
      PatternN -> 
         NewState = processDataAccordingToPattern(Data, PatternN),
         From ! {processed, NewState}
      after 2300 ->
         timeOutRoutine(),
         From ! {false, timeout_occured}
   end.


While the processDataAccordingToPattern-functions return the NewState for the original loop (if desired)

In case i missunderstood the requirements of the loop-functionalities and your initial question was left unanswered 
your are welcome to explain your exact specifications and the process-topology in detail.

Best regards,

Christoph Grasl
Embedded Software Entwickler

KEYTRONIX
Gesellschaft für industrielle Elektronik und Informationstechnologie mbH

Ungargasse 64-66/1/109
A-1030 WIEN

E-Mail: 
Tel.: +43 (1) 718 06 60 - 323
Mobil: +43 (664) 8556456
WWW: http://www.keytronix.com

HG Wien FN 261131t

Confidentiality Notice:
This message may contain privileged and confidential information. If you think, for any reason, that this message may have been addressed to you in error, you must not disseminate, copy or take any action in reliance on it, and we would ask you to notify us immediately by return email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080805/bc64e793/attachment.html>


More information about the erlang-questions mailing list