CsAdvertiseService.h
Go to the documentation of this file.
00001 #ifndef CSADVERTISESERVICE
00002 #define CSADVERTISESERVICE 1
00003 #include "Runable.h"
00004 #include "ros/ros.h"
00005 #include <ros/callback_queue.h>
00006 #include <ros/transport_hints.h>
00007 
00008 namespace RosCs {
00009         template<class M,class MRes,class MReq>
00010         class CsAdvertiseService : Runable {
00011                 protected:
00012                         bool (*cscallback)(char**,char**);
00013                         void (*translator)(MReq &,char**);
00014                         void (*translator2Ros)(MRes &,char**);
00015                         void (*freeCS)(char*);
00016                         ros::ServiceServer sub;
00017                         ros::NodeHandle nh;
00018                         ros::CallbackQueue queue;
00019                 public:
00020                         CsAdvertiseService(bool (*cshandle)(char**,char**),void (*translator)(MReq &,char**),void (*translator2Ros)(MRes &,char**),void (*freeCS)(char*)) {
00021                                 this->cscallback=cshandle;
00022                                 this->translator=translator;
00023                                 this->translator2Ros=translator2Ros;
00024                                 this->freeCS = freeCS;
00025                                 this->running = true;
00026                         }
00027                         
00028                         bool HandleServiceCall(MReq &request, MRes &response) {
00029                                 char** requestP = (char**)malloc(sizeof(char*));
00030                                 
00031                                 char** responseP = (char**)malloc(sizeof(char*));
00032                                 translator(request,requestP);
00033 
00034                                 bool ret = cscallback(requestP,responseP);
00035                                 //std::cout << "Success : " << ret << std::endl;
00036                                 if( ret ) {
00037                                         translator2Ros(response,responseP);
00038                                 } else {
00039                                         ret = false;
00040                                 }
00041 
00042                                 free(*requestP);
00043                                 free(requestP);
00044                                 freeCS(*responseP);
00045                                 free(responseP);
00046                                 return ret;
00047                         }
00048                         void AdvertiseSubscribe(ros::NodeHandle* orignh, char* topic) {
00049                                 nh = ros::NodeHandle(*orignh);
00050                                 nh.setCallbackQueue(&queue);
00051                                         sub = nh.advertiseService(topic, &CsAdvertiseService<M,MRes,MReq>::HandleServiceCall,this);
00052                         }
00053                         void Listen() {
00054                                 while(nh.ok() && this->running) {
00055                                         queue.callAvailable(ros::WallDuration(1));
00056                                 }       
00057                                 sub.shutdown();
00058                         }
00059                         void AdvertiseAndListen(ros::NodeHandle* orignh, char* topic) {
00060                                 AdvertiseSubscribe(orignh,topic);
00061                                 Listen();
00062                         }
00063                         ~CsAdvertiseService() {
00064                                 running = false;
00065                                 sub.shutdown();
00066                         }
00067         };
00068 };
00069 #endif


roscs
Author(s): hendrik
autogenerated on Sat Dec 28 2013 16:51:21