36 #include <sys/types.h>
37 #include <sys/socket.h>
47 #include "BESServerHandler.h"
48 #include "Connection.h"
50 #include "BESXMLInterface.h"
51 #include "TheBESKeys.h"
52 #include "BESInternalError.h"
53 #include "ServerExitConditions.h"
55 #include "PPTStreamBuf.h"
56 #include "PPTProtocolNames.h"
59 #include "BESStopWatch.h"
63 #define MODULE "server"
64 #define prolog std::string("BESServerHandler::").append(__func__).append("() - ")
68 #define EXIT_ON_INTERNAL_ERROR "BES.ExitOnInternalError"
70 BESServerHandler::BESServerHandler()
77 cerr <<
"Unable to determine method to handle clients, "
78 <<
"single or multiple as defined by BES.ProcessManagerMethod" <<
": " << e.
get_message() << endl;
79 exit(SERVER_EXIT_FATAL_CANNOT_START);
82 if (_method !=
"multiple" && _method !=
"single") {
83 cerr <<
"Unable to determine method to handle clients, "
84 <<
"single or multiple as defined by BES.ProcessManagerMethod" << endl;
85 exit(SERVER_EXIT_FATAL_CANNOT_START);
98 if (_method ==
"single") {
110 if ((pid = fork()) < 0) {
111 string error(
"fork error");
112 const char* error_info = strerror(errno);
113 if (error_info) error +=
" " + (string) error_info;
122 void BESServerHandler::execute(
Connection *connection)
127 strm <<
"ip " << connection->getSocket()->getIp() <<
", port " << connection->getSocket()->getPort();
128 string from = strm.str();
130 map<string, string> extensions;
132 int socket_d = connection->getSocket()->getSocketDescriptor();
133 unsigned int bufsize = connection->getSendChunkSize();
135 ostream my_ostrm(&fds);
139 msg << prolog <<
"Using ostream: " << (
void *) &my_ostrm <<
" cout: " << (
void *) &cout << endl;
140 BESDEBUG(MODULE, msg.str());
141 INFO_LOG( msg.str());
153 BESDEBUG(MODULE,prolog <<
"Waiting for client to send commands." << endl);
155 done = connection->receive(extensions, &ss);
157 BESDEBUG(MODULE,prolog <<
"Received client command. status: '" << extensions[
"status"] <<
"'" << endl);
161 if (extensions[
"status"] == connection->exit()) {
170 BESDEBUG(MODULE,prolog <<
"Received PPT_EXIT_NOW in an extension chunk." << endl);
176 connection->closeConnection();
178 BESDEBUG(MODULE,prolog <<
"Calling exit(CHILD_SUBPROCESS_READY) which has a value of " << CHILD_SUBPROCESS_READY << endl);
180 INFO_LOG(
"Received exit command." << endl);
182 exit(CHILD_SUBPROCESS_READY);
185 string cmd_str = ss.str();
187 BESDEBUG(MODULE, prolog <<
"Processing client command:" << endl << cmd_str << endl);
197 int status = cmd.execute_request(from);
201 BESDEBUG(MODULE, prolog <<
"Client command successfully processed." << endl);
204 BESDEBUG(MODULE, prolog <<
"ERROR - cmd.execute_request() returned: " << status << endl);
209 map<string, string> extensions;
210 extensions[
"status"] =
"error";
211 if (status == BES_INTERNAL_FATAL_ERROR) {
212 extensions[
"exit"] =
"true";
214 connection->sendExtensions(extensions);
223 case BES_INTERNAL_FATAL_ERROR:
224 ERROR_LOG(
"BES Internal Fatal Error; child returning "
225 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
227 connection->closeConnection();
228 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
232 case BES_INTERNAL_ERROR:
235 ERROR_LOG(
"BES Internal Error; child returning "
236 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
238 connection->closeConnection();
239 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
244 case BES_SYNTAX_USER_ERROR:
245 case BES_FORBIDDEN_ERROR:
246 case BES_NOT_FOUND_ERROR:
262 strm << BESIndent::LMarg <<
"BESServerHandler::dump - (" << (
void *)
this <<
")" << endl;
264 strm << BESIndent::LMarg <<
"server method: " << _method << endl;
265 BESIndent::UnIndent();
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Abstract exception class for the BES with basic string message.
virtual std::string get_message()
get the error message for this exception
exception thrown if internal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual bool start(std::string name)
Entry point into BES using xml document requests.
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()