34 #include <sys/types.h> 35 #include <sys/socket.h> 44 using std::ostringstream;
50 #include "BESServerHandler.h" 51 #include "Connection.h" 53 #include "BESXMLInterface.h" 54 #include "TheBESKeys.h" 55 #include "BESInternalError.h" 56 #include "ServerExitConditions.h" 58 #include "PPTStreamBuf.h" 59 #include "PPTProtocol.h" 62 #include "BESStopWatch.h" 64 BESServerHandler::BESServerHandler()
71 cerr <<
"Unable to determine method to handle clients, " 72 <<
"single or multiple as defined by BES.ProcessManagerMethod" <<
": " << e.
get_message() << endl;
73 exit(SERVER_EXIT_FATAL_CANNOT_START);
75 if (_method !=
"multiple" && _method !=
"single") {
76 cerr <<
"Unable to determine method to handle clients, " 77 <<
"single or multiple as defined by BES.ProcessManagerMethod" << endl;
78 exit(SERVER_EXIT_FATAL_CANNOT_START);
91 if (_method ==
"single") {
103 if ((pid = fork()) < 0) {
104 string error(
"fork error");
105 const char* error_info = strerror(errno);
106 if (error_info) error +=
" " + (string) error_info;
122 if( ( pid1 = fork() ) < 0 )
126 kill( main_process, 9 );
127 perror(
"fork error" );
128 exit( SERVER_EXIT_CHILD_SUBPROCESS_ABNORMAL_TERMINATION );
138 c->closeConnection();
139 exit( SERVER_EXIT_CHILD_SUBPROCESS_NORMAL_TERMINATION );
141 if( waitpid( pid, NULL, 0 ) != pid )
144 string error(
"waitpid error" );
145 const char *error_info = strerror( errno );
147 error +=
" " + (string)error_info;
150 c->closeConnection();
158 string ip = c->getSocket()->getIp();
159 strm <<
"ip " << ip <<
", port " << c->getSocket()->getPort();
160 string from = strm.str();
162 map<string, string> extensions;
174 done = c->receive(extensions, &ss);
178 if (extensions[
"status"] == c->exit()) {
187 BESDEBUG(
"beslistener",
"BESServerHandler::execute() - Received PPT_EXIT_NOW in an extension chunk." << endl);
193 BESDEBUG(
"beslistener",
"BESServerHandler::execute() - Closing client connection." << endl);
195 c->closeConnection();
197 BESDEBUG(
"beslistener",
"BESServerHandler::execute() - Client connection has been closed." << endl);
199 BESDEBUG(
"beslistener",
"BESServerHandler::execute() - Calling exit(CHILD_SUBPROCESS_READY) which has a value of " 200 << CHILD_SUBPROCESS_READY << endl);
202 exit(CHILD_SUBPROCESS_READY);
210 string cmd_str = ss.str();
211 BESDEBUG(
"server2",
"BESServerHandler::execute - command = " << cmd_str << endl);
212 BESDEBUG(
"server",
"BESServerHandler::execute - command ... " << endl);
215 if (BESISDEBUG( TIMING_LOG ))
216 sw.
start(
"BESServerHandler::execute");
218 int descript = c->getSocket()->getSocketDescriptor();
220 unsigned int bufsize = c->getSendChunkSize();
222 std::streambuf *holder;
223 holder = cout.rdbuf();
227 int status = cmd.execute_request(from);
234 BESDEBUG(
"server",
"BESServerHandler::execute - " <<
"executed successfully" << endl);
240 BESDEBUG(
"server",
"BESServerHandler::execute - " 241 <<
"error occurred" << endl );
248 map<string,string> extensions;
249 extensions[
"status"] =
"error";
250 if( status == BES_INTERNAL_FATAL_ERROR )
252 extensions[
"exit"] =
"true";
254 c->sendExtensions( extensions );
258 cmd.finish_with_error( status );
264 cout.rdbuf( holder );
270 case BES_INTERNAL_FATAL_ERROR:
274 cout <<
"BES server " << getpid()
275 <<
": Status not OK, dispatcher returned value " 278 c->closeConnection();
279 exit( CHILD_SUBPROCESS_READY );
281 BESDEBUG(
"beslistener",
"BES Internal Fatal Error");
283 *(BESLog::TheLog()) <<
"beslistener: BES Internal Fatal Error; child returning " 284 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl;
286 c->closeConnection();
287 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
290 case BES_INTERNAL_ERROR:
291 case BES_SYNTAX_USER_ERROR:
292 case BES_FORBIDDEN_ERROR:
293 case BES_NOT_FOUND_ERROR:
303 c->closeConnection();
314 strm << BESIndent::LMarg <<
"BESServerHandler::dump - (" << (
void *)
this <<
")" << endl;
316 strm << BESIndent::LMarg <<
"server method: " << _method << endl;
317 BESIndent::UnIndent();
exception thrown if inernal error encountered
virtual std::string get_message()
get the error message for this exception
virtual bool start(string name)
Abstract exception class for the BES with basic string message.
virtual void dump(ostream &strm) const
dumps information about this object
Entry point into BES using xml document requests.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
static BESKeys * TheKeys()