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:
- Hot code swapping - i.e. "upgrade" code in S such that it has the function foo(). Execute and send back to the client.
- In a distributed manner where nodes are all appropriately registered, do something along the lines of S ! C:foo() - for the purpose of "sending" the function to process/node S
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). Afun
can be sent across the network and applied by a remote machine and in side thefun
, the sender has embedded their address and a way of getting the answer back. So the executor may only see afun
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. Thefun
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:
The server then has code like this:
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: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:
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 !!!