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"
70BESServerHandler::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;
122void 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());
152 BESDEBUG(MODULE,prolog <<
"Waiting for client to send commands." << endl);
154 done = connection->receive(extensions, &ss);
156 BESDEBUG(MODULE,prolog <<
"Received client command. status: '" << extensions[
"status"] <<
"'" << endl);
160 if (extensions[
"status"] == connection->exit()) {
169 BESDEBUG(MODULE,prolog <<
"Received PPT_EXIT_NOW in an extension chunk." << endl);
175 connection->closeConnection();
177 BESDEBUG(MODULE,prolog <<
"Calling exit(CHILD_SUBPROCESS_READY) which has a value of " << CHILD_SUBPROCESS_READY << endl);
179 INFO_LOG(
"Received exit command." << endl);
181 exit(CHILD_SUBPROCESS_READY);
184 string cmd_str = ss.str();
186 BESDEBUG(MODULE, prolog <<
"Processing client command:" << endl << cmd_str << endl);
195 int status = cmd.execute_request(from);
199 BESDEBUG(MODULE, prolog <<
"Client command successfully processed." << endl);
202 BESDEBUG(MODULE, prolog <<
"ERROR - cmd.execute_request() returned: " << status << endl);
207 map<string, string> extensions;
208 extensions[
"status"] =
"error";
209 if (status == BES_INTERNAL_FATAL_ERROR) {
210 extensions[
"exit"] =
"true";
212 connection->sendExtensions(extensions);
221 case BES_INTERNAL_FATAL_ERROR:
222 ERROR_LOG(
"BES Internal Fatal Error; child returning "
223 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
225 connection->closeConnection();
226 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
230 case BES_INTERNAL_ERROR:
233 ERROR_LOG(
"BES Internal Error; child returning "
234 << SERVER_EXIT_ABNORMAL_TERMINATION <<
" to the master listener." << endl);
236 connection->closeConnection();
237 exit(SERVER_EXIT_ABNORMAL_TERMINATION);
242 case BES_SYNTAX_USER_ERROR:
243 case BES_FORBIDDEN_ERROR:
244 case BES_NOT_FOUND_ERROR:
260 strm << BESIndent::LMarg <<
"BESServerHandler::dump - (" << (
void *)
this <<
")" << endl;
262 strm << BESIndent::LMarg <<
"server method: " << _method << endl;
263 BESIndent::UnIndent();
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Base exception class for the BES with basic string message.
std::string get_message() const
get the error message for this exception
exception thrown if internal error encountered
void dump(std::ostream &strm) const override
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()