Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00041
00042 #include "ros/ros.h"
00043
00044 #include <iostream>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <unistd.h>
00049 #include <fstream>
00050 #include <cstdlib>
00051
00052
00053 #include <sys/socket.h>
00054 #include <arpa/inet.h>
00055 #include <netinet/in.h>
00056
00057
00058 #include <perception_srvs/David.h>
00059
00060 #define BUFFSIZE 32
00061
00062 using namespace std;
00063 using namespace perception_srvs;
00064
00065 class DavidService
00066 {
00067
00068 private:
00069 ros::NodeHandle node_handle_;
00070 ros::ServiceServer service_;
00071
00072
00073 int sock;
00074
00075 struct sockaddr_in server;
00076 char buffer[BUFFSIZE];
00077 unsigned int msglen;
00078 std::string david_ip;
00079
00080 std::string port_string;
00081 std::string argument;
00082
00083
00084 bool debug_out;
00085
00087 public:
00088 DavidService(const ros::NodeHandle& node_handle) : node_handle_(node_handle)
00089 {
00090 if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
00091 {
00092 ROS_WARN("Failed to create socket");
00093 }
00094 }
00095
00098 void init()
00099 {
00100 debug_out = true;
00101 node_handle_.param("~ip",david_ip,string("127.0.0.1"));
00102 node_handle_.param("~port",port_string,string("19919"));
00103 service_ = node_handle_.advertiseService("david", &DavidService::david, this);
00104 ROS_INFO("DAVID service has been initialized");
00105 }
00106
00109 void start()
00110 {
00111 char *test = "start\n";
00112 msglen = strlen(test);
00113
00114 if (send(sock, test, msglen, 0) != msglen)
00115 {
00116 ROS_INFO("Mismatch in number of sent bytes");
00117 }
00118 if (debug_out)
00119 ROS_INFO("DAVID method: start");
00120 }
00121
00124 void stop()
00125 {
00126 char *test = "stop\n";
00127 msglen = strlen(test);
00128
00129 if (send(sock, test, msglen, 0) != msglen)
00130 {
00131 ROS_INFO("Mismatch in number of sent bytes");
00132 }
00133 if (debug_out)
00134 ROS_INFO("DAVID mehtod: stop");
00135 }
00136
00139 void grabTexture()
00140 {
00141 char *test = "grabTexture\n";
00142 msglen = strlen(test);
00143
00144 if (send(sock, test, msglen, 0) != msglen)
00145 {
00146 ROS_INFO("Mismatch in number of sent bytes");
00147 }
00148 if (debug_out)
00149 ROS_INFO("DAVID mehtod: grabTexture");
00150 }
00151
00154 void save(std::string arg)
00155 {
00156
00157
00158
00159 std::string temp = arg;
00160 temp.append("\n");
00161 const char *test = temp.c_str();
00162 msglen = strlen(test);
00163
00164
00165
00166
00167 if (send(sock, test, msglen, 0) != msglen)
00168 {
00169 ROS_INFO("Mismatch in number of sent bytes");
00170 }
00171 if (debug_out)
00172 ROS_INFO("DAVID mehtod: save");
00173 }
00174
00177 void erase()
00178 {
00179 char *test = "erase\n";
00180 msglen = strlen(test);
00181
00182 if (send(sock, test, msglen, 0) != msglen)
00183 {
00184 ROS_INFO("Mismatch in number of sent bytes");
00185 }
00186 if (debug_out)
00187 ROS_INFO("DAVID mehtod: erase");
00188 }
00189
00192 void eraseTexture()
00193 {
00194 char *test = "eraseTexture\n";
00195 msglen = strlen(test);
00196
00197 if (send(sock, test, msglen, 0) != msglen)
00198 {
00199 ROS_INFO("Mismatch in number of sent bytes");
00200 }
00201 if (debug_out)
00202 ROS_INFO("DAVID mehtod: eraseTexture");
00203 }
00204
00207 void connect2David()
00208 {
00209 ROS_INFO("DAVID: Connecting to DAVID Server...");
00210
00211
00212
00213
00214
00215 msglen = strlen(david_ip.c_str());
00216
00217 memset(&server, 0, sizeof(server));
00218 server.sin_family = AF_INET;
00219 server.sin_addr.s_addr = inet_addr(david_ip.c_str());
00220 server.sin_port = htons(atoi(port_string.c_str()));
00221
00222
00223
00224 if (connect(sock,(struct sockaddr *) &server,
00225 sizeof(server)) < 0)
00226 {
00227 ROS_INFO("Failed to connect with server");
00228 }
00229
00230 ROS_INFO("DAVID: Connected to DAVID Server");
00231 }
00234 void disconnect()
00235 {
00236 close(sock);
00237 }
00238
00240 ~DavidService()
00241 {
00242 close(sock);
00243 }
00244
00246
00247
00248
00249 bool david(David::Request &request, David::Response &resp)
00250 {
00251 argument = request.david_method;
00252 if (argument.find("connect")!=-1)
00253 connect2David();
00254 if (argument.find("start") != -1)
00255 start();
00256 if (argument.find("stop") != -1)
00257 stop();
00258 if (argument.find("erase") != -1)
00259 {
00260 if (argument.find("eraseTexture") != -1)
00261 {
00262 eraseTexture();
00263 }
00264 else
00265 {
00266 erase();
00267 }
00268 }
00269 if (argument.find("grabTexture") != -1)
00270 grabTexture();
00271 if (argument.find("save") != -1)
00272 save(argument);
00273 return true;
00274 }
00275
00276
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 };
00295
00296
00298 int main(int argc, char **argv)
00299 {
00300
00301 ros::init(argc, argv, "david");
00302 ros::NodeHandle n;
00303 DavidService david(n);
00304
00305 david.init();
00306 ros::spin();
00307 return 0;
00308 }
00309