Reputation: 45
Is there a way to command one node using another node to move to a specific location, such as a particular coordinate? If so, please share the sample code (if possible).
Thank You
Upvotes: 2
Views: 58
Reputation: 2280
You can write an agent that receives a message from another node and just sets its own location based on that. Example code snippet:
void processMessage(Message msg) {
if (msg instanceof DatagramNtf && msg.protocol == MY_PROTOCOL) {
// extract location x, y from msg based on your PDU encoding
def node = agentForService Services.NODE_INFO
node.location = [x, y]
}
}
This method will allow you to implement complex behaviors such as slowly moving the node to the location, in your agent.
As an alternative, if all you need is to instantaneously change the location, you could also consider running a command/script using the remote service on the target node (e.g. 2) to set its own location (e.g. [10,10]):
send new RemoteScriptReq(to: 2, scriptName: '@node.location = [10,10]')
The @
prefix causes the scriptName
to be interpreted as a command, rather than a locally stored script. Do note that this prefix will work in releases up to 1.4, but the behavior will be changed in the upcoming UnetStack release to use a new message RemoteExecReq
:
send new RemoteExecReq(to: 2, command: 'node.location = [10,10]')
For the remote service to work, you need the RemoteControl
agent enabled in the stack on the target node.
Upvotes: 3