diff options
-rw-r--r-- | erlbal.erl | 59 | ||||
-rw-r--r-- | erlbal_tests.erl | 44 |
2 files changed, 0 insertions, 103 deletions
diff --git a/erlbal.erl b/erlbal.erl deleted file mode 100644 index a8b8713..0000000 --- a/erlbal.erl +++ /dev/null @@ -1,59 +0,0 @@ --module(erlbal). --export([make_request/2, start_bal/0, stop_bal/1, start_server/4, stop_server/2, list_servers/1]). - -start_server(Balancer, Node, Fun, INITSTATE) when is_function(Fun, 2) andalso is_list(INITSTATE) -> - PID = spawn(Node, fun() -> server_loop(Fun, INITSTATE) end), - Balancer ! {add_node, PID}. - -stop_server(Balancer, PID) -> - Balancer ! {del_node, PID}, - PID ! die. - -list_servers(Balancer) -> - Balancer ! {list_nodes, self()}, - receive Nodes -> Nodes end. - -server_loop(Fun, STATE) -> - receive - {request, From, ARGS} -> - {Ret, NEWSTATE} = Fun(ARGS, STATE), - From ! Ret, - server_loop(Fun, NEWSTATE); - die -> - ok - end. - -start_bal() -> - spawn(fun() -> bal_loop([], 1) end). - -stop_bal(Balancer) -> - Balancer ! die. - -bal_loop(Serverlist, Nextserver) -> - receive - {add_node, PID} -> - bal_loop(Serverlist ++ [PID], Nextserver); - {del_node, PID} -> - bal_loop(Serverlist -- [PID], Nextserver); - {list_nodes, From} -> - From ! Serverlist, - bal_loop(Serverlist, Nextserver); - {request, From, ARGS} -> - Serv = lists:nth(Nextserver, Serverlist), - Serv ! {request, From, ARGS}, - NS = Nextserver + 1, - SLL = length(Serverlist), - if - NS > SLL -> - bal_loop(Serverlist, 1); - true -> - bal_loop(Serverlist, NS) - end; - die -> - ok - end. - - -make_request(Balancer, ARGS) when is_list(ARGS)-> - Balancer ! {request, self(), ARGS}, - receive Ret -> Ret end. diff --git a/erlbal_tests.erl b/erlbal_tests.erl deleted file mode 100644 index 961ba08..0000000 --- a/erlbal_tests.erl +++ /dev/null @@ -1,44 +0,0 @@ --module(erlbal_tests). --export([start/1]). - -start(simple) -> - Bal = erlbal:start_bal(), - erlbal:start_server(Bal, node(), fun(_, _) -> {7, []} end, []), - erlbal:start_server(Bal, node(), fun(_, _) -> {8, []} end, []), - 7 = erlbal:make_request(Bal, []), - 8 = erlbal:make_request(Bal, []), - lists:foreach(fun(X) -> erlbal:stop_server(Bal, X) end, erlbal:list_servers(Bal)), - erlbal:stop_bal(Bal), - ok; - -start(args) -> - Bal = erlbal:start_bal(), - erlbal:start_server(Bal, node(), fun([X], _) -> {X+1, []} end, []), - erlbal:start_server(Bal, node(), fun([X], _) -> {X+2, []} end, []), - 7 = erlbal:make_request(Bal, [6]), - 8 = erlbal:make_request(Bal, [6]), - lists:foreach(fun(X) -> erlbal:stop_server(Bal, X) end, erlbal:list_servers(Bal)), - erlbal:stop_bal(Bal), - ok; - -start(state) -> - Bal = erlbal:start_bal(), - erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S]} end, [1]), - erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S]} end, [10]), - 7 = erlbal:make_request(Bal, [6]), - 16 = erlbal:make_request(Bal, [6]), - lists:foreach(fun(X) -> erlbal:stop_server(Bal, X) end, erlbal:list_servers(Bal)), - erlbal:stop_bal(Bal), - ok; - -start(complex_state) -> - Bal = erlbal:start_bal(), - erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S+1]} end, [1]), - erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S+1]} end, [10]), - 7 = erlbal:make_request(Bal, [6]), - 16 = erlbal:make_request(Bal, [6]), - 8 = erlbal:make_request(Bal, [6]), - 17 = erlbal:make_request(Bal, [6]), - lists:foreach(fun(X) -> erlbal:stop_server(Bal, X) end, erlbal:list_servers(Bal)), - erlbal:stop_bal(Bal), - ok. |