Loading...
Searching...
No Matches
XrdClMessageUtils.hh
Go to the documentation of this file.
//------------------------------------------------------------------------------
// Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#ifndef __XRD_CL_MESSAGE_UTILS_HH__
#define __XRD_CL_MESSAGE_UTILS_HH__
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClMessage.hh"
#include "XrdSys/XrdSysKernelBuffer.hh"
#include "XrdSys/XrdSysPthread.hh"
namespace XrdCl
{
class LocalFileHandler;
struct XAttr;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
class SyncResponseHandler: public ResponseHandler
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
SyncResponseHandler():
pStatus(0),
pResponse(0),
pCondVar(0) {}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
virtual ~SyncResponseHandler()
{
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
virtual void HandleResponse( XRootDStatus *status,
AnyObject *response )
{
XrdSysCondVarHelper scopedLock(pCondVar);
pStatus = status;
pResponse = response;
pCondVar.Broadcast();
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
XRootDStatus *GetStatus()
{
return pStatus;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
AnyObject *GetResponse()
{
return pResponse;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void WaitForResponse()
{
XrdSysCondVarHelper scopedLock(pCondVar);
while (pStatus == 0) {
pCondVar.Wait();
}
}
private:
SyncResponseHandler(const SyncResponseHandler &other);
SyncResponseHandler &operator = (const SyncResponseHandler &other);
XRootDStatus *pStatus;
AnyObject *pResponse;
XrdSysCondVar pCondVar;
};
//----------------------------------------------------------------------------
// We're not interested in the response just commit suicide
//----------------------------------------------------------------------------
class NullResponseHandler: public XrdCl::ResponseHandler
{
public:
//------------------------------------------------------------------------
// Handle the response
//------------------------------------------------------------------------
virtual void HandleResponseWithHosts( XrdCl::XRootDStatus *status,
XrdCl::AnyObject *response,
XrdCl::HostList *hostList )
{
delete this;
}
};
//----------------------------------------------------------------------------
// Sending parameters
//----------------------------------------------------------------------------
struct MessageSendParams
{
MessageSendParams():
timeout(0), expires(0), followRedirects(true), chunkedResponse(false),
stateful(true), hostList(0), chunkList(0), redirectLimit(0), kbuff(0){}
uint16_t timeout;
time_t expires;
HostInfo loadBalancer;
bool followRedirects;
bool chunkedResponse;
bool stateful;
HostList *hostList;
ChunkList *chunkList;
uint16_t redirectLimit;
XrdSys::KernelBuffer *kbuff;
std::vector<uint32_t> crc32cDigests;
};
class MessageUtils
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus WaitForStatus( SyncResponseHandler *handler )
{
handler->WaitForResponse();
XRootDStatus *status = handler->GetStatus();
XRootDStatus ret( *status );
delete status;
return ret;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
template<class Type>
static XrdCl::XRootDStatus WaitForResponse(
SyncResponseHandler *handler,
Type *&response )
{
handler->WaitForResponse();
AnyObject *resp = handler->GetResponse();
XRootDStatus *status = handler->GetStatus();
XRootDStatus ret( *status );
delete status;
if( ret.IsOK() )
{
if( !resp )
return XRootDStatus( stError, errInternal );
resp->Get( response );
resp->Set( (int *)0 );
delete resp;
if( !response )
return XRootDStatus( stError, errInternal );
}
return ret;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
template<class Request>
static void CreateRequest( Message *&msg,
Request *&req,
uint32_t payloadSize = 0 )
{
msg = new Message( sizeof(Request) + payloadSize );
req = (Request*)msg->GetBuffer();
msg->Zero();
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus SendMessage( const URL &url,
Message *msg,
ResponseHandler *handler,
MessageSendParams &sendParams,
LocalFileHandler *lFileHandler );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status RedirectMessage( const URL &url,
Message *msg,
ResponseHandler *handler,
MessageSendParams &sendParams,
LocalFileHandler *lFileHandler );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void ProcessSendParams( MessageSendParams &sendParams );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void RewriteCGIAndPath( Message *msg,
const URL::ParamsMap &newCgi,
bool replace,
const std::string &newPath );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void MergeCGI( URL::ParamsMap &cgi1,
const URL::ParamsMap &cgi2,
bool replace );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status CreateXAttrVec( const std::vector<xattr_t> &attrs,
std::vector<char> &avec );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status CreateXAttrVec( const std::vector<std::string> &attrs,
std::vector<char> &nvec );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
template<typename T>
static Status CreateXAttrBody( Message *msg,
const std::vector<T> &vec,
const std::string &path = "" )
{
ClientRequestHdr *hdr = reinterpret_cast<ClientRequestHdr*>( msg->GetBuffer() );
std::vector<char> xattrvec;
Status st = MessageUtils::CreateXAttrVec( vec, xattrvec );
if( !st.IsOK() )
return st;
// update body size in the header
hdr->dlen = path.size() + 1;
hdr->dlen += xattrvec.size();
// append the body
size_t offset = sizeof( ClientRequestHdr );
msg->Append( path.c_str(), path.size() + 1, offset );
offset += path.size() + 1;
msg->Append( xattrvec.data(), xattrvec.size(), offset );
return Status();
}
};
}
#endif // __XRD_CL_MESSAGE_UTILS_HH__
ClientRequestHdr::dlen
kXR_int32 dlen
Definition XProtocol.hh:159
ClientRequestHdr
Definition XProtocol.hh:155
XrdClMessage.hh
XrdClURL.hh
XrdClXRootDResponses.hh
XrdSysKernelBuffer.hh
XrdSysPthread.hh
XrdCl::AnyObject
Definition XrdClAnyObject.hh:33
XrdCl::AnyObject::Set
void Set(Type object, bool own=true)
Definition XrdClAnyObject.hh:59
XrdCl::AnyObject::Get
void Get(Type &object)
Retrieve the object being held.
Definition XrdClAnyObject.hh:78
XrdCl::Buffer::Zero
void Zero()
Zero.
Definition XrdClBuffer.hh:124
XrdCl::Buffer::Append
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition XrdClBuffer.hh:164
XrdCl::Buffer::GetBuffer
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition XrdClBuffer.hh:72
XrdCl::LocalFileHandler
Definition XrdClLocalFileHandler.hh:33
XrdCl::MessageUtils
Definition XrdClMessageUtils.hh:150
XrdCl::MessageUtils::MergeCGI
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
Merge cgi2 into cgi1.
Definition XrdClMessageUtils.cc:378
XrdCl::MessageUtils::CreateXAttrVec
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
Create xattr vector.
Definition XrdClMessageUtils.cc:404
XrdCl::MessageUtils::ProcessSendParams
static void ProcessSendParams(MessageSendParams &sendParams)
Process sending params.
Definition XrdClMessageUtils.cc:220
XrdCl::MessageUtils::RewriteCGIAndPath
static void RewriteCGIAndPath(Message *msg, const URL::ParamsMap &newCgi, bool replace, const std::string &newPath)
Append cgi to the one already present in the message.
Definition XrdClMessageUtils.cc:250
XrdCl::MessageUtils::CreateXAttrBody
static Status CreateXAttrBody(Message *msg, const std::vector< T > &vec, const std::string &path="")
Definition XrdClMessageUtils.hh:286
XrdCl::MessageUtils::WaitForResponse
static XrdCl::XRootDStatus WaitForResponse(SyncResponseHandler *handler, Type *&response)
Wait for the response.
Definition XrdClMessageUtils.hh:168
XrdCl::MessageUtils::RedirectMessage
static Status RedirectMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Redirect message.
Definition XrdClMessageUtils.cc:150
XrdCl::MessageUtils::SendMessage
static XRootDStatus SendMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Send message.
Definition XrdClMessageUtils.cc:43
XrdCl::MessageUtils::CreateRequest
static void CreateRequest(Message *&msg, Request *&req, uint32_t payloadSize=0)
Create a message.
Definition XrdClMessageUtils.hh:198
XrdCl::MessageUtils::WaitForStatus
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
Definition XrdClMessageUtils.hh:155
XrdCl::Message
The message representation used throughout the system.
Definition XrdClMessage.hh:32
XrdCl::NullResponseHandler
Definition XrdClMessageUtils.hh:115
XrdCl::NullResponseHandler::HandleResponseWithHosts
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Definition XrdClMessageUtils.hh:120
XrdCl::ResponseHandler
Handle an async response.
Definition XrdClXRootDResponses.hh:1127
XrdCl::SyncResponseHandler
Synchronize the response.
Definition XrdClMessageUtils.hh:44
XrdCl::SyncResponseHandler::GetResponse
AnyObject * GetResponse()
Get the response.
Definition XrdClMessageUtils.hh:85
XrdCl::SyncResponseHandler::HandleResponse
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Handle the response.
Definition XrdClMessageUtils.hh:65
XrdCl::SyncResponseHandler::~SyncResponseHandler
virtual ~SyncResponseHandler()
Destructor.
Definition XrdClMessageUtils.hh:57
XrdCl::SyncResponseHandler::GetStatus
XRootDStatus * GetStatus()
Get the status.
Definition XrdClMessageUtils.hh:77
XrdCl::SyncResponseHandler::SyncResponseHandler
SyncResponseHandler()
Constructor.
Definition XrdClMessageUtils.hh:49
XrdCl::SyncResponseHandler::WaitForResponse
void WaitForResponse()
Wait for the arrival of the response.
Definition XrdClMessageUtils.hh:93
XrdCl::URL
URL representation.
Definition XrdClURL.hh:31
XrdCl::URL::ParamsMap
std::map< std::string, std::string > ParamsMap
Definition XrdClURL.hh:33
XrdCl::XRootDStatus
Request status.
Definition XrdClXRootDResponses.hh:219
XrdSysCondVarHelper
Definition XrdSysPthread.hh:129
XrdSysCondVar
Definition XrdSysPthread.hh:79
XrdSys::KernelBuffer
Definition XrdSysKernelBuffer.hh:46
XrdCl
Definition XrdClAction.hh:34
XrdCl::stError
const uint16_t stError
An error occurred that could potentially be retried.
Definition XrdClStatus.hh:32
XrdCl::HostList
std::vector< HostInfo > HostList
Definition XrdClXRootDResponses.hh:1121
XrdCl::errInternal
const uint16_t errInternal
Internal error.
Definition XrdClStatus.hh:56
XrdCl::ChunkList
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition XrdClXRootDResponses.hh:1056
XrdCl::HostInfo
Definition XrdClXRootDResponses.hh:1110
XrdCl::MessageSendParams
Definition XrdClMessageUtils.hh:132
XrdCl::MessageSendParams::hostList
HostList * hostList
Definition XrdClMessageUtils.hh:142
XrdCl::MessageSendParams::stateful
bool stateful
Definition XrdClMessageUtils.hh:141
XrdCl::MessageSendParams::crc32cDigests
std::vector< uint32_t > crc32cDigests
Definition XrdClMessageUtils.hh:146
XrdCl::MessageSendParams::kbuff
XrdSys::KernelBuffer * kbuff
Definition XrdClMessageUtils.hh:145
XrdCl::MessageSendParams::timeout
uint16_t timeout
Definition XrdClMessageUtils.hh:136
XrdCl::MessageSendParams::loadBalancer
HostInfo loadBalancer
Definition XrdClMessageUtils.hh:138
XrdCl::MessageSendParams::chunkList
ChunkList * chunkList
Definition XrdClMessageUtils.hh:143
XrdCl::MessageSendParams::expires
time_t expires
Definition XrdClMessageUtils.hh:137
XrdCl::MessageSendParams::redirectLimit
uint16_t redirectLimit
Definition XrdClMessageUtils.hh:144
XrdCl::MessageSendParams::chunkedResponse
bool chunkedResponse
Definition XrdClMessageUtils.hh:140
XrdCl::MessageSendParams::MessageSendParams
MessageSendParams()
Definition XrdClMessageUtils.hh:133
XrdCl::MessageSendParams::followRedirects
bool followRedirects
Definition XrdClMessageUtils.hh:139
XrdCl::Status
Procedure execution status.
Definition XrdClStatus.hh:115
XrdCl::Status::IsOK
bool IsOK() const
We're fine.
Definition XrdClStatus.hh:124
XrdCl::XAttr
Extended attributes with status.
Definition XrdClXRootDResponses.hh:313