Skip to content
Snippets Groups Projects
Commit 521b389c authored by Michael Davis's avatar Michael Davis
Browse files

[cta_frontend] Refactors dispatcher to use a map of functions

For the admin functions, it is very clunky to map <cmd,subcmd> pairs to
which function to call using nested switches. Instead we lookup which
function to call in a static const map of <cmd,subcmd> to method function
pointers. Now new commands can be added or old ones removed by editing a
single line in the map definition.
parent 28b9d5a7
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,16 @@ using XrdSsiPb::PbException;
namespace cta { namespace xrd {
// Static const member to map workflow events to methods
const std::map<cta::eos::Workflow_EventType, RequestMessage::notification_event_ptr_t> RequestMessage::notificationEvent = {
{ cta::eos::Workflow::CLOSEW, &RequestMessage::processCLOSEW },
{ cta::eos::Workflow::PREPARE, &RequestMessage::processPREPARE },
{ cta::eos::Workflow::DELETE, &RequestMessage::processDELETE },
};
void RequestMessage::process(const cta::xrd::Request &request, cta::xrd::Response &response)
{
// Branch on the Request payload type
......@@ -34,36 +44,25 @@ void RequestMessage::process(const cta::xrd::Request &request, cta::xrd::Respons
{
using namespace cta::xrd;
case Request::kAdmincmd: processAdminCmd(request.admincmd(), response); break;
case Request::kNotification: processNotification(request.notification(), response); break;
case Request::REQUEST_NOT_SET: throw PbException("Request message has not been set.");
default: throw PbException("Unrecognized Request message. "
"Possible Protocol Buffer version mismatch between client and server.");
}
}
case Request::kAdmincmd:
// Validate the Protocol Buffer
validateCmd(request.admincmd());
// Map the <Cmd, SubCmd> to a method
//cmd_key_t cmd{ admin_cmd.cmd(), admin_cmd.subcmd() };
break;
void RequestMessage::processAdminCmd(const cta::admin::AdminCmd &admin_cmd, cta::xrd::Response &response)
{
// Validate the Protocol Buffer
validateCmd(admin_cmd);
}
void RequestMessage::processNotification(const cta::eos::Notification &notification, cta::xrd::Response &response)
{
switch(notification.wf().event())
{
using namespace cta::eos;
case Request::kNotification: {
auto notificationFunctionPtr = notificationEvent.at(request.notification().wf().event());
(this->*notificationFunctionPtr)(request.notification(), response);
break;
}
case Workflow::CLOSEW: processCLOSEW (notification, response); break;
case Workflow::PREPARE: processPREPARE(notification, response); break;
case Workflow::DELETE: processDELETE (notification, response); break;
case Request::REQUEST_NOT_SET:
throw PbException("Request message has not been set.");
default:
throw PbException("Workflow Event type " + std::to_string(notification.wf().event()) + " is not supported.");
throw PbException("Unrecognized Request message. "
"Possible Protocol Buffer version mismatch between client and server.");
}
}
......
......@@ -46,50 +46,26 @@ public:
private:
/*!
* Process the Admin Command message type
*
* @param[in] admincmd Admin command from the cta admin cli
* @param[out] response Response message
*/
void processAdminCmd(const cta::admin::AdminCmd &admin_cmd, cta::xrd::Response &response);
/*!
* Process the EOS WFE Notification message type
*
* @param[in] notification Notification request message from EOS WFE
* @param[out] response Response message
*/
void processNotification(const cta::eos::Notification &notification, cta::xrd::Response &response);
/*!
* Process the CLOSEW workflow event
* Process Notification events
*
* @param[in] notification Notification request message from EOS WFE
* @param[out] response Response message to return to EOS
*/
void processCLOSEW(const cta::eos::Notification &notification, cta::xrd::Response &response);
typedef void notification_event_t(const cta::eos::Notification &notification, cta::xrd::Response &response);
typedef void (RequestMessage::*notification_event_ptr_t)(const cta::eos::Notification &notification, cta::xrd::Response &response);
/*!
* Process the PREPARE workflow event
*
* @param[in] notification Notification request message from EOS WFE
* @param[out] response Response message to return to EOS
*/
void processPREPARE(const cta::eos::Notification &notification, cta::xrd::Response &response);
// Map Workflow Events to methods
static const std::map<cta::eos::Workflow_EventType, notification_event_ptr_t> notificationEvent;
/*!
* Process the DELETE workflow event
*
* @param[in] notification Notification request message from EOS WFE
* @param[out] response Response message to return to EOS
*/
void processDELETE(const cta::eos::Notification &notification, cta::xrd::Response &response);
notification_event_t processCLOSEW; //!< Archive file event
notification_event_t processPREPARE; //!< Retrieve file event
notification_event_t processDELETE; //!< Delete file event
// Member variables
cta::Scheduler &m_scheduler; //< Reference to CTA Scheduler
cta::log::LogContext m_lc; //< CTA Log Context
const char * const m_instance_name; //< Instance name = XRootD client name
cta::Scheduler &m_scheduler; //!< Reference to CTA Scheduler
cta::log::LogContext m_lc; //!< CTA Log Context
const char * const m_instance_name; //!< Instance name = XRootD client name
};
}} // namespace cta::xrd
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment