Skip to content

Commit

Permalink
added examples of server clients
Browse files Browse the repository at this point in the history
  • Loading branch information
shehzi001 committed Sep 10, 2014
1 parent 4b45603 commit e165ad3
Show file tree
Hide file tree
Showing 15 changed files with 488 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.8.3)
project(ros_simple_client_cpp)

find_package(catkin REQUIRED COMPONENTS
ros_services_examples
roscpp
)

catkin_package(
CATKIN_DEPENDS
ros_services_examples
roscpp
)

include_directories(
${catkin_INCLUDE_DIRS}
)


add_executable(simple_client_cpp_example_node
ros/src/simple_client_cpp_example_node.cpp)

target_link_libraries(simple_client_cpp_example_node
${catkin_LIBRARIES}
)

add_executable(client_cpp_class_based_example_node
ros/src/client_cpp_class_based_example_node.cpp)

target_link_libraries(client_cpp_class_based_example_node
${catkin_LIBRARIES}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<package>
<name>ros_simple_client_cpp</name>
<version>0.0.0</version>
<description>The ros_simple_client_cpp package</description>

<maintainer email="[email protected]">Shehzad Ahmed</maintainer>

<license>GPLv3</license>

<buildtool_depend>catkin</buildtool_depend>
<build_depend>ros_services_examples</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>ros_services_examples</run_depend>
<run_depend>roscpp</run_depend>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* simple_client_cpp_example_node.cpp
*
* Created on: September 10, 2014
* Author: Shehzad Ahmed
**/

#include <ros/ros.h>
#include <ros_services_examples/NodeInfoQuery.h>
#include <ros_services_examples/NodeInfoQueryRequest.h>
#include <ros_services_examples/NodeInfoQueryResponse.h>

class DemoClass
{
public:
DemoClass(const ros::NodeHandle &nh):
nh_(nh)
{
client_ = nh_.serviceClient<ros_services_examples::NodeInfoQuery>("node_info_query");
}

~DemoClass()
{
}

void call_service()
{
if(client_.exists()) {
ROS_INFO("server is running.....");
ros_services_examples::NodeInfoQuery node_info_query;
ros_services_examples::NodeInfoQueryRequest request;
request.node_name.data = "simple_client_cpp_example_node";

node_info_query.request = request;

if(client_.call(node_info_query)) {
ROS_INFO_STREAM("Response recieved :" << node_info_query.response.node_id.data);
} else {
ROS_INFO("Response failed.....");
}
} else {
ROS_INFO("server is not running.....");
}
}

private:
ros::ServiceClient client_;
ros::NodeHandle nh_;
};

int main(int argc, char *argv[])
{
/**
* Initilization of node
**/
ros::init(argc, argv, "simple_client_cpp_example_node");

/**
* Initilization of node handle
**/
ros::NodeHandle nh;

/**
* Printing Information message on the console.
**/
ROS_INFO("simple_client_cpp_example_node is now running");

DemoClass demo_class(nh);

demo_class.call_service();

/**
* Running a thread to keep spinning untill the user kills the node.
**/

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* simple_client_cpp_example_node.cpp
*
* Created on: September 10, 2014
* Author: Shehzad Ahmed
**/

#include <ros/ros.h>
#include <ros_services_examples/NodeInfoQuery.h>
#include <ros_services_examples/NodeInfoQueryRequest.h>
#include <ros_services_examples/NodeInfoQueryResponse.h>


int main(int argc, char *argv[])
{
/**
* Initilization of node
**/
ros::init(argc, argv, "simple_client_cpp_example_node");

/**
* Initilization of node handle
**/
ros::NodeHandle nh;

/**
* Printing Information message on the console.
**/
ROS_INFO("simple_client_cpp_example_node is now running");

ros::ServiceClient client = nh.serviceClient<ros_services_examples::NodeInfoQuery>("node_info_query");

if(client.exists()) {
ROS_INFO("server is running.....");
ros_services_examples::NodeInfoQuery node_info_query;
ros_services_examples::NodeInfoQueryRequest request;
request.node_name.data = "simple_client_cpp_example_node";

node_info_query.request = request;

if(client.call(node_info_query)) {
ROS_INFO_STREAM("Response recieved :" << node_info_query.response.node_id.data);
} else {
ROS_INFO("Response failed.....");
}

} else {
ROS_INFO("server is not running.....");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8.3)
project(ros_simple_client_python)

find_package(catkin REQUIRED COMPONENTS
ros_services_examples
rospy
)

catkin_package(
CATKIN_DEPENDS
ros_services_examples
rospy
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<package>
<name>ros_simple_client_python</name>
<version>0.1.0</version>
<description>The ros_simple_client_python package</description>

<maintainer email="[email protected]">Shehzad Ahmed</maintainer>

<license>GPLv3</license>

<buildtool_depend>catkin</buildtool_depend>
<build_depend>ros_services_examples</build_depend>
<build_depend>rospy</build_depend>
<run_depend>ros_services_examples</run_depend>
<run_depend>rospy</run_depend>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
"""
This script demonstrates the way of requesting service.
"""
#-*- encoding: utf-8 -*-
__author__ = 'shehzad ahmed'
import rospy
from ros_services_examples.srv import *

def initlize_node():
'''
Initilize node and spin which simply keeps python
from exiting until this node is stopped
'''
rospy.init_node('simple_client_python_example_node', anonymous=False)
rospy.loginfo("simple_client_python_example_node is now running")

rospy.wait_for_service('node_info_query')

try:
node_info_service = rospy.ServiceProxy('node_info_query', NodeInfoQuery)
req = NodeInfoQueryRequest()
req.node_name.data = "simple_client_python_example_node"
resp = node_info_service(req)
rospy.loginfo("Response recieved : %d",resp.node_id.data)
except rospy.ServiceException, e:
print "Service call failed: %s"%e

rospy.spin()

if __name__ == '__main__':
initlize_node()
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.8.3)
project(ros_simple_server_cpp)

find_package(catkin REQUIRED COMPONENTS
ros_services_examples
roscpp
)

catkin_package(
CATKIN_DEPENDS
ros_services_examples
roscpp
)

include_directories(
${catkin_INCLUDE_DIRS}
)


add_executable(simple_server_cpp_example_node
ros/src/simple_server_cpp_example_node.cpp)

target_link_libraries(simple_server_cpp_example_node
${catkin_LIBRARIES}
)

add_executable(server_cpp_class_based_example_node
ros/src/server_cpp_class_based_example_node.cpp)

target_link_libraries(server_cpp_class_based_example_node
${catkin_LIBRARIES}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<package>
<name>ros_simple_server_cpp</name>
<version>0.0.0</version>
<description>The ros_simple_server_cpp package</description>

<maintainer email="[email protected]">Shehzad Ahmed</maintainer>

<license>GPLv3</license>

<buildtool_depend>catkin</buildtool_depend>
<build_depend>ros_services_examples</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>ros_services_examples</run_depend>
<run_depend>roscpp</run_depend>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* server_cpp_class_based_example_node.cpp
*
* Created on: September 10, 2014
* Author: Shehzad Ahmed
*/
#include <ros/ros.h>
#include <ros_services_examples/NodeInfoQuery.h>
#include <ros_services_examples/NodeInfoQueryRequest.h>
#include <ros_services_examples/NodeInfoQueryResponse.h>

class DemoClass
{
public:
DemoClass(ros::NodeHandle nh):
nh_(nh)
{
service = nh.advertiseService("node_info_query", &DemoClass::serviceCB, this);
}

~DemoClass()
{
}

bool serviceCB(ros_services_examples::NodeInfoQueryRequest &req,
ros_services_examples::NodeInfoQueryResponse &resp)
{
ROS_INFO_STREAM("Request recieved :" << req.node_name.data);
resp.node_id.data = 1.0;
return true;
}

private:
ros::ServiceServer service;
ros::NodeHandle nh_;

};

int main(int argc, char *argv[])
{
/**
* Initilization of node
**/
ros::init(argc, argv, "server_cpp_class_based_example_node");

/**
* Initilization of node handle
**/
ros::NodeHandle nh;

/**
* Printing Information message on the console.
**/
ROS_INFO("server_cpp_class_based_example_node is now running");

DemoClass demo_class(nh);
/**
* Running a thread to keep spinning untill the user kills the node.
**/
ros::spin();

return 0;
}
Loading

0 comments on commit e165ad3

Please sign in to comment.