diagnosis_repair.cpp
Go to the documentation of this file.
00001 /*
00002 * Diagnosis Repair is a TCP protocol based client that interacts with diagnosis planner for the repair.
00003 *
00004 * Copyright (c).2012. OWNER: Institute for Software Technology, TU Graz Austria.
00005 * Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00006 * All rights reserved.
00007 *    This program is free software: you can redistribute it and/or modify
00008 *    it under the terms of the GNU General Public License as published by
00009 *    the Free Software Foundation, either version 3 of the License, or
00010 *    (at your option) any later version.
00011 *
00012 *    This program is distributed in the hope that it will be useful,
00013 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 *    GNU General Public License for more details.
00016 *
00017 *    You should have received a copy of the GNU General Public License
00018 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 *
00020 */
00021 
00022 #include <sys/socket.h>
00023 #include <sys/types.h>
00024 #include <netinet/in.h>
00025 #include <iostream>
00026 #include <netdb.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 #include <errno.h>
00032 #include <map>
00033 #include <ros/ros.h>
00034 #include <actionlib/client/simple_action_client.h>
00035 #include <actionlib/client/terminal_state.h>
00036 #include <tug_ist_diagnosis_msgs/Observations.h>
00037 #include <tug_ist_diagnosis_msgs/Diagnosis.h>
00038 #include <tug_ist_diagnosis_msgs/DiagnosisResults.h>
00039 #include <tug_ist_diagnosis_msgs/DiagnosisRepairAction.h>
00040 #include <tug_ist_diagnosis_msgs/DiagnosisRepairGoal.h>
00041 #include <tug_ist_diagnosis_msgs/DiagnosisRepairResult.h>
00042 typedef actionlib::SimpleActionClient<tug_ist_diagnosis_msgs::DiagnosisRepairAction> Node_client;
00043 using std::vector;
00044 using namespace std;
00045 class Diagnosis_Repair_Client
00046 {
00047 
00048 protected:
00049          ros::NodeHandle nh_;
00050          ros::Subscriber diag_sub,obs_sub;
00051          int sock, bytes_recieved;  
00052          char recv_data_buff[1024];
00053          char recv_data[1024];
00054          int last_rcv_indx;
00055          struct hostent *host;
00056          struct sockaddr_in server_addr;  
00057          char *send_data;
00058          vector<std::string> obs_list;
00059          tug_ist_diagnosis_msgs::DiagnosisRepairGoal goal;
00060          tug_ist_diagnosis_msgs::DiagnosisRepairResult result;
00061          Node_client strn,stpn;
00062          bool executing_plan;
00063          map<string,Node_client> ac_map;
00064          int total_chunk;
00065          
00066 public:
00067 
00068     Diagnosis_Repair_Client(ros::NodeHandle nh) : strn("start_node", true), stpn("stop_node", true)
00069     {      nh_ = nh;
00070            executing_plan = false;  
00071            last_rcv_indx = 0;                              
00072            diag_sub = nh_.subscribe("/diagnosis", 1, &Diagnosis_Repair_Client::diagnosisCB, this);
00073            obs_sub = nh_.subscribe("/observations", 1, &Diagnosis_Repair_Client::observationsCB, this);
00074            ROS_INFO("\nWaiting for the Node Action Servers.......");
00075            strn.waitForServer();
00076            stpn.waitForServer();
00077            connect_to_Server();
00078            send_data = "upload domain_file\r\n\r\n\r\n";
00079            send(sock,send_data,strlen(send_data), 0);
00080            ROS_INFO("\nupload domain_file command sent and now wait for recieve.......");
00081            recieve_from_server();
00082            
00083            
00084   }
00085   
00086   ~Diagnosis_Repair_Client(){
00087       disconnect_to_Server();
00088   }
00089   
00090   void get_plan(string pfile_str){
00091         string str="";
00092         str.append("upload problem_file\r\n");
00093         str.append(pfile_str);
00094         str.append("\r\n\r\n\r\n");
00095         send_data=const_cast<char*>(str.data());
00096         send(sock,send_data,strlen(send_data), 0);
00097         recieve_from_server();
00098         send_data = "result\r\n\r\n\r\n";
00099         send_query_to_server(send_data);
00100         int i = 5;
00101         std::string res;
00102         std::string action_server;
00103         vector<std::string> params;
00104         if(recv_data[5]=='N' && recv_data[6]== 'i' && recv_data[7]== 'l' )
00105                executing_plan = false;
00106         else while(i<total_chunk-1){
00107                 res+=recv_data[i];
00108                 if(recv_data[i]==' '){
00109                         int indx1 = res.find("(")+1;
00110                         int indx2 = res.find(")");
00111                         action_server = res.substr(0,indx1-1);
00112                         params.push_back(res.substr(indx1,indx2-indx1).c_str());
00113                         execute_plan(action_server,params);
00114                         params.clear();
00115                         res="";
00116                 }
00117                 i++;
00118              }  
00119   }
00120 
00121   void execute_plan(std::string action_server ,vector<std::string> params){
00122         ROS_INFO("ac:%s, params:%s",action_server.c_str(),params[0].c_str());
00123         std::string str_start,str_stop;
00124         str_start = "start_node";
00125         str_stop = "stop_node";
00126         if(action_server.compare(str_start.c_str())==0){
00127                 ROS_INFO("start_node compared");
00128                 goal.parameter = params;
00129                 strn.sendGoal(goal);
00130                 bool finished_before_timeout = strn.waitForResult(ros::Duration(30.0));
00131                 if(finished_before_timeout)
00132                         executing_plan = false;
00133         }
00134         else if(action_server.compare(str_stop.c_str())==0){
00135                 ROS_INFO("stop_node compared");
00136                 goal.parameter = params;
00137                 stpn.sendGoal(goal);
00138                 bool finished_before_timeout = stpn.waitForResult(ros::Duration(30.0));
00139                 if(finished_before_timeout)
00140                         executing_plan = false;
00141         }
00142         
00143   }  
00144 
00145   void set_port(int port) {
00146         server_addr.sin_family = AF_INET;     
00147         server_addr.sin_port = htons(port);   
00148         server_addr.sin_addr = *((struct in_addr *)host->h_addr);
00149         bzero(&(server_addr.sin_zero),8); 
00150   }
00151   void set_ip(char *ip) {
00152         host = gethostbyname(ip);
00153   }
00154   void send_query_to_server(char *data) {
00155         send(sock,data,strlen(data), 0);
00156         recieve_from_server();
00157   }
00158 
00159 
00160   void connect_to_Server() {
00161         set_ip("127.0.0.1");
00162         set_port(10001);
00163         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00164             perror("Socket Error!");
00165             exit(1);
00166         }
00167 
00168         if (connect(sock, (struct sockaddr *)&server_addr,
00169                     sizeof(struct sockaddr)) == -1) 
00170         {
00171             perror("Connection Error!");
00172             exit(1);
00173         }
00174         ROS_INFO("\nConnected to Repair Server....");
00175   }
00176   void disconnect_to_Server() {
00177         close(sock);
00178         ROS_INFO("\nDisconnected from DiagTCP Server....");
00179   }
00180   void recieve_from_server() {
00181         bool receive_end = false;
00182         total_chunk = 0;
00183        do {     bytes_recieved=recv(sock,recv_data_buff,1024,0);
00184                 memcpy(recv_data, recv_data_buff, bytes_recieved);
00185                 total_chunk+=bytes_recieved;
00186                 if(recv_data[total_chunk-1]=='\n'){
00187                         receive_end = true;
00188                         recv_data[total_chunk-1] = '\0';
00189                         ROS_INFO("receive end is now true %s",recv_data);               
00190                 }
00191         }while(!receive_end);
00192   }
00193 
00194   void observationsCB(const tug_ist_diagnosis_msgs::ObservationsConstPtr & obs_msg){
00195                 for(int o=0;o<obs_msg->obs.size();o++){
00196                    std::string s = obs_msg->obs[o].c_str();
00197                    if (std::find(obs_list.begin(), obs_list.end(), s) != obs_list.end()){
00198                         // DO NOTHING 
00199                    } 
00200                    else {
00201                        std::string ns = "@";
00202                        if(s.at(0)=='~') 
00203                            ns = s.substr(1);
00204                        else
00205                            ns = "~" + s;
00206                        std::vector<std::string>::iterator iter=std::find(obs_list.begin(), obs_list.end(), ns);
00207                        if(iter != obs_list.end()){
00208                            obs_list.erase(iter);
00209                            obs_list.push_back(s);
00210                         }
00211                         else
00212                             obs_list.push_back(s);
00213                    }        
00214                    
00215                  
00216                }// for loop
00217 
00218   }
00219 
00220   void diagnosisCB(const tug_ist_diagnosis_msgs::DiagnosisConstPtr & diag_msg){
00221            vector<tug_ist_diagnosis_msgs::DiagnosisResults> diag_results;
00222            diag_results = diag_msg->diag;
00223            vector<std::string> good,bad;
00224            good = diag_results[0].good;
00225            bad = diag_results[0].bad;
00226            if(bad.size()>0 && !executing_plan){
00227              executing_plan = true;
00228              string pfile_str = make_problem_file_str(good,bad);
00229              get_plan(pfile_str);
00230            }
00231 
00232   }
00233 
00234   string make_problem_file_str(vector<std::string> good,vector<std::string> bad){
00235           vector<std::string> ob_list;
00236           ob_list = obs_list; 
00237           std::string prob_text = "define (problem repair_problem)(:domain repair_domain)\r\n(:requirements :strips :equality :typing :negative-preconditions)\r\n(:objects ";
00238           std::string co_problem="";
00239           std::string goal = "(:goal (and ";
00240           std::string init="(:init ";
00241           for(int i=0;i<ob_list.size();i++) {
00242                std::string obs_str = ob_list[i].c_str();
00243                int indx1 = obs_str.find("(")+1;
00244                int indx2 = obs_str.find(")");
00245                std::string param = obs_str.substr(indx1,indx2-indx1);
00246                std::replace( param.begin(), param.end(), ',', '_');
00247                co_problem = co_problem + param + " ";
00248                if(obs_str.at(0)=='~') {
00249                   std::string predicate = obs_str.substr(1,indx1-2);
00250                   init = init + "(not_"+predicate+" "+param+")\r\n";
00251                   ROS_INFO("~%s",predicate.c_str());
00252                }else{
00253                      std::string predicate = obs_str.substr(0,indx1-1);
00254                      init = init + "("+predicate+" "+param+")\r\n";
00255                      ROS_INFO("%s",predicate.c_str());
00256                     }
00257         }
00258         for(std::vector<std::string>::iterator good_it = good.begin(); good_it != good.end(); ++good_it) {
00259                 init = init + "(good "+ *good_it + ")\r\n";
00260                 goal=goal+"(good "+ *good_it +")\r\n";
00261         }
00262         for(std::vector<std::string>::iterator bad_it = bad.begin(); bad_it != bad.end(); ++bad_it) {
00263                 init = init + "(bad "+ *bad_it +")\r\n";
00264                 goal=goal+"(good "+*bad_it+")\r\n";
00265         }
00266         co_problem = co_problem + ")";
00267         init = init + ")";
00268         goal=goal+"))";
00269         std::string prob = "(" + prob_text + co_problem + init + goal + ")";
00270         ROS_INFO("%s",prob.c_str());
00271         return prob;
00272 
00273   }
00274            
00275   
00276   void spin(){
00277       while( nh_.ok() ){
00278          ros::spinOnce();
00279       }
00280   }
00281 
00282 };
00283 
00284 
00285 
00286 int main(int argc, char** argv)
00287 
00288 {
00289   ros::init(argc, argv, "Diagnosis_Repair_Client_Node");
00290   ros::NodeHandle n;
00291   Diagnosis_Repair_Client *rep_c = new Diagnosis_Repair_Client(n);
00292   rep_c->spin();
00293   return 0;
00294 }
00295 


tug_ist_diagnosis_repair
Author(s): Safdar Zaman
autogenerated on Mon Jan 6 2014 11:51:12