Commit 151fdb3f by Francois Gygi

Server mode accepts multi-line input files.

Output in server mode is a valid XML fragment.
Use <inputfile>.lock file for synchronization.


git-svn-id: http://qboxcode.org/svn/qb/trunk@673 cba15fb0-1239-40c8-b417-11db7ca47a34
parent c7c71f05
......@@ -15,9 +15,10 @@
// UserInterface.C: definition of readCmd and processCmds
//
////////////////////////////////////////////////////////////////////////////////
// $Id: UserInterface.C,v 1.11 2008-11-14 22:11:30 fgygi Exp $
// $Id: UserInterface.C,v 1.12 2008-12-04 20:03:48 fgygi Exp $
#include "UserInterface.h"
#include "qbox_xmlns.h"
#include <string>
#include <list>
#include <unistd.h> // isatty
......@@ -30,30 +31,19 @@
using namespace std;
////////////////////////////////////////////////////////////////////////////////
void wait_for_no_file(const string& readyfilename)
void wait_for_no_file(const string& lockfilename)
{
cout << " waiting for no " << readyfilename << endl;
#if 0
ifstream readyfile;
readyfile.open(readyfilename.c_str());
while ( readyfile )
{
readyfile.open(readyfilename.c_str());
usleep(500000);
cerr << ".";
}
#else
cerr << " waiting for no " << lockfilename << endl;
struct stat statbuf;
int status;
do
{
// stat returns 0 if the file exists
status = stat(readyfilename.c_str(),&statbuf);
usleep(500000);
cerr << ".";
status = stat(lockfilename.c_str(),&statbuf);
usleep(100000);
// cerr << ".";
}
while ( status == 0 );
#endif
}
////////////////////////////////////////////////////////////////////////////////
......@@ -297,27 +287,13 @@ void UserInterface::processCmdsServer ( string inputfilename,
const char *separators = " ;\t";
int i,done=0,cmd_read,status;
string readyfilename = inputfilename + ".ready";
string lockfilename = inputfilename + ".lock";
// in server mode, redirect output to stream qbout
streambuf *qbout_buf;
streambuf *cout_buf;
ofstream qbout;
ifstream qbin;
if ( onpe0_ )
{
cout << " processCmds: switching output to file qbout" << endl;
qbout.open(outputfilename.c_str(),ios_base::trunc);
qbout_buf = qbout.rdbuf();
// save copy of cout streambuf
cout_buf = cout.rdbuf();
// redirect cout
cout.rdbuf(qbout_buf);
cout << " processCmds: redirected cout " << endl;
}
if ( onpe0_ )
cout << prompt << " ";
ofstream tstfile;
......@@ -325,23 +301,38 @@ void UserInterface::processCmdsServer ( string inputfilename,
{
if ( onpe0_ )
{
// create link to signal that Qbox is waiting for a command on cmdstream
tstfile.open(readyfilename.c_str());
// create file to signal that Qbox is waiting for a command on qbin
tstfile.open(lockfilename.c_str());
tstfile << "1" << endl;
tstfile.close();
sync();
//link(inputfilename.c_str(),readyfilename.c_str());
// wait for link to be removed by the driver
// wait for tstfile to be removed by the driver
usleep(100000);
wait_for_no_file(readyfilename.c_str());
wait_for_no_file(lockfilename.c_str());
qbin.open(inputfilename.c_str());
qbin.sync();
qbin.clear();
// save copy of cout streambuf
cout_buf = cout.rdbuf();
qbout.open(outputfilename.c_str(),ios_base::trunc);
qbout_buf = qbout.rdbuf();
// redirect cout
cout.rdbuf(qbout_buf);
cerr << " processCmdsServer: cout streambuf redirected" << endl;
cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
cout << "<fpmd:simulation xmlns:fpmd=\"" << qbox_xmlns() << "\">" << endl;
}
do
{
if ( onpe0_ )
{
// readCmd returns 1 if a command is read, 0 if at EOF
cmd_read = readCmd(cmdline, 256, qbin, echo );
qbin.close();
cout << cmdline << endl;
cout << prompt << " " << cmdline << endl;
}
MPI_Bcast(&cmdline[0],256,MPI_CHAR,0,MPI_COMM_WORLD);
MPI_Bcast(&cmd_read,1,MPI_INT,0,MPI_COMM_WORLD);
......@@ -352,9 +343,6 @@ void UserInterface::processCmdsServer ( string inputfilename,
{
cerr << " read cmd: " << cmdline << endl;
cerr << " executing ... ";
qbout.open(outputfilename.c_str(),ios_base::trunc);
cout.rdbuf(qbout.rdbuf());
cout << prompt << " " << cmdline << endl;
}
// cmdline contains a string of tokens terminated by '\0'
......@@ -373,7 +361,7 @@ void UserInterface::processCmdsServer ( string inputfilename,
if ( onpe0_ )
{
system ( &cmdline[i+1] );
cout << prompt << " ";
// cout << prompt << " ";
}
}
else
......@@ -469,42 +457,40 @@ void UserInterface::processCmdsServer ( string inputfilename,
}
}
delete [] av;
if ( onpe0_ )
cout << prompt << " ";
}
if ( onpe0_ )
{
cerr << " done" << endl;
cout.flush();
qbout.close();
}
}
else // if cmd_read
{
// no command was read from qbin
if ( onpe0_ )
cerr << " eof found in qbin" << endl;
}
// wait before retrying
usleep(500000);
cerr << "done" << endl;
// check if terminate_ flag was set during command execution
if ( onpe0_ )
done = terminate_;
MPI_Bcast(&done,1,MPI_INT,0,MPI_COMM_WORLD);
}
if ( onpe0_ )
cout << " End of command stream " << endl;
} // if cmd_read
} while ( !done && cmd_read );
if ( onpe0_ )
{
// reassign cout streambuf
qbin.close();
cout << " End of command stream " << endl;
cout << "</fpmd:simulation>" << endl;
cout.flush();
qbout.close();
cout.rdbuf(cout_buf);
cout << " processCmds: reassigned cout streambuf" << endl;
cerr << " processCmdsServer: cout streambuf reassigned" << endl;
}
// wait before retrying
usleep(200000);
// remove ready link
remove(readyfilename.c_str());
} // while !done
if ( onpe0_ )
{
// remove lock file
remove(lockfilename.c_str());
sync();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment