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() funcon, it won’t actually cause any results to be sent
back to the caller.
This is where the callback() funcon 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() funcon will be invoked on node B, and the results automacally
reported back to node A via the tellSensorReading() funcon.
Noce that in the actual callback() invocaon, you must provide the final funcon to be invoked (“called back”)
in addion to sll having to specify the inial funcon to be called, as well as any parameters it needs.
Noce also that in this case, we only had to add code to node A’s script – we didn’t have to create an
“askSensorReading” funcon at all.
It’s also important to note that callback is not limited to invoking built-in funcons. For example, if we had
retained the original readSensor() roune, it could be remotely invoked and the result automacally returned
via:
rpc(address_of_node_B, "callback", "tellSensorReading", "readSensor")
One common user of the callback() funcon is Portal itself. When you click on a funcon name in the Node Info
pane of Portal, Portal is using callback() – not rpc() – behind the scenes to automacally get the result value so
that it can print it in the Portal Event Log. For example, if you click on the random() funcon 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 funcon callout() just takes the callback() concept one step further.
Instead of asking a node to invoke a funcon and then call you back with the result, callout() is used to ask a
node to call a funcon and then call a third node with the results.
36 SNAP® Network Operang System