mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-04 08:20:35 +08:00
ServiceClient<>::hasPendingCallToServer()
This commit is contained in:
@@ -165,6 +165,13 @@ protected:
|
||||
bool operator()(const CallState& state) const { return (state.getCallID() == id) && !state.hasTimedOut(); }
|
||||
};
|
||||
|
||||
struct ServerSearchPredicate
|
||||
{
|
||||
const NodeID server_node_id;
|
||||
ServerSearchPredicate(NodeID nid) : server_node_id(nid) { }
|
||||
bool operator()(const CallState& state) const { return state.getCallID().server_node_id == server_node_id; }
|
||||
};
|
||||
|
||||
MonotonicDuration request_timeout_;
|
||||
|
||||
ServiceClientBase(INode& node)
|
||||
@@ -329,6 +336,11 @@ public:
|
||||
*/
|
||||
void cancelAllCalls();
|
||||
|
||||
/**
|
||||
* Checks whether there's currently a pending call addressed to the specified node ID.
|
||||
*/
|
||||
bool hasPendingCallToServer(NodeID server_node_id) const;
|
||||
|
||||
/**
|
||||
* Service response callback must be set prior service call.
|
||||
*/
|
||||
@@ -538,6 +550,12 @@ void ServiceClient<DataType_, Callback_, NumStaticCalls_>::cancelAllCalls()
|
||||
SubscriberType::stop();
|
||||
}
|
||||
|
||||
template <typename DataType_, typename Callback_, unsigned NumStaticCalls_>
|
||||
bool ServiceClient<DataType_, Callback_, NumStaticCalls_>::hasPendingCallToServer(NodeID server_node_id) const
|
||||
{
|
||||
return NULL != call_registry_.find(ServerSearchPredicate(server_node_id));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // UAVCAN_NODE_SERVICE_CLIENT_HPP_INCLUDED
|
||||
|
||||
@@ -109,6 +109,8 @@ TEST(ServiceClient, Basic)
|
||||
ASSERT_EQ(0, client2.getNumPendingCalls());
|
||||
ASSERT_EQ(0, client3.getNumPendingCalls());
|
||||
|
||||
ASSERT_FALSE(client1.hasPendingCallToServer(1));
|
||||
|
||||
client1.setCallback(handler.bind());
|
||||
client2.setCallback(client1.getCallback());
|
||||
client3.setCallback(client1.getCallback());
|
||||
@@ -128,6 +130,11 @@ TEST(ServiceClient, Basic)
|
||||
ASSERT_LT(0, client3.call(99, request)); // Will timeout!
|
||||
ASSERT_LT(0, client3.call(1, request)); // OK - second request
|
||||
|
||||
ASSERT_TRUE(client1.hasPendingCallToServer(1));
|
||||
ASSERT_TRUE(client2.hasPendingCallToServer(1));
|
||||
ASSERT_TRUE(client3.hasPendingCallToServer(99));
|
||||
ASSERT_TRUE(client3.hasPendingCallToServer(1));
|
||||
|
||||
std::cout << "!!! Spinning!" << std::endl;
|
||||
|
||||
ASSERT_EQ(3, nodes.b.getDispatcher().getNumServiceResponseListeners()); // Listening now!
|
||||
@@ -211,9 +218,11 @@ TEST(ServiceClient, Rejection)
|
||||
|
||||
ASSERT_EQ(1, nodes.b.getDispatcher().getNumServiceResponseListeners());
|
||||
ASSERT_TRUE(client1.hasPendingCalls());
|
||||
ASSERT_TRUE(client1.hasPendingCallToServer(1));
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(200));
|
||||
ASSERT_FALSE(client1.hasPendingCalls());
|
||||
ASSERT_FALSE(client1.hasPendingCallToServer(1));
|
||||
|
||||
ASSERT_EQ(0, nodes.b.getDispatcher().getNumServiceResponseListeners()); // Timed out
|
||||
|
||||
|
||||
Reference in New Issue
Block a user