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"
66 #define EXIT_ON_INTERNAL_ERROR "BES.ExitOnInternalError"
68 BESServerHandler::BESServerHandler()
75 cerr <<
"Unable to determine method to handle clients, "
76 <<
"single or multiple as defined by BES.ProcessManagerMethod" <<
": " << e.
get_message() << endl;
77 exit(SERVER_EXIT_FATAL_CANNOT_START);
80 if (_method !=
"multiple" && _method !=
"single") {
81 cerr <<
"Unable to determine method to handle clients, "
82 <<
"single or multiple as defined by BES.ProcessManagerMethod" << endl;
83 exit(SERVER_EXIT_FATAL_CANNOT_START);
96 if (_method ==
"single") {
108 if ((pid = fork()) < 0) {
109 string error(
"fork error");
110 const char* error_info = strerror(errno);
111 if (error_info) error +=
" " + (string) error_info;
125 strm <<
"ip " << c->getSocket()->getIp() <<
", port " << c->getSocket()->getPort();
126 string from = strm.str();
128 map<string, string> extensions;
140 done = c->receive(extensions, &ss);
144 if (extensions[
"status"] == c->exit()) {
153 BESDEBUG(
"beslistener",
154 "BESServerHandler::execute() - Received PPT_EXIT_NOW in an extension chunk." << endl);
160 c->closeConnection();
162 BESDEBUG(
"beslistener",
163 "BESServerHandler::execute() - Calling exit(CHILD_SUBPROCESS_READY) which has a value of " << CHILD_SUBPROCESS_READY << endl);
165 exit(CHILD_SUBPROCESS_READY);
173 string cmd_str = ss.str();
175 BESDEBUG(
"server",
"BESServerHandler::execute - command ... " << cmd_str << endl);
178 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"BESServerHandler::execute");
182 int descript = c->getSocket()->getSocketDescriptor();
183 unsigned int bufsize = c->getSendChunkSize();
185 std::streambuf *holder;
186 holder = cout.rdbuf();
190 int status = cmd.execute_request(from);
198 BESDEBUG(
"server",
"BESServerHandler::execute - " <<
"error occurred" << endl);
203 map<string, string> extensions;
204 extensions[
"status"] =
"error";
205 if (status == BES_INTERNAL_FATAL_ERROR) {
206 extensions[
"exit"] =
"true";
208 c->sendExtensions(extensions);
219 case BES_INTERNAL_FATAL_ERROR:
220 LOG(
"BES Internal Fatal Error; child returning "
221 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
223 c->closeConnection();
224 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
228 case BES_INTERNAL_ERROR:
231 LOG(
"BES Internal Error; child returning "
232 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
234 c->closeConnection();
235 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
240 case BES_SYNTAX_USER_ERROR:
241 case BES_FORBIDDEN_ERROR:
242 case BES_NOT_FOUND_ERROR:
249 c->closeConnection();
260 strm << BESIndent::LMarg <<
"BESServerHandler::dump - (" << (
void *)
this <<
")" << endl;
262 strm << BESIndent::LMarg <<
"server method: " << _method << endl;
263 BESIndent::UnIndent();