This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KillaVanilla's profile picture

[1.32+] Remote Procedure Calls - Now with Turtle API Bindings!

Started by KillaVanilla, 27 October 2012 - 03:23 AM
KillaVanilla #1
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:
  1. Have the target computer (the one with the function you want to run) register the function with
    rpc.new(function_name, function_body)
    function_body can be an actual function object or a string that can be loaded (like from string.dump or loadstring.)
  2. The computer that is calling the function simply calls
    
    										rpc.remoteCall(targetID, functionName, arguments)
    										
    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(20, "myAPI.doStuff", 50, aFunction, {50,25,7612}, "A string")
    										
  3. rpc.remoteCall() will return whatever the function would normally return, or an error like attempt to call nil or etc.
There are also a few things you may want to keep in mind:
  • 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.
Download:
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.
Sp1rit #2
Posted 01 January 2013 - 04:40 AM
Thx for that API. Could be really useful but i didnt quite get it.

Could you provide a basic example?

If I use rpc.run() it runs in an endless loop so how do i get the events?
And if I use the turtle functions the remoteCall stucks after sending. It seems that it doesnt get the return value.
The Blue daNoob #3
Posted 01 January 2013 - 01:37 PM
The method that this API uses is not at all the one I expected. However, it works well and is very secure, since the "slave" machine has to register a function before the other computer can run it. Well done.
coaster3000 #4
Posted 22 May 2013 - 03:33 PM
One security risk is having random people on server using this.. This is incredibly useful. But when you do implement it.
Be sure to implement password logins and such so your remote computer is not controlled by a unwanted computer..

:)/>

However then. This should be simple to do. Just put in a unique passcode (Not the most secure but will do temporarily for anyone)


Thx for that API. Could be really useful but i didnt quite get it.

Could you provide a basic example?

If I use rpc.run() it runs in an endless loop so how do i get the events?
And if I use the turtle functions the remoteCall stucks after sending. It seems that it doesnt get the return value.

The run function is actually a endless loop because its actually looping and readin its events. Basically when the function runs. It turns the computer into remote control mode.