00001
00011 #include "libsarathi.hpp"
00012 #include "../gsoap/sarathi.nsmap"
00013 #include <iostream>
00014 #include <string>
00015 #include <sstream>
00016 #include <cassert>
00017 #include <cstring>
00018 #include <cstdio>
00019 #include <cerrno>
00020 #include <unistd.h>
00021
00022 namespace libsarathi {
00023
00024 sException::sException() throw() : std::runtime_error("Undefined fault")
00025 {
00026 this->hintstring = "No hint available";
00027 this->detailstring = "No details available";
00028 this->errcode = 0;
00029 }
00030
00031 sException::sException(std::string faultstr, std::string detailstr) throw() : std::runtime_error(faultstr)
00032 {
00033 this->detailstring = (detailstr);
00034 this->hintstring = "No hint available";
00035 this->errcode = 0;
00036
00037 }
00038
00039 sException::sException(std::string faultstr, std::string detailstr, int err) throw() : std::runtime_error(faultstr)
00040 {
00041 this->detailstring = (detailstr);
00042 this->hintstring = "No hint available";
00043 this->errcode = err;
00044
00045 }
00046
00047 sException::~sException() throw()
00048 {
00049 this->exceptiondata_array.reset();
00050 this->exceptiondata_point.reset();
00051 this->errcode = 0;
00052 }
00053
00054 const char* sException::detail() const throw()
00055 {
00056 return this->detailstring.c_str();
00057 }
00058
00059 const char* sException::hint() const throw()
00060 {
00061 return this->hintstring.c_str();
00062 }
00063
00064 void sException::get_exceptiondata(hyperpoint &point) const throw()
00065 {
00066 point = this->exceptiondata_point;
00067 return;
00068 }
00069
00070 void sException::get_exceptiondata(hyperarray &array) const throw()
00071 {
00072 array = this->exceptiondata_array;
00073 return;
00074 }
00075
00076 int sException::errorcode() const throw()
00077 {
00078 return this->errcode;
00079 }
00080
00081 serverProxy::serverProxy()
00082 {
00083 sarathiServer = new sarathiProxy;
00084 sarathiServer->omode = SOAP_XML_INDENT;
00085 sarathiServer->soap_endpoint = NULL;
00086 }
00087
00088 serverProxy::~serverProxy()
00089 {
00090 if (sarathiServer->soap_endpoint != NULL)
00091 free ((void *)sarathiServer->soap_endpoint);
00092 delete sarathiServer;
00093 }
00094
00095 int serverProxy::initialize (const std::string url, int port)
00096 {
00097 std::string endpoint;
00098 endpoint += url;
00099 endpoint += ":";
00100 std::stringstream ss;
00101 ss << port;
00102 endpoint += ss.str();
00103
00104 sarathiServer->soap_endpoint = strdup(endpoint.c_str());
00105
00106
00107 return (ALL_OK);
00108 }
00109
00110 int serverProxy::stop (bool terminate_on_error)
00111 {
00112 bool ok=false;
00113 if (sarathiServer->stop(terminate_on_error, ok)){
00114 sarathiServer->soap_print_fault(stderr);
00115 return (SOME_ERROR_OCCURRED);
00116 }
00117 return (ALL_OK);
00118 }
00119
00120 int serverProxy::set_planner_file(std::string filename)
00121 {
00122 bool ok = false;
00123 if (sarathiServer->set_planner_file(filename, ok) != SOAP_OK){
00124 handle_error();
00125 return (SOME_ERROR_OCCURRED);
00126 }
00127 return (ALL_OK);
00128 }
00129
00130 int serverProxy::plan_to(std::string spacetype, hyperpoint *from, hyperpoint *to)
00131 {
00132 bool ok = false;
00133 if (sarathiServer->plan_to(spacetype, from, to, ok)){
00134 handle_error();
00135 return (SOME_ERROR_OCCURRED);
00136 }
00137 return (ALL_OK);
00138 }
00139
00140 int serverProxy::plan_along(std::string spacetype, hyperarray *keypoints)
00141 {
00142 bool ok = false;
00143 if (sarathiServer->plan_along(spacetype, keypoints, ok) != SOAP_OK){
00144 handle_error();
00145 return (SOME_ERROR_OCCURRED);
00146 }
00147 return (ALL_OK);
00148 }
00149
00150 int serverProxy::get_path(std::string spacetype, hyperarray *path)
00151 {
00152 if (sarathiServer->get_path(spacetype, path) != SOAP_OK){
00153 handle_error();
00154 return (SOME_ERROR_OCCURRED);
00155 }
00156 return (ALL_OK);
00157 }
00158
00159 int serverProxy::clear_path(void)
00160 {
00161 bool ok = false;
00162 if (sarathiServer->clear_path(NULL,ok) != SOAP_OK){
00163 handle_error();
00164 return (SOME_ERROR_OCCURRED);
00165 }
00166 return (ALL_OK);
00167 }
00168
00169 int serverProxy::get_position (std::string spacetype, hyperpoint *curr_pos)
00170 {
00171 if (sarathiServer->get_position(spacetype, curr_pos) != SOAP_OK){
00172 handle_error();
00173 return (SOME_ERROR_OCCURRED);
00174 }
00175 return (ALL_OK);
00176 }
00177
00178 int serverProxy::set_position(std::string spacetype, hyperpoint *des_pos)
00179 {
00180 bool ok = false;
00181 if (sarathiServer->set_position(spacetype, des_pos, ok) != SOAP_OK){
00182 handle_error();
00183 return (SOME_ERROR_OCCURRED);
00184 }
00185 return (ALL_OK);
00186 }
00187
00188 int serverProxy::move(void)
00189 {
00190 bool ok = false;
00191 if (sarathiServer->move(NULL, ok) != SOAP_OK){
00192 handle_error();
00193 return (SOME_ERROR_OCCURRED);
00194 }
00195 return (ALL_OK);
00196 }
00197
00198 int serverProxy::move_to (std::string spacetype, hyperpoint *point)
00199 {
00200 bool ok = false;
00201 if (sarathiServer->move_to(spacetype, point, ok)){
00202 handle_error();
00203 return (SOME_ERROR_OCCURRED);
00204 }
00205 return (ALL_OK);
00206 }
00207
00208 int serverProxy::move_along (std::string spacetype, hyperarray *pointlist)
00209 {
00210 bool ok = false;
00211 if (sarathiServer->move_along(spacetype, pointlist, ok)){
00212 handle_error();
00213 return (SOME_ERROR_OCCURRED);
00214 }
00215 return (ALL_OK);
00216 }
00217
00218 int serverProxy::get_planning_status(std::string &status, bool &done, bool &result)
00219 {
00220 status.clear();
00221 if (sarathiServer->get_planning_status(NULL, status) != SOAP_OK){
00222 handle_error();
00223 return (SOME_ERROR_OCCURRED);
00224 }
00225
00226
00227 if (status.find("idle") != std::string::npos)
00228 done = true;
00229 else{
00230 done = false;
00231 result = false;
00232 return (ALL_OK);
00233 }
00234 if (status.find("success") != std::string::npos)
00235 result = true;
00236 else
00237 result = false;
00238 return (ALL_OK);
00239 }
00240
00241 int serverProxy::get_motion_status(std::string &status, bool &done, bool &result)
00242 {
00243 status.clear();
00244 if (sarathiServer->get_motion_status(NULL, status) != SOAP_OK){
00245 handle_error();
00246 return (SOME_ERROR_OCCURRED);
00247 }
00248 if (status.find("idle") != std::string::npos)
00249 done = true;
00250 else{
00251 done = false;
00252 result = false;
00253 return (ALL_OK);
00254 }
00255 if (status.find("ERROR") != std::string::npos){
00256 done = true;
00257 result = false;
00258 }
00259 else
00260 result = true;
00261
00262 return (ALL_OK);
00263 }
00264
00265 void serverProxy::handle_error()
00266 {
00267 int error_code = 0;
00268 std::string fault = sarathiServer->soap_fault_string();
00269 if (fault.find("Connection refused") != std::string::npos){
00270 error_code = ECONNREFUSED;
00271 }
00272 else if (fault.find("busy") != std::string::npos){
00273 error_code = EBUSY;
00274 }
00275 else if (fault.find("not found") != std::string::npos){
00276 error_code = ENOENT;
00277 }
00278 else if (fault.find("not yet implemented") != std::string::npos){
00279 error_code = EBADRQC;
00280 }
00281 else {
00282 error_code = EREMOTEIO;
00283 }
00284
00285 throw sException(sarathiServer->soap_fault_string(), sarathiServer->soap_fault_detail(), error_code);
00286 }
00287 }