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__
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:970
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:154
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Definition: XrdClMessageUtils.hh:120
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
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:167
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
bool IsOK() const
We&#39;re fine.
Definition: XrdClStatus.hh:122
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
Procedure execution status.
Definition: XrdClStatus.hh:112
SyncResponseHandler & operator=(const SyncResponseHandler &other)
XrdSys::KernelBuffer * kbuff
Definition: XrdClMessageUtils.hh:145
Definition: XrdClMessageUtils.hh:148
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:1023
std::vector< HostInfo > HostList
Definition: XrdClXRootDResponses.hh:1035
Definition: XrdSysPthread.hh:78
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
kXR_int32 dlen
Definition: XProtocol.hh:158
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
Request status.
Definition: XrdClXRootDResponses.hh:218
Definition: XrdClAnyObject.hh:25
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:154
void Broadcast()
Definition: XrdSysPthread.hh:89
Definition: XrdSysPthread.hh:129
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:1040
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:285
AnyObject * pResponse
Definition: XrdClMessageUtils.hh:106
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:197