// // Copyright (C) 2000 Stephen A. Torri // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Questions, comments or improvements should be sent to: // s.torri@lancaster.ac.uk or storri@stratos.net // // // Copyright (C) 2000 Stephen A. Torri // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Questions, comments or improvements should be sent to: // s.torri@lancaster.ac.uk or storri@stratos.net #include #include #include #include #include //**************************** // Variables //**************************** //**************************** // Constructor/Destructor //**************************** Job_Process::Job_Process(Job_List* todo, Comms::Master_ptr mstr, Comms::Slave_ptr slv ) : todo_list_(todo), mstr_ptr(mstr), slv_ptr(slv) { isNeeded = true; } Job_Process::~Job_Process(){} //**************************** // Runtime functions //**************************** void Job_Process::run(){ dout << "Job_Process::run - Entering run" << endl; while(isNeeded){ dout << "Job_Process::run - Waiting for jobs" << endl; todo_list_ -> waitForJob(); // get job dout << "Job_Process::run - Getting job" << endl; job = todo_list_ ->getJob(); // perform work if ( job -> directory.size() == 0) { cerr << "Job_Process::run - directory string is blank" << endl; exit(1); } else if ( job -> command.size() == 0) { cerr << "Job_Process::run - command string is blank" << endl; exit(1); } else { dout << "Job_Process::run - Directory: " << job -> directory << endl; dout << "Job_Process::run - Command: " << job -> command << endl; } // Work string dir = job->directory; int x = dir.find("\n"); while (x < string::npos ) { dir.replace(x, 1, "/"); x = dir.find("\n", x+1); } int result = chdir(dir.data()); if ( result < 0) { cerr << "Job_Process::run - Could not change directory" << endl; exit(1); } else { #ifdef DEBUG char buffer[2000]; char* pwd = getcwd(buffer,2000); cout << "Job_Process::run - Present directory is " << pwd << endl; #endif } // perform work //string cmd = (job -> command).insert(0,"|"); cout << job -> command << endl; // Attempt #1 to do work // // Result: No control over process. No ability to ensure work // is done. //ipfstream* jobPipe = new ipfstream (cmd.c_str(), ios::in, 0644); // // Attempt #2 to do work with control by system call result = system((job->command).c_str()); if (result != 0) { cerr << "Job_Process::run - FAILURE" << endl; exit(0); } // inform slave_impl that you are done if (CORBA::is_nil(mstr_ptr)) { cerr << "Job_Process::run - mstr_ptr IS nil" << endl; exit(1); } else if (!(mstr_ptr -> _is_a("IDL:torri.linux/Comms/Master:1.0"))) { cerr << "Job_Process::run - mstr_ptr is not of type Master" << endl; exit(1); } else { dout << "Job_Process::run - mstr_ptr [OK]" << endl; } if (CORBA::is_nil(slv_ptr)) { cerr << "Job_Process::run - slv_ptr IS nil" << endl; exit(1); } else if (!(slv_ptr -> _is_a("IDL:torri.linux/Comms/Slave:1.0"))) { cerr << "Job_Process::run - slv_ptr is not of type Slave" << endl; exit(1); } else { dout << "Job_Process::run - slv_ptr [OK]" << endl; } // Desired Action: Perform one job successfully and then // announce to master than we are free. At present the // slave starts the job and then announcing its free // without waiting for job to end. // Attempt #1 if (( todo_list_ -> size()) == 0 ){ mstr_ptr->slaveFree(slv_ptr); } // Attempt #2: grab pid of the command. dout << "Job_Process::run - called slaveFree on master" << endl; } }