[erlang-questions] code mobility

Jiansen He jiansenhe@REDACTED
Thu Oct 21 17:27:29 CEST 2010

Thank you for your reply,

code:load_binary/3 is what I used in one of my demo programs.  My code looks
like this:

-export([start_server/0, server/0]).
server() ->
  io:format("Server initializing...~n", []),
    {FUN, I, Client} ->
      io:format("Server:message received~n"),
      io:format("erlang:fun_info: ~w~n", [erlang:fun_info(FUN)]),
      io:format("result for ~w ~w is ~w~n",[FUN, I,FUN(I)]),
      Client ! FUN(I),
      io:format("result sent~n"),

% initialize
start_server() -> register(server, spawn(server7, server, [])).

-export([start_client/1, client/3, fact/1, isEven/1, isOdd/1]).

% client side code
fact(0) -> 1;
fact(N) -> N * fact(N-1).

isEven(0) -> true;
isEven(N) -> isOdd(N-1).

isOdd(0) -> false;
isOdd(N) -> isEven(N-1).

start_client(Server_Node) ->
    {Mod, Bin, File} = code:get_object_code(client),
    rpc:multicall(code, load_binary, [Mod, File, Bin]),
    spawn(client, client, [Server_Node, fun client:fact/1, 4]).

The above code will load client.beam to all nodes.  If I only want send
client.beam to server, where the code is really executed, I can use
rpc:call/4 instead.   There are two other trivial consideration in the above

For one thing, I send the whole file to the server.  The function is sent
with other irrelevant functions.

For another thing, what if I want to send a complex function which employs
functions from other modules?  Shall I know every details about the complex
function and send involved files myself?  For example, isEven/1 is defined
in E.erl and isOdd is defined in O.erl.  Both files are well documented
because the functionality and usage of each function are clearly specified.
 Programmers could use isEven and isOdd as APIs.  What users don't know (and
don't need to know) is, how isOdd and isEven is implemented.


More information about the erlang-questions mailing list