My scenario is as follows - I have a client C with function foo() which performs some computation.
I'd like a server S, which doesn't know about foo(), to execute this function instead, and send the result back to the client.
I am trying to determine the best way to perform this in Erlang. I am considering:
Are there other methods (or features of the language) that I am not thinking of?
Thanks for the help!
If the computation function is self contained i.e. does not depend on any other modules or functions on the client C, then what you need to do is a fun
(Functional Objects). A fun
can be sent across the network and applied by a remote machine and in side the fun
, the sender has embedded their address and a way of getting the answer back. So the executor may only see a fun
to which they may or may not give an argument, yet inside the fun, the sender has forced a method where by the answer will automatically be sent back. The fun
is an abstraction of very many tasks within one thing, and it can be moved around as arguments.
At the client, you can have code like this:
%% somewhere in the client %% client runs on node() == '[email protected]' -module(client). -compile(export_all). -define(SERVER,{server,'[email protected]'}). give_a_server_a_job(Number)-> ?SERVER ! {build_fun(),Number}. build_fun()-> FunObject = fun(Param)-> Answer = Param * 20/1000, %% computation here rpc:call('[email protected]',client,answer_ready,[Answer]) end, FunObject. answer_ready(Answer)-> %%% use Answer for all sorts of funny things.... io:format("\n\tAnswer is here: ~p~n",[Answer]).
The server then has code like this:
%%% somewhere on the server %%% server runs on node() == '[email protected]' -module(server). -compile(export_all). start()-> register(server,spawn(?MODULE,loop,[])). loop()-> receive {Fun,Arg} -> Fun(Arg), %% server executes job %% job automatically sends answer back %% to client loop(); stop -> exit(normal); _ -> loop() end.
In this way, the job executor needs not know about how to send back the reply, The job itself comes knowing how it will send back the answer to however sent the job!. I have used this method of sending functional objects across the network in several project, its so cool !!!
#### EDIT #####
If you have a recursive problem, You manipulate recursion using funs
. However, you will need at least one library function at the client and/or the server to assist in recursive manipulations. Create a function which should be in the code path of the client as well as the server.
Another option is to dynamically send code from the server to the client and then using the library: Dynamic Compile erlang
to load and execute erlang code at the server from the client. Using dynamic compile, here is an example:
1> String = "-module(add).\n -export([add/2]). \n add(A,B) -> A + B. \n". "-module(add).\n -export([add/2]). \n add(A,B) -> A + B. \n" 2> dynamic_compile:load_from_string(String). {module,add} 3> add:add(2,5). 7 4>
What we see above is a piece of module code that is compiled and loaded dynamically from a string. If the library enabling this is available at the server and client , then each entity can send code as a string and its loaded and executed dynamically at the other. This code can be unloaded after use. Lets look at the Fibonacci function and how it can be sent and executed at the server:
%% This is the normal Fibonacci code which we are to convert into a string: -module(fib). -export([fib/1]). fib(N) when N == 0 -> 0; fib(N) when (N < 3) and (N > 0) -> 1; fib(N) when N > 0 -> fib(N-1) + fib(N-2). %% In String format, this would now become this piece of code StringCode = " -module(fib).\n -export([fib/1]). \nfib(N) when N == 0 -> 0;\n fib(N) when (N < 3) and (N > 0) -> 1;\n fib(N) when N > 0 -> fib(N-1) + fib(N-2). \n". %% Then the client would send this string above to the server and the server would
%% dynamically load the code and execute it
send_fib_code(Arg)-> {ServerRegName,ServerNode} ! {string,StringCode,fib,Arg}, ok. get_answer({fib,of,This,is,That}) -> io:format("Fibonacci (from server) of ~p is: ~p~n",[This,That]). %%% At Server loop(ServerState)-> receive {string,StringCode,Fib,Arg} when Fib == fib -> try dynamic_compile:load_from_string(StringCode) of {module,AnyMod} -> Answer = AnyMod:fib(Arg), %%% send answer back to client %%% should be asynchronously %%% as the channels are different & not make %% client wait rpc:call('[email protected]',client,get_answer,[{fib,of,Arg,is,Answer}]) catch _:_ -> error_logger:error_report(["Failed to Dynamic Compile & Load Module from client"]) end, loop(ServerState); _ -> loop(ServerState) end.
That piece of rough code can show you what am trying to say. However, you remember to unload all un-usable dynamic modules. Also you can a have a way in which the server tries to check wether such a module was loaded already before loading it again. I advise that you donot copy and paste the above code. Look at it and understand it and then write your own version that can do the job.
success !!!
If you do S ! C:foo()
it will compute on client side function foo/1
from module C
and send its result to process S
. It doesn't seem like what you want to do. You should do something like:
% In client
call(S, M, F, A) ->
S ! {do, {M, F, A}, self()},
receive
{ok, V} -> V
end.
% In server
loop() ->
receive
{do, {M, F, A}, C} ->
C ! {ok, apply(M, F, A)},
loop()
end.
But in real scenario you would have to do a lot more work e.g. mark your client message to perform selective receive (make_ref/0
), catch error in server and send it back to client, monitor server from client to catch server down, add some timeout and so. Look how are gen_server:call/2
and rpc:call/4,5
implemented and it is reason why there is OTP to save you from most of gotcha.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With