The message responding works
Now we need to implement/test a request.
A request is done with MessageResponder::SendRequest and for this we need to set the send function with MessageResponder::SetSendFunction.
Then we can launch a request in 2 ways with
std::future<Message> SendRequest(const Message &, Handler = {});
- We take the returned future and wait for it to become available and has a value
wait_for and valid in case it is not set directly - give a callback as parameter, this function has as signature Handler which is std::function<Reply(const Message &)>
So the first choice gives the possibility to block and wait for a result for a certain amount of time, the second gives the possibility to go straight on and have a routine called when the reply message arrives.
In which cases do we use this, some examples:
- Do a hardware/configuration detection : multiple options here, but we go for b.
- In the UI we just send the message with the detection command and we have a message with the same ID set as a responder. This is completely asynchronous. This will also respond to detections coming in without having requested one
- We use sendrequest with a handler: this will set a one time handler in the requests_ map of MessageResponder. So it will be handled only once. If a request with the same ID is launched twice, we get an exception
- We use the returned future with Wait_for. If this is in the main UI thread, it blocks the UI interactions, so not option for this
- We have a lasertracker that has a list of operations, the next operation can only be started if the previous is completed.
Laser tracker list of commands
We need to chain commands, so the callback of command 1 has to send command 2 and needs access to the list of commands to retrieve the next command.