34 #include <sys/types.h>
35 #include <sys/socket.h>
45 using std::ostringstream;
54 #include "BESServerHandler.h"
55 #include "Connection.h"
57 #include "BESXMLInterface.h"
58 #include "TheBESKeys.h"
59 #include "BESInternalError.h"
60 #include "ServerExitConditions.h"
62 #include "PPTStreamBuf.h"
63 #include "PPTProtocol.h"
66 #include "BESStopWatch.h"
70 #define EXIT_ON_INTERNAL_ERROR "BES.ExitOnInternalError"
72 BESServerHandler::BESServerHandler()
79 cerr <<
"Unable to determine method to handle clients, "
80 <<
"single or multiple as defined by BES.ProcessManagerMethod" <<
": " << e.
get_message() << endl;
81 exit(SERVER_EXIT_FATAL_CANNOT_START);
84 if (_method !=
"multiple" && _method !=
"single") {
85 cerr <<
"Unable to determine method to handle clients, "
86 <<
"single or multiple as defined by BES.ProcessManagerMethod" << endl;
87 exit(SERVER_EXIT_FATAL_CANNOT_START);
100 if (_method ==
"single") {
112 if ((pid = fork()) < 0) {
113 string error(
"fork error");
114 const char* error_info = strerror(errno);
115 if (error_info) error +=
" " + (string) error_info;
129 strm <<
"ip " << c->getSocket()->getIp() <<
", port " << c->getSocket()->getPort();
130 string from = strm.str();
132 map<string, string> extensions;
144 done = c->receive(extensions, &ss);
148 if (extensions[
"status"] == c->exit()) {
157 BESDEBUG(
"beslistener",
158 "BESServerHandler::execute() - Received PPT_EXIT_NOW in an extension chunk." << endl);
164 c->closeConnection();
166 BESDEBUG(
"beslistener",
167 "BESServerHandler::execute() - Calling exit(CHILD_SUBPROCESS_READY) which has a value of " << CHILD_SUBPROCESS_READY << endl);
169 exit(CHILD_SUBPROCESS_READY);
177 string cmd_str = ss.str();
179 BESDEBUG(
"server",
"BESServerHandler::execute - command ... " << cmd_str << endl);
182 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"BESServerHandler::execute");
186 int descript = c->getSocket()->getSocketDescriptor();
187 unsigned int bufsize = c->getSendChunkSize();
189 std::streambuf *holder;
190 holder = cout.rdbuf();
194 int status = cmd.execute_request(from);
202 BESDEBUG(
"server",
"BESServerHandler::execute - " <<
"error occurred" << endl);
207 map<string, string> extensions;
208 extensions[
"status"] =
"error";
209 if (status == BES_INTERNAL_FATAL_ERROR) {
210 extensions[
"exit"] =
"true";
212 c->sendExtensions(extensions);
223 case BES_INTERNAL_FATAL_ERROR:
224 LOG(
"BES Internal Fatal Error; child returning "
225 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
227 c->closeConnection();
228 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
232 case BES_INTERNAL_ERROR:
235 LOG(
"BES Internal Error; child returning "
236 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
238 c->closeConnection();
239 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
244 case BES_SYNTAX_USER_ERROR:
245 case BES_FORBIDDEN_ERROR:
246 case BES_NOT_FOUND_ERROR:
253 c->closeConnection();
264 strm << BESIndent::LMarg <<
"BESServerHandler::dump - (" << (
void *)
this <<
")" << endl;
266 strm << BESIndent::LMarg <<
"server method: " << _method << endl;
267 BESIndent::UnIndent();