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;
71  pCondVar.Broadcast();
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:
103  SyncResponseHandler &operator = (const SyncResponseHandler &other);
104 
105  XRootDStatus *pStatus;
106  AnyObject *pResponse;
107  XrdSysCondVar pCondVar;
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  std::vector<uint32_t> crc32cDigests;
147  };
148 
150  {
151  public:
152  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
156  {
157  handler->WaitForResponse();
158  XRootDStatus *status = handler->GetStatus();
159  XRootDStatus ret( *status );
160  delete status;
161  return ret;
162  }
163 
164  //------------------------------------------------------------------------
166  //------------------------------------------------------------------------
167  template<class Type>
169  SyncResponseHandler *handler,
170  Type *&response )
171  {
172  handler->WaitForResponse();
173 
174  AnyObject *resp = handler->GetResponse();
175  XRootDStatus *status = handler->GetStatus();
176  XRootDStatus ret( *status );
177  delete status;
178 
179  if( ret.IsOK() )
180  {
181  if( !resp )
182  return XRootDStatus( stError, errInternal );
183  resp->Get( response );
184  resp->Set( (int *)0 );
185  delete resp;
186 
187  if( !response )
188  return XRootDStatus( stError, errInternal );
189  }
190 
191  return ret;
192  }
193 
194  //------------------------------------------------------------------------
196  //------------------------------------------------------------------------
197  template<class Request>
198  static void CreateRequest( Message *&msg,
199  Request *&req,
200  uint32_t payloadSize = 0 )
201  {
202  msg = new Message( sizeof(Request) + payloadSize );
203  req = (Request*)msg->GetBuffer();
204  msg->Zero();
205  }
206 
207  //------------------------------------------------------------------------
209  //------------------------------------------------------------------------
210  static XRootDStatus SendMessage( const URL &url,
211  Message *msg,
212  ResponseHandler *handler,
213  MessageSendParams &sendParams,
214  LocalFileHandler *lFileHandler );
215 
216  //------------------------------------------------------------------------
218  //------------------------------------------------------------------------
219  static Status RedirectMessage( const URL &url,
220  Message *msg,
221  ResponseHandler *handler,
222  MessageSendParams &sendParams,
223  LocalFileHandler *lFileHandler );
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  static void ProcessSendParams( MessageSendParams &sendParams );
229 
230  //------------------------------------------------------------------------
240  //------------------------------------------------------------------------
241  static void RewriteCGIAndPath( Message *msg,
242  const URL::ParamsMap &newCgi,
243  bool replace,
244  const std::string &newPath );
245 
246  //------------------------------------------------------------------------
254  //------------------------------------------------------------------------
255  static void MergeCGI( URL::ParamsMap &cgi1,
256  const URL::ParamsMap &cgi2,
257  bool replace );
258 
259  //------------------------------------------------------------------------
265  //------------------------------------------------------------------------
266  static Status CreateXAttrVec( const std::vector<xattr_t> &attrs,
267  std::vector<char> &avec );
268 
269  //------------------------------------------------------------------------
274  //------------------------------------------------------------------------
275  static Status CreateXAttrVec( const std::vector<std::string> &attrs,
276  std::vector<char> &nvec );
277 
278  //------------------------------------------------------------------------
284  //------------------------------------------------------------------------
285  template<typename T>
287  const std::vector<T> &vec,
288  const std::string &path = "" )
289  {
290  ClientRequestHdr *hdr = reinterpret_cast<ClientRequestHdr*>( msg->GetBuffer() );
291 
292  std::vector<char> xattrvec;
293  Status st = MessageUtils::CreateXAttrVec( vec, xattrvec );
294  if( !st.IsOK() )
295  return st;
296 
297  // update body size in the header
298  hdr->dlen = path.size() + 1;
299  hdr->dlen += xattrvec.size();
300 
301  // append the body
302  size_t offset = sizeof( ClientRequestHdr );
303  msg->Append( path.c_str(), path.size() + 1, offset );
304  offset += path.size() + 1;
305  msg->Append( xattrvec.data(), xattrvec.size(), offset );
306 
307  return Status();
308  }
309  };
310 }
311 
312 #endif // __XRD_CL_MESSAGE_UTILS_HH__
kXR_int32 dlen
Definition: XProtocol.hh:159
void Set(Type object, bool own=true)
void Get(Type &object)
Retrieve the object being held.
void Zero()
Zero.
Definition: XrdClBuffer.hh:124
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition: XrdClBuffer.hh:164
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
Merge cgi2 into cgi1.
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
Create xattr vector.
static void ProcessSendParams(MessageSendParams &sendParams)
Process sending params.
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.
static Status CreateXAttrBody(Message *msg, const std::vector< T > &vec, const std::string &path="")
static XrdCl::XRootDStatus WaitForResponse(SyncResponseHandler *handler, Type *&response)
Wait for the response.
static Status RedirectMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Redirect message.
static XRootDStatus SendMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Send message.
static void CreateRequest(Message *&msg, Request *&req, uint32_t payloadSize=0)
Create a message.
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
The message representation used throughout the system.
Definition: XrdClMessage.hh:32
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Handle an async response.
Synchronize the response.
AnyObject * GetResponse()
Get the response.
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Handle the response.
virtual ~SyncResponseHandler()
Destructor.
XRootDStatus * GetStatus()
Get the status.
void WaitForResponse()
Wait for the arrival of the response.
URL representation.
Definition: XrdClURL.hh:31
std::map< std::string, std::string > ParamsMap
Definition: XrdClURL.hh:33
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
std::vector< HostInfo > HostList
const uint16_t errInternal
Internal error.
Definition: XrdClStatus.hh:56
std::vector< ChunkInfo > ChunkList
List of chunks.
std::vector< uint32_t > crc32cDigests
XrdSys::KernelBuffer * kbuff
Procedure execution status.
Definition: XrdClStatus.hh:115
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124