xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  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__
Definition: XrdClMessageUtils.hh:114
Definition: XrdClAnyObject.hh:32
std::map< std::string, std::string > ParamsMap
Definition: XrdClURL.hh:33
Definition: XrdClMessageUtils.hh:131
uint16_t redirectLimit
Definition: XrdClMessageUtils.hh:144
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition: XrdClXRootDResponses.hh:1046
XRootDStatus * GetStatus()
Get the status.
Definition: XrdClMessageUtils.hh:77
void Get(Type &object)
Retrieve the object being held.
Definition: XrdClAnyObject.hh:78
The message representation used throughout the system.
Definition: XrdClMessage.hh:29
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
Definition: XrdClMessageUtils.hh:155
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Definition: XrdClMessageUtils.hh:120
bool followRedirects
Definition: XrdClMessageUtils.hh:139
bool chunkedResponse
Definition: XrdClMessageUtils.hh:140
uint16_t timeout
Definition: XrdClMessageUtils.hh:136
Synchronize the response.
Definition: XrdClMessageUtils.hh:43
SyncResponseHandler()
Constructor.
Definition: XrdClMessageUtils.hh:49
static XRootDStatus SendMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Send message.
static XrdCl::XRootDStatus WaitForResponse(SyncResponseHandler *handler, Type *&response)
Wait for the response.
Definition: XrdClMessageUtils.hh:168
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
Procedure execution status.
Definition: XrdClStatus.hh:113
SyncResponseHandler & operator=(const SyncResponseHandler &other)
XrdSys::KernelBuffer * kbuff
Definition: XrdClMessageUtils.hh:145
Definition: XrdClMessageUtils.hh:149
virtual ~SyncResponseHandler()
Destructor.
Definition: XrdClMessageUtils.hh:57
AnyObject * GetResponse()
Get the response.
Definition: XrdClMessageUtils.hh:85
static void RewriteCGIAndPath(Message *msg, const URL::ParamsMap &newCgi, bool replace, const std::string &newPath)
Definition: XrdClXRootDResponses.hh:1099
std::vector< HostInfo > HostList
Definition: XrdClXRootDResponses.hh:1111
Definition: XrdSysPthread.hh:78
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
kXR_int32 dlen
Definition: XProtocol.hh:159
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
Request status.
Definition: XrdClXRootDResponses.hh:218
ChunkList * chunkList
Definition: XrdClMessageUtils.hh:143
HostList * hostList
Definition: XrdClMessageUtils.hh:142
void WaitForResponse()
Wait for the arrival of the response.
Definition: XrdClMessageUtils.hh:93
HostInfo loadBalancer
Definition: XrdClMessageUtils.hh:138
Definition: XrdSysKernelBuffer.hh:45
Definition: XProtocol.hh:155
void Broadcast()
Definition: XrdSysPthread.hh:89
Definition: XrdSysPthread.hh:128
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition: XrdClBuffer.hh:164
MessageSendParams()
Definition: XrdClMessageUtils.hh:133
bool stateful
Definition: XrdClMessageUtils.hh:141
Handle an async response.
Definition: XrdClXRootDResponses.hh:1116
std::vector< uint32_t > crc32cDigests
Definition: XrdClMessageUtils.hh:146
Definition: XrdClLocalFileHandler.hh:32
URL representation.
Definition: XrdClURL.hh:30
time_t expires
Definition: XrdClMessageUtils.hh:137
const uint16_t errInternal
Internal error.
Definition: XrdClStatus.hh:56
static Status CreateXAttrBody(Message *msg, const std::vector< T > &vec, const std::string &path="")
Definition: XrdClMessageUtils.hh:286
AnyObject * pResponse
Definition: XrdClMessageUtils.hh:106
bool IsOK() const
We&#39;re fine.
Definition: XrdClStatus.hh:123
XrdSysCondVar pCondVar
Definition: XrdClMessageUtils.hh:107
XRootDStatus * pStatus
Definition: XrdClMessageUtils.hh:105
static void ProcessSendParams(MessageSendParams &sendParams)
Process sending params.
void Zero()
Zero.
Definition: XrdClBuffer.hh:124
static Status RedirectMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Redirect message.
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Handle the response.
Definition: XrdClMessageUtils.hh:65
static void CreateRequest(Message *&msg, Request *&req, uint32_t payloadSize=0)
Create a message.
Definition: XrdClMessageUtils.hh:198