Hey all,
I need to implement a service. I can do it using two separate functions or classes each for service server and client server. But my idea is to implement both service server and service client in the same class like below or using interface design pattern.
Class Dummy
{
private:
ros::ServiceClient client;
ros::ServiceServer server;
//constructor
Dummy(ros::NodeHandle &nh) : m_nh(nh) {
//advertising the service
}
bool ServiceCallBack(){};
};
And then
Class instantiation in other class constructor.
Now the problem is, How to initialize both server node and client node by instantiate just one class?. Any idea to do using my concept or using interface concept. your idea is also most welcomed including any documentation about advanced ros.
Thanks in advance
Originally posted by Suri on ROS Answers with karma: 3 on 2020-04-07
Post score: 0
Original comments
Comment by Weasfas on 2020-04-07:
Well, technically you do not need a ServiceClient node to use the ServiceServer, you can use the rosservice call
command to call the service implemented in the node. Having said that, what is the point of having both in the same node, by definition the Server node is executing always waiting for a request, if you manage to have both in the same class you will need to bind an additional thread to the class in order to execute the ros::spin of the server and not stall the Client requests. One of the characteristics of ROS is its modularity, that may be the reason by the tutorials (Server/Client)(Publisher/Subscriber) are explained in separate nodes.
Nevertheless I am going to add my approach as an answer. I did manage to have both in a class, but you really need another thread for the server to process incoming request, if not, the nodehandle stucks.
Comment by Suri on 2020-04-08:
@Weasfas, thank you so much for the good explanation. I will try it in my own way, if it fits, i will come back for discuss in order to get your view.