User manual

return readAdc(0)
def askSensorReading():
value = readSensor()
rpc(rpcSourceAddr(),"tellSensorReading", value)
which can be simplified even further to:
def askSensorReading():
value = readAdc(0)
rpc(rpcSourceAddr(),"tellSensorReading", value)
You might think that instead of calling
rpc(address_of_node_B, "askSensorReading")
node A could simply call
rpc(address_of_node_B, "readAdc", 0)
Although this will result in node B calling his readAdc() funcon, it won’t actually cause any results to be sent
back to the caller.
This is where the callback() funcon comes in. Replacing
rpc(address_of_node_B, "readAdc", 0)
with
rpc(address_of_node_B, "callback", "tellSensorReading", "readAdc", 0)
will do what you want the readAdc() funcon will be invoked on node B, and the results automacally
reported back to node A via the tellSensorReading() funcon.
Noce that in the actual callback() invocaon, you must provide the final funcon to be invoked (“called back”)
in addion to sll having to specify the inial funcon to be called, as well as any parameters it needs.
Noce also that in this case, we only had to add code to node A’s script we didn’t have to create an
“askSensorReading” funcon at all.
It’s also important to note that callback is not limited to invoking built-in funcons. For example, if we had
retained the original readSensor() roune, it could be remotely invoked and the result automacally returned
via:
rpc(address_of_node_B, "callback", "tellSensorReading", "readSensor")
One common user of the callback() funcon is Portal itself. When you click on a funcon name in the Node Info
pane of Portal, Portal is using callback() not rpc() behind the scenes to automacally get the result value so
that it can print it in the Portal Event Log. For example, if you click on the random() funcon of a node, what
Portal really sends is something equivalent to:
rpc(address_of_node, "callback", "printRtnVal", "random")
To summarize: callback() just lets you get the same data with less SNAPpy script.
Callout(nodeAddress, callback, remoteFunction, remoteFunctionArgs…)
Built-in funcon callout() just takes the callback() concept one step further.
Instead of asking a node to invoke a funcon and then call you back with the result, callout() is used to ask a
node to call a funcon and then call a third node with the results.
36 SNAP® Network Operang System