xrootd
XrdClMessageUtils.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef __XRD_CL_MESSAGE_UTILS_HH__
26 #define __XRD_CL_MESSAGE_UTILS_HH__
27 
29 #include "XrdCl/XrdClURL.hh"
30 #include "XrdCl/XrdClMessage.hh"
32 #include "XrdSys/XrdSysPthread.hh"
33 
34 namespace XrdCl
35 {
36  class LocalFileHandler;
37 
38  class XAttr;
39 
40  //----------------------------------------------------------------------------
42  //----------------------------------------------------------------------------
44  {
45  public:
46  //------------------------------------------------------------------------
48  //------------------------------------------------------------------------
50  pStatus(0),
51  pResponse(0),
52  pCondVar(0) {}
53 
54  //------------------------------------------------------------------------
56  //------------------------------------------------------------------------
58  {
59  }
60 
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  virtual void HandleResponse( XRootDStatus *status,
66  AnyObject *response )
67  {
68  XrdSysCondVarHelper scopedLock(pCondVar);
69  pStatus = status;
70  pResponse = response;
72  }
73 
74  //------------------------------------------------------------------------
76  //------------------------------------------------------------------------
78  {
79  return pStatus;
80  }
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
86  {
87  return pResponse;
88  }
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
94  {
95  XrdSysCondVarHelper scopedLock(pCondVar);
96  while (pStatus == 0) {
97  pCondVar.Wait();
98  }
99  }
100 
101  private:
104 
108  };
109 
110 
111  //----------------------------------------------------------------------------
112  // We're not interested in the response just commit suicide
113  //----------------------------------------------------------------------------
115  {
116  public:
117  //------------------------------------------------------------------------
118  // Handle the response
119  //------------------------------------------------------------------------
121  XrdCl::AnyObject *response,
122  XrdCl::HostList *hostList )
123  {
124  delete this;
125  }
126  };
127 
128  //----------------------------------------------------------------------------
129  // Sending parameters
130  //----------------------------------------------------------------------------
132  {
134  timeout(0), expires(0), followRedirects(true), chunkedResponse(false),
135  stateful(true), hostList(0), chunkList(0), redirectLimit(0), kbuff(0) {}
136  uint16_t timeout;
137  time_t expires;
141  bool stateful;
144  uint16_t redirectLimit;
146  };
147 
149  {
150  public:
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
155  {
156  handler->WaitForResponse();
157  XRootDStatus *status = handler->GetStatus();
158  XRootDStatus ret( *status );
159  delete status;
160  return ret;
161  }
162 
163  //------------------------------------------------------------------------
165  //------------------------------------------------------------------------
166  template<class Type>
168  SyncResponseHandler *handler,
169  Type *&response )
170  {
171  handler->WaitForResponse();
172 
173  AnyObject *resp = handler->GetResponse();
174  XRootDStatus *status = handler->GetStatus();
175  XRootDStatus ret( *status );
176  delete status;
177 
178  if( ret.IsOK() )
179  {
180  if( !resp )
181  return XRootDStatus( stError, errInternal );
182  resp->Get( response );
183  resp->Set( (int *)0 );
184  delete resp;
185 
186  if( !response )
187  return XRootDStatus( stError, errInternal );
188  }
189 
190  return ret;
191  }
192 
193  //------------------------------------------------------------------------
195  //------------------------------------------------------------------------
196  template<class Request>
197  static void CreateRequest( Message *&msg,
198  Request *&req,
199  uint32_t payloadSize = 0 )
200  {
201  msg = new Message( sizeof(Request) + payloadSize );
202  req = (Request*)msg->GetBuffer();
203  msg->Zero();
204  }
205 
206  //------------------------------------------------------------------------
208  //------------------------------------------------------------------------
209  static XRootDStatus SendMessage( const URL &url,
210  Message *msg,
211  ResponseHandler *handler,
212  MessageSendParams &sendParams,
213  LocalFileHandler *lFileHandler );
214 
215  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  static Status RedirectMessage( const URL &url,
219  Message *msg,
220  ResponseHandler *handler,
221  MessageSendParams &sendParams,
222  LocalFileHandler *lFileHandler );
223 
224  //------------------------------------------------------------------------
226  //------------------------------------------------------------------------
227  static void ProcessSendParams( MessageSendParams &sendParams );
228 
229  //------------------------------------------------------------------------
239  //------------------------------------------------------------------------
240  static void RewriteCGIAndPath( Message *msg,
241  const URL::ParamsMap &newCgi,
242  bool replace,
243  const std::string &newPath );
244 
245  //------------------------------------------------------------------------
253  //------------------------------------------------------------------------
254  static void MergeCGI( URL::ParamsMap &cgi1,
255  const URL::ParamsMap &cgi2,
256  bool replace );
257 
258  //------------------------------------------------------------------------
264  //------------------------------------------------------------------------
265  static Status CreateXAttrVec( const std::vector<xattr_t> &attrs,
266  std::vector<char> &avec );
267 
268  //------------------------------------------------------------------------
273  //------------------------------------------------------------------------
274  static Status CreateXAttrVec( const std::vector<std::string> &attrs,
275  std::vector<char> &nvec );
276 
277  //------------------------------------------------------------------------
283  //------------------------------------------------------------------------
284  template<typename T>
286  const std::vector<T> &vec,
287  const std::string &path = "" )
288  {
289  ClientRequestHdr *hdr = reinterpret_cast<ClientRequestHdr*>( msg->GetBuffer() );
290 
291  std::vector<char> xattrvec;
292  Status st = MessageUtils::CreateXAttrVec( vec, xattrvec );
293  if( !st.IsOK() )
294  return st;
295 
296  // update body size in the header
297  hdr->dlen = path.size() + 1;
298  hdr->dlen += xattrvec.size();
299 
300  // append the body
301  size_t offset = sizeof( ClientRequestHdr );
302  msg->Append( path.c_str(), path.size() + 1, offset );
303  offset += path.size() + 1;
304  msg->Append( xattrvec.data(), xattrvec.size(), offset );
305 
306  return Status();
307  }
308  };
309 }
310 
311 #endif // __XRD_CL_MESSAGE_UTILS_HH__
XrdCl::MessageSendParams::timeout
uint16_t timeout
Definition: XrdClMessageUtils.hh:136
XrdCl::MessageSendParams
Definition: XrdClMessageUtils.hh:132
ClientRequestHdr
Definition: XProtocol.hh:154
XrdClXRootDResponses.hh
XrdCl::ResponseHandler
Handle an async response.
Definition: XrdClXRootDResponses.hh:1051
XrdCl::SyncResponseHandler::WaitForResponse
void WaitForResponse()
Wait for the arrival of the response.
Definition: XrdClMessageUtils.hh:93
XrdCl::SyncResponseHandler::pResponse
AnyObject * pResponse
Definition: XrdClMessageUtils.hh:106
XrdCl::SyncResponseHandler::pStatus
XRootDStatus * pStatus
Definition: XrdClMessageUtils.hh:105
XrdCl::LocalFileHandler
Definition: XrdClLocalFileHandler.hh:33
XrdSysCondVarHelper
Definition: XrdSysPthread.hh:129
XrdSysPthread.hh
XrdClMessage.hh
XrdSys::KernelBuffer
Definition: XrdSysKernelBuffer.hh:46
XrdCl::Buffer::GetBuffer
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
XrdCl::MessageSendParams::followRedirects
bool followRedirects
Definition: XrdClMessageUtils.hh:139
XrdCl::AnyObject::Get
void Get(Type &object)
Retrieve the object being held.
Definition: XrdClAnyObject.hh:78
XrdCl::MessageSendParams::expires
time_t expires
Definition: XrdClMessageUtils.hh:137
XrdCl::AnyObject::Set
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
XrdCl::errInternal
const uint16_t errInternal
Internal error.
Definition: XrdClStatus.hh:56
XrdCl::SyncResponseHandler
Synchronize the response.
Definition: XrdClMessageUtils.hh:44
XrdCl::Message
The message representation used throughout the system.
Definition: XrdClMessage.hh:30
XrdCl::MessageSendParams::loadBalancer
HostInfo loadBalancer
Definition: XrdClMessageUtils.hh:138
XrdCl::MessageSendParams::redirectLimit
uint16_t redirectLimit
Definition: XrdClMessageUtils.hh:144
XrdSysCondVar::Broadcast
void Broadcast()
Definition: XrdSysPthread.hh:89
ClientRequestHdr::dlen
kXR_int32 dlen
Definition: XProtocol.hh:158
XrdCl::MessageUtils::SendMessage
static XRootDStatus SendMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Send message.
XrdCl::MessageUtils::ProcessSendParams
static void ProcessSendParams(MessageSendParams &sendParams)
Process sending params.
XrdCl::XRootDStatus
Request status.
Definition: XrdClXRootDResponses.hh:219
XrdCl::MessageUtils::WaitForStatus
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
Definition: XrdClMessageUtils.hh:154
XrdCl::MessageUtils::RedirectMessage
static Status RedirectMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Redirect message.
XrdSysCondVar
Definition: XrdSysPthread.hh:79
XrdCl::ChunkList
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition: XrdClXRootDResponses.hh:980
XrdCl::MessageUtils::CreateXAttrBody
static Status CreateXAttrBody(Message *msg, const std::vector< T > &vec, const std::string &path="")
Definition: XrdClMessageUtils.hh:285
XrdCl::URL::ParamsMap
std::map< std::string, std::string > ParamsMap
Definition: XrdClURL.hh:33
XrdCl::MessageUtils::RewriteCGIAndPath
static void RewriteCGIAndPath(Message *msg, const URL::ParamsMap &newCgi, bool replace, const std::string &newPath)
XrdCl::MessageUtils::MergeCGI
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
XrdCl::MessageUtils
Definition: XrdClMessageUtils.hh:149
XrdCl::MessageUtils::CreateRequest
static void CreateRequest(Message *&msg, Request *&req, uint32_t payloadSize=0)
Create a message.
Definition: XrdClMessageUtils.hh:197
XrdCl::MessageSendParams::chunkList
ChunkList * chunkList
Definition: XrdClMessageUtils.hh:143
XrdCl::MessageUtils::CreateXAttrVec
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
XrdCl::MessageSendParams::stateful
bool stateful
Definition: XrdClMessageUtils.hh:141
XrdCl::MessageSendParams::hostList
HostList * hostList
Definition: XrdClMessageUtils.hh:142
XrdCl::SyncResponseHandler::SyncResponseHandler
SyncResponseHandler(const SyncResponseHandler &other)
XrdCl::Status::IsOK
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:122
XrdCl::SyncResponseHandler::pCondVar
XrdSysCondVar pCondVar
Definition: XrdClMessageUtils.hh:107
XrdSysCondVar::Wait
int Wait()
XrdCl::SyncResponseHandler::GetStatus
XRootDStatus * GetStatus()
Get the status.
Definition: XrdClMessageUtils.hh:77
XrdCl::SyncResponseHandler::HandleResponse
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Handle the response.
Definition: XrdClMessageUtils.hh:65
XrdCl::NullResponseHandler
Definition: XrdClMessageUtils.hh:115
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
XrdSysKernelBuffer.hh
XrdCl
Definition: XrdClAnyObject.hh:26
XrdCl::MessageSendParams::chunkedResponse
bool chunkedResponse
Definition: XrdClMessageUtils.hh:140
XrdCl::HostList
std::vector< HostInfo > HostList
Definition: XrdClXRootDResponses.hh:1045
XrdCl::SyncResponseHandler::~SyncResponseHandler
virtual ~SyncResponseHandler()
Destructor.
Definition: XrdClMessageUtils.hh:57
XrdCl::MessageSendParams::MessageSendParams
MessageSendParams()
Definition: XrdClMessageUtils.hh:133
XrdCl::MessageUtils::CreateXAttrVec
static Status CreateXAttrVec(const std::vector< std::string > &attrs, std::vector< char > &nvec)
XrdCl::stError
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
XrdCl::MessageUtils::WaitForResponse
static XrdCl::XRootDStatus WaitForResponse(SyncResponseHandler *handler, Type *&response)
Wait for the response.
Definition: XrdClMessageUtils.hh:167
XrdCl::HostInfo
Definition: XrdClXRootDResponses.hh:1034
XrdClURL.hh
XrdCl::Status
Procedure execution status.
Definition: XrdClStatus.hh:113
XrdCl::URL
URL representation.
Definition: XrdClURL.hh:31
XrdCl::MessageSendParams::kbuff
XrdSys::KernelBuffer * kbuff
Definition: XrdClMessageUtils.hh:145
XrdCl::SyncResponseHandler::SyncResponseHandler
SyncResponseHandler()
Constructor.
Definition: XrdClMessageUtils.hh:49
XrdCl::Buffer::Zero
void Zero()
Zero.
Definition: XrdClBuffer.hh:124
XrdCl::SyncResponseHandler::operator=
SyncResponseHandler & operator=(const SyncResponseHandler &other)
XrdCl::NullResponseHandler::HandleResponseWithHosts
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Definition: XrdClMessageUtils.hh:120
XrdCl::AnyObject
Definition: XrdClAnyObject.hh:33
XrdCl::SyncResponseHandler::GetResponse
AnyObject * GetResponse()
Get the response.
Definition: XrdClMessageUtils.hh:85