Posted 27 October 2012 - 05:23 AM
Hello all.
This API (of sorts) implements "remote procedure calls" which operate over rednet.
In short, this utility allows someone to call functions and receive return values from another computer over rednet.
Using this API/Utility is very simple:
Remote Procedure Calls on Pastebin
Or run this in-game:
Those of you who want to work with turtles may want to look here:
Turtle RPC Bindings on Pastebin
Alternatively:
This program registers every function in the turtle API with RPC.
This API (of sorts) implements "remote procedure calls" which operate over rednet.
In short, this utility allows someone to call functions and receive return values from another computer over rednet.
Using this API/Utility is very simple:
- Have the target computer (the one with the function you want to run) register the function with
function_body can be an actual function object or a string that can be loaded (like from string.dump or loadstring.)rpc.new(function_name, function_body)
- The computer that is calling the function simply calls
where functionName is the full name of the function (e.x myFunction and myAPI.myFunction) and arguments are the arguments you would pass to the function normally. For example:rpc.remoteCall(targetID, functionName, arguments)
rpc.remoteCall(20, "myAPI.doStuff", 50, aFunction, {50,25,7612}, "A string")
- rpc.remoteCall() will return whatever the function would normally return, or an error like attempt to call nil or etc.
- This utility/API provides events for developers to use:
- rpc_call is fired whenever another computer calls a function on the local computer, regardless of whether the function call would actually go through or not. Parameters are the function to be called and whatever arguments to pass to it in a table.
- rpc_return is fired whenever another computer sends the return values from a previous remote call. Parameters are the returning ID (the computer with the function on it), the function that was called, and the return values in a table.
- For obvious reasons, you cannot call rpc.new and rpc.loadAPI remotely.
- Even though there are measures in place to help prevent hijacking, I cannot stop everything. Don't be an idiot and you'll be safe and fine.
- In order to use rpc.remoteCall and events, you need to run rpc.run alongside shell at startup. How you do this is up to you.
- You can use APIs with this. Simply call rpc.loadAPI(file) and have your clients/servers do rpc.remoteCall(target, "myAPI.myFunction", args) instead of rpc.remoteCall(target, "myFunction", args).
- You need to register your functions with rpc.new and rpc.loadAPI before you use them remotely.
Remote Procedure Calls on Pastebin
Or run this in-game:
pastebin get AJEnqBtA rpc
Those of you who want to work with turtles may want to look here:
Turtle RPC Bindings on Pastebin
Alternatively:
pastebin get LiB1sHBe turtleRPC
This program registers every function in the turtle API with RPC.