ex3a.erl

ex3a.erl — Erlang source code, 1 kB (1368 bytes)

Continguts del fitxer

-module(ex3a).
-export([start/0,botonera/0, proxy/3,bucle_proxy/2]).

start() -> register(bot2, spawn(?MODULE, botonera, [])).

botonera()->
	%Iniciar la botonera de 2 
	B2 = proxy(2,b2,self()), % B2 -> Pid de la botonera de 2
	bucle(B2).

bucle(L)->
  receive
    {b2,clicked,Pis}->
	  if
	    (Pis == 0) -> encen(0);
	    (Pis == 1) -> encen(1)
	  end,
	  bucle(L);
	
	{b8,clicked,Pis}->
	  if
	    (Pis rem 2 =:= 0) -> apaga(0);
		(Pis rem 2 =:= 1) -> apaga(1)
	  end,
	  bucle(L);
	
	{b8, abort} -> 
	  L!close,
	  receive _ -> "ok" end;
	
	{b2, abort} ->
	  {bot8, node8@TONIF}!close,
	  receive _ -> "ok28" end

  end.

proxy(N,Nom,Pid)->%N es la mida total
  B = spawn(ex3a,bucle_proxy,[Nom,Pid]),
  bots:nou(N,B).

bucle_proxy(Nom,Pid)->
  receive
    {clicked,Pis} ->
	  Pid!{Nom,clicked,Pis},
	  bucle_proxy(Nom,Pid);
	
	abort ->
	  Pid!{Nom,abort},
	  ok
  end.

encen(Pis) ->
    if
		Pis < 6 -> %Mentres no has acabat ves incrementant Pis
			{bot8, node8@TONIF}!{light_on,Pis},
			encen(Pis+2);
		Pis >= 6 -> % Si es mes gran que 7, per tant ja és l'ultim, 8, has acabat, torna al bucle
			{bot8, node8@TONIF}!{light_on,Pis}
    end.

apaga(Pis)->
	if
		Pis < 6 ->
			{bot8, node8@TONIF}!{light_off,Pis},
			apaga(Pis+2);
		Pis >= 6 ->
			{bot8, node8@TONIF}!{light_off,Pis}
    end.