aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README3
-rw-r--r--erlbal.erl25
2 files changed, 23 insertions, 5 deletions
diff --git a/README b/README
index 5b4d3cd..ed6e7b3 100644
--- a/README
+++ b/README
@@ -5,9 +5,10 @@ start_bal/0 spawns a balancer.
stop_bal/1 takes a balancer and kills all of its servers and then kills the balancer.
list_servers/1 takes a balancer and returns a list of the servers it balances over.
-start_server/2 takes a balancer and a function and spawns a server to serve the function.
+start_server/2 takes a balancer, a node and a function and spawns a server on the node to serve the function.
stop_server/2 takes a balancer and a server PID and stops the server and removes it from the balancer.
+make_request/1 takes a balancer and returns the response from the server.
make_request/2 takes a balancer and an argument and returns the response from the server.
diff --git a/erlbal.erl b/erlbal.erl
index 7d3f09b..a3f22ca 100644
--- a/erlbal.erl
+++ b/erlbal.erl
@@ -1,9 +1,8 @@
-module(erlbal).
-% test
--export([make_request/2, start_bal/0, stop_bal/1, start_server/2, stop_server/2, list_servers/1]).
+-export([make_request/1, make_request/2, start_bal/0, stop_bal/1, start_server/3, stop_server/2, list_servers/1]).
-start_server(Balancer, Fun) ->
- PID = spawn(fun() -> server_loop(Balancer, Fun) end),
+start_server(Balancer, Node, Fun) ->
+ PID = spawn(Node, fun() -> server_loop(Balancer, Fun) end),
Balancer ! {add_node, PID}.
stop_server(Balancer, PID) ->
@@ -16,6 +15,10 @@ list_servers(Balancer) ->
server_loop(Balancer, Fun) ->
receive
+ {request, From} ->
+ Ret = Fun(),
+ Balancer ! {response, From, Ret},
+ server_loop(Balancer, Fun);
{request, From, ARGS} ->
Ret = Fun(ARGS),
Balancer ! {response, From, Ret},
@@ -39,6 +42,17 @@ bal_loop(Serverlist, Nextserver) ->
{list_nodes, From} ->
From ! Serverlist,
bal_loop(Serverlist, Nextserver);
+ {request, From} ->
+ Serv = lists:nth(Nextserver, Serverlist),
+ Serv ! {request, From},
+ NS = Nextserver + 1,
+ SLL = length(Serverlist),
+ if
+ NS > SLL ->
+ bal_loop(Serverlist, 1);
+ true ->
+ bal_loop(Serverlist, NS)
+ end;
{request, From, ARGS} ->
Serv = lists:nth(Nextserver, Serverlist),
Serv ! {request, From, ARGS},
@@ -58,6 +72,9 @@ bal_loop(Serverlist, Nextserver) ->
end.
+make_request(Balancer) ->
+ Balancer ! {request, self()},
+ receive Ret -> Ret end.
make_request(Balancer, ARGS) ->
Balancer ! {request, self(), ARGS},
receive Ret -> Ret end.