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

[EOS-CTA] Simplifies exception handling on client side

parent 45678889
Branches
Tags
No related merge requests found
......@@ -29,37 +29,14 @@
//! Usage exception
const std::runtime_error Usage("Usage: eoscta_stub archive|retrieve|delete [options] [--stderr]");
// Define the XRootD SSI callbacks
// Define XRootD SSI Alert message callback
namespace XrdSsiPb {
/*!
* Response callback.
*
* This is the CTA Front End response to the Notification.
*/
template<>
void RequestCallback<eos::wfe::Response>::operator()(const eos::wfe::Response &response)
{
using namespace std;
std::cout << "ResponseCallback():" << std::endl;
OutputJsonString(std::cout, &response);
}
/*!
* Alert callback.
*
* This is for messages which should be logged by EOS or directed to the User.
* Defines how Alert messages should be logged by EOS or directed to the User.
*/
template<>
......@@ -69,22 +46,13 @@ void RequestCallback<eos::wfe::Alert>::operator()(const eos::wfe::Alert &alert)
OutputJsonString(std::cout, &alert);
}
} // namespace XrdSsiPb
/*!
* Convert exceptions into Alerts.
*
* This tells the framework how to log exceptions received on the client side
*/
template<>
void ExceptionToAlert<eos::wfe::Alert>::operator()(const std::exception &e, eos::wfe::Alert &alert)
{
alert.set_audience(eos::wfe::Alert::EOSLOG);
alert.set_message_txt(e.what());
}
//! Usage exception
} // namespace XrdSsiPb
const std::runtime_error Usage("Usage: eoscta_stub archive|retrieve|delete [options] [--stderr]");
......
......@@ -20,6 +20,7 @@
#define __XRD_SSI_PB_REQUEST_H
#include <future>
#include <XrdSsi/XrdSsiRequest.hh>
namespace XrdSsiPb {
......@@ -39,21 +40,6 @@ public:
/*!
* Convert Exceptions to Alerts
*
* The client should specialize on this class to specify how to log exceptions
*/
template<typename AlertType>
class ExceptionToAlert
{
public:
void operator()(const std::exception &e, AlertType &alert);
};
/*!
* XRootD SSI + Protocol Buffers Request class
*/
......@@ -64,7 +50,7 @@ class Request : public XrdSsiRequest
public:
Request(const std::string &buffer_str, unsigned int response_bufsize, uint16_t timeout) :
m_request_bufptr(buffer_str.c_str()),
m_request_len(buffer_str.size()),
m_request_bufsize(buffer_str.size()),
m_response_bufsize(response_bufsize)
{
#ifdef XRDSSI_DEBUG
......@@ -96,7 +82,7 @@ public:
virtual char *GetRequest(int &reqlen) override
{
reqlen = m_request_len;
reqlen = m_request_bufsize;
return const_cast<char*>(m_request_bufptr);
}
......@@ -129,21 +115,13 @@ public:
auto GetFuture() { return m_promise.get_future(); }
private:
const char *m_request_bufptr; //!< Pointer to the Request buffer
int m_request_len; //!< Size of the Request buffer
int m_response_bufsize; //!< Size of the Response buffer
//! Promise a reply of Metadata type
std::promise<MetadataType> m_promise;
const char *m_request_bufptr; //!< Pointer to the Request buffer
int m_request_bufsize; //!< Size of the Request buffer
char *m_response_bufptr; //!< Pointer to the Response buffer
int m_response_bufsize; //!< Size of the Response buffer
//! Convert exceptions to Alerts. Must be defined on the client side.
ExceptionToAlert<AlertType> ExceptionHandler;
// Callbacks for each of the XRootD reply types
RequestCallback<AlertType> AlertCallback;
std::promise<MetadataType> m_promise; //!< Promise a reply of Metadata type
RequestCallback<AlertType> AlertCallback; //!< Callback for Alerts
// Responses and stream Responses will be implemented as a binary blob/stream. The format of the
// response will not be a protocol buffer. If the response was a protocol buffer we would need to
......@@ -223,16 +201,14 @@ bool Request<RequestType, MetadataType, AlertType>::ProcessResponse(const XrdSsi
{
// Allocate response buffer
char *response_bufptr = new char[m_response_bufsize];
m_response_bufptr = new char[m_response_bufsize];
// Read the first chunk of data into the buffer, and pass it to ProcessResponseData()
GetResponseData(response_bufptr, m_response_bufsize);
GetResponseData(m_response_bufptr, m_response_bufsize);
}
}
}
catch(std::exception &e)
{
} catch(std::exception &ex) {
// Use the exception to fulfil the promise
m_promise.set_exception(std::current_exception());
......@@ -244,6 +220,9 @@ bool Request<RequestType, MetadataType, AlertType>::ProcessResponse(const XrdSsi
// It is now safe to delete the Request object (which implies that the pointer on the calling side
// will never refer to it again and the destructor of the base class doesn't access any class members).
delete this;
} catch(...) {
Finished();
delete this;
}
......@@ -292,22 +271,31 @@ XrdSsiRequest::PRD_Xeq Request<RequestType, MetadataType, AlertType>
template<typename RequestType, typename MetadataType, typename AlertType>
void Request<RequestType, MetadataType, AlertType>::Alert(XrdSsiRespInfoMsg &alert_msg)
{
// Get the Alert
try
{
// Get the Alert
int alert_len;
char *alert_buffer = alert_msg.GetMsg(alert_len);
int alert_len;
char *alert_buffer = alert_msg.GetMsg(alert_len);
// Deserialize the Alert
// Deserialize the Alert
AlertType alert;
AlertType alert;
if(!alert.ParseFromArray(alert_buffer, alert_len))
if(alert.ParseFromArray(alert_buffer, alert_len))
{
AlertCallback(alert);
}
else
{
throw PbException("alert.ParseFromArray() failed");
}
}
catch(std::exception &ex)
{
PbException e("alert.ParseFromArray() failed");
ExceptionHandler(e, alert);
m_promise.set_exception(std::current_exception());
}
AlertCallback(alert);
catch(...) {}
// Recycle the message to free memory
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment